srfi-60.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. ;;;; srfi-60.test --- Test suite for Guile's SRFI-60 functions. -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2005, 2006 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This program is free software; you can redistribute it and/or modify
  6. ;;;; it under the terms of the GNU General Public License as published by
  7. ;;;; the Free Software Foundation; either version 2, or (at your option)
  8. ;;;; any later version.
  9. ;;;;
  10. ;;;; This program is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;;;; GNU General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU General Public License
  16. ;;;; along with this software; see the file COPYING. If not, write to
  17. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. ;;;; Boston, MA 02110-1301 USA
  19. (define-module (test-srfi-60)
  20. #:duplicates (last) ;; avoid warning about srfi-60 replacing `bit-count'
  21. #:use-module (test-suite lib)
  22. #:use-module (srfi srfi-60))
  23. (pass-if "cond-expand srfi-60"
  24. (cond-expand (srfi-60 #t)
  25. (else #f)))
  26. ;;
  27. ;; logand
  28. ;;
  29. (with-test-prefix "logand"
  30. (pass-if (eqv? 6 (logand 14 6))))
  31. ;;
  32. ;; bitwise-and
  33. ;;
  34. (with-test-prefix "bitwise-and"
  35. (pass-if (eqv? 6 (bitwise-and 14 6))))
  36. ;;
  37. ;; logior
  38. ;;
  39. (with-test-prefix "logior"
  40. (pass-if (eqv? 14 (logior 10 12))))
  41. ;;
  42. ;; bitwise-ior
  43. ;;
  44. (with-test-prefix "bitwise-ior"
  45. (pass-if (eqv? 14 (bitwise-ior 10 12))))
  46. ;;
  47. ;; logxor
  48. ;;
  49. (with-test-prefix "logxor"
  50. (pass-if (eqv? 6 (logxor 10 12))))
  51. ;;
  52. ;; bitwise-xor
  53. ;;
  54. (with-test-prefix "bitwise-xor"
  55. (pass-if (eqv? 6 (bitwise-xor 10 12))))
  56. ;;
  57. ;; lognot
  58. ;;
  59. (with-test-prefix "lognot"
  60. (pass-if (eqv? -1 (lognot 0)))
  61. (pass-if (eqv? 0 (lognot -1))))
  62. ;;
  63. ;; bitwise-not
  64. ;;
  65. (with-test-prefix "bitwise-not"
  66. (pass-if (eqv? -1 (bitwise-not 0)))
  67. (pass-if (eqv? 0 (bitwise-not -1))))
  68. ;;
  69. ;; bitwise-if
  70. ;;
  71. (with-test-prefix "bitwise-if"
  72. (pass-if (eqv? 9 (bitwise-if 3 1 8)))
  73. (pass-if (eqv? 0 (bitwise-if 3 8 1))))
  74. ;;
  75. ;; bitwise-merge
  76. ;;
  77. (with-test-prefix "bitwise-merge"
  78. (pass-if (eqv? 9 (bitwise-merge 3 1 8)))
  79. (pass-if (eqv? 0 (bitwise-merge 3 8 1))))
  80. ;;
  81. ;; logtest
  82. ;;
  83. (with-test-prefix "logtest"
  84. (pass-if (eq? #t (logtest 3 6)))
  85. (pass-if (eq? #f (logtest 3 12))))
  86. ;;
  87. ;; any-bits-set?
  88. ;;
  89. (with-test-prefix "any-bits-set?"
  90. (pass-if (eq? #t (any-bits-set? 3 6)))
  91. (pass-if (eq? #f (any-bits-set? 3 12))))
  92. ;;
  93. ;; logcount
  94. ;;
  95. (with-test-prefix "logcount"
  96. (pass-if (eqv? 2 (logcount 12))))
  97. ;;
  98. ;; bit-count
  99. ;;
  100. (with-test-prefix "bit-count"
  101. (pass-if (eqv? 2 (bit-count 12))))
  102. ;;
  103. ;; integer-length
  104. ;;
  105. (with-test-prefix "integer-length"
  106. (pass-if (eqv? 0 (integer-length 0)))
  107. (pass-if (eqv? 8 (integer-length 128)))
  108. (pass-if (eqv? 8 (integer-length 255)))
  109. (pass-if (eqv? 9 (integer-length 256))))
  110. ;;
  111. ;; log2-binary-factors
  112. ;;
  113. (with-test-prefix "log2-binary-factors"
  114. (pass-if (eqv? -1 (log2-binary-factors 0)))
  115. (pass-if (eqv? 0 (log2-binary-factors 1)))
  116. (pass-if (eqv? 0 (log2-binary-factors 3)))
  117. (pass-if (eqv? 2 (log2-binary-factors 4)))
  118. (pass-if (eqv? 1 (log2-binary-factors 6)))
  119. (pass-if (eqv? 0 (log2-binary-factors -1)))
  120. (pass-if (eqv? 1 (log2-binary-factors -2)))
  121. (pass-if (eqv? 0 (log2-binary-factors -3)))
  122. (pass-if (eqv? 2 (log2-binary-factors -4)))
  123. (pass-if (eqv? 128 (log2-binary-factors #x100000000000000000000000000000000))))
  124. ;;
  125. ;; first-set-bit
  126. ;;
  127. (with-test-prefix "first-set-bit"
  128. (pass-if (eqv? -1 (first-set-bit 0)))
  129. (pass-if (eqv? 0 (first-set-bit 1)))
  130. (pass-if (eqv? 0 (first-set-bit 3)))
  131. (pass-if (eqv? 2 (first-set-bit 4)))
  132. (pass-if (eqv? 1 (first-set-bit 6)))
  133. (pass-if (eqv? 0 (first-set-bit -1)))
  134. (pass-if (eqv? 1 (first-set-bit -2)))
  135. (pass-if (eqv? 0 (first-set-bit -3)))
  136. (pass-if (eqv? 2 (first-set-bit -4))))
  137. ;;
  138. ;; logbit?
  139. ;;
  140. (with-test-prefix "logbit?"
  141. (pass-if (eq? #t (logbit? 0 1)))
  142. (pass-if (eq? #f (logbit? 1 1)))
  143. (pass-if (eq? #f (logbit? 1 8)))
  144. (pass-if (eq? #t (logbit? 1000 -1))))
  145. ;;
  146. ;; bit-set?
  147. ;;
  148. (with-test-prefix "bit-set?"
  149. (pass-if (eq? #t (bit-set? 0 1)))
  150. (pass-if (eq? #f (bit-set? 1 1)))
  151. (pass-if (eq? #f (bit-set? 1 8)))
  152. (pass-if (eq? #t (bit-set? 1000 -1))))
  153. ;;
  154. ;; copy-bit
  155. ;;
  156. (with-test-prefix "copy-bit"
  157. (pass-if (eqv? 0 (copy-bit 0 0 #f)))
  158. (pass-if (eqv? 0 (copy-bit 30 0 #f)))
  159. (pass-if (eqv? 0 (copy-bit 31 0 #f)))
  160. (pass-if (eqv? 0 (copy-bit 62 0 #f)))
  161. (pass-if (eqv? 0 (copy-bit 63 0 #f)))
  162. (pass-if (eqv? 0 (copy-bit 128 0 #f)))
  163. (pass-if (eqv? -1 (copy-bit 0 -1 #t)))
  164. (pass-if (eqv? -1 (copy-bit 30 -1 #t)))
  165. (pass-if (eqv? -1 (copy-bit 31 -1 #t)))
  166. (pass-if (eqv? -1 (copy-bit 62 -1 #t)))
  167. (pass-if (eqv? -1 (copy-bit 63 -1 #t)))
  168. (pass-if (eqv? -1 (copy-bit 128 -1 #t)))
  169. (pass-if (eqv? 1 (copy-bit 0 0 #t)))
  170. (pass-if (eqv? #x106 (copy-bit 8 6 #t)))
  171. (pass-if (eqv? 6 (copy-bit 8 6 #f)))
  172. (pass-if (eqv? -2 (copy-bit 0 -1 #f)))
  173. (pass-if "bignum becomes inum"
  174. (eqv? 0 (copy-bit 128 #x100000000000000000000000000000000 #f)))
  175. ;; bignums unchanged
  176. (pass-if (eqv? #x100000000000000000000000000000000
  177. (copy-bit 128 #x100000000000000000000000000000000 #t)))
  178. (pass-if (eqv? #x100000000000000000000000000000000
  179. (copy-bit 64 #x100000000000000000000000000000000 #f)))
  180. (pass-if (eqv? #x-100000000000000000000000000000000
  181. (copy-bit 64 #x-100000000000000000000000000000000 #f)))
  182. (pass-if (eqv? #x-100000000000000000000000000000000
  183. (copy-bit 256 #x-100000000000000000000000000000000 #t))))
  184. ;;
  185. ;; bit-field
  186. ;;
  187. (with-test-prefix "bit-field"
  188. (pass-if (eqv? 0 (bit-field 6 0 1)))
  189. (pass-if (eqv? 3 (bit-field 6 1 3)))
  190. (pass-if (eqv? 1 (bit-field 6 2 999)))
  191. (pass-if (eqv? 1 (bit-field #x100000000000000000000000000000000 128 129))))
  192. ;;
  193. ;; copy-bit-field
  194. ;;
  195. (with-test-prefix "copy-bit-field"
  196. (pass-if (eqv? #b111 (copy-bit-field #b110 1 0 1)))
  197. (pass-if (eqv? #b110 (copy-bit-field #b110 1 1 2)))
  198. (pass-if (eqv? #b010 (copy-bit-field #b110 1 1 3))))
  199. ;;
  200. ;; ash
  201. ;;
  202. (with-test-prefix "ash"
  203. (pass-if (eqv? 2 (ash 1 1)))
  204. (pass-if (eqv? 0 (ash 1 -1))))
  205. ;;
  206. ;; arithmetic-shift
  207. ;;
  208. (with-test-prefix "arithmetic-shift"
  209. (pass-if (eqv? 2 (arithmetic-shift 1 1)))
  210. (pass-if (eqv? 0 (arithmetic-shift 1 -1))))
  211. ;;
  212. ;; rotate-bit-field
  213. ;;
  214. (with-test-prefix "rotate-bit-field"
  215. (pass-if (eqv? #b110 (rotate-bit-field #b110 1 1 2)))
  216. (pass-if (eqv? #b1010 (rotate-bit-field #b110 1 2 4)))
  217. (pass-if (eqv? #b1011 (rotate-bit-field #b0111 -1 1 4)))
  218. (pass-if (eqv? #b0 (rotate-bit-field #b0 128 0 256)))
  219. (pass-if (eqv? #b1 (rotate-bit-field #b1 128 1 256)))
  220. (pass-if
  221. (eqv? #x100000000000000000000000000000000
  222. (rotate-bit-field #x100000000000000000000000000000000 128 0 64)))
  223. (pass-if
  224. (eqv? #x100000000000000000000000000000008
  225. (rotate-bit-field #x100000000000000000000000000000001 3 0 64)))
  226. (pass-if
  227. (eqv? #x100000000000000002000000000000000
  228. (rotate-bit-field #x100000000000000000000000000000001 -3 0 64)))
  229. (pass-if (eqv? #b110 (rotate-bit-field #b110 0 0 10)))
  230. (pass-if (eqv? #b110 (rotate-bit-field #b110 0 0 256)))
  231. (pass-if "bignum becomes inum"
  232. (eqv? 1 (rotate-bit-field #x100000000000000000000000000000000 1 0 129))))
  233. ;;
  234. ;; reverse-bit-field
  235. ;;
  236. (with-test-prefix "reverse-bit-field"
  237. (pass-if (eqv? 6 (reverse-bit-field 6 1 3)))
  238. (pass-if (eqv? 12 (reverse-bit-field 6 1 4)))
  239. (pass-if (eqv? #x80000000 (reverse-bit-field 1 0 32)))
  240. (pass-if (eqv? #x40000000 (reverse-bit-field 1 0 31)))
  241. (pass-if (eqv? #x20000000 (reverse-bit-field 1 0 30)))
  242. (pass-if (eqv? (logior (ash -1 32) #xFBFFFFFF)
  243. (reverse-bit-field -2 0 27)))
  244. (pass-if (eqv? (logior (ash -1 32) #xF7FFFFFF)
  245. (reverse-bit-field -2 0 28)))
  246. (pass-if (eqv? (logior (ash -1 32) #xEFFFFFFF)
  247. (reverse-bit-field -2 0 29)))
  248. (pass-if (eqv? (logior (ash -1 32) #xDFFFFFFF)
  249. (reverse-bit-field -2 0 30)))
  250. (pass-if (eqv? (logior (ash -1 32) #xBFFFFFFF)
  251. (reverse-bit-field -2 0 31)))
  252. (pass-if (eqv? (logior (ash -1 32) #x7FFFFFFF)
  253. (reverse-bit-field -2 0 32)))
  254. (pass-if "bignum becomes inum"
  255. (eqv? 5 (reverse-bit-field #x140000000000000000000000000000000 0 129))))
  256. ;;
  257. ;; integer->list
  258. ;;
  259. (with-test-prefix "integer->list"
  260. (pass-if (equal? '(#t #t #f) (integer->list 6)))
  261. (pass-if (equal? '(#f #t #t #f) (integer->list 6 4)))
  262. (pass-if (equal? '(#t #f) (integer->list 6 2)))
  263. (pass-if "zeros above top of positive inum"
  264. (equal? '(#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  265. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  266. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  267. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  268. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  269. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  270. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  271. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #t)
  272. (integer->list 1 128)))
  273. (pass-if "ones above top of negative inum"
  274. (equal? '(#t #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t
  275. #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t
  276. #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t
  277. #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t
  278. #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t
  279. #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t
  280. #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t
  281. #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t #t)
  282. (integer->list -1 128)))
  283. (pass-if (equal? '(#t
  284. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  285. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  286. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  287. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  288. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  289. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  290. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  291. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f)
  292. (integer->list #x100000000000000000000000000000000))))
  293. ;;
  294. ;; list->integer
  295. ;;
  296. (with-test-prefix "list->integer"
  297. (pass-if (eqv? 6 (list->integer '(#t #t #f))))
  298. (pass-if (eqv? 6 (list->integer '(#f #t #t #f))))
  299. (pass-if (eqv? 2 (list->integer '(#t #f))))
  300. (pass-if "leading #f's"
  301. (eqv? 1 (list->integer
  302. '(#f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  303. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  304. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  305. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  306. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  307. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  308. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  309. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #t))))
  310. (pass-if (eqv? #x100000000000000000000000000000000
  311. (list->integer
  312. '(#t
  313. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  314. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  315. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  316. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  317. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  318. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  319. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
  320. #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f))))
  321. (pass-if (eqv? #x03FFFFFF (list->integer '(#t #t
  322. #t #t #t #t #t #t #t #t
  323. #t #t #t #t #t #t #t #t
  324. #t #t #t #t #t #t #t #t))))
  325. (pass-if (eqv? #x07FFFFFF (list->integer '(#t #t #t
  326. #t #t #t #t #t #t #t #t
  327. #t #t #t #t #t #t #t #t
  328. #t #t #t #t #t #t #t #t))))
  329. (pass-if (eqv? #x0FFFFFFF (list->integer '(#t #t #t #t
  330. #t #t #t #t #t #t #t #t
  331. #t #t #t #t #t #t #t #t
  332. #t #t #t #t #t #t #t #t))))
  333. (pass-if (eqv? #x1FFFFFFF (list->integer '(#t #t #t #t #t
  334. #t #t #t #t #t #t #t #t
  335. #t #t #t #t #t #t #t #t
  336. #t #t #t #t #t #t #t #t))))
  337. (pass-if (eqv? #x3FFFFFFF (list->integer '(#t #t #t #t #t #t
  338. #t #t #t #t #t #t #t #t
  339. #t #t #t #t #t #t #t #t
  340. #t #t #t #t #t #t #t #t))))
  341. (pass-if (eqv? #x7FFFFFFF (list->integer '(#t #t #t #t #t #t #t
  342. #t #t #t #t #t #t #t #t
  343. #t #t #t #t #t #t #t #t
  344. #t #t #t #t #t #t #t #t))))
  345. (pass-if (eqv? #xFFFFFFFF (list->integer '(#t #t #t #t #t #t #t #t
  346. #t #t #t #t #t #t #t #t
  347. #t #t #t #t #t #t #t #t
  348. #t #t #t #t #t #t #t #t))))
  349. (pass-if (eqv? #x1FFFFFFFF (list->integer '(#t
  350. #t #t #t #t #t #t #t #t
  351. #t #t #t #t #t #t #t #t
  352. #t #t #t #t #t #t #t #t
  353. #t #t #t #t #t #t #t #t)))))
  354. ;;
  355. ;; list->integer
  356. ;;
  357. (with-test-prefix "list->integer"
  358. (pass-if (eqv? 0 (booleans->integer)))
  359. (pass-if (eqv? 6 (booleans->integer #t #t #f))))