bit-operations.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. ;;;; bit-operations.test --- bitwise operations on numbers -*- scheme -*-
  2. ;;;; Copyright (C) 2000, 2001, 2003, 2006, 2009 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 2.1 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (define-module (test-bit-operations)
  18. :use-module (test-suite lib)
  19. :use-module (ice-9 documentation))
  20. ;;;
  21. ;;; miscellaneous
  22. ;;;
  23. (define (run-tests name-proc test-proc arg-sets)
  24. (for-each
  25. (lambda (arg-set)
  26. (pass-if (apply name-proc arg-set)
  27. (apply test-proc arg-set)))
  28. arg-sets))
  29. (define (documented? object)
  30. (not (not (object-documentation object))))
  31. (define fixnum-bit
  32. (inexact->exact (round (+ (/ (log (+ most-positive-fixnum 1)) (log 2)) 1))))
  33. (define fixnum-min most-negative-fixnum)
  34. (define fixnum-max most-positive-fixnum)
  35. (with-test-prefix "bit-extract"
  36. (pass-if "documented?"
  37. (documented? bit-extract))
  38. (with-test-prefix "extract from zero"
  39. (run-tests
  40. (lambda (a b c d)
  41. (string-append "single bit " (number->string b)))
  42. (lambda (a b c d)
  43. (= (bit-extract a b c) d))
  44. (list
  45. (list 0 0 1 0)
  46. (list 0 1 2 0)
  47. (list 0 (+ fixnum-bit -2) (+ fixnum-bit -1) 0)
  48. (list 0 (+ fixnum-bit -1) (+ fixnum-bit 0) 0)
  49. (list 0 (+ fixnum-bit 0) (+ fixnum-bit 1) 0)
  50. (list 0 (+ fixnum-bit 1) (+ fixnum-bit 2) 0)))
  51. (run-tests
  52. (lambda (a b c d)
  53. (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
  54. (lambda (a b c d)
  55. (= (bit-extract a b c) d))
  56. (list
  57. (list 0 0 (+ fixnum-bit -1) 0)
  58. (list 0 1 (+ fixnum-bit 0) 0)
  59. (list 0 2 (+ fixnum-bit 1) 0)
  60. (list 0 (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -3) 0)
  61. (list 0 (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -2) 0)
  62. (list 0 (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit -1) 0)
  63. (list 0 (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 0) 0)))
  64. (run-tests
  65. (lambda (a b c d)
  66. (string-append "fixnum-bit bits starting at " (number->string b)))
  67. (lambda (a b c d)
  68. (= (bit-extract a b c) d))
  69. (list
  70. (list 0 0 (+ fixnum-bit 0) 0)
  71. (list 0 1 (+ fixnum-bit 1) 0)
  72. (list 0 2 (+ fixnum-bit 2) 0)
  73. (list 0 (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -2) 0)
  74. (list 0 (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -1) 0)
  75. (list 0 (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit 0) 0)
  76. (list 0 (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 1) 0)))
  77. (run-tests
  78. (lambda (a b c d)
  79. (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
  80. (lambda (a b c d)
  81. (= (bit-extract a b c) d))
  82. (list
  83. (list 0 0 (+ fixnum-bit 1) 0)
  84. (list 0 1 (+ fixnum-bit 2) 0)
  85. (list 0 2 (+ fixnum-bit 3) 0)
  86. (list 0 (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -1) 0)
  87. (list 0 (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit 0) 0)
  88. (list 0 (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit 1) 0)
  89. (list 0 (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 2) 0))))
  90. (with-test-prefix "extract from fixnum-max"
  91. (run-tests
  92. (lambda (a b c d)
  93. (string-append "single bit " (number->string b)))
  94. (lambda (a b c d)
  95. (= (bit-extract a b c) d))
  96. (list
  97. (list fixnum-max 0 1 1)
  98. (list fixnum-max 1 2 1)
  99. (list fixnum-max (+ fixnum-bit -2) (+ fixnum-bit -1) 1)
  100. (list fixnum-max (+ fixnum-bit -1) (+ fixnum-bit 0) 0)
  101. (list fixnum-max (+ fixnum-bit 0) (+ fixnum-bit 1) 0)
  102. (list fixnum-max (+ fixnum-bit 1) (+ fixnum-bit 2) 0)))
  103. (run-tests
  104. (lambda (a b c d)
  105. (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
  106. (lambda (a b c d)
  107. (= (bit-extract a b c) d))
  108. (list
  109. (list fixnum-max 0 (+ fixnum-bit -1) (ash fixnum-max 0))
  110. (list fixnum-max 1 (+ fixnum-bit 0) (ash fixnum-max -1))
  111. (list fixnum-max 2 (+ fixnum-bit 1) (ash fixnum-max -2))
  112. (list fixnum-max (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -3) 1)
  113. (list fixnum-max (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -2) 0)
  114. (list fixnum-max (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit -1) 0)
  115. (list fixnum-max (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 0) 0)))
  116. (run-tests
  117. (lambda (a b c d)
  118. (string-append "fixnum-bit bits starting at " (number->string b)))
  119. (lambda (a b c d)
  120. (= (bit-extract a b c) d))
  121. (list
  122. (list fixnum-max 0 (+ fixnum-bit 0) (ash fixnum-max 0))
  123. (list fixnum-max 1 (+ fixnum-bit 1) (ash fixnum-max -1))
  124. (list fixnum-max 2 (+ fixnum-bit 2) (ash fixnum-max -2))
  125. (list fixnum-max (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -2) 1)
  126. (list fixnum-max (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -1) 0)
  127. (list fixnum-max (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit 0) 0)
  128. (list fixnum-max (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 1) 0)))
  129. (run-tests
  130. (lambda (a b c d)
  131. (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
  132. (lambda (a b c d)
  133. (= (bit-extract a b c) d))
  134. (list
  135. (list fixnum-max 0 (+ fixnum-bit 1) (ash fixnum-max 0))
  136. (list fixnum-max 1 (+ fixnum-bit 2) (ash fixnum-max -1))
  137. (list fixnum-max 2 (+ fixnum-bit 3) (ash fixnum-max -2))
  138. (list fixnum-max (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -1) 1)
  139. (list fixnum-max (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit 0) 0)
  140. (list fixnum-max (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit 1) 0)
  141. (list fixnum-max (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 2) 0))))
  142. (with-test-prefix "extract from fixnum-max + 1"
  143. (run-tests
  144. (lambda (a b c d)
  145. (string-append "single bit " (number->string b)))
  146. (lambda (a b c d)
  147. (= (bit-extract a b c) d))
  148. (list
  149. (list (+ fixnum-max 1) 0 1 0)
  150. (list (+ fixnum-max 1) 1 2 0)
  151. (list (+ fixnum-max 1) (+ fixnum-bit -2) (+ fixnum-bit -1) 0)
  152. (list (+ fixnum-max 1) (+ fixnum-bit -1) (+ fixnum-bit 0) 1)
  153. (list (+ fixnum-max 1) (+ fixnum-bit 0) (+ fixnum-bit 1) 0)
  154. (list (+ fixnum-max 1) (+ fixnum-bit 1) (+ fixnum-bit 2) 0)))
  155. (run-tests
  156. (lambda (a b c d)
  157. (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
  158. (lambda (a b c d)
  159. (= (bit-extract a b c) d))
  160. (list
  161. (list (+ fixnum-max 1) 0 (+ fixnum-bit -1) (ash 0 (- fixnum-bit 1)))
  162. (list (+ fixnum-max 1) 1 (+ fixnum-bit 0) (ash 1 (- fixnum-bit 2)))
  163. (list (+ fixnum-max 1) 2 (+ fixnum-bit 1) (ash 1 (- fixnum-bit 3)))
  164. (list (+ fixnum-max 1) (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -3) 2)
  165. (list (+ fixnum-max 1) (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -2) 1)
  166. (list (+ fixnum-max 1) (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit -1) 0)
  167. (list (+ fixnum-max 1) (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 0) 0)))
  168. (run-tests
  169. (lambda (a b c d)
  170. (string-append "fixnum-bit bits starting at " (number->string b)))
  171. (lambda (a b c d)
  172. (= (bit-extract a b c) d))
  173. (list
  174. (list (+ fixnum-max 1) 0 (+ fixnum-bit 0) (ash 1 (- fixnum-bit 1)))
  175. (list (+ fixnum-max 1) 1 (+ fixnum-bit 1) (ash 1 (- fixnum-bit 2)))
  176. (list (+ fixnum-max 1) 2 (+ fixnum-bit 2) (ash 1 (- fixnum-bit 3)))
  177. (list (+ fixnum-max 1) (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -2) 2)
  178. (list (+ fixnum-max 1) (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -1) 1)
  179. (list (+ fixnum-max 1) (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit 0) 0)
  180. (list (+ fixnum-max 1) (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 1) 0)))
  181. (run-tests
  182. (lambda (a b c d)
  183. (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
  184. (lambda (a b c d)
  185. (= (bit-extract a b c) d))
  186. (list
  187. (list (+ fixnum-max 1) 0 (+ fixnum-bit 1) (ash 1 (- fixnum-bit 1)))
  188. (list (+ fixnum-max 1) 1 (+ fixnum-bit 2) (ash 1 (- fixnum-bit 2)))
  189. (list (+ fixnum-max 1) 2 (+ fixnum-bit 3) (ash 1 (- fixnum-bit 3)))
  190. (list (+ fixnum-max 1) (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -1) 2)
  191. (list (+ fixnum-max 1) (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit 0) 1)
  192. (list (+ fixnum-max 1) (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit 1) 0)
  193. (list (+ fixnum-max 1) (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 2) 0))))
  194. (with-test-prefix "extract from fixnum-min"
  195. (run-tests
  196. (lambda (a b c d)
  197. (string-append "single bit " (number->string b)))
  198. (lambda (a b c d)
  199. (= (bit-extract a b c) d))
  200. (list
  201. (list fixnum-min 0 1 0)
  202. (list fixnum-min 1 2 0)
  203. (list fixnum-min (+ fixnum-bit -2) (+ fixnum-bit -1) 0)
  204. (list fixnum-min (+ fixnum-bit -1) (+ fixnum-bit 0) 1)
  205. (list fixnum-min (+ fixnum-bit 0) (+ fixnum-bit 1) 1)
  206. (list fixnum-min (+ fixnum-bit 1) (+ fixnum-bit 2) 1)))
  207. (run-tests
  208. (lambda (a b c d)
  209. (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
  210. (lambda (a b c d)
  211. (= (bit-extract a b c) d))
  212. (list
  213. (list fixnum-min 0 (+ fixnum-bit -1) (ash 0 (- fixnum-bit 1)))
  214. (list fixnum-min 1 (+ fixnum-bit 0) (ash 1 (- fixnum-bit 2)))
  215. (list fixnum-min 2 (+ fixnum-bit 1) (ash 3 (- fixnum-bit 3)))
  216. (list fixnum-min (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -3)
  217. (- (ash 1 (- fixnum-bit 1)) 2))
  218. (list fixnum-min (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -2)
  219. (- (ash 1 (- fixnum-bit 1)) 1))
  220. (list fixnum-min (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit -1)
  221. (- (ash 1 (- fixnum-bit 1)) 1))
  222. (list fixnum-min (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 0)
  223. (- (ash 1 (- fixnum-bit 1)) 1))))
  224. (run-tests
  225. (lambda (a b c d)
  226. (string-append "fixnum-bit bits starting at " (number->string b)))
  227. (lambda (a b c d)
  228. (= (bit-extract a b c) d))
  229. (list
  230. (list fixnum-min 0 (+ fixnum-bit 0) (ash 1 (- fixnum-bit 1)))
  231. (list fixnum-min 1 (+ fixnum-bit 1) (ash 3 (- fixnum-bit 2)))
  232. (list fixnum-min 2 (+ fixnum-bit 2) (ash 7 (- fixnum-bit 3)))
  233. (list fixnum-min (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -2)
  234. (- (ash 1 fixnum-bit) 2))
  235. (list fixnum-min (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit -1)
  236. (- (ash 1 fixnum-bit) 1))
  237. (list fixnum-min (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit 0)
  238. (- (ash 1 fixnum-bit) 1))
  239. (list fixnum-min (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 1)
  240. (- (ash 1 fixnum-bit) 1))))
  241. (run-tests
  242. (lambda (a b c d)
  243. (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
  244. (lambda (a b c d)
  245. (= (bit-extract a b c) d))
  246. (list
  247. (list fixnum-min 0 (+ fixnum-bit 1) (ash 3 (- fixnum-bit 1)))
  248. (list fixnum-min 1 (+ fixnum-bit 2) (ash 7 (- fixnum-bit 2)))
  249. (list fixnum-min 2 (+ fixnum-bit 3) (ash 15 (- fixnum-bit 3)))
  250. (list fixnum-min (+ fixnum-bit -2) (+ fixnum-bit fixnum-bit -1)
  251. (- (ash 1 (+ fixnum-bit 1)) 2))
  252. (list fixnum-min (+ fixnum-bit -1) (+ fixnum-bit fixnum-bit 0)
  253. (- (ash 1 (+ fixnum-bit 1)) 1))
  254. (list fixnum-min (+ fixnum-bit 0) (+ fixnum-bit fixnum-bit 1)
  255. (- (ash 1 (+ fixnum-bit 1)) 1))
  256. (list fixnum-min (+ fixnum-bit 1) (+ fixnum-bit fixnum-bit 2)
  257. (- (ash 1 (+ fixnum-bit 1)) 1)))))
  258. (with-test-prefix "extract from fixnum-min - 1"
  259. (run-tests
  260. (lambda (a b c d)
  261. (string-append "single bit " (number->string b)))
  262. (lambda (a b c d)
  263. (= (bit-extract a b c) d))
  264. (list
  265. (list (- fixnum-min 1) 0 1 1)
  266. (list (- fixnum-min 1) 1 2 1)
  267. (list (- fixnum-min 1) (+ fixnum-bit -2) (+ fixnum-bit -1) 1)
  268. (list (- fixnum-min 1) (+ fixnum-bit -1) (+ fixnum-bit 0) 0)
  269. (list (- fixnum-min 1) (+ fixnum-bit 0) (+ fixnum-bit 1) 1)
  270. (list (- fixnum-min 1) (+ fixnum-bit 1) (+ fixnum-bit 2) 1)))
  271. (run-tests
  272. (lambda (a b c d)
  273. (string-append "fixnum-bit - 1 bits starting at " (number->string b)))
  274. (lambda (a b c d)
  275. (= (bit-extract a b c) d))
  276. (list
  277. (list (- fixnum-min 1) 0 (+ fixnum-bit -1)
  278. (- (ash 1 (- fixnum-bit 1)) 1 (ash 0 (- fixnum-bit 1))))
  279. (list (- fixnum-min 1) 1 (+ fixnum-bit 0)
  280. (- (ash 1 (- fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 2))))
  281. (list (- fixnum-min 1) 2 (+ fixnum-bit 1)
  282. (- (ash 1 (- fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 3))))
  283. (list (- fixnum-min 1) (+ fixnum-bit -2)
  284. (+ fixnum-bit fixnum-bit -3) (- (ash 1 (- fixnum-bit 1)) 3))
  285. (list (- fixnum-min 1) (+ fixnum-bit -1)
  286. (+ fixnum-bit fixnum-bit -2) (- (ash 1 (- fixnum-bit 1)) 2))
  287. (list (- fixnum-min 1) (+ fixnum-bit 0)
  288. (+ fixnum-bit fixnum-bit -1) (- (ash 1 (- fixnum-bit 1)) 1))
  289. (list (- fixnum-min 1) (+ fixnum-bit 1)
  290. (+ fixnum-bit fixnum-bit 0) (- (ash 1 (- fixnum-bit 1)) 1))))
  291. (run-tests
  292. (lambda (a b c d)
  293. (string-append "fixnum-bit bits starting at " (number->string b)))
  294. (lambda (a b c d)
  295. (= (bit-extract a b c) d))
  296. (list
  297. (list (- fixnum-min 1) 0 (+ fixnum-bit 0)
  298. (- (ash 1 fixnum-bit) 1 (ash 1 (- fixnum-bit 1))))
  299. (list (- fixnum-min 1) 1 (+ fixnum-bit 1)
  300. (- (ash 1 fixnum-bit) 1 (ash 1 (- fixnum-bit 2))))
  301. (list (- fixnum-min 1) 2 (+ fixnum-bit 2)
  302. (- (ash 1 fixnum-bit) 1 (ash 1 (- fixnum-bit 3))))
  303. (list (- fixnum-min 1) (+ fixnum-bit -2)
  304. (+ fixnum-bit fixnum-bit -2) (- (ash 1 fixnum-bit) 3))
  305. (list (- fixnum-min 1) (+ fixnum-bit -1)
  306. (+ fixnum-bit fixnum-bit -1) (- (ash 1 fixnum-bit) 2))
  307. (list (- fixnum-min 1) (+ fixnum-bit 0)
  308. (+ fixnum-bit fixnum-bit 0) (- (ash 1 fixnum-bit) 1))
  309. (list (- fixnum-min 1) (+ fixnum-bit 1)
  310. (+ fixnum-bit fixnum-bit 1) (- (ash 1 fixnum-bit) 1))))
  311. (run-tests
  312. (lambda (a b c d)
  313. (string-append "fixnum-bit + 1 bits starting at " (number->string b)))
  314. (lambda (a b c d)
  315. (= (bit-extract a b c) d))
  316. (list
  317. (list (- fixnum-min 1) 0 (+ fixnum-bit 1)
  318. (- (ash 1 (+ fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 1))))
  319. (list (- fixnum-min 1) 1 (+ fixnum-bit 2)
  320. (- (ash 1 (+ fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 2))))
  321. (list (- fixnum-min 1) 2 (+ fixnum-bit 3)
  322. (- (ash 1 (+ fixnum-bit 1)) 1 (ash 1 (- fixnum-bit 3))))
  323. (list (- fixnum-min 1) (+ fixnum-bit -2)
  324. (+ fixnum-bit fixnum-bit -1) (- (ash 1 (+ fixnum-bit 1)) 3))
  325. (list (- fixnum-min 1) (+ fixnum-bit -1)
  326. (+ fixnum-bit fixnum-bit 0) (- (ash 1 (+ fixnum-bit 1)) 2))
  327. (list (- fixnum-min 1) (+ fixnum-bit 0)
  328. (+ fixnum-bit fixnum-bit 1) (- (ash 1 (+ fixnum-bit 1)) 1))
  329. (list (- fixnum-min 1) (+ fixnum-bit 1)
  330. (+ fixnum-bit fixnum-bit 2) (- (ash 1 (+ fixnum-bit 1)) 1))))))