case-test.scm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. (test-begin "case")
  2. ;; special cases
  3. (test 1 (lambda (key)
  4. (case key
  5. (else 1)))
  6. 5)
  7. (test 1 (lambda (key)
  8. (case key
  9. ((5) 1)))
  10. 5)
  11. (test 1 (lambda (key)
  12. (case key
  13. ((5) 1)
  14. (else 2)))
  15. 5)
  16. (test 2 (lambda (key)
  17. (case key
  18. ((5) 1)
  19. (else 2)))
  20. 4)
  21. (test #!void (lambda (key)
  22. (case key
  23. ((5) 1)
  24. ((3) 2)))
  25. 4)
  26. ;; integer datums, integer key
  27. (test 1 (lambda (key::byte)
  28. (case key
  29. ((1 2 3 4) 3)
  30. ((5 6 7 8) 1)))
  31. 5)
  32. (test 1 (lambda (key::byte)
  33. (case key
  34. ((1 2 3 4) 3)
  35. ((6 7 8) 2)
  36. (else 1)))
  37. 5)
  38. (test 1 (lambda (key::short)
  39. (case key
  40. ((1 2 3 4) 3)
  41. ((5 6 7 8) 1)))
  42. 5)
  43. (test 1 (lambda (key::short)
  44. (case key
  45. ((1 2 3 4) 3)
  46. ((6 7 8) 2)
  47. (else 1)))
  48. 5)
  49. (test 1 (lambda (key::int)
  50. (case key
  51. ((1 2 3 4) 3)
  52. ((5 6 7 8) 1)))
  53. 5)
  54. (test 1 (lambda (key::int)
  55. (case key
  56. ((1 2 3 4) 3)
  57. ((6 7 8) 2)
  58. (else 1)))
  59. 5)
  60. (test 5 (lambda (key::int)
  61. (case key
  62. ((1) 1)
  63. ((2) 2)
  64. ((3) 3)
  65. ((4) 4)
  66. ((5) 5)
  67. ((6) 6)
  68. ((7) 7)
  69. ((8) 8)
  70. ((9) 9)
  71. ((10) 10)
  72. ((11) 11)
  73. ((12) 12)))
  74. 5)
  75. (test 1 (lambda (key::long)
  76. (case key
  77. ((1 2 3 4) 3)
  78. ((5 6 7 8) 1)))
  79. 5)
  80. (test 1 (lambda (key::long)
  81. (case key
  82. ((1 2 3 4) 3)
  83. ((6 7 8) 2)
  84. (else 1)))
  85. 5)
  86. ;; integer datums, object key
  87. (test 1 (lambda (key)
  88. (case key
  89. ((1 2 3 4) 3)
  90. ((5 6 7 8) 1)))
  91. 5)
  92. (test 1 (lambda (key)
  93. (case key
  94. ((1 2 3 4) 3)
  95. ((6 7 8) 2)
  96. (else 1)))
  97. 5)
  98. (test 5 (lambda (key)
  99. (case key
  100. ((1) 1)
  101. ((2) 2)
  102. ((3) 3)
  103. ((4) 4)
  104. ((5) 5)
  105. ((6) 6)
  106. ((7) 7)
  107. ((8) 8)
  108. ((9) 9)
  109. ((10) 10)
  110. ((11) 11)
  111. ((12) 12)))
  112. 5)
  113. ;; big integer datums, integer key
  114. (test 12 (lambda (key::int)
  115. (case key
  116. ((-2147483636) 1)
  117. ((-2147483637) 2)
  118. ((-2147483638) 3)
  119. ((-2147483639) 4)
  120. ((-2147483640) 5)
  121. ((-2147483641) 6)
  122. ((-2147483642) 7)
  123. ((-2147483643) 8)
  124. ((-2147483644) 9)
  125. ((-2147483645) 10)
  126. ((-2147483646) 11)
  127. ((-2147483647) 12)))
  128. -2147483647)
  129. (test 1 (lambda (key::int)
  130. (case key
  131. ((-2147483636) 1)
  132. ((-2147483637) 2)
  133. ((2147483638) 3)
  134. ((2147483639) 4)
  135. ((2147483640) 5)
  136. ((2147483641) 6)
  137. ((2147483642) 7)
  138. ((2147483643) 8)
  139. ((-2147483644) 9)
  140. ((2147483645) 10)
  141. ((2147483646) 11)
  142. ((2147483647) 12)))
  143. -2147483636)
  144. (test 1 (lambda (key::int)
  145. (case key
  146. ((2147483636) 1)
  147. ((2147483637) 2)
  148. ((2147483638) 3)
  149. ((2147483639) 4)
  150. ((2147483640) 5)
  151. ((2147483641) 6)
  152. ((2147483642) 7)
  153. ((2147483643) 8)
  154. ((2147483644) 9)
  155. ((2147483645) 10)
  156. ((2147483646) 11)
  157. ((2147483647) 12)))
  158. 2147483636)
  159. ;; big integer datums, object key
  160. (test 12 (lambda (key)
  161. (case key
  162. ((2147483636) 1)
  163. ((2147483637) 2)
  164. ((2147483638) 3)
  165. ((2147483639) 4)
  166. ((2147483640) 5)
  167. ((2147483641) 6)
  168. ((2147483642) 7)
  169. ((2147483643) 8)
  170. ((2147483644) 9)
  171. ((2147483645) 10)
  172. ((2147483646) 11)
  173. ((2147483647) 12)))
  174. 2147483647)
  175. ;; char datums, char key
  176. (test 1 (lambda (key::char)
  177. (case key
  178. ((#\a #\b #\c #\d) 3)
  179. ((#\e #\f #\g #\h) 1)))
  180. #\e)
  181. (test 1 (lambda (key::char)
  182. (case key
  183. ((#\a #\b #\c #\d) 3)
  184. ((#\e #\f #\g #\h) 2)
  185. (else 1)))
  186. #\z)
  187. ;; integer datums, arrow syntax
  188. (test 5 (lambda (key)
  189. (case key
  190. ((1 2 3 4) 3)
  191. ((5 6 7 8) => (lambda (x) x))
  192. (else 1)))
  193. 5)
  194. (test 5 (lambda (key)
  195. (case key
  196. ((1 2 3 4) 3)
  197. ((6 7 8) 2)
  198. (else => (lambda (x) x))))
  199. 5)
  200. (test 5 (lambda (key)
  201. (case key
  202. ((1 2 3 4) 3)
  203. ((5 6 7 8) => (lambda (x) x))
  204. (else => (lambda (x) (+ x 1)))))
  205. 5)
  206. (test 5 (lambda (key)
  207. (case key
  208. ((1 2 3 4) => (lambda (x) x))
  209. ((5 6 7 8) => (lambda (x) x))
  210. (else => (lambda (x) (+ x 1)))))
  211. 5)
  212. ;; mixed types
  213. (test 5 (lambda (key)
  214. (case key
  215. ((#\s) (display 1))
  216. ((s foo bar) (display 2) (display 3))
  217. ((5) => (lambda (x) x))
  218. ((6) (lambda (x)(display x)))
  219. (else (lambda (x)(display x)))))
  220. 5)
  221. (test 'sym (lambda (key)
  222. (case key
  223. ((10000000000) 3)
  224. ((2 3 4 5) 'sym)
  225. ((#\a) #\a)
  226. ((sym) 54)
  227. (else 3)))
  228. 5)
  229. ;; mixed types, same expression
  230. ;; in some clauses
  231. (test 'sym (lambda (key)
  232. (case key
  233. ((10000000000) (cons key '()) 'sym)
  234. ((2 3 4 5) (cons key '()) 'sym)
  235. ((#\a) #\a)
  236. ((sym) 54)
  237. (else 3)))
  238. 5)
  239. ;; nested cases
  240. (test 1 (lambda (key)
  241. (case key
  242. ((1 2 3 4)
  243. (case key
  244. ((2 3 4) 3)
  245. ((1 5 6 7 8)
  246. (case key
  247. ((1 2 3 4) 1)
  248. ((5 6 7 8) 1)))))
  249. ((5 6 7 8) 1)
  250. ((9 10 11 12)
  251. (case key
  252. ((1 2 3 4)
  253. (case key
  254. ((1 2 3 4) 3)
  255. ((5 6 7 8)
  256. (case key
  257. ((1 2 3 4) 3)
  258. ((5 6 7 8) 1)))))))))
  259. 1)
  260. (test 2 (lambda (key)
  261. (case key
  262. ((1) 1)
  263. ((2)
  264. (case key
  265. ((4) 4)
  266. ((3) 3)
  267. ((5) 5)
  268. ((2)
  269. (let ((k #\c))
  270. (case k
  271. ((#\a) 4)
  272. ((#\b) 3)
  273. ((#\c) 2)
  274. ((#\d) 5))))))
  275. (else 'error)))
  276. 2)
  277. ;; hashcode collisions
  278. (test 'a (lambda (key)
  279. (let ((k (case key
  280. ((4) 'a)
  281. ((3) #\b)
  282. (else 200))))
  283. (case k
  284. ((97) 97)
  285. ((#\a) #\a)
  286. ((a) 'a))))
  287. 4)
  288. (define (test-collision-same-branch x)
  289. (case (list-ref
  290. '(#\a #\b c #\d e #\f 103 #\h #\i 106)
  291. (mod x 10))
  292. ((#\a a 97) 0)
  293. ((#\b b 98) 1)
  294. ((#\c c 99) 2)
  295. ((#\d d 100) 3)
  296. ((#\e e 101) 4)
  297. ((#\f f 102) 5)
  298. ((#\g g 103) 6)
  299. ((#\h h 104) 7)
  300. ((#\i i 105) 8)
  301. ((#\j j 106) 9)
  302. (else 'error)))
  303. (define (test-collision-different-branch x)
  304. (case (list-ref
  305. '(#\a 98 99 d a #\b c 100 97 b #\c #\d)
  306. (mod x 12))
  307. ((#\a) 0)
  308. ((#\b) 5)
  309. ((#\c) 10)
  310. ((#\d) 11)
  311. ((a) 4)
  312. ((b) 9)
  313. ((c) 6)
  314. ((d) 3)
  315. ((97) 8)
  316. ((98) 1)
  317. ((99) 2)
  318. ((100) 7)
  319. (else 'error)))
  320. (define (case-loop x end-val f)
  321. (when (< x end-val)
  322. (begin
  323. (test x f x)
  324. (case-loop (+ x 1) end-val f))))
  325. (case-loop 0 10 test-collision-same-branch)
  326. (case-loop 0 12 test-collision-different-branch)
  327. ;; inside letrec
  328. (test 2 (lambda ()
  329. (letrec ((f (lambda (x)
  330. (if (eqv? x 1)
  331. (g x)
  332. (h x))))
  333. (g (lambda (x)
  334. (h x)))
  335. (h (lambda (x)
  336. (case x
  337. ((0) (h x))
  338. ((2) x)))))
  339. (f 2))))