find.lsp 21.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Fri Aug 23 07:49:49 2002
;;;; Contains: Tests for FIND

(in-package :cl-test)

(deftest find-list.1
  (find 'c '(a b c d e c a))
  c)

(deftest find-list.2
  (find 'c '(a b c d e c a) :from-end t)
  c)

(deftest find-list.3
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
18
        (find 'c '(a b c d e c a) :start i))
19 20 21 22
  (c c c c c c nil nil))

(deftest find-list.4
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
23
        (find 'c '(a b c d e c a) :start i :end nil))
24 25 26 27
  (c c c c c c nil nil))

(deftest find-list.5
  (loop for i from 7 downto 0 collect
sds's avatar
sds committed
28
        (find 'c '(a b c d e c a) :end i))
29 30 31 32
  (c c c c c nil nil nil))

(deftest find-list.6
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
33
        (find 'c '(a b c d e c a) :start i :from-end t))
34 35 36 37
  (c c c c c c nil nil))

(deftest find-list.7
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
38
        (find 'c '(a b c d e c a) :start i :end nil :from-end t))
39 40 41 42
  (c c c c c c nil nil))

(deftest find-list.8
  (loop for i from 7 downto 0 collect
sds's avatar
sds committed
43
        (find 'c '(a b c d e c a) :end i :from-end t))
44 45 46 47
  (c c c c c nil nil nil))

(deftest find-list.9
  (loop for i from 0 to 6 collect
sds's avatar
sds committed
48 49 50
        (loop for j from (1+ i) to 7
              collect
              (find 'c '(a b c d e c a) :start i :end j)))
51 52 53 54 55 56 57 58 59 60
  ((nil nil c c c c c)
   (nil c c c c c)
   (c c c c c)
   (nil nil c c)
   (nil c c)
   (c c)
   (nil)))

(deftest find-list.10
  (loop for i from 0 to 6 collect
sds's avatar
sds committed
61 62 63
        (loop for j from (1+ i) to 7
              collect
              (find 'c '(a b c d e c a) :start i :end j :from-end t)))
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  ((nil nil c c c c c)
   (nil c c c c c)
   (c c c c c)
   (nil nil c c)
   (nil c c)
   (c c)
   (nil)))

(deftest find-list.11
  (find 5 '(1 2 3 4 5 6 4 8) :key #'1+)
  4)

(deftest find-list.12
  (find 5 '(1 2 3 4 5 6 4 8) :key '1+)
  4)

(deftest find-list.13
  (find 5 '(1 2 3 4 5 6 4 8) :key #'1+ :from-end t)
  4)

