File: test.lisp

package info (click to toggle)
cl-utilities 1.2.4-3.2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 180 kB
  • sloc: lisp: 760; sh: 16; makefile: 13
file content (178 lines) | stat: -rw-r--r-- 6,657 bytes parent folder | download | duplicates (5)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
;; This file requires the FiveAM unit testing framework.
(eval-when (:compile-toplevel :load-toplevel :execute)
  (asdf:oos 'asdf:load-op :fiveam)
  (asdf:oos 'asdf:load-op :cl-utilities))

;; To run all the tests:
;; (5am:run! 'cl-utilities-tests::cl-utilities-suite)

(defpackage :cl-utilities-tests
  (:use :common-lisp :cl-utilities :5am))

(in-package :cl-utilities-tests)

(def-suite cl-utilities-suite :description "Test suite for cl-utilities")
(in-suite cl-utilities-suite)

;; These tests were taken directly from the comments at the top of
;; split-sequence.lisp
(test split-sequence
  (is (tree-equal (values (split-sequence #\; "a;;b;c"))
		  '("a" "" "b" "c") :test #'equal))
  (is (tree-equal (values (split-sequence #\; "a;;b;c" :from-end t))
		  '("a" "" "b" "c") :test #'equal))
  (is (tree-equal (values (split-sequence #\; "a;;b;c" :from-end t :count 1))
		  '("c") :test #'equal))
  (is (tree-equal (values (split-sequence #\; "a;;b;c" :remove-empty-subseqs t))
		  '("a" "b" "c") :test #'equal))
  (is (tree-equal (values (split-sequence-if (lambda (x)
					       (member x '(#\a #\b)))
					     "abracadabra"))
		  '("" "" "r" "c" "d" "" "r" "") :test #'equal))
  (is (tree-equal (values (split-sequence-if-not (lambda (x)
						   (member x '(#\a #\b)))
						 "abracadabra"))
		  '("ab" "a" "a" "ab" "a") :test #'equal))
  (is (tree-equal (values (split-sequence #\; ";oo;bar;ba;" :start 1 :end 9))
		  '("oo" "bar" "b") :test #'equal)))

(test extremum
  (is (= (extremum '(1 23 3 4 5 0) #'< :start 1 :end 4) 3))
  (signals no-extremum (extremum '() #'<))
  (is-false (handler-bind ((no-extremum #'continue))
	      (extremum '() #'<)))
  (is (= (extremum '(2/3 2 3 4) #'> :key (lambda (x) (/ 1 x))) 2/3))
  (is (= (locally (declare (optimize (speed 3) (safety 0)))
	   (extremum #(1 23 3 4 5 0) #'>))
	 23))
  (is (= (extremum-fastkey '(2/3 2 3 4) #'> :key (lambda (x) (/ 1 x))) 2/3)))

(test extrema
  (is (tree-equal (extrema '(3 2 1 1 2 1) #'<)
		  '(1 1 1)))
  (is (tree-equal (extrema #(3 2 1 1 2 1) #'<)
		  '(1 1 1)))
  (is (tree-equal (extrema #(3 2 1 1 2 1) #'< :end 4)
		  '(1 1)))
  (is (tree-equal (extrema '(3 2 1 1 2 1) #'< :end 4)
		  '(1 1)))
  (is (tree-equal (extrema #(3 2 1 1 2 1) #'< :start 3 :end 4)
		  '(1)))
  (is (tree-equal (extrema '((A . 3) (B . 1) (C . 2) (D . 1)) #'< :key #'cdr)
		  '((B . 1) (D . 1)))))

(defmacro quietly (&body body)
  "Perform BODY quietly, muffling any warnings that may arise"
  `(handler-bind ((warning #'muffle-warning))
    ,@body))

(test n-most-extreme
  (is (tree-equal (n-most-extreme 1 '(3 1 2 1) #'>)
		  '(3)))
  (is (tree-equal (n-most-extreme 2 '(3 1 2 1) #'>)
		  '(3 2)))
  (is (tree-equal (n-most-extreme 2 '(3 1 2 1) #'<)
		  '(1 1)))
  (is (tree-equal (n-most-extreme 1 '((A . 3) (B . 1) (C . 2) (D . 1)) #'> :key #'cdr)
		  '((A . 3))))
  (is (tree-equal (n-most-extreme 2 '((A . 3) (B . 1) (C . 2) (D . 1)) #'< :key #'cdr)
		  '((B . 1) (D . 1))))
  (is (tree-equal (quietly (n-most-extreme 20 '((A . 3) (B . 1) (C . 2) (D . 1)) #'< :key #'cdr))
		  '((B . 1) (D . 1) (C . 2) (A . 3))))
  (is (tree-equal (quietly (n-most-extreme 2 '((A . 3) (B . 1) (C . 2) (D . 1)) #'< :key #'cdr :start 1 :end 2))
		  '((B . 1))))
  (signals n-most-extreme-not-enough-elements (n-most-extreme 2 '((A . 3) (B . 1) (C . 2) (D . 1)) #'< :key #'cdr :start 1 :end 2)))

(defun delimited-test (&key (delimiter #\|) (start 0) end
		       (string "foogo|ogreogrjejgierjijri|bar|baz"))
  (with-input-from-string (str string)
    (let ((buffer (copy-seq "            ")))
      (multiple-value-bind (position delimited-p)
	  (read-delimited buffer str
			  :delimiter delimiter :start start :end end)
	(declare (ignore delimited-p))
	(subseq buffer 0 position)))))

(test read-delimited
  (is (string= (delimited-test) "foogo"))
  (is (string= (delimited-test :delimiter #\t) "foogo|ogreog"))
  (is (string= (delimited-test :delimiter #\t :start 3) "   foogo|ogr"))
  (is (string= (delimited-test :start 3) "   foogo"))
  (is (string= (delimited-test :end 3) "foo"))
  (is (string= (delimited-test :start 1 :end 3) " fo"))
  (is (string= (delimited-test :string "Hello") "Hello"))
  (is (string= (delimited-test :string "Hello" :start 3) "   Hello"))
  (is (string= (handler-bind ((read-delimited-bounds-error #'continue))
		 (delimited-test :start 3 :end 1))
	       " fo"))
  (signals type-error (delimited-test :start 3/2))
  (signals read-delimited-bounds-error (delimited-test :start -3))
  (signals read-delimited-bounds-error (delimited-test :end 30))
  (signals read-delimited-bounds-error (delimited-test :start 3 :end 1)))

;; Random testing would probably work better here.
(test expt-mod
  (is (= (expt-mod 2 34 54) (mod (expt 2 34) 54)))
  (is (= (expt-mod 20 3 54) (mod (expt 20 3) 54)))
  (is (= (expt-mod 2.5 3.8 34.9) (mod (expt 2.5 3.8) 34.9)))
  (is (= (expt-mod 2/5 3/8 34/9) (mod (expt 2/5 3/8) 34/9))))

(test collecting
  (is (tree-equal (collecting (dotimes (x 10) (collect x)))
		  '(0 1 2 3 4 5 6 7 8 9)))
  (is (tree-equal (collecting
		   (labels ((collect-it (x) (collect x)))
		     (mapcar #'collect-it (reverse '(c b a)))))
		  '(a b c)))
  (is (tree-equal (multiple-value-bind (a b)
		      (with-collectors (x y)
			(x 1)
			(y 2)
			(x 3))
		    (append a b))
		  '(1 3 2))))

(test with-unique-names
  (is (equalp (subseq (with-unique-names (foo)
			(string foo))
		      0 3)
	      "foo"))
  (is (equalp (subseq (with-unique-names ((foo "bar"))
			(string foo))
		      0 3)
	      "bar"))
  (is (equalp (subseq (with-unique-names ((foo baz))
			(string foo))
		      0 3)
	      "baz"))
  (is (equalp (subseq (with-unique-names ((foo #\y))
			(string foo))
		      0 1)
	      "y"))
  (is (equalp (subseq (with-gensyms (foo)
			(string foo))
		      0 3)
	      "foo")))

;; Taken from spec
(test rotate-byte
  (is (= (rotate-byte 3 (byte 32 0) 3) 24))
  (is (= (rotate-byte 3 (byte 5 5) 3) 3))
  (is (= (rotate-byte 6 (byte 8 0) -3) -129)))

(test copy-array
  (let ((test-array (make-array '(10 10) :initial-element 5)))
    (is (not (eq (copy-array test-array) test-array)))
    (is (equalp (copy-array test-array) test-array))))

(test compose
  (labels ((2* (x) (* 2 x)))
    (is (= (funcall (compose #'1+ #'1+) 1) 3))
    (is (= (funcall (compose '1+ #'2*) 5) 11))
    (is (= (funcall (compose #'1+ #'2* '1+) 6) 15))
    ;; This should signal an undefined function error, since we're
    ;; using '2* rather than #'2*, which means that COMPOSE will use
    ;; the dynamic binding at the time it is called rather than the
    ;; lexical binding here.
    (signals undefined-function
	     (= (funcall (compose #'1+ '2* '1+) 6) 15))))