srfi-4.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. ;;;; srfi-4.test --- Test suite for Guile's SRFI-4 functions. -*- scheme -*-
  2. ;;;; Martin Grabmueller, 2001-06-26
  3. ;;;;
  4. ;;;; Copyright (C) 2001, 2006, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (use-modules (srfi srfi-4)
  20. (srfi srfi-4 gnu)
  21. (test-suite lib))
  22. (with-test-prefix "u8 vectors"
  23. (pass-if "u8vector? success"
  24. (u8vector? (u8vector)))
  25. (pass-if "u8vector? failure"
  26. (not (u8vector? (s8vector))))
  27. (pass-if "u8vector-length success 1"
  28. (= (u8vector-length (u8vector)) 0))
  29. (pass-if "u8vector-length success 2"
  30. (= (u8vector-length (u8vector 3)) 1))
  31. (pass-if "u8vector-length failure"
  32. (not (= (u8vector-length (u8vector 3)) 3)))
  33. (pass-if "u8vector-ref"
  34. (= (u8vector-ref (u8vector 1 2 3) 1) 2))
  35. (pass-if "u8vector-set!/ref"
  36. (= (let ((s (make-u8vector 10 0)))
  37. (u8vector-set! s 4 33)
  38. (u8vector-ref s 4)) 33))
  39. (pass-if "u8vector->list/list->u8vector"
  40. (equal? (u8vector->list (u8vector 1 2 3 4))
  41. (u8vector->list (list->u8vector '(1 2 3 4)))))
  42. (pass-if "u8vector->list/array->list"
  43. (equal? (u8vector->list (u8vector 1 2 3 4))
  44. (array->list (u8vector 1 2 3 4))))
  45. (pass-if "make-u8vector"
  46. (equal? (list->u8vector '(7 7 7 7))
  47. (make-u8vector 4 7))))
  48. (with-test-prefix "s8 vectors"
  49. (pass-if "s8vector? success"
  50. (s8vector? (s8vector)))
  51. (pass-if "s8vector? failure"
  52. (not (s8vector? (u8vector))))
  53. (pass-if "s8vector-length success 1"
  54. (= (s8vector-length (s8vector)) 0))
  55. (pass-if "s8vector-length success 2"
  56. (= (s8vector-length (s8vector -3)) 1))
  57. (pass-if "s8vector-length failure"
  58. (not (= (s8vector-length (s8vector 3)) 3)))
  59. (pass-if "s8vector-ref"
  60. (= (s8vector-ref (s8vector 1 2 3) 1) 2))
  61. (pass-if "s8vector-set!/ref"
  62. (= (let ((s (make-s8vector 10 0)))
  63. (s8vector-set! s 4 33)
  64. (s8vector-ref s 4)) 33))
  65. (pass-if "s8vector->list/list->s8vector"
  66. (equal? (s8vector->list (s8vector 1 2 3 4))
  67. (s8vector->list (list->s8vector '(1 2 3 4)))))
  68. (pass-if "s8vector->list/array->list"
  69. (equal? (s8vector->list (s8vector 1 2 3 4))
  70. (array->list (s8vector 1 2 3 4))))
  71. (pass-if "make-s8vector"
  72. (equal? (list->s8vector '(7 7 7 7))
  73. (make-s8vector 4 7))))
  74. (with-test-prefix "u16 vectors"
  75. (pass-if "u16vector? success"
  76. (u16vector? (u16vector)))
  77. (pass-if "u16vector? failure"
  78. (not (u16vector? (s16vector))))
  79. (pass-if "u16vector-length success 1"
  80. (= (u16vector-length (u16vector)) 0))
  81. (pass-if "u16vector-length success 2"
  82. (= (u16vector-length (u16vector 3)) 1))
  83. (pass-if "u16vector-length failure"
  84. (not (= (u16vector-length (u16vector 3)) 3)))
  85. (pass-if "u16vector-ref"
  86. (= (u16vector-ref (u16vector 1 2 3) 1) 2))
  87. (pass-if "u16vector-set!/ref"
  88. (= (let ((s (make-u16vector 10 0)))
  89. (u16vector-set! s 4 33)
  90. (u16vector-ref s 4)) 33))
  91. (pass-if "u16vector->list/list->u16vector"
  92. (equal? (u16vector->list (u16vector 1 2 3 4))
  93. (u16vector->list (list->u16vector '(1 2 3 4)))))
  94. (pass-if "u16vector->list/array->list"
  95. (equal? (u16vector->list (u16vector 1 2 3 4))
  96. (array->list (u16vector 1 2 3 4))))
  97. (pass-if "make-u16vector"
  98. (equal? (list->u16vector '(7 7 7 7))
  99. (make-u16vector 4 7))))
  100. (with-test-prefix "s16 vectors"
  101. (pass-if "s16vector? success"
  102. (s16vector? (s16vector)))
  103. (pass-if "s16vector? failure"
  104. (not (s16vector? (u16vector))))
  105. (pass-if "s16vector-length success 1"
  106. (= (s16vector-length (s16vector)) 0))
  107. (pass-if "s16vector-length success 2"
  108. (= (s16vector-length (s16vector -3)) 1))
  109. (pass-if "s16vector-length failure"
  110. (not (= (s16vector-length (s16vector 3)) 3)))
  111. (pass-if "s16vector-ref"
  112. (= (s16vector-ref (s16vector 1 2 3) 1) 2))
  113. (pass-if "s16vector-set!/ref"
  114. (= (let ((s (make-s16vector 10 0)))
  115. (s16vector-set! s 4 33)
  116. (s16vector-ref s 4)) 33))
  117. (pass-if "s16vector->list/list->s16vector"
  118. (equal? (s16vector->list (s16vector 1 2 3 4))
  119. (s16vector->list (list->s16vector '(1 2 3 4)))))
  120. (pass-if "s16vector->list/array->list"
  121. (equal? (s16vector->list (s16vector 1 2 3 4))
  122. (array->list (s16vector 1 2 3 4))))
  123. (pass-if "make-s16vector"
  124. (equal? (list->s16vector '(7 7 7 7))
  125. (make-s16vector 4 7))))
  126. (with-test-prefix "u32 vectors"
  127. (pass-if "u32vector? success"
  128. (u32vector? (u32vector)))
  129. (pass-if "u32vector? failure"
  130. (not (u32vector? (s32vector))))
  131. (pass-if "u32vector-length success 1"
  132. (= (u32vector-length (u32vector)) 0))
  133. (pass-if "u32vector-length success 2"
  134. (= (u32vector-length (u32vector 3)) 1))
  135. (pass-if "u32vector-length failure"
  136. (not (= (u32vector-length (u32vector 3)) 3)))
  137. (pass-if "u32vector-ref"
  138. (= (u32vector-ref (u32vector 1 2 3) 1) 2))
  139. (pass-if "u32vector-set!/ref"
  140. (= (let ((s (make-u32vector 10 0)))
  141. (u32vector-set! s 4 33)
  142. (u32vector-ref s 4)) 33))
  143. (pass-if "u32vector->list/list->u32vector"
  144. (equal? (u32vector->list (u32vector 1 2 3 4))
  145. (u32vector->list (list->u32vector '(1 2 3 4)))))
  146. (pass-if "u32vector->list/array->list"
  147. (equal? (u32vector->list (u32vector 1 2 3 4))
  148. (array->list (u32vector 1 2 3 4))))
  149. (pass-if "make-u32vector"
  150. (equal? (list->u32vector '(7 7 7 7))
  151. (make-u32vector 4 7))))
  152. (with-test-prefix "s32 vectors"
  153. (pass-if "s32vector? success"
  154. (s32vector? (s32vector)))
  155. (pass-if "s32vector? failure"
  156. (not (s32vector? (u32vector))))
  157. (pass-if "s32vector-length success 1"
  158. (= (s32vector-length (s32vector)) 0))
  159. (pass-if "s32vector-length success 2"
  160. (= (s32vector-length (s32vector -3)) 1))
  161. (pass-if "s32vector-length failure"
  162. (not (= (s32vector-length (s32vector 3)) 3)))
  163. (pass-if "s32vector-ref"
  164. (= (s32vector-ref (s32vector 1 2 3) 1) 2))
  165. (pass-if "s32vector-set!/ref"
  166. (= (let ((s (make-s32vector 10 0)))
  167. (s32vector-set! s 4 33)
  168. (s32vector-ref s 4)) 33))
  169. (pass-if "s32vector->list/list->s32vector"
  170. (equal? (s32vector->list (s32vector 1 2 3 4))
  171. (s32vector->list (list->s32vector '(1 2 3 4)))))
  172. (pass-if "s32vector->list/array->list"
  173. (equal? (s32vector->list (s32vector 1 2 3 4))
  174. (array->list (s32vector 1 2 3 4))))
  175. (pass-if "make-s32vector"
  176. (equal? (list->s32vector '(7 7 7 7))
  177. (make-s32vector 4 7))))
  178. (with-test-prefix "u64 vectors"
  179. (pass-if "u64vector? success"
  180. (u64vector? (u64vector)))
  181. (pass-if "u64vector? failure"
  182. (not (u64vector? (s64vector))))
  183. (pass-if "u64vector-length success 1"
  184. (= (u64vector-length (u64vector)) 0))
  185. (pass-if "u64vector-length success 2"
  186. (= (u64vector-length (u64vector 3)) 1))
  187. (pass-if "u64vector-length failure"
  188. (not (= (u64vector-length (u64vector 3)) 3)))
  189. (pass-if "u64vector-ref"
  190. (= (u64vector-ref (u64vector 1 2 3) 1) 2))
  191. (pass-if "u64vector-set!/ref"
  192. (= (let ((s (make-u64vector 10 0)))
  193. (u64vector-set! s 4 33)
  194. (u64vector-ref s 4)) 33))
  195. (pass-if "u64vector->list/list->u64vector"
  196. (equal? (u64vector->list (u64vector 1 2 3 4))
  197. (u64vector->list (list->u64vector '(1 2 3 4)))))
  198. (pass-if "u64vector->list/array->list"
  199. (equal? (u64vector->list (u64vector 1 2 3 4))
  200. (array->list (u64vector 1 2 3 4))))
  201. (pass-if "make-u64vector"
  202. (equal? (list->u64vector '(7 7 7 7))
  203. (make-u64vector 4 7))))
  204. (with-test-prefix "s64 vectors"
  205. (pass-if "s64vector? success"
  206. (s64vector? (s64vector)))
  207. (pass-if "s64vector? failure"
  208. (not (s64vector? (u64vector))))
  209. (pass-if "s64vector-length success 1"
  210. (= (s64vector-length (s64vector)) 0))
  211. (pass-if "s64vector-length success 2"
  212. (= (s64vector-length (s64vector -3)) 1))
  213. (pass-if "s64vector-length failure"
  214. (not (= (s64vector-length (s64vector 3)) 3)))
  215. (pass-if "s64vector-ref"
  216. (= (s64vector-ref (s64vector 1 2 3) 1) 2))
  217. (pass-if "s64vector-set!/ref"
  218. (= (let ((s (make-s64vector 10 0)))
  219. (s64vector-set! s 4 33)
  220. (s64vector-ref s 4)) 33))
  221. (pass-if "s64vector->list/list->s64vector"
  222. (equal? (s64vector->list (s64vector 1 2 3 4))
  223. (s64vector->list (list->s64vector '(1 2 3 4)))))
  224. (pass-if "s64vector->list/array->list"
  225. (equal? (s64vector->list (s64vector 1 2 3 4))
  226. (array->list (s64vector 1 2 3 4))))
  227. (pass-if "make-s64vector"
  228. (equal? (list->s64vector '(7 7 7 7))
  229. (make-s64vector 4 7))))
  230. (with-test-prefix "f32 vectors"
  231. (pass-if "f32vector? success"
  232. (f32vector? (f32vector)))
  233. (pass-if "f32vector? failure"
  234. (not (f32vector? (s8vector))))
  235. (pass-if "f32vector-length success 1"
  236. (= (f32vector-length (f32vector)) 0))
  237. (pass-if "f32vector-length success 2"
  238. (= (f32vector-length (f32vector -3)) 1))
  239. (pass-if "f32vector-length failure"
  240. (not (= (f32vector-length (f32vector 3)) 3)))
  241. (pass-if "f32vector-ref"
  242. (= (f32vector-ref (f32vector 1 2 3) 1) 2))
  243. (pass-if "f32vector-set!/ref"
  244. (= (let ((s (make-f32vector 10 0)))
  245. (f32vector-set! s 4 33)
  246. (f32vector-ref s 4)) 33))
  247. (pass-if "f32vector->list/list->f32vector"
  248. (equal? (f32vector->list (f32vector 1 2 3 4))
  249. (f32vector->list (list->f32vector '(1 2 3 4)))))
  250. (pass-if "f32vector->list/array->list"
  251. (equal? (f32vector->list (f32vector 1 2 3 4))
  252. (array->list (f32vector 1 2 3 4))))
  253. (pass-if "make-f32vector"
  254. (equal? (list->f32vector '(7 7 7 7))
  255. (make-f32vector 4 7)))
  256. (pass-if "+inf.0, -inf.0, +nan.0 in f32vector"
  257. (f32vector? #f32(+inf.0 -inf.0 +nan.0))))
  258. (with-test-prefix "f64 vectors"
  259. (pass-if "f64vector? success"
  260. (f64vector? (f64vector)))
  261. (pass-if "f64vector? failure"
  262. (not (f64vector? (f32vector))))
  263. (pass-if "f64vector-length success 1"
  264. (= (f64vector-length (f64vector)) 0))
  265. (pass-if "f64vector-length success 2"
  266. (= (f64vector-length (f64vector -3)) 1))
  267. (pass-if "f64vector-length failure"
  268. (not (= (f64vector-length (f64vector 3)) 3)))
  269. (pass-if "f64vector-ref"
  270. (= (f64vector-ref (f64vector 1 2 3) 1) 2))
  271. (pass-if "f64vector-set!/ref"
  272. (= (let ((s (make-f64vector 10 0)))
  273. (f64vector-set! s 4 33)
  274. (f64vector-ref s 4)) 33))
  275. (pass-if "f64vector->list/list->f64vector"
  276. (equal? (f64vector->list (f64vector 1 2 3 4))
  277. (f64vector->list (list->f64vector '(1 2 3 4)))))
  278. (pass-if "f64vector->list/array->list"
  279. (equal? (f64vector->list (f64vector 1 2 3 4))
  280. (array->list (f64vector 1 2 3 4))))
  281. (pass-if "make-f64vector"
  282. (equal? (list->f64vector '(7 7 7 7))
  283. (make-f64vector 4 7)))
  284. (pass-if "+inf.0, -inf.0, +nan.0 in f64vector"
  285. (f64vector? #f64(+inf.0 -inf.0 +nan.0))))
  286. (with-test-prefix "c32 vectors"
  287. (pass-if "c32vector? success"
  288. (c32vector? (c32vector)))
  289. (pass-if "c32vector? failure"
  290. (not (c32vector? (s8vector))))
  291. (pass-if "c32vector-length success 1"
  292. (= (c32vector-length (c32vector)) 0))
  293. (pass-if "c32vector-length success 2"
  294. (= (c32vector-length (c32vector -3-2i)) 1))
  295. (pass-if "c32vector-length failure"
  296. (not (= (c32vector-length (c32vector 3)) 3)))
  297. (pass-if "c32vector-ref"
  298. (= (c32vector-ref (c32vector 1 2+13i 3) 1) 2+13i))
  299. (pass-if "c32vector-set!/ref"
  300. (= (let ((s (make-c32vector 10 0)))
  301. (c32vector-set! s 4 33-1i)
  302. (c32vector-ref s 4)) 33-1i))
  303. (pass-if "c32vector->list/list->c32vector"
  304. (equal? (c32vector->list (c32vector 1 2 3 4))
  305. (c32vector->list (list->c32vector '(1 2 3 4)))))
  306. (pass-if "c32vector->list/array->list"
  307. (equal? (c32vector->list (c32vector 1 2 3 4))
  308. (array->list (c32vector 1 2 3 4))))
  309. (pass-if "make-c32vector"
  310. (equal? (list->c32vector '(7 7 7 7))
  311. (make-c32vector 4 7)))
  312. (pass-if "+inf.0, -inf.0, +nan.0 in c32vector"
  313. (c32vector? #c32(+inf.0 -inf.0 +nan.0)))
  314. (pass-if "array-ref"
  315. (let ((v (c32vector 1+1i)))
  316. (= (c32vector-ref v 0)
  317. (array-ref v 0))))
  318. (pass-if "array-set!"
  319. (let ((x 1+1i)
  320. (v (c32vector 0)))
  321. (array-set! v x 0)
  322. (= x (array-ref v 0))))
  323. (pass-if-exception "array-ref, out-of-range"
  324. exception:out-of-range
  325. (array-ref (c32vector 1.0) 1))
  326. (pass-if-exception "array-set!, out-of-range"
  327. exception:out-of-range
  328. (array-set! (c32vector 1.0) 2.0 1)))
  329. (with-test-prefix "c64 vectors"
  330. (pass-if "c64vector? success"
  331. (c64vector? (c64vector)))
  332. (pass-if "c64vector? failure"
  333. (not (c64vector? (s8vector))))
  334. (pass-if "c64vector-length success 1"
  335. (= (c64vector-length (c64vector)) 0))
  336. (pass-if "c64vector-length success 2"
  337. (= (c64vector-length (c64vector -3-2i)) 1))
  338. (pass-if "c64vector-length failure"
  339. (not (= (c64vector-length (c64vector 3)) 3)))
  340. (pass-if "c64vector-ref"
  341. (= (c64vector-ref (c64vector 1+2i 2+3i 3) 1) 2+3i))
  342. (pass-if "c64vector-set!/ref"
  343. (= (let ((s (make-c64vector 10 0)))
  344. (c64vector-set! s 4 33+1i)
  345. (c64vector-ref s 4)) 33+1i))
  346. (pass-if "c64vector->list/list->c64vector"
  347. (equal? (c64vector->list (c64vector 1 2 3 4))
  348. (c64vector->list (list->c64vector '(1 2 3 4)))))
  349. (pass-if "c64vector->list/array->list"
  350. (equal? (c64vector->list (c64vector 1 2 3 4))
  351. (array->list (c64vector 1 2 3 4))))
  352. (pass-if "make-c64vector"
  353. (equal? (list->c64vector '(7 7 7 7))
  354. (make-c64vector 4 7)))
  355. (pass-if "+inf.0, -inf.0, +nan.0 in c64vector"
  356. (c64vector? #c64(+inf.0 -inf.0 +nan.0)))
  357. (pass-if "array-ref"
  358. (let ((v (c64vector 1+1i)))
  359. (= (c64vector-ref v 0)
  360. (array-ref v 0))))
  361. (pass-if "array-set!"
  362. (let ((x 1+1i)
  363. (v (c64vector 0)))
  364. (array-set! v x 0)
  365. (= x (array-ref v 0))))
  366. (pass-if-exception "array-ref, out-of-range"
  367. exception:out-of-range
  368. (array-ref (c64vector 1.0) 1))
  369. (pass-if-exception "array-set!, out-of-range"
  370. exception:out-of-range
  371. (array-set! (c64vector 1.0) 2.0 1)))
  372. (with-test-prefix "accessing uniform vectors of different types"
  373. (pass-if "u32vector-length of u16vector"
  374. (= 2 (u32vector-length (make-u16vector 4))))
  375. (pass-if "u32vector-length of u8vector"
  376. (= 2 (u32vector-length (make-u8vector 8))))
  377. (pass-if "u8vector-length of u16vector"
  378. (= 4 (u8vector-length (make-u16vector 2))))
  379. (pass-if "u8vector-length of u32vector"
  380. (= 8 (u8vector-length (make-u32vector 2))))
  381. (pass-if "u32vector-set! of u16vector"
  382. (let ((v (make-u16vector 4 #xFFFF)))
  383. (u32vector-set! v 1 0)
  384. (equal? v #u16(#xFFFF #xFFFF 0 0))))
  385. (pass-if "u16vector-set! of u32vector"
  386. (let ((v (make-u32vector 2 #xFFFFFFFF)))
  387. (u16vector-set! v 2 0)
  388. (u16vector-set! v 3 0)
  389. (equal? v #u32(#xFFFFFFFF 0)))))