1 ;;;; tests related to sequences
3 ;;;; This file is impure because we want to be able to use DEFUN.
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; While most of SBCL is derived from the CMU CL system, the test
9 ;;;; files (like this one) were written from scratch after the fork
12 ;;;; This software is in the public domain and is provided with
13 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
14 ;;;; more information.
18 ;;; helper functions for exercising SEQUENCE code on data of many
19 ;;; specialized types, and in many different optimization scenarios
20 (defun for-every-seq-1 (base-seq snippet)
21 (dolist (seq-type '(list
24 (simple-array character 1)
26 (simple-array (signed-byte 4) 1)
27 (vector (signed-byte 4))))
28 (flet ((entirely (eltype)
29 (every (lambda (el) (typep el eltype)) base-seq)))
30 (dolist (declaredness '(nil t))
31 (dolist (optimization '(((speed 3) (space 0))
34 ((speed 0) (space 1))))
35 (let* ((seq (if (eq seq-type 'list)
36 (coerce base-seq 'list)
37 (destructuring-bind (type-first &rest type-rest)
41 (destructuring-bind (eltype one) type-rest
44 (coerce base-seq seq-type)
47 (destructuring-bind (eltype) type-rest
49 (replace (make-array (length base-seq)
54 (lambda-expr `(lambda (seq)
56 `((declare (type ,seq-type seq))))
57 (declare (optimize ,@optimization))
59 (multiple-value-bind (fun warnings-p failure-p)
60 (compile nil lambda-expr)
61 (when (or warnings-p failure-p)
62 (error "~@<failed compilation:~2I ~_WARNINGS-P=~S ~_FAILURE-P=~S ~_LAMBDA-EXPR=~S~:@>" lambda-expr))
63 (unless (funcall fun seq)
64 (error "~@<failed test:~2I ~_BASE-SEQ=~S ~_SNIPPET=~S ~_SEQ-TYPE=~S ~_DECLAREDNESS=~S ~_OPTIMIZATION=~S~:@>"
70 (defun for-every-seq (base-seq snippets)
71 (dolist (snippet snippets)
72 (for-every-seq-1 base-seq snippet)))
74 ;;; tests of FIND, POSITION, FIND-IF, and POSITION-IF (and a few for
75 ;;; deprecated FIND-IF-NOT and POSITION-IF-NOT too)
78 (null (find 1 seq :from-end t))
79 (null (position 1 seq :key #'abs))
80 (null (position nil seq :test (constantly t)))
81 (null (position nil seq :test nil))
82 (null (position nil seq :test-not nil))
83 (null (find-if #'1+ seq :key #'log))
84 (null (position-if #'identity seq :from-end t))
85 (null (find-if-not #'packagep seq))
86 (null (position-if-not #'packagep seq :key nil))))
89 (find 2 seq :key #'1+)
90 (find 1 seq :from-end t)
91 (null (find 0 seq :from-end t))
92 (eql 0 (position 1 seq :key #'abs))
93 (null (position nil seq :test 'equal))
94 (eql 1 (find-if #'1- seq :key #'log))
95 (eql 0 (position-if #'identity seq :from-end t))
96 (null (find-if-not #'sin seq))
97 (eql 0 (position-if-not #'packagep seq :key 'identity))))
98 (for-every-seq #(1 2 3 2 1)
100 (find 3 seq :from-end 'yes)
101 (eql 0 (position 0 seq :key '1-))
102 (eql 4 (position 0 seq :key '1- :from-end t))
103 (eql 2 (position 4 seq :key '1+))
104 (eql 2 (position 4 seq :key '1+ :from-end t))
105 (eql 1 (position 2 seq))
106 (eql 3 (position 2 seq :key nil :from-end t))
107 (eql 2 (position 3 seq :test '=))
108 (eql 0 (position 3 seq :test-not 'equalp))
109 (eql 2 (position 3 seq :test 'equal :from-end t))
110 (null (position 4 seq :test #'eql))
111 (null (find-if #'packagep seq))
112 (eql 1 (find-if #'plusp seq))
113 (eql 3 (position-if #'plusp seq :key #'1- :from-end t))
114 (eql 1 (position-if #'evenp seq))
115 (eql 3 (position-if #'evenp seq :from-end t))
116 (null (find-if-not #'plusp seq))
117 (eql 0 (position-if-not #'evenp seq))))
118 (for-every-seq "string test"
119 '((null (find 0 seq))
120 (null (find #\D seq :key #'char-upcase))
121 (find #\E seq :key #'char-upcase)
122 (null (find #\e seq :key #'char-upcase))
123 (eql 3 (position #\i seq))
124 (eql 0 (position #\s seq :key #'char-downcase))
125 (eql 1 (position #\s seq :key #'char-downcase :test #'char/=))
126 (eql 9 (position #\s seq :from-end t :test #'char=))
127 (eql 10 (position #\s seq :from-end t :test #'char/=))
128 (eql 4 (position #\N seq :from-end t :key 'char-upcase :test #'char-equal))
129 (eql 5 (position-if (lambda (c) (equal #\g c)) seq))
130 (eql 5 (position-if (lambda (c) (equal #\g c)) seq :from-end t))
131 (find-if #'characterp seq)
132 (find-if #'(lambda (c) (typep c 'base-char)) seq :from-end t)
133 (null (find-if 'upper-case-p seq))))
136 (quit :unix-status 104)