reverse.lsp 4.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Tue Aug 20 23:47:28 2002
;;;; Contains: Tests for REVERSE

(in-package :cl-test)

(deftest reverse-list.1
  (reverse nil)
  nil)

(deftest reverse-list.2
  (let ((x '(a b c)))
    (values (reverse x) x))
  (c b a)
  (a b c))

(deftest reverse-vector.1
  (reverse #())
  #())

(deftest reverse-vector.2
  (let ((x #(a b c d e)))
    (values (reverse x) x))
  #(e d c b a)
  #(a b c d e))

28
(deftest reverse-vector.3
29 30 31 32
  (let ((x (make-array 0 :fill-pointer t :adjustable t)))
    (reverse x))
  #())

33
(deftest reverse-vector.4
34
  (let* ((x (make-array 5 :initial-contents '(1 2 3 4 5)
sds's avatar
sds committed
35 36
                        :fill-pointer t :adjustable t))
         (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)
sds's avatar
sds committed
43 44
                        :fill-pointer 5))
         (y (reverse x)))
45 46 47
    y)
  #(5 4 3 2 1))

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
;;; Other unusual vectors

(deftest reverse-vector.6
  (do-special-integer-vectors
   (v #(1 1 0 1 1 0) nil)
   (let ((nv (reverse v)))
     (assert (typep nv 'simple-array))
     (assert (not (eql v nv)))
     (assert (equalp nv #(0 1 1 0 1 1)))
     (assert (equalp v #(1 1 0 1 1 0)))))
  nil)

(deftest reverse-vector.7
  (do-special-integer-vectors
   (v #(-1 -1 0 -1 -1 0) nil)
   (let ((nv (reverse v)))
     (assert (typep nv 'simple-array))
     (assert (not (eql v nv)))
     (assert (equalp nv #(0 -1 -1 0 -1 -1)))
     (assert (equalp v #(-1 -1 0 -1 -1 0)))))
  nil)

(deftest reverse-vector.8
  (let ((len 10))
    (loop for etype in '(short-float single-float double-float long-float rational)
sds's avatar
sds committed
73 74 75 76 77 78 79 80 81
          for vals = (loop for i from 1 to len collect (coerce i etype))
          for vec = (make-array len :element-type etype :initial-contents vals)
          for nvec = (reverse vec)
          unless (and (eql (length nvec) len)
                      (typep nvec 'simple-array)
                      (not (eql vec nvec))
                      (every #'eql (reverse vals) nvec)
                      (every #'eql vals vec))
          collect (list etype vals vec nvec)))
82 83 84 85 86
  nil)

(deftest reverse-vector.9
  (let ((len 10))
    (loop for cetype in '(short-float single-float double-float long-float rational integer)
sds's avatar
sds committed
87 88 89 90 91 92 93 94 95 96 97
          for etype = `(complex ,cetype)
          for vals = (loop for i from 1 to len collect (complex (coerce i cetype)
                                                                (coerce (- i) cetype)))
          for vec = (make-array len :element-type etype :initial-contents vals)
          for nvec = (reverse vec)
          unless (and (eql (length nvec) len)
                      (typep nvec 'simple-array)
                      (not (eql vec nvec))
                      (every #'eql (reverse vals) nvec)
                      (every #'eql vals vec))
          collect (list etype vals vec nvec)))
98 99 100 101
  nil)

;;; Bit vectors

102
(deftest reverse-bit-vector.1
103 104 105
  (reverse #*)
  #*)

106
(deftest reverse-bit-vector.2
107 108 109 110 111
  (let ((x #*000110110110))
    (values (reverse x) x))
  #*011011011000
  #*000110110110)

112 113
(deftest reverse-bit-vector.3
  (let* ((x (make-array 10 :initial-contents '(0 0 0 1 1 0 1 0 1 0)
sds's avatar
sds committed
114 115 116
                        :fill-pointer 5
                        :element-type 'bit))
         (y (reverse x)))
117 118 119
    y)
  #*11000)

120 121
;;; Strings

122 123 124 125 126 127 128 129 130 131
(deftest reverse-string.1
  (reverse "")
  "")

(deftest reverse-string.2
  (let ((x "000110110110"))
    (values (reverse x) x))
  "011011011000"
  "000110110110")

132
(deftest reverse-string.3
133
  (let* ((x (make-array 10 :initial-contents "abcdefghij"
sds's avatar
sds committed
134 135 136
                        :fill-pointer 5
                        :element-type 'character))
         (y (reverse x)))
137 138 139 140
    y)
  "edcba")

(deftest reverse-string.4
141
  (let* ((x (make-array 10 :initial-contents "abcdefghij"
sds's avatar
sds committed
142 143 144
                        :fill-pointer 5
                        :element-type 'base-char))
         (y (reverse x)))
145 146 147
    y)
  "edcba")

148 149 150 151 152 153 154 155 156 157 158 159
;;; Specialized string tests

(deftest reverse-string.5
  (do-special-strings
   (s (copy-seq "12345") nil)
   (let ((s2 (reverse s)))
     (assert (typep s2 'simple-array))
     (assert (equal (array-element-type s) (array-element-type s2)))
     (assert (string= "12345" s))
     (assert (string= "54321" s2))))
  nil)

160 161
;;; Order, number of times of evaluation

162 163 164 165 166 167 168
(deftest reverse.order.1
  (let ((i 0))
    (values
     (reverse (progn (incf i) (list 'a 'b 'c 'd)))
     i))
  (d c b a) 1)

169 170
;;; Constant folding tests

171 172 173 174
(def-fold-test reverse.fold.1 (reverse '(a b c)))
(def-fold-test reverse.fold.2 (reverse #(a b c)))
(def-fold-test reverse.fold.3 (reverse #*00111101011011))
(def-fold-test reverse.fold.4 (reverse "abcdefgh"))
175

176 177 178
;;; Error cases

(deftest reverse.error.1
179 180
  (check-type-error #'reverse #'sequencep)
  nil)
181

182
(deftest reverse.error.6
183 184
  (signals-error (reverse) program-error)
  t)
185 186

(deftest reverse.error.7
187 188
  (signals-error (reverse nil nil) program-error)
  t)
189

190
(deftest reverse.error.8
191 192
  (signals-error (locally (reverse 'a) t) type-error)
  t)