1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Package: kmrcl-tests -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: kmrcl-tests.lisp
6 ;;;; Purpose: kmrcl tests file
7 ;;;; Author: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2003
12 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
14 ;;;; *************************************************************************
17 (defpackage #:kmrcl-tests
18 (:use #:kmrcl #:cl #:rtest))
19 (in-package #:kmrcl-tests)
24 (deftest :str.0 (substitute-chars-strings "" nil) "")
25 (deftest :str.1 (substitute-chars-strings "abcd" nil) "abcd")
26 (deftest :str.2 (substitute-chars-strings "abcd" nil) "abcd")
27 (deftest :str.3 (substitute-chars-strings "abcd" '((#\j . "ef"))) "abcd")
28 (deftest :str.4 (substitute-chars-strings "abcd" '((#\a . "ef"))) "efbcd")
30 (substitute-chars-strings "abcd" '((#\a . "ef") (#\j . "ghi")))
33 (substitute-chars-strings "abcd" '((#\a . "ef") (#\d . "ghi")))
36 (deftest :str.7 (escape-xml-string "") "")
37 (deftest :str.8 (escape-xml-string "abcd") "abcd")
38 (deftest :str.9 (escape-xml-string "ab&cd") "ab&cd")
39 (deftest :str.10 (escape-xml-string "ab&cd<") "ab&cd<")
40 (deftest :str.12 (string-trim-last-character "") "")
41 (deftest :str.13 (string-trim-last-character "a") "")
42 (deftest :str.14 (string-trim-last-character "ab") "a")
43 (deftest :str.15 (nstring-trim-last-character "") "")
44 (deftest :str.16 (nstring-trim-last-character "a") "")
45 (deftest :str.17 (nstring-trim-last-character "ab") "a")
47 (deftest :str.18 (delimited-string-to-list "ab|cd|ef" #\|)
49 (deftest :str.19 (delimited-string-to-list "ab|cd|ef" #\| t)
51 (deftest :str.20 (delimited-string-to-list "") (""))
52 (deftest :str.21 (delimited-string-to-list "" #\space t) (""))
53 (deftest :str.22 (delimited-string-to-list "ab") ("ab"))
54 (deftest :str.23 (delimited-string-to-list "ab" #\space t) ("ab"))
55 (deftest :str.24 (delimited-string-to-list "ab|" #\|) ("ab" ""))
56 (deftest :str.25 (delimited-string-to-list "ab|" #\| t) ("ab"))
58 (deftest :sdstl.1 (string-delimited-string-to-list "ab|cd|ef" "|a")
60 (deftest :sdstl.2 (string-delimited-string-to-list "ab|cd|ef" "|")
62 (deftest :sdstl.3 (string-delimited-string-to-list "ab|cd|ef" "cd")
64 (deftest :sdstl.4 (string-delimited-string-to-list "ab|cd|ef" "ab")
67 (deftest :apsl.1 (append-sublists '((a b) (c d))) (a b c d))
68 (deftest :apsl.2 (append-sublists nil) nil)
69 (deftest :apsl.3 (append-sublists '((a b))) (a b))
70 (deftest :apsl.4 (append-sublists '((a))) (a))
71 (deftest :apsl.5 (append-sublists '((a) (b) (c d (e f g)))) (a b c d (e f g)))
73 (deftest :pss.0 (with-output-to-string (s) (print-separated-strings s "|" nil))
77 (with-output-to-string (s) (print-separated-strings s "|" '("ab")) )
81 (with-output-to-string (s) (print-separated-strings s "|" '("ab" "cd")))
85 (with-output-to-string (s) (print-separated-strings s "|" '("ab" "cd") nil))
89 (with-output-to-string (s)
90 (print-separated-strings s "|" '("ab" "cd") nil nil))
94 (with-output-to-string (s)
95 (print-separated-strings s "|" '("ab" "cd") nil '("ef") nil))
98 (deftest :css.0 (concat-separated-strings "|" nil) "")
99 (deftest :css.1 (concat-separated-strings "|" nil nil) "")
100 (deftest :css.2 (concat-separated-strings "|" '("ab")) "ab")
101 (deftest :css.3 (concat-separated-strings "|" '("ab" "cd")) "ab|cd")
102 (deftest :css.4 (concat-separated-strings "|" '("ab" "cd") nil) "ab|cd")
103 (deftest :css.5 (concat-separated-strings "|" '("ab" "cd") nil '("ef")) "ab|cd|ef")
105 (deftest :f.1 (map-and-remove-nils #'(lambda (x) (when (oddp x) (* x x)))
106 '(0 1 2 3 4 5 6 7 8 9)) (1 9 25 49 81))
107 (deftest :f.2 (filter #'(lambda (x) (when (oddp x) (* x x)))
108 '(0 1 2 3 4 5 6 7 8 9)) (1 3 5 7 9))
109 (deftest :an.1 (appendnew '(a b c d) '(c c e f)) (a b c d e f))
113 (xml-tag-contents "tag1" "<tag>Test</tag>")
117 (xml-tag-contents "tag" "<tag>Test</tag>")
121 (xml-tag-contents "tag" "<tag >Test</tag>")
125 (xml-tag-contents "tag" "<tag a=\"b\"></tag>")
129 (xml-tag-contents "tag" "<tag a=\"b\" >Test</tag>")
130 "Test" 22 ("a=\"b\""))
133 (xml-tag-contents "tag" "<tag a=\"b\" c=\"ab\">Test</tag>")
134 "Test" 29 ("a=\"b\"" "c=\"ab\""))
137 (xml-tag-contents "tag" "<taga a=\"b\" c=\"ab\">Test</taga>")
141 (xml-tag-contents "tag" "<taga a=\"b\" c=\"ab\">Test<tag>ab</tag></taga>")
145 (xml-tag-contents "tag" "<taga a=\"b\" c=\"ab\">Test<tag>ab</ag></taga>")
148 (deftest :fss.1 (fast-string-search "" "" 0 0 0) 0)
149 (deftest :fss.2 (fast-string-search "" "abc" 0 0 2) 0)
150 (deftest :fss.3 (fast-string-search "abc" "" 3 0 0) nil)
151 (deftest :fss.4 (fast-string-search "abc" "abcde" 3 0 4) 0)
152 (deftest :fss.5 (fast-string-search "abc" "012abcde" 3 0 7) 3)
153 (deftest :fss.6 (fast-string-search "abc" "012abcde" 3 0 7) 3)
154 (deftest :fss.7 (fast-string-search "abc" "012abcde" 3 3 7) 3)
155 (deftest :fss.8 (fast-string-search "abc" "012abcde" 3 4 7) nil)
156 (deftest :fss.9 (fast-string-search "abcde" "012abcde" 5 3 8) 3)
157 (deftest :fss.9b (cl:search "abcde" "012abcde" :start2 3 :end2 8) 3)
158 (deftest :fss.10 (fast-string-search "abcde" "012abcde" 5 3 7) nil)
159 (deftest :fss.10b (cl:search "abcde" "012abcde" :start2 3 :end2 7) nil)
161 (deftest :stlsd.1 (string-to-list-skip-delimiter "") ())
162 (deftest :stlsd.2 (string-to-list-skip-delimiter "abc") ("abc"))
163 (deftest :stlsd.3 (string-to-list-skip-delimiter "ab c") ("ab" "c"))
164 (deftest :stlsd.4 (string-to-list-skip-delimiter "ab c") ("ab" "c"))
165 (deftest :stlsd.5 (string-to-list-skip-delimiter "ab c") ("ab" "c"))
166 (deftest :stlsd.6 (string-to-list-skip-delimiter "ab c ") ("ab" "c"))
167 (deftest :stlsd.7 (string-to-list-skip-delimiter " ab c ") ("ab" "c"))
168 (deftest :stlsd.8 (string-to-list-skip-delimiter "ab,,c" #\,) ("ab" "c"))
169 (deftest :stlsd.9 (string-to-list-skip-delimiter "ab,,c,," #\,) ("ab" "c"))
170 (deftest :stlsd.10 (string-to-list-skip-delimiter " ab") ("ab"))
172 (deftest :csc.1 (count-string-char "" #\a) 0)
173 (deftest :csc.2 (count-string-char "abc" #\d) 0)
174 (deftest :csc.3 (count-string-char "abc" #\b) 1)
175 (deftest :csc.4 (count-string-char "abcb" #\b) 2)
177 (deftest :duqs.1 (decode-uri-query-string "") "")
178 (deftest :duqs.2 (decode-uri-query-string "abc") "abc")
179 (deftest :duqs.3 (decode-uri-query-string "abc+") "abc ")
180 (deftest :duqs.4 (decode-uri-query-string "abc+d") "abc d")
181 (deftest :duqs.5 (decode-uri-query-string "abc%20d") "abc d")
183 (deftest :sse.1 (string-strip-ending "" nil) "")
184 (deftest :sse.2 (string-strip-ending "abc" nil) "abc")
185 (deftest :sse.3 (string-strip-ending "abc" "ab") "abc")
186 (deftest :sse.4 (string-strip-ending "abc" '("ab")) "abc")
187 (deftest :sse.5 (string-strip-ending "abcd" '("a" "cd")) "ab")
190 (defun test-color-conversion ()
197 (multiple-value-bind (r g b) (hsv->rgb h s v)
198 (multiple-value-bind (h2 s2 v2) (rgb->hsv r g b)
199 (unless (hsv-equal h s v h2 s2 v2)
200 (warn "Colors not equal: ~4D ~4D ~4D | ~6D:~6D ~6D:~6D ~6D:~6D~%"
201 (float r) (float g) (float b)
202 (when (typep h 'number) (float h))
203 (when (typep h2 'number) (float h2))
204 (float s) (float s2) (float v) (float v2))
205 (return-from test-color-conversion nil))))))))
208 (defun test-color-conversion-float-255 ()
215 (multiple-value-bind (r g b) (hsv->rgb h s v)
216 (setf r (round (* 255 r))
219 (multiple-value-bind (h2 s2 v2) (rgb255->hsv255 r g b)
220 (unless (hsv-similar h s v h2 (/ s2 255) (/ v2 255)
221 :hue-range 10 :saturation-range .1
222 :value-range 1 :black-limit 0 :gray-limit 0)
223 (warn "Colors not equal: ~4D ~4D ~4D | ~6D:~6D ~6D:~6D ~6D:~6D~%"
225 (when (typep h 'number) (float h))
226 (when (typep h2 'number) (float h2))
227 (float s) (float (/ s2 255)) (float v) (float (/ v2 255)))
228 (return-from test-color-conversion-float-255 nil))))))))
231 (defun test-color-conversion-255-float ()
238 (multiple-value-bind (r g b) (hsv255->rgb255 h (truncate (* 255 s))
239 (truncate (* 255 v)))
244 (multiple-value-bind (h2 s2 v2) (rgb->hsv r g b)
245 (unless (hsv-similar h s v h2 s2 v2
246 :hue-range 10 :saturation-range .1
247 :value-range 1 :black-limit 0 :gray-limit 0)
248 (warn "Colors not equal: ~4D ~4D ~4D | ~6D:~6D ~6D:~6D ~6D:~6D~%"
250 (when (typep h 'number) (float h))
251 (when (typep h2 'number) (float h2))
252 (float s) (float (/ s2 255)) (float v) (float (/ v2 255)))
253 (return-from test-color-conversion-255-float nil))))))))
256 (defun test-color-conversion-255 ()
261 (s (truncate (* 255 (/ is 10))))
262 (v (truncate (* 255 (/ iv 10)))))
263 (multiple-value-bind (r g b) (hsv255->rgb255 h s v)
264 (multiple-value-bind (h2 s2 v2) (rgb255->hsv255 r g b)
265 (unless (hsv255-similar h s v h2 s2 v2 :hue-range 10 :saturation-range 5
266 :value-range 5 :black-limit 0 :gray-limit 0)
267 (warn "Colors not equal: ~D ~D ~D |~
268 ~3,'0D:~3,'0D ~3,'0D:~3,'0D ~3,'0D:~3,'0D~%"
271 (return-from test-color-conversion-255 nil))))))))
274 (deftest :color.conv (test-color-conversion) t)
275 (deftest :color.conv.float.255 (test-color-conversion-float-255) t)
276 (deftest :color.conv.255.float (test-color-conversion-255-float) t)
277 (deftest :color.conv.255 (test-color-conversion-255) t)
279 (deftest :hue.diff.1 (hue-difference 10 10) 0)
280 (deftest :hue.diff.2 (hue-difference 10 9) -1)
281 (deftest :hue.diff.3 (hue-difference 9 10) 1)
282 (deftest :hue.diff.4 (hue-difference 10 nil) 360)
283 (deftest :hue.diff.5 (hue-difference nil 1) 360)
284 (deftest :hue.diff.7 (hue-difference 10 190) 180)
285 (deftest :hue.diff.8 (hue-difference 190 10) -180)
286 (deftest :hue.diff.9 (hue-difference 1 359) -2)
287 (deftest :hue.diff.10 (hue-difference 1 182) -179)
288 (deftest :hue.diff.11 (hue-difference 1 270) -91)
290 (deftest :hsv.sim.1 (hsv-similar 100 .5 .5 110 .5 .5 :hue-range 5
291 :value-range 0 :saturation-range 0
292 :black-limit 0 :gray-limit 0) nil)
293 (deftest :hsv.sim.2 (hsv-similar 100 .5 .5 110 .5 .5 :hue-range 15
294 :value-range 0 :saturation-range 0
295 :black-limit 0 :gray-limit 0) t)
296 (deftest :hsv.sim.3 (hsv-similar 100 .5 .5 110 .5 .6 :hue-range 15
297 :value-range .2 :saturation-range 0
298 :black-limit 0 :gray-limit 0) t)
299 (deftest :hsv.sim.4 (hsv-similar 100 .5 .5 110 .5 .8 :hue-range 15
300 :value-range 0.2 :saturation-range 0
301 :black-limit 0 :gray-limit 0) nil)
302 (deftest :hsv.sim.5 (hsv-similar 100 .5 .5 110 .6 .6 :hue-range 15
303 :value-range 0.2 :saturation-range .2
304 :black-limit 0 :gray-limit 0) t)
305 (deftest :hsv.sim.6 (hsv-similar 100 .5 .5 110 .6 .8 :hue-range 15
306 :value-range 0.2 :saturation-range .2
307 :black-limit 0 :gray-limit 0) nil)
308 (deftest :hsv.sim.7 (hsv-similar 100 .5 .05 110 .6 .01 :hue-range 0
309 :value-range 0 :saturation-range 0
310 :black-limit .1 :gray-limit 0) t)
311 (deftest :hsv.sim.8 (hsv-similar 100 .01 .5 110 .09 .6 :hue-range 0
312 :value-range 0.2 :saturation-range 0
313 :black-limit 0 :gray-limit .1) t)
314 (deftest :hsv.sim.9 (hsv-similar 100 .01 .5 110 .09 .6 :hue-range 0
315 :value-range 0.05 :saturation-range 0
316 :black-limit 0 :gray-limit .1) nil)
321 (is-dst-change-usa-spring-utime
322 (encode-universal-time 0 0 0 2 4 2000)) t)
324 (is-dst-change-usa-spring-utime
325 (encode-universal-time 0 0 0 1 4 2000)) nil)
327 (is-dst-change-usa-spring-utime
328 (encode-universal-time 0 0 0 3 4 2000)) nil)
330 (is-dst-change-usa-fall-utime
331 (encode-universal-time 0 0 0 31 10 2004)) t)
333 (is-dst-change-usa-fall-utime
334 (encode-universal-time 0 0 0 30 10 2004)) nil)
336 (is-dst-change-usa-fall-utime
337 (encode-universal-time 0 0 0 1 11 2000)) nil)
342 (ensure-keyword-default-case (read-from-string "TYPE")) :type)
345 (ensure-keyword-default-case (read-from-string "type")) :type)
349 (string-elide "A Test string" 10 :end) "A Test ..." )
352 (string-elide "A Test string" 13 :end) "A Test string")
355 (string-elide "A Test string" 11 :end) "A Test s..." )
358 (string-elide "A Test string" 2 :middle) "...")
361 (string-elide "A Test string" 11 :middle) "A Te...ring")
364 (string-elide "A Test string" 12 :middle) "A Tes...ring")
371 (make-url "pg" :anchor "now")
375 (make-url "pg" :vars '(("a" . "5")))
379 (make-url "pg" :anchor "then" :vars '(("a" . "5") ("b" . "pi")))
382 (defclass test-unique ()
388 (let ((list (list (make-instance 'test-unique :a 1 :b 1)
389 (make-instance 'test-unique :a 2 :b 2)
390 (make-instance 'test-unique :a 3 :b 2))))
392 (unique-slot-values list 'a)
393 (unique-slot-values list 'b)))
397 (unique-slot-values nil 'a)
401 (number-within-percentage 1. 1.1 9)
405 (number-within-percentage 1. 1.1 11)
411 ;; Disable attrib class until understand changes in sbcl/cmucl
412 ;; using COMPUTE-SLOT-ACCESSOR-INFO and defining method
413 ;; for slot access of ALL-ATTRIBUTES. Does this work on Allegro/LW?
417 (eval-when (:compile-toplevel :load-toplevel :execute)
418 (when (find-package '#:kmr-mop)
419 (pushnew :kmrtest-mop cl:*features*)))
422 (setf (find-class 'monitored-credit-rating) nil)
424 (setf (find-class 'credit-rating) nil)
427 (defclass credit-rating ()
428 ((level :attributes (date-set time-set))
429 (id :attributes (person-setting)))
430 #+lispworks (:optimize-slot-access nil)
431 (:metaclass attributes-class))
435 (defclass monitored-credit-rating ()
436 ((level :attributes (last-checked interval date-set))
438 (id :attributes (verified)))
439 (:metaclass attributes-class))
442 (deftest :attrib.mop.1
443 (let ((cr (make-instance 'credit-rating)))
444 (slot-attribute cr 'level 'date-set))
448 (deftest :attrib.mop.2
449 (let ((cr (make-instance 'credit-rating)))
450 (setf (slot-attribute cr 'level 'date-set) "12/15/1990")
451 (let ((result (slot-attribute cr 'level 'date-set)))
452 (setf (slot-attribute cr 'level 'date-set) nil)
457 (deftest :attrib.mop.3
458 (let ((mcr (make-instance 'monitored-credit-rating)))
459 (setf (slot-attribute mcr 'level 'date-set) "01/05/2002")
460 (let ((result (slot-attribute mcr 'level 'date-set)))
461 (setf (slot-attribute mcr 'level 'date-set) nil)
467 (eval-when (:compile-toplevel :load-toplevel :execute)
468 (setq cl:*features* (delete :kmrtest-mop cl:*features*)))