nreverse.lsp 4.21 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
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Wed Aug 21 00:04:57 2002
;;;; Contains: Tests for NREVERSE

(in-package :cl-test)

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

(deftest nreverse-list.2
  (let ((x (copy-seq '(a b c))))
    (nreverse x))
  (c b a))

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

(deftest nreverse-vector.2
  (let ((x (copy-seq #(a b c d e))))
    (nreverse x))
  #(e d c b a))

26
(deftest nreverse-vector.4
27 28 29 30
  (let ((x (make-array 0 :fill-pointer t :adjustable t)))
    (nreverse x))
  #())

31
(deftest nreverse-vector.5
32
  (let* ((x (make-array 5 :initial-contents '(1 2 3 4 5)
sds's avatar
sds committed
33 34
                        :fill-pointer t :adjustable t))
         (y (nreverse x)))
35
    (values y (equalt (type-of x) (type-of y))))
36 37 38
  #(5 4 3 2 1)
  t)

39
(deftest nreverse-vector.6
40
  (let* ((x (make-array 10 :initial-contents '(1 2 3 4 5 6 7 8 9 10)
sds's avatar
sds committed
41 42
                        :fill-pointer 5))
         (y (nreverse x)))
43 44 45 46
    (values y (equalt (type-of x) (type-of y))))
  #(5 4 3 2 1)
  t)

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
;;; Unusual vectors

(deftest nreverse-vector.7
  (do-special-integer-vectors
   (v #(0 0 1 0 1 1) nil)
   (let ((nv (nreverse v)))
     (assert (= (length nv) 6))
     (assert (every #'= nv #(1 1 0 1 0 0)))))
  nil)

(deftest nreverse-vector.8
  (do-special-integer-vectors
   (v #(0 0 -1 0 -1 -1 0 -1) nil)
   (let ((nv (nreverse v)))
     (assert (= (length nv) 8))
     (assert (every #'= nv #(-1 0 -1 -1 0 -1 0 0)))))
  nil)

(deftest nreverse-vector.9
  (let ((len 10))
    (loop for etype in '(short-float single-float double-float long-float rational)
sds's avatar
sds committed
68 69 70 71 72 73
          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 = (nreverse vec)
          unless (and (eql (length nvec) len)
                      (every #'eql (reverse vals) nvec))
          collect (list etype vals nvec)))
74 75 76 77 78
  nil)

(deftest nreverse-vector.10
  (let ((len 10))
    (loop for cetype in '(short-float single-float double-float long-float rational integer)
sds's avatar
sds committed
79 80 81 82 83 84 85 86
          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 = (nreverse vec)
          unless (and (eql (length nvec) len)
                      (every #'eql (reverse vals) nvec))
          collect (list etype vals nvec)))
87 88 89 90 91
  nil)


;;; Bit vectors

92
(deftest nreverse-bit-vector.1
93 94 95
  (nreverse #*)
  #*)

96
(deftest nreverse-bit-vector.2
97 98 99 100
  (let ((x (copy-seq #*000110110110)))
    (nreverse x))
  #*011011011000)

101 102
(deftest nreverse-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
103 104 105
                        :fill-pointer 5
                        :element-type 'bit))
         (y (nreverse x)))
106 107 108
    y)
  #*11000)

109 110
;;; Strings

111 112 113 114 115 116 117 118 119
(deftest nreverse-string.1
  (nreverse "")
  "")

(deftest nreverse-string.2
  (let ((x (copy-seq "000110110110")))
    (nreverse x))
  "011011011000")

120
(deftest nreverse-string.3
121
  (let* ((x (make-array 10 :initial-contents "abcdefghij"
sds's avatar
sds committed
122 123 124
                        :fill-pointer 5
                        :element-type 'character))
         (y (nreverse x)))
125 126 127 128
    y)
  "edcba")

(deftest nreverse-string.4
129
  (let* ((x (make-array 10 :initial-contents "abcdefghij"
sds's avatar
sds committed
130 131 132
                        :fill-pointer 5
                        :element-type 'base-char))
         (y (nreverse x)))
133 134 135
    y)
  "edcba")

136 137 138 139 140 141 142 143 144
(deftest nreverse-string.5
  (do-special-strings
   (s (copy-seq "12345") nil)
   (let ((s2 (nreverse s)))
     (assert (stringp s2))
     (assert (string= s2 "54321"))
     (assert (equal (array-element-type s) (array-element-type s2)))))
  nil)

145 146
;;; Argument is evaluated only once

147 148 149 150 151 152 153
(deftest nreverse.order.1
  (let ((i 0))
    (values
     (nreverse (progn (incf i) (list 'a 'b 'c 'd)))
     i))
  (d c b a) 1)

154 155
;;; Error tests

156
(deftest nreverse.error.1
pfdietz's avatar
pfdietz committed
157 158
  (check-type-error #'nreverse #'sequencep)
  nil)
159

160
(deftest nreverse.error.6
161 162
  (signals-error (nreverse) program-error)
  t)
163

164
(deftest nreverse.error.7
165 166
  (signals-error (nreverse nil nil) program-error)
  t)
167

168
(deftest nreverse.error.8
169 170
  (signals-error (locally (nreverse 'a) t) type-error)
  t)