search-vector.lsp 6.2 KB
Newer Older
1 2 3 4 5 6 7
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Aug 25 13:06:54 2002
;;;; Contains: Tests for SEARCH on vectors

(in-package :cl-test)

8 9
(compile-and-load "search-aux.lsp")

10 11
(deftest search-vector.1
  (let ((target *searched-vector*)
sds's avatar
sds committed
12
        (pat #(a)))
13
    (loop for i from 0 to (1- (length target))
sds's avatar
sds committed
14 15 16 17
          for tail = (subseq target i)
          always
          (let ((pos (search pat tail)))
            (search-check pat tail pos))))
18 19 20 21
  t)

(deftest search-vector.2
  (let ((target *searched-vector*)
sds's avatar
sds committed
22
        (pat #(a)))
23
    (loop for i from 1 to (length target)
sds's avatar
sds committed
24 25 26
          always
          (let ((pos (search pat target :end2 i :from-end t)))
            (search-check pat target pos :end2 i :from-end t))))
27 28 29 30 31
  t)

(deftest search-vector.3
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
32 33 34
          for pos = (search pat target)
          unless (search-check pat target pos)
          collect pat))
35 36 37 38 39
  nil)

(deftest search-vector.4
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
40 41 42
          for pos = (search pat target :from-end t)
          unless (search-check pat target pos :from-end t)
          collect pat))
43 44 45 46 47
  nil)

(deftest search-vector.5
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
48 49 50
          for pos = (search pat target :start2 25 :end2 75)
          unless (search-check pat target pos :start2 25 :end2 75)
          collect pat))
51 52 53 54 55
  nil)

(deftest search-vector.6
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
56 57 58 59
          for pos = (search pat target :from-end t :start2 25 :end2 75)
          unless (search-check pat target pos :from-end t
                               :start2 25 :end2 75)
          collect pat))
60 61 62 63 64
  nil)

(deftest search-vector.7
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
65 66 67
          for pos = (search pat target :start2 20)
          unless (search-check pat target pos :start2 20)
          collect pat))
68 69 70 71 72
  nil)

(deftest search-vector.8
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
73 74 75 76
          for pos = (search pat target :from-end t :start2 20)
          unless (search-check pat target pos :from-end t
                               :start2 20)
          collect pat))
77 78 79 80
  nil)

(deftest search-vector.9
  (let ((target (map 'vector #'(lambda (x) (sublis '((a . 1) (b . 2)) x))
sds's avatar
sds committed
81
                     *searched-list*)))
82
    (loop for pat in (mapcar #'(lambda (x)
sds's avatar
sds committed
83 84 85 86 87 88 89 90
                                 (map 'vector
                                      #'(lambda (y)
                                          (sublis '((a . 3) (b . 4)) y))
                                      x))
                             *pattern-sublists*)
          for pos = (search pat target :start2 20 :key #'evenp)
          unless (search-check pat target pos :start2 20 :key #'evenp)
          collect pat))
91 92 93 94
  nil)

(deftest search-vector.10
  (let ((target (map 'vector #'(lambda (x) (sublis '((a . 1) (b . 2)) x))
sds's avatar
sds committed
95
                     *searched-list*)))
96
    (loop for pat in (mapcar #'(lambda (x)
sds's avatar
sds committed
97 98 99 100 101 102 103 104 105
                                 (map 'vector
                                      #'(lambda (y)
                                          (sublis '((a . 3) (b . 4)) y))
                                      x))
                             *pattern-sublists*)
          for pos = (search pat target :from-end t :start2 20 :key 'oddp)
          unless (search-check pat target pos :from-end t
                               :start2 20 :key 'oddp)
          collect pat))
106 107 108 109 110
  nil)

(deftest search-vector.11
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
111 112 113 114
          for pos = (search pat target :start2 20 :test (complement #'eql))
          unless (search-check pat target pos :start2 20
                               :test (complement #'eql))
          collect pat))
115 116 117 118 119
  nil)

(deftest search-vector.12
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
120 121 122 123
          for pos = (search pat target :from-end t :start2 20 :test-not #'eql)
          unless (search-check pat target pos :from-end t
                               :start2 20 :test (complement #'eql))
          collect pat))
124 125 126 127 128
  nil)

(deftest search-vector.13
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
129 130 131 132 133 134 135
          when (and (> (length pat) 0)
                    (let ((pos (search pat target :start1 1
                                       :test (complement #'eql))))
                      (not (search-check pat target pos
                                         :start1 1
                                         :test (complement #'eql)))))
          collect pat))
136 137 138 139 140
  nil)

(deftest search-vector.14
  (let ((target *searched-vector*))
    (loop for pat in *pattern-subvectors*
sds's avatar
sds committed
141 142 143 144 145 146 147 148
          when (let ((len (length pat)))
                 (and (> len 0)
                      (let ((pos (search pat target :end1 (1- len)
                                         :test (complement #'eql))))
                      (not (search-check pat target pos
                                         :end1 (1- len)
                                         :test (complement #'eql))))))
          collect pat))
149
  nil)
150 151 152

(deftest search-vector.15
  (let ((a (make-array '(10) :initial-contents '(a b b a a a b a b b)
sds's avatar
sds committed
153
                       :fill-pointer 5)))
154 155 156 157 158 159 160 161 162 163 164
    (values
     (search '(a) a)
     (search '(a) a :from-end t)
     (search '(a b) a)
     (search '(a b) a :from-end t)
     (search '(a b a) a)
     (search '(a b a) a :from-end t)))
  0 4 0 0 nil nil)

(deftest search-vector.16
  (let ((pat (make-array '(3) :initial-contents '(a b a)
sds's avatar
sds committed
165 166
                         :fill-pointer 1))
        (a #(a b b a a)))
167 168 169 170 171 172 173 174 175 176 177 178
    (values
     (search pat a)
     (search pat a :from-end t)
     (progn
       (setf (fill-pointer pat) 2)
       (search pat a))
     (search pat a :from-end t)
     (progn
       (setf (fill-pointer pat) 3)
       (search pat a))
     (search pat a :from-end t)))
  0 4 0 0 nil nil)
179 180 181 182 183

;; Order of test, test-not

(deftest search-vector.17
  (let ((pat #(10))
sds's avatar
sds committed
184
        (target #(1 4 6 10 15 20)))
185 186 187 188 189
    (search pat target :test #'<))
  4)

(deftest search-vector.18
  (let ((pat #(10))
sds's avatar
sds committed
190
        (target #(1 4 6 10 15 20)))
191
    (search pat target :test-not #'>=))
sds's avatar
sds committed
192
  4)