comparison-check.scm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Robert Ransom
  3. (define-test-suite r6rs-comparison-tests)
  4. (define-test-case boolean=?/2 r6rs-comparison-tests
  5. (check
  6. (boolean=? #f #f) => #t)
  7. (check
  8. (boolean=? #f #t) => #f)
  9. (check
  10. (boolean=? #t #f) => #f)
  11. (check
  12. (boolean=? #t #t) => #t)
  13. (check-exception
  14. (boolean=? 'foo 'foo))
  15. (check-exception
  16. (boolean=? 'foo #f))
  17. (check-exception
  18. (boolean=? #f 'foo)))
  19. (define-test-case boolean=?/3 r6rs-comparison-tests
  20. (check
  21. (boolean=? #f #f #f) => #t)
  22. (check
  23. (boolean=? #f #f #t) => #f)
  24. (check
  25. (boolean=? #f #t #f) => #f)
  26. (check
  27. (boolean=? #f #t #t) => #f)
  28. (check
  29. (boolean=? #t #f #f) => #f)
  30. (check
  31. (boolean=? #t #f #t) => #f)
  32. (check
  33. (boolean=? #t #t #f) => #f)
  34. (check
  35. (boolean=? #t #t #t) => #t)
  36. (check-exception
  37. (boolean=? #f 'foo 'foo))
  38. (check-exception
  39. (boolean=? #f 'foo #f))
  40. (check-exception
  41. (boolean=? #f #f 'foo))
  42. (check-exception
  43. (boolean=? #f #t 'foo))
  44. (check-exception
  45. (boolean=? 'foo #f #f)))
  46. (define-test-case symbol=?/2 r6rs-comparison-tests
  47. (check
  48. (symbol=? 'foo 'foo) => #t)
  49. (check
  50. (symbol=? 'foo 'bar) => #f)
  51. (check-exception
  52. (symbol=? #f 'foo))
  53. (check-exception
  54. (symbol=? 'foo #f))
  55. (check-exception
  56. (symbol=? #f #f)))
  57. (define-test-case symbol=?/3 r6rs-comparison-tests
  58. (check
  59. (symbol=? 'foo 'foo 'foo) => #t)
  60. (check
  61. (symbol=? 'foo 'foo 'bar) => #f)
  62. (check
  63. (symbol=? 'foo 'bar 'foo) => #f)
  64. (check
  65. (symbol=? 'foo 'bar 'bar) => #f)
  66. (check-exception
  67. (symbol=? 'foo 'foo #f))
  68. (check-exception
  69. (symbol=? 'foo 'bar #f))
  70. (check-exception
  71. (symbol=? #f 'foo 'foo))
  72. (check-exception
  73. (symbol=? 'foo #f 'foo)))
  74. (define-test-case string=?/2 r6rs-comparison-tests
  75. (check
  76. (string=? "foo" "Foo") => #f)
  77. (check
  78. (string=? "foo" "foo") => #t)
  79. (check
  80. (string=? "foo" "bar") => #f)
  81. (check-exception
  82. (string=? "foo" 'bar))
  83. (check-exception
  84. (string=? 'foo "bar"))
  85. (check-exception
  86. (string=? 'foo 'bar)))
  87. (define-test-case string=?/3 r6rs-comparison-tests
  88. (check
  89. (string=? "foo" "foo" "foo") => #t)
  90. (check
  91. (string=? "foo" "foo" "Foo") => #f)
  92. (check
  93. (string=? "foo" "Foo" "foo") => #f)
  94. (check
  95. (string=? "foo" "Foo" "Foo") => #f)
  96. (check
  97. (string=? "Foo" "foo" "foo") => #f)
  98. (check
  99. (string=? "Foo" "foo" "Foo") => #f)
  100. (check
  101. (string=? "Foo" "Foo" "foo") => #f)
  102. (check
  103. (string=? "Foo" "Foo" "Foo") => #t)
  104. (check-exception
  105. (string=? "foo" "foo" 'foo))
  106. (check-exception
  107. (string=? "foo" "bar" 'foo)))
  108. (define-test-case string<?/2 r6rs-comparison-tests
  109. (check
  110. (string<? "abb" "abc") => #t)
  111. (check
  112. (string<? "abb" "abb") => #f)
  113. (check-exception
  114. (string<? "abb" 'abc)))
  115. (define-test-case string<?/3 r6rs-comparison-tests
  116. (check
  117. (string<? "abb" "abc" "abc") => #f)
  118. (check
  119. (string<? "abb" "abc" "abd") => #t)
  120. (check
  121. (string<? "abb" "abb" "abd") => #f)
  122. (check-exception
  123. (string<? "abb" "abc" 3))
  124. (check-exception
  125. (string<? "abb" "abb" 3)))
  126. ; For the remaining (non-case-insensitive) string comparisons, just check
  127. ; that the correct 2-ary comparison is performed.
  128. ; An operator (roughly) from Haskell.
  129. ; TODO - move into a utility package
  130. (define (liftM2-list-uncurried f xs ys)
  131. (srfi-1:append-map (lambda (x) (map (lambda (y) (f x y)) ys)) xs))
  132. (define-test-case liftM2-list-uncurried r6rs-comparison-tests
  133. (check
  134. (liftM2-list-uncurried list '(1 2 3) '(4 5 6))
  135. => '((1 4) (1 5) (1 6) (2 4) (2 5) (2 6) (3 4) (3 5) (3 6))))
  136. (define test-list-1 '("foo" "bar" "baz"))
  137. (define-test-case other-non-ci-comparisons r6rs-comparison-tests
  138. (check
  139. (liftM2-list-uncurried string<=? test-list-1 test-list-1)
  140. => (liftM2-list-uncurried prim:string<=? test-list-1 test-list-1))
  141. (check
  142. (liftM2-list-uncurried string>? test-list-1 test-list-1)
  143. => (liftM2-list-uncurried prim:string>? test-list-1 test-list-1))
  144. (check
  145. (liftM2-list-uncurried string>=? test-list-1 test-list-1)
  146. => (liftM2-list-uncurried prim:string>=? test-list-1 test-list-1)))
  147. ; TODO? - move into a utility package?
  148. (define (int-permutations n)
  149. (cond
  150. ((not (and (integer? n)
  151. (exact? n)
  152. (not (negative? n))))
  153. (assertion-violation 'int-permutations
  154. "expected non-negative exact integer"
  155. n))
  156. ((zero? n)
  157. '())
  158. ((prim:= n 1)
  159. '((0)))
  160. (else
  161. (let ((ps-n-1 (int-permutations (- n 1))))
  162. (let loop ((i (- n 1))
  163. (acc '()))
  164. (if (negative? i)
  165. acc
  166. (loop (- i 1)
  167. (append (map (lambda (p)
  168. (let ((f (lambda (j)
  169. (if (prim:>= j i)
  170. (+ j 1)
  171. j))))
  172. (cons i (map f p))))
  173. ps-n-1)
  174. acc))))))))
  175. (define-test-case int-permutations r6rs-comparison-tests
  176. (check
  177. (int-permutations 0) => '())
  178. (check
  179. (int-permutations 1) => '((0)))
  180. (check
  181. (int-permutations 2) => '((0 1) (1 0)))
  182. (check
  183. (int-permutations 3) => '((0 1 2)
  184. (0 2 1)
  185. (1 0 2)
  186. (1 2 0)
  187. (2 0 1)
  188. (2 1 0)))
  189. (check
  190. (length (int-permutations 4)) => 24)
  191. (check
  192. (length (int-permutations 5)) => 120)
  193. (check
  194. (length (int-permutations 6)) => 720))
  195. ; (int-permutations 8) overflows the default maximum heap size
  196. ; TODO? - move into a utility package?
  197. (define (vector->list-of-permutations v)
  198. (let* ((n (vector-length v))
  199. (ps (int-permutations n)))
  200. (map (lambda (p)
  201. (map (lambda (i) (vector-ref v i)) p))
  202. ps)))
  203. (define-test-case vector->list-of-permutations r6rs-comparison-tests
  204. (check
  205. (vector->list-of-permutations '#(foo bar baz)) => '((foo bar baz)
  206. (foo baz bar)
  207. (bar foo baz)
  208. (bar baz foo)
  209. (baz foo bar)
  210. (baz bar foo))))
  211. (define sharp-s-str (string (integer->char #xDF)))
  212. (define-test-case string-ci=?/4 r6rs-comparison-tests
  213. (check
  214. (map (lambda (p) (apply string-ci=? p))
  215. (vector->list-of-permutations (vector "strasse"
  216. (string-append "Stra" sharp-s-str "e")
  217. "STRASSE"
  218. (string-append "stra" sharp-s-str "e"))))
  219. => (srfi-1:make-list 24 #t))
  220. (check
  221. (map (lambda (p) (apply string-ci=? p))
  222. (vector->list-of-permutations '#("Hello"
  223. "hello"
  224. "HELLO"
  225. "world")))
  226. => (srfi-1:make-list 24 #f))
  227. (check-exception
  228. (string-ci=? "foo" "foo" 'baz))
  229. (check-exception
  230. (string-ci=? "foo" "bar" 'baz)))
  231. (define-test-case string-ci<?/2 r6rs-comparison-tests
  232. (check
  233. (string-ci<? "bar" "foo") => #t)
  234. (check
  235. (string-ci<? "bar" "FOO") => #t)
  236. (check
  237. (string-ci<? "BAR" "bar") => #f)
  238. (check
  239. (string-ci<? "FOO" "bar") => #f)
  240. (check-exception
  241. (string-ci<? "foo" 'bar)))
  242. (define-test-case string-ci<=?/2 r6rs-comparison-tests
  243. (check
  244. (string-ci<=? "bar" "foo") => #t)
  245. (check
  246. (string-ci<=? "bar" "FOO") => #t)
  247. (check
  248. (string-ci<=? "BAR" "bar") => #t)
  249. (check
  250. (string-ci<=? "FOO" "bar") => #f)
  251. (check-exception
  252. (string-ci<=? "foo" 'bar)))
  253. (define-test-case string-ci>?/2 r6rs-comparison-tests
  254. (check
  255. (string-ci>? "foo" "bar") => #t)
  256. (check
  257. (string-ci>? "FOO" "bar") => #t)
  258. (check
  259. (string-ci>? "bar" "BAR") => #f)
  260. (check
  261. (string-ci>? "bar" "FOO") => #f)
  262. (check-exception
  263. (string-ci>? "foo" 'bar)))
  264. (define-test-case string-ci>=?/2 r6rs-comparison-tests
  265. (check
  266. (string-ci>=? "foo" "bar") => #t)
  267. (check
  268. (string-ci>=? "FOO" "bar") => #t)
  269. (check
  270. (string-ci>=? "bar" "BAR") => #t)
  271. (check
  272. (string-ci>=? "bar" "FOO") => #f)
  273. (check-exception
  274. (string-ci>=? "foo" 'bar)))