(deftest find-list.14
85
  (find 'a '(a a b a c e d a f a) :test (complement #'eql))
86 87 88
  b)

(deftest find-list.15
89
  (find 'a '(a a b a c e d a f a) :test (complement #'eql)
sds's avatar
sds committed
90
            :from-end t)
91 92 93
  f)

(deftest find-list.16
94
  (find 'a '(a a b a c e d a f a) :test-not #'eql)
95 96 97
  b)

(deftest find-list.17
98
  (find 'a '(a a b a c e d a f a) :test-not 'eql
sds's avatar
sds committed
99
            :from-end t)
100 101 102
  f)

(deftest find-list.18
103
  (find 'a '(a a b a c e d a f a) :test-not 'eql)
104 105 106
  b)

(deftest find-list.19
107
  (find 'a '(a a b a c e d a f a) :test-not #'eql
sds's avatar
sds committed
108
            :from-end t)
109 110 111
  f)

(deftest find-list.20
112
  (find 'a '(a a b a c e d a f a) :test-not #'eql)
113 114 115
  b)

(deftest find-list.21
116
  (find 'a '(a a b a c e d a f a) :test #'eql
sds's avatar
sds committed
117
            :start 2)
118 119 120
  a)

(deftest find-list.22
121
  (find 'a '(a a b a c e d a f a) :test #'eql
sds's avatar
sds committed
122
            :start 2 :end nil)
123 124 125
  a)

(deftest find-list.23
126
  (find 'a '(a a b a c e d a f a) :test-not #'eql
sds's avatar
sds committed
127
            :start 0 :end 5)
128 129 130
  b)

(deftest find-list.24
131
  (find 'a '(a a b a c e d a f a) :test-not #'eql
sds's avatar
sds committed
132
            :start 0 :end 5 :from-end t)
133 134 135 136 137 138 139 140 141 142 143 144
  c)

(deftest find-list.25
  (find "ab" '("a" #(#\b #\a) #(#\a #\b #\c) #(#\a #\b) #(#\d #\e) f) :test #'equalp)
  #(#\a #\b))

(deftest find-list.26
  (find 'a '((c) (b a) (a b c) (a b) (d e) f) :key #'car)
  (a b c))

(deftest find-list.27
  (find 'a '((c) (b a) (a b c) (z) (a b) (d e) f) :key #'car
sds's avatar
sds committed
145
            :start 3)
146 147 148 149
  (a b))

(deftest find-list.28
  (find 'a '((c) (b a) (a b c) (z) (a b) (d e) (f)) :key #'car
sds's avatar
sds committed
150
            :start 2 :from-end t)
151 152
  (a b))

153 154 155 156 157 158 159 160
(deftest find-list.29
  (find 10 '(1 2 3 8 20 3 1 21 3) :test #'<)
  20)

(deftest find-list.30
  (find 10 '(1 2 3 8 20 3 1 21 3) :test-not #'>=)
  20)

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
;;; Tests on vectors

(deftest find-vector.1
  (find 'c #(a b c d e c a))
  c)

(deftest find-vector.1a
  (find 'z #(a b c d e c a))
  nil)

(deftest find-vector.2
  (find 'c #(a b c d e c a) :from-end t)
  c)

(deftest find-vector.2a
  (find 'z #(a b c d e c a) :from-end t)
  nil)

(deftest find-vector.3
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
181
        (find 'c #(a b c d e c a) :start i))
182 183 184 185
  (c c c c c c nil nil))

(deftest find-vector.4
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
186
        (find 'c #(a b c d e c a) :start i :end nil))
187 188 189 190
  (c c c c c c nil nil))

(deftest find-vector.5
  (loop for i from 7 downto 0 collect
sds's avatar
sds committed
191
        (find 'c #(a b c d e c a) :end i))
192 193 194 195
  (c c c c c nil nil nil))

(deftest find-vector.6
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
196
        (find 'c #(a b c d e c a) :start i :from-end t))
197 198 199 200
  (c c c c c c nil nil))

(deftest find-vector.7
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
201
        (find 'c #(a b c d e c a) :start i :end nil :from-end t))
202 203 204 205
  (c c c c c c nil nil))

(deftest find-vector.8
  (loop for i from 7 downto 0 collect
sds's avatar
sds committed
206
        (find 'c #(a b c d e c a) :end i :from-end t))
207 208 209 210
  (c c c c c nil nil nil))

(deftest find-vector.9
  (loop for i from 0 to 6 collect
sds's avatar
sds committed
211 212 213
        (loop for j from (1+ i) to 7
              collect
              (find 'c #(a b c d e c a) :start i :end j)))
214 215 216 217 218 219 220 221 222 223
  ((nil nil c c c c c)
   (nil c c c c c)
   (c c c c c)
   (nil nil c c)
   (nil c c)
   (c c)
   (nil)))

(deftest find-vector.10
  (loop for i from 0 to 6 collect
sds's avatar
sds committed
224 225 226
        (loop for j from (1+ i) to 7
              collect
              (find 'c #(a b c d e c a) :start i :end j :from-end t)))
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  ((nil nil c c c c c)
   (nil c c c c c)
   (c c c c c)
   (nil nil c c)
   (nil c c)
   (c c)
   (nil)))

(deftest find-vector.11
  (find 5 #(1 2 3 4 5 6 4 8) :key #'1+)
  4)

(deftest find-vector.12
  (find 5 #(1 2 3 4 5 6 4 8) :key '1+)
  4)

(deftest find-vector.13
  (find 5 #(1 2 3 4 5 6 4 8) :key #'1+ :from-end t)
  4)

(deftest find-vector.14
248
  (find 'a #(a a b a c e d a f a) :test (complement #'eql))
249 250 251
  b)

(deftest find-vector.15
252
  (find 'a #(a a b a c e d a f a) :test (complement #'eql)
sds's avatar
sds committed
253
            :from-end t)
254 255 256
  f)

(deftest find-vector.16
257
  (find 'a #(a a b a c e d a f a) :test-not #'eql)
258 259 260
  b)

(deftest find-vector.17
261
  (find 'a #(a a b a c e d a f a) :test-not 'eql
sds's avatar
sds committed
262
            :from-end t)
263 264 265
  f)

(deftest find-vector.18
266
  (find 'a #(a a b a c e d a f a) :test-not 'eql)
267 268 269
  b)

(deftest find-vector.19
270
  (find 'a #(a a b a c e d a f a) :test-not #'eql
sds's avatar
sds committed
271
            :from-end t)
272 273 274
  f)

(deftest find-vector.20
275
  (find 'a #(a a b a c e d a f a) :test-not #'eql)
276 277 278
  b)

(deftest find-vector.21
279
  (find 'a #(a a b a c e d a f a) :test #'eql
sds's avatar
sds committed
280
            :start 2)
281 282 283
  a)

(deftest find-vector.22
284
  (find 'a #(a a b a c e d a f a) :test #'eql
sds's avatar
sds committed
285
            :start 2 :end nil)
286 287 288
  a)

(deftest find-vector.23
289
  (find 'a #(a a b a c e d a f a) :test-not #'eql
sds's avatar
sds committed
290
            :start 0 :end 5)
291 292 293
  b)

(deftest find-vector.24
294
  (find 'a #(a a b a c e d a f a) :test-not #'eql
sds's avatar
sds committed
295
            :start 0 :end 5 :from-end t)
296 297 298 299 300 301 302 303 304 305 306 307
  c)

(deftest find-vector.25
  (find "ab" #("a" #(#\b #\a) #(#\a #\b #\c) #(#\a #\b) #(#\d #\e) f) :test #'equalp)
  #(#\a #\b))

(deftest find-vector.26
  (find 'a #((c) (b a) (a b c) (a b) (d e) f) :key #'car)
  (a b c))

(deftest find-vector.27
  (find 'a #((c) (b a) (a b c) (z) (a b) (d e) f) :key #'car
sds's avatar
sds committed
308
            :start 3)
309 310 311 312
  (a b))

(deftest find-vector.28
  (find 'a #((c) (b a) (a b c) (z) (a b) (d e) (f)) :key #'car
sds's avatar
sds committed
313
            :start 2 :from-end t)
314 315
  (a b))

316 317
(deftest find-vector.29
  (let ((a (make-array '(10)
sds's avatar
sds committed
318 319
                       :initial-contents '(1 2 3 4 5 6 7 8 9 10)
                       :fill-pointer 5)))
320 321 322 323 324
    (loop for i from 1 to 10 collect (find i a)))
  (1 2 3 4 5 nil nil nil nil nil))

(deftest find-vector.30
  (let ((a (make-array '(10)
sds's avatar
sds committed
325 326 327
                       :initial-contents (loop for i from 1 for e in '(1 2 3 4 5 5 4 3 2 1)
                                               collect (list e i))
                       :fill-pointer 5)))
328 329 330
    (loop for i from 1 to 5 collect (find i a :from-end t :key #'car)))
  ((1 1) (2 2) (3 3) (4 4) (5 5)))

331 332 333 334 335 336 337 338
(deftest find-vector.31
  (find 10 #(1 2 3 8 20 3 1 21 3) :test #'<)
  20)

(deftest find-vector.32
  (find 10 #(1 2 3 8 20 3 1 21 3) :test-not #'>=)
  20)

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
(deftest find-vector.33
  (do-special-integer-vectors
   (v #(1 2 3 4 5 6 7) nil)
   (assert (null (find 0 v)))
   (assert (= (find 4 v) 4))
   (assert (= (find -1 v :test #'<) 1))
   (assert (= (find -1 v :test #'< :from-end t) 7)))
  nil)

(deftest find-vector.34
  (do-special-integer-vectors
   (v #(0 0 0 0) nil)
   (assert (eql (find 0 v) 0))
   (assert (eql (find 0 v :start 1) 0))
   (assert (eql (find 0 v :from-end t) 0))
   (assert (null (find 1 v)))
   (assert (null (find 'a v)))
   (assert (null (find 0.0 v)))
   (assert (null (find #c(1.0 0.0) v)))
   (assert (null (find -1 v)))
   (assert (null (find 2 v))))
  nil)

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
;;; tests on bit vectors

(deftest find-bit-vector.1
  (find 1 #*001001010100)
  1)

(deftest find-bit-vector.1a
  (find 0 #*001001010100)
  0)

(deftest find-bit-vector.1b
  (find 2 #*001001010100)
  nil)

(deftest find-bit-vector.1c
  (find 'a #*001001010100)
  nil)

(deftest find-bit-vector.1d
  (find 1 #*000000)
  nil)

(deftest find-bit-vector.2
  (find 1 #*001001010100 :from-end t)
  1)

(deftest find-bit-vector.2a
  (find 1 #*00000 :from-end t)
  nil)

(deftest find-bit-vector.2b
  (find 0 #*00000 :from-end t)
  0)

(deftest find-bit-vector.2c
  (find 0 #*11111 :from-end t)
  nil)

(deftest find-bit-vector.2d
  (find 2 #*11111 :from-end t)
  nil)

(deftest find-bit-vector.2e
  (find 'a #*11111 :from-end t)
  nil)

(deftest find-bit-vector.3
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
410
        (find 1 #*0010010 :start i))
411 412 413 414
  (1 1 1 1 1 1 nil nil))

(deftest find-bit-vector.4
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
415
        (find 1 #*0010010 :start i :end nil))
416 417 418 419
  (1 1 1 1 1 1 nil nil))

(deftest find-bit-vector.5
  (loop for i from 7 downto 0 collect
sds's avatar
sds committed
420
        (find 1 #*0010010 :end i))
421 422 423 424
  (1 1 1 1 1 nil nil nil))

(deftest find-bit-vector.6
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
425
        (find 1 #*0010010 :start i :from-end t))
426 427 428 429
  (1 1 1 1 1 1 nil nil))

(deftest find-bit-vector.7
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
430
        (find 0 #*1101101 :start i :end nil :from-end t))
431 432 433 434
  (0 0 0 0 0 0 nil nil))

(deftest find-bit-vector.8
  (loop for i from 7 downto 0 collect
sds's avatar
sds committed
435
        (find 0 #*1101101 :end i :from-end t))
436 437 438 439
  (0 0 0 0 0 nil nil nil))

(deftest find-bit-vector.9
  (loop for i from 0 to 6 collect
sds's avatar
sds committed
440 441 442
        (loop for j from (1+ i) to 7
              collect
              (find 1 #*0010010 :start i :end j)))
443 444 445 446 447 448 449 450 451 452
  ((nil nil 1 1 1 1 1)
   (nil 1 1 1 1 1)
   (1 1 1 1 1)
   (nil nil 1 1)
   (nil 1 1)
   (1 1)
   (nil)))

(deftest find-bit-vector.10
  (loop for i from 0 to 6 collect
sds's avatar
sds committed
453 454 455
        (loop for j from (1+ i) to 7
              collect
              (find 1 #*0010010 :start i :end j :from-end t)))
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  ((nil nil 1 1 1 1 1)
   (nil 1 1 1 1 1)
   (1 1 1 1 1)
   (nil nil 1 1)
   (nil 1 1)
   (1 1)
   (nil)))

(deftest find-bit-vector.11
  (find 2 #*00010001010 :key #'1+)
  1)

(deftest find-bit-vector.12
  (find 2 #*00010001010 :key '1+)
  1)

(deftest find-bit-vector.13
  (find 2 #*0010001000 :key #'1+ :from-end t)
  1)

(deftest find-bit-vector.14
477
  (find 0 #*0010111010 :test (complement #'eql))
478 479 480
  1)

(deftest find-bit-vector.15
481
  (find 0 #*0010111010 :test (complement #'eql)
sds's avatar
sds committed
482
            :from-end t)
483 484 485
  1)

(deftest find-bit-vector.16
486
  (find 0 #*0010111010 :test-not #'eql)
487 488 489
  1)

(deftest find-bit-vector.16a
490
  (find 1 #*111111111111 :test-not #'eql)
491 492 493
  nil)

(deftest find-bit-vector.16b
494
  (find 0 #*0000000 :test-not #'eql)
495 496 497
  nil)

(deftest find-bit-vector.17
498
  (find 0 #*001011101 :test-not 'eql
sds's avatar
sds committed
499
        :from-end t)
500 501 502
  1)

(deftest find-bit-vector.17a
503
  (find 0 #*0000000 :test-not 'eql
sds's avatar
sds committed
504
        :from-end t)
505 506 507
  nil)

(deftest find-bit-vector.17b
508
  (find 1 #*111111111111 :test-not 'eql
sds's avatar
sds committed
509
        :from-end t)
510 511 512
  nil)

(deftest find-bit-vector.18
513
  (find 0 #*00101110 :test-not 'eql)
514 515 516
  1)

(deftest find-bit-vector.18a
517
  (find 0 #*00000000 :test-not 'eql)
518 519 520
  nil)

(deftest find-bit-vector.19
521
  (find 0 #*00101110 :test-not #'eql
sds's avatar
sds committed
522
            :from-end t)
523 524 525
  1)

(deftest find-bit-vector.19a
526
  (find 0 #*00000000 :test-not #'eql
sds's avatar
sds committed
527
        :from-end t)
528 529 530
  nil)

(deftest find-bit-vector.20
531
  (find 0 #*00101110 :test-not #'eql)
532 533 534
  1)

(deftest find-bit-vector.21
535
  (find 0 #*00101110 :test #'eql
sds's avatar
sds committed
536
        :start 2)
537 538 539
  0)

(deftest find-bit-vector.21a
540
  (find 0 #*00111111 :test #'eql
sds's avatar
sds committed
541
        :start 2)
542 543 544
  nil)

(deftest find-bit-vector.21b
545
  (find 1 #*00111111 :test #'eql
sds's avatar
sds committed
546
        :start 2)
547 548 549
  1)

(deftest find-bit-vector.22
550
  (find 0 #*00101110 :test #'eql
sds's avatar
sds committed
551
        :start 2 :end nil)
552 553 554
  0)

(deftest find-bit-vector.22a
555
  (find 0 #*001111111 :test #'eql
sds's avatar
sds committed
556
        :start 2 :end nil)
557 558 559
  nil)

(deftest find-bit-vector.22b
560
  (find 1 #*001111111 :test #'eql
sds's avatar
sds committed
561
        :start 2 :end nil)
562 563 564
  1)

(deftest find-bit-vector.23
565
  (find 0 #*00101110 :test-not #'eql
sds's avatar
sds committed
566
        :start 0 :end 5)
567 568 569
  1)

(deftest find-bit-vector.23a
570
  (find 0 #*00000111 :test-not #'eql
sds's avatar
sds committed
571
        :start 0 :end 5)
572 573 574
  nil)

(deftest find-bit-vector.23b
575
  (find 0 #*00001000 :test-not #'eql
sds's avatar
sds committed
576
        :start 0 :end 5)
577 578 579
  1)

(deftest find-bit-vector.24
580
  (find 0 #*00101110 :test-not #'eql
sds's avatar
sds committed
581
            :start 0 :end 5 :from-end t)
582 583 584
  1)

(deftest find-bit-vector.24a
585
  (find 0 #*0000001111 :test-not #'eql
sds's avatar
sds committed
586
            :start 0 :end 5 :from-end t)
587 588 589
  nil)

(deftest find-bit-vector.24b
590
  (find 0 #*0000100 :test-not #'eql
sds's avatar
sds committed
591
            :start 0 :end 5 :from-end t)
592 593 594 595
  1)

(deftest find-bit-vector.25
  (find 2 #*1100001010 :key #'1+
sds's avatar
sds committed
596
            :start 3)
597 598 599 600
  1)

(deftest find-bit-vector.26
  (find 2 #*11100000 :key #'1+
sds's avatar
sds committed
601
            :start 3)
602 603 604 605
  nil)

(deftest find-bit-vector.26a
  (find 2 #*11110000 :key #'1+
sds's avatar
sds committed
606
            :start 3)
607 608 609 610
  1)

(deftest find-bit-vector.27
  (find 2 #*1100001010 :key #'1+
sds's avatar
sds committed
611
            :start 2 :from-end t)
612 613 614 615
  1)

(deftest find-bit-vector.28
  (find 2 #*1100000000 :key #'1+
sds's avatar
sds committed
616
            :start 2 :from-end t)
617 618
  nil)

619 620
(deftest find-bit-vector.29
  (let ((a
sds's avatar
sds committed
621 622 623
         (make-array '(10) :initial-contents '(1 1 1 1 1 0 0 0 0 0)
                     :element-type 'bit
                     :fill-pointer 5)))
624
    (values (find 0 a)
sds's avatar
sds committed
625
            (find 0 a :from-end t)))
626 627 628 629
  nil nil)

(deftest find-bit-vector.30
  (let ((a (make-array '(10) :initial-contents '(1 1 1 1 0 0 0 0 0 0)
sds's avatar
sds committed
630 631
                       :element-type 'bit
                       :fill-pointer 5)))
632 633 634
    (values (find 0 a) (find 0 a :from-end t)))
  0 0)

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
(deftest find-bit-vector.31
  (find 2 #*00011010010 :test #'<)
  nil)

(deftest find-bit-vector.32
  (find 2 #*0010101101 :test-not #'>=)
  nil)

(deftest find-bit-vector.33
  (find 0 #*00011010010 :test #'<)
  1)

(deftest find-bit-vector.34
  (find 0 #*0010101101 :test-not #'>=)
  1)

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
;;; strings

(deftest find-string.1
  (find #\c "abcdeca")
  #\c)

(deftest find-string.1a
  (find #\c "abCa")
  nil)

(deftest find-string.2
  (find #\c "abcdeca" :from-end t)
  #\c)

(deftest find-string.2a
  (find #\c "abCCCa" :from-end t)
  nil)

(deftest find-string.3
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
671
        (find #\c "abcdeca" :start i))
672 673 674 675
  (#\c #\c #\c #\c #\c #\c nil nil))

(deftest find-string.4
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
676
        (find #\c "abcdeca" :start i :end nil))
677 678 679 680
  (#\c #\c #\c #\c #\c #\c nil nil))

(deftest find-string.5
  (loop for i from 7 downto 0 collect
sds's avatar
sds committed
681
        (find #\c "abcdeca" :end i))
682 683 684 685
  (#\c #\c #\c #\c #\c nil nil nil))

(deftest find-string.6
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
686
        (find #\c "abcdeca" :start i :from-end t))
687 688 689 690
  (#\c #\c #\c #\c #\c #\c nil nil))

(deftest find-string.7
  (loop for i from 0 to 7 collect
sds's avatar
sds committed
691
        (find #\c "abcdeca" :start i :end nil :from-end t))
692 693 694 695
  (#\c #\c #\c #\c #\c #\c nil nil))

(deftest find-string.8
  (loop for i from 7 downto 0 collect
sds's avatar
sds committed
696
        (find #\c "abcdeca" :end i :from-end t))
697 698 699 700
  (#\c #\c #\c #\c #\c nil nil nil))

(deftest find-string.9
  (loop for i from 0 to 6 collect
sds's avatar
sds committed
701 702 703
        (loop for j from (1+ i) to 7
              collect
              (find #\c "abcdeca" :start i :end j)))
704 705 706 707 708 709 710 711 712 713
  ((nil nil #\c #\c #\c #\c #\c)
   (nil #\c #\c #\c #\c #\c)
   (#\c #\c #\c #\c #\c)
   (nil nil #\c #\c)
   (nil #\c #\c)
   (#\c #\c)
   (nil)))

(deftest find-string.10
  (loop for i from 0 to 6 collect
sds's avatar
sds committed
714 715 716
        (loop for j from (1+ i) to 7
              collect
              (find #\c "abcdeca" :start i :end j :from-end t)))
717 718 719 720 721 722 723 724 725 726
  ((nil nil #\c #\c #\c #\c #\c)
   (nil #\c #\c #\c #\c #\c)
   (#\c #\c #\c #\c #\c)
   (nil nil #\c #\c)
   (nil #\c #\c)
   (#\c #\c)
   (nil)))

(deftest find-string.11
  (find 5 "12345648" :key #'(lambda (c)
sds's avatar
sds committed
727
                              (1+ (read-from-string (string c)))))
728 729 730 731
  #\4)

(deftest find-string.13
  (find 5 "12345648" :key #'(lambda (c)
sds's avatar
sds committed
732 733
                                  (1+ (read-from-string (string c))))
            :from-end t)
734 735 736
  #\4)

(deftest find-string.14
737
  (find #\a "aabacedafa" :test (complement #'eql))
738 739 740
  #\b)

(deftest find-string.15
741
  (find #\a "aabacedafa" :test (complement #'eql)
sds's avatar
sds committed
742
            :from-end t)
743 744 745
  #\f)

(deftest find-string.16
746
  (find #\a "aabacedafa" :test-not #'eql)
747 748 749
  #\b)

(deftest find-string.17
750
  (find #\a "aabacedafa" :test-not 'eql
sds's avatar
sds committed
751
            :from-end t)
752 753 754
  #\f)

(deftest find-string.18
755
  (find #\a "aabacedafa" :test-not 'eql)
756 757 758
  #\b)

(deftest find-string.19
759
  (find #\a "aabacedafa" :test-not #'eql
sds's avatar
sds committed
760
            :from-end t)
761 762 763
  #\f)

(deftest find-string.20
764
  (find #\a "aabacedafa" :test-not #'eql)
765 766 767 768
  #\b)

(deftest find-string.21
  (find #\a "aabAcedafa" :test #'char-equal
sds's avatar
sds committed
769
        :start 2)
770 771 772 773
  #\A)

(deftest find-string.22
  (find #\a "aabAcedafa" :test #'char-equal
sds's avatar
sds committed
774
            :start 2 :end nil)
775 776 777 778
  #\A)

(deftest find-string.23
  (find #\a "aAbAcedafa" :test-not #'char-equal
sds's avatar
sds committed
779
            :start 0 :end 5)
780 781 782 783
  #\b)

(deftest find-string.24
  (find #\a "aabacedafa" :test-not #'char-equal
sds's avatar
sds committed
784
        :start 0 :end 5 :from-end t)
785 786
  #\c)

787
(deftest find-string.25
788
  (let ((s (make-array '(10) :initial-contents "abcdefghij"
sds's avatar
sds committed
789 790
                       :element-type 'character
                       :fill-pointer 5)))
791 792
    (values
     (loop for e across "abcdefghij"
sds's avatar
sds committed
793
           collect (find e s))
794
     (loop for e across "abcdefghij"
sds's avatar
sds committed
795
           collect (find e s :from-end t))))
796 797 798
  (#\a #\b #\c #\d #\e nil nil nil nil nil)
  (#\a #\b #\c #\d #\e nil nil nil nil nil))

799 800 801 802 803 804 805 806
(deftest find-string.26
  (find #\k "abcdmnop" :test #'char<)
  #\m)

(deftest find-string.27
  (find #\k "abcdmnop" :test-not #'char>=)
  #\m)

807 808 809 810 811 812
(deftest find-string.28
  (do-special-strings
   (s "abcdef" nil)
   (assert (char= (find #\c s :test #'char<) #\d)))
  nil)

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
;;; Test & test not

(defharmless find-list.test-and-test-not.1
  (find 'b '(a b c) :test #'eql :test-not #'eql))

(defharmless find-list.test-and-test-not.2
  (find 'b '(a b c) :test-not #'eql :test #'eql))

(defharmless find-vector.test-and-test-not.1
  (find 'b #(a b c) :test #'eql :test-not #'eql))

(defharmless find-vector.test-and-test-not.2
  (find 'b #(a b c) :test-not #'eql :test #'eql))

(defharmless find-string.test-and-test-not.1
  (find #\b "abc" :test #'eql :test-not #'eql))

(defharmless find-string.test-and-test-not.2
  (find #\b "abc" :test-not #'eql :test #'eql))

(defharmless find-bit-string.test-and-test-not.1
  (find 0 #*110110 :test #'eql :test-not #'eql))

(defharmless find-bit-string.test-and-test-not.2
  (find 0 #*110110 :test-not #'eql :test #'eql))

839 840 841 842
;;; Keyword tests

(deftest find.allow-other-keys.1
  (find 0 '(1 2 3 4 5) :key #'(lambda (x) (mod x 2))
sds's avatar
sds committed
843
        :bad t :allow-other-keys t)
844 845 846 847
  2)

(deftest find.allow-other-keys.2
  (find 0 '(1 2 3 4 5) :key #'(lambda (x) (mod x 2))
sds's avatar
sds committed
848
               :allow-other-keys t :also-bad t)
849 850 851 852 853
  2)

;;; The leftmost of two :allow-other-keys arguments is the one that  matters.
(deftest find.allow-other-keys.3
  (find 0 '(1 2 3 4 5) :key #'(lambda (x) (mod x 2))
sds's avatar
sds committed
854 855 856
        :allow-other-keys t
        :allow-other-keys nil
        :bad t)
857 858 859 860 861 862 863 864 865 866 867
  2)

(deftest find.keywords.4
  (find 2 '(1 2 3 4 5) :key #'identity :key #'1+)
  2)

(deftest find.allow-other-keys.5
  (find 'b '(nil a b c nil) :allow-other-keys nil)
  b)


868 869
;;; Error tests

870
(deftest find.error.1
871 872
  (check-type-error #'(lambda (x) (find 'a x)) #'(lambda (x) (typep x 'sequence)))
  nil)
873

874
(deftest find.error.4
875 876
  (signals-error (find 'e '(a b c . d)) type-error)
  t)
877

878
(deftest find.error.5
879 880
  (signals-error (find) program-error)
  t)
881 882

(deftest find.error.6
883 884
  (signals-error (find 'a) program-error)
  t)
885 886

(deftest find.error.7
887 888
  (signals-error (find 'a nil :bad t) program-error)
  t)
889 890

(deftest find.error.8
891
  (signals-error (find 'a nil :bad t :allow-other-keys nil)
sds's avatar
sds committed
892
                 program-error)
893
  t)
894 895

(deftest find.error.9
896 897
  (signals-error (find 'a nil 1 1) program-error)
  t)
898 899

(deftest find.error.10
900 901
  (signals-error (find 'a nil :key) program-error)
  t)
902 903

(deftest find.error.11
904 905
  (signals-error (locally (find 'a 'b) t) type-error)
  t)
906

907
(deftest find.error.12
908 909
  (signals-error (find 'b '(a b c) :test #'identity) program-error)
  t)
910 911

(deftest find.error.13
912 913
  (signals-error (find 'b '(a b c) :test-not #'identity) program-error)
  t)
914 915

(deftest find.error.14
916 917
  (signals-error (find 'c '(a b c) :key #'cons) program-error)
  t)
918 919

(deftest find.error.15
920 921
  (signals-error (find 'c '(a b c) :key #'car) type-error)
  t)
922 923


924 925 926 927 928 929
;;; Order of evaluation tests

(deftest find.order.1
  (let ((i 0) x y)
    (values
     (find (progn (setf x (incf i)) 'a)
sds's avatar
sds committed
930
           (progn (setf y (incf i)) '(nil nil nil a nil nil)))
931 932 933 934
     i x y))
  a 2 1 2)

(deftest find.order.2
935
  (let ((i 0) a b c d e f)
936 937
    (values
     (find (progn (setf a (incf i)) nil)
sds's avatar
sds committed
938 939 940 941 942 943
           (progn (setf b (incf i)) '(nil nil nil a nil nil))
           :start (progn (setf c (incf i)) 1)
           :end   (progn (setf d (incf i)) 4)
           :from-end (setf e (incf i))
           :key   (progn (setf f (incf i)) #'null)
           )
944 945 946 947
     i a b c d e f))
  a 6 1 2 3 4 5 6)

(deftest find.order.3
948
  (let ((i 0) a b c d e f)
949 950
    (values
     (find (progn (setf a (incf i)) nil)
sds's avatar
sds committed
951 952 953 954 955 956
           (progn (setf b (incf i)) '(nil nil nil a nil nil))
           :key   (progn (setf c (incf i)) #'null)
           :from-end (setf d (incf i))
           :end   (progn (setf e (incf i)) 4)
           :start (progn (setf f (incf i)) 1)
           )
957 958
     i a b c d e f))
  a 6 1 2 3 4 5 6)