1 | ;-*- Mode: Lisp -*- |
---|
2 | ;;;; Author: Paul Dietz |
---|
3 | ;;;; Created: Tue Aug 20 23:47:28 2002 |
---|
4 | ;;;; Contains: Tests for REVERSE |
---|
5 | |
---|
6 | (in-package :cl-test) |
---|
7 | |
---|
8 | (deftest reverse-list.1 |
---|
9 | (reverse nil) |
---|
10 | nil) |
---|
11 | |
---|
12 | (deftest reverse-list.2 |
---|
13 | (let ((x '(a b c))) |
---|
14 | (values (reverse x) x)) |
---|
15 | (c b a) |
---|
16 | (a b c)) |
---|
17 | |
---|
18 | (deftest reverse-vector.1 |
---|
19 | (reverse #()) |
---|
20 | #()) |
---|
21 | |
---|
22 | (deftest reverse-vector.2 |
---|
23 | (let ((x #(a b c d e))) |
---|
24 | (values (reverse x) x)) |
---|
25 | #(e d c b a) |
---|
26 | #(a b c d e)) |
---|
27 | |
---|
28 | (deftest reverse-vector.3 |
---|
29 | (let ((x (make-array 0 :fill-pointer t :adjustable t))) |
---|
30 | (reverse x)) |
---|
31 | #()) |
---|
32 | |
---|
33 | (deftest reverse-vector.4 |
---|
34 | (let* ((x (make-array 5 :initial-contents '(1 2 3 4 5) |
---|
35 | :fill-pointer t :adjustable t)) |
---|
36 | (y (reverse x))) |
---|
37 | (values y x)) |
---|
38 | #(5 4 3 2 1) |
---|
39 | #(1 2 3 4 5)) |
---|
40 | |
---|
41 | (deftest reverse-vector.5 |
---|
42 | (let* ((x (make-array 10 :initial-contents '(1 2 3 4 5 6 7 8 9 10) |
---|
43 | :fill-pointer 5)) |
---|
44 | (y (reverse x))) |
---|
45 | y) |
---|
46 | #(5 4 3 2 1)) |
---|
47 | |
---|
48 | ;;; Other unusual vectors |
---|
49 | |
---|
50 | (deftest reverse-vector.6 |
---|
51 | (do-special-integer-vectors |
---|
52 | (v #(1 1 0 1 1 0) nil) |
---|
53 | (let ((nv (reverse v))) |
---|
54 | (assert (typep nv 'simple-array)) |
---|
55 | (assert (not (eql v nv))) |
---|
56 | (assert (equalp nv #(0 1 1 0 1 1))) |
---|
57 | (assert (equalp v #(1 1 0 1 1 0))))) |
---|
58 | nil) |
---|
59 | |
---|
60 | (deftest reverse-vector.7 |
---|
61 | (do-special-integer-vectors |
---|
62 | (v #(-1 -1 0 -1 -1 0) nil) |
---|
63 | (let ((nv (reverse v))) |
---|
64 | (assert (typep nv 'simple-array)) |
---|
65 | (assert (not (eql v nv))) |
---|
66 | (assert (equalp nv #(0 -1 -1 0 -1 -1))) |
---|
67 | (assert (equalp v #(-1 -1 0 -1 -1 0))))) |
---|
68 | nil) |
---|
69 | |
---|
70 | (deftest reverse-vector.8 |
---|
71 | (let ((len 10)) |
---|
72 | (loop for etype in '(short-float single-float double-float long-float rational) |
---|
73 | for vals = (loop for i from 1 to len collect (coerce i etype)) |
---|
74 | for vec = (make-array len :element-type etype :initial-contents vals) |
---|
75 | for nvec = (reverse vec) |
---|
76 | unless (and (eql (length nvec) len) |
---|
77 | (typep nvec 'simple-array) |
---|
78 | (not (eql vec nvec)) |
---|
79 | (every #'eql (reverse vals) nvec) |
---|
80 | (every #'eql vals vec)) |
---|
81 | collect (list etype vals vec nvec))) |
---|
82 | nil) |
---|
83 | |
---|
84 | (deftest reverse-vector.9 |
---|
85 | (let ((len 10)) |
---|
86 | (loop for cetype in '(short-float single-float double-float long-float rational integer) |
---|
87 | for etype = `(complex ,cetype) |
---|
88 | for vals = (loop for i from 1 to len collect (complex (coerce i cetype) |
---|
89 | (coerce (- i) cetype))) |
---|
90 | for vec = (make-array len :element-type etype :initial-contents vals) |
---|
91 | for nvec = (reverse vec) |
---|
92 | unless (and (eql (length nvec) len) |
---|
93 | (typep nvec 'simple-array) |
---|
94 | (not (eql vec nvec)) |
---|
95 | (every #'eql (reverse vals) nvec) |
---|
96 | (every #'eql vals vec)) |
---|
97 | collect (list etype vals vec nvec))) |
---|
98 | nil) |
---|
99 | |
---|
100 | ;;; Bit vectors |
---|
101 | |
---|
102 | (deftest reverse-bit-vector.1 |
---|
103 | (reverse #*) |
---|
104 | #*) |
---|
105 | |
---|
106 | (deftest reverse-bit-vector.2 |
---|
107 | (let ((x #*000110110110)) |
---|
108 | (values (reverse x) x)) |
---|
109 | #*011011011000 |
---|
110 | #*000110110110) |
---|
111 | |
---|
112 | (deftest reverse-bit-vector.3 |
---|
113 | (let* ((x (make-array 10 :initial-contents '(0 0 0 1 1 0 1 0 1 0) |
---|
114 | :fill-pointer 5 |
---|
115 | :element-type 'bit)) |
---|
116 | (y (reverse x))) |
---|
117 | y) |
---|
118 | #*11000) |
---|
119 | |
---|
120 | ;;; Strings |
---|
121 | |
---|
122 | (deftest reverse-string.1 |
---|
123 | (reverse "") |
---|
124 | "") |
---|
125 | |
---|
126 | (deftest reverse-string.2 |
---|
127 | (let ((x "000110110110")) |
---|
128 | (values (reverse x) x)) |
---|
129 | "011011011000" |
---|
130 | "000110110110") |
---|
131 | |
---|
132 | (deftest reverse-string.3 |
---|
133 | (let* ((x (make-array 10 :initial-contents "abcdefghij" |
---|
134 | :fill-pointer 5 |
---|
135 | :element-type 'character)) |
---|
136 | (y (reverse x))) |
---|
137 | y) |
---|
138 | "edcba") |
---|
139 | |
---|
140 | (deftest reverse-string.4 |
---|
141 | (let* ((x (make-array 10 :initial-contents "abcdefghij" |
---|
142 | :fill-pointer 5 |
---|
143 | :element-type 'base-char)) |
---|
144 | (y (reverse x))) |
---|
145 | y) |
---|
146 | "edcba") |
---|
147 | |
---|
148 | ;;; Specialized string tests |
---|
149 | |
---|
150 | (deftest reverse-string.5 |
---|
151 | (do-special-strings |
---|
152 | (s (copy-seq "12345") nil) |
---|
153 | (let ((s2 (reverse s))) |
---|
154 | (assert (typep s2 'simple-array)) |
---|
155 | (assert (equal (array-element-type s) (array-element-type s2))) |
---|
156 | (assert (string= "12345" s)) |
---|
157 | (assert (string= "54321" s2)))) |
---|
158 | nil) |
---|
159 | |
---|
160 | ;;; Order, number of times of evaluation |
---|
161 | |
---|
162 | (deftest reverse.order.1 |
---|
163 | (let ((i 0)) |
---|
164 | (values |
---|
165 | (reverse (progn (incf i) (list 'a 'b 'c 'd))) |
---|
166 | i)) |
---|
167 | (d c b a) 1) |
---|
168 | |
---|
169 | ;;; Constant folding tests |
---|
170 | |
---|
171 | (def-fold-test reverse.fold.1 (reverse '(a b c))) |
---|
172 | (def-fold-test reverse.fold.2 (reverse #(a b c))) |
---|
173 | (def-fold-test reverse.fold.3 (reverse #*00111101011011)) |
---|
174 | (def-fold-test reverse.fold.4 (reverse "abcdefgh")) |
---|
175 | |
---|
176 | ;;; Error cases |
---|
177 | |
---|
178 | (deftest reverse.error.1 |
---|
179 | (check-type-error #'reverse #'sequencep) |
---|
180 | nil) |
---|
181 | |
---|
182 | (deftest reverse.error.6 |
---|
183 | (signals-error (reverse) program-error) |
---|
184 | t) |
---|
185 | |
---|
186 | (deftest reverse.error.7 |
---|
187 | (signals-error (reverse nil nil) program-error) |
---|
188 | t) |
---|
189 | |
---|
190 | (deftest reverse.error.8 |
---|
191 | (signals-error (locally (reverse 'a) t) type-error) |
---|
192 | t) |
---|