bytevectors.test 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. ;;;; bytevectors.test --- R6RS bytevectors. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2009-2015, 2018, 2021 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; Ludovic Courtès
  6. ;;;;
  7. ;;;; This library is free software; you can redistribute it and/or
  8. ;;;; modify it under the terms of the GNU Lesser General Public
  9. ;;;; License as published by the Free Software Foundation; either
  10. ;;;; version 3 of the License, or (at your option) any later version.
  11. ;;;;
  12. ;;;; This library is distributed in the hope that it will be useful,
  13. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;;;; Lesser General Public License for more details.
  16. ;;;;
  17. ;;;; You should have received a copy of the GNU Lesser General Public
  18. ;;;; License along with this library; if not, write to the Free Software
  19. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. (define-module (test-bytevector)
  21. #:use-module (test-suite lib)
  22. #:use-module (system base compile)
  23. #:use-module (rnrs bytevectors)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-4))
  26. (define exception:decoding-error
  27. (cons 'decoding-error "input (locale conversion|decoding) error"))
  28. ;;; Some of the tests in here are examples taken from the R6RS Standard
  29. ;;; Libraries document.
  30. (with-test-prefix/c&e "2.2 General Operations"
  31. (pass-if "native-endianness"
  32. (not (not (memq (native-endianness) '(big little)))))
  33. (pass-if "make-bytevector"
  34. (and (bytevector? (make-bytevector 20))
  35. (bytevector? (make-bytevector 20 3))))
  36. (pass-if "bytevector-length"
  37. (= (bytevector-length (make-bytevector 20)) 20))
  38. (pass-if "bytevector=?"
  39. (and (bytevector=? (make-bytevector 20 7)
  40. (make-bytevector 20 7))
  41. (not (bytevector=? (make-bytevector 20 7)
  42. (make-bytevector 20 0)))))
  43. ;; This failed prior to Guile 2.0.12.
  44. ;; See <http://bugs.gnu.org/19027>.
  45. (pass-if-equal "bytevector-fill! with fill 255"
  46. #vu8(255 255 255 255)
  47. (let ((bv (make-bytevector 4)))
  48. (bytevector-fill! bv 255)
  49. bv))
  50. ;; This is a Guile-specific extension.
  51. (pass-if-equal "bytevector-fill! with fill -128"
  52. #vu8(128 128 128 128)
  53. (let ((bv (make-bytevector 4)))
  54. (bytevector-fill! bv -128)
  55. bv))
  56. ;; This is a Guile-specific extension.
  57. (pass-if-equal "bytevector-fill! range arguments I"
  58. #vu8(0 0 1 1 1)
  59. (let ((bv (make-bytevector 5 0)))
  60. (bytevector-fill! bv 1 2)
  61. bv))
  62. ;; This is a Guile-specific extension.
  63. (pass-if-equal "bytevector-fill! range arguments II"
  64. #vu8(0 0 1 1 0)
  65. (let ((bv (make-bytevector 5 0)))
  66. (bytevector-fill! bv 1 2 4)
  67. bv))
  68. (pass-if "bytevector-copy! overlapping"
  69. ;; See <http://debbugs.gnu.org/10070>.
  70. (let ((b (u8-list->bytevector '(1 2 3 4 5 6 7 8))))
  71. (bytevector-copy! b 0 b 3 4)
  72. (bytevector->u8-list b)
  73. (bytevector=? b #vu8(1 2 3 1 2 3 4 8)))))
  74. (with-test-prefix/c&e "2.3 Operations on Bytes and Octets"
  75. (pass-if "bytevector-{u8,s8}-ref"
  76. (equal? '(-127 129 -1 255)
  77. (let ((b1 (make-bytevector 16 -127))
  78. (b2 (make-bytevector 16 255)))
  79. (list (bytevector-s8-ref b1 0)
  80. (bytevector-u8-ref b1 0)
  81. (bytevector-s8-ref b2 0)
  82. (bytevector-u8-ref b2 0)))))
  83. (pass-if "bytevector-{u8,s8}-set!"
  84. (equal? '(-126 130 -10 246)
  85. (let ((b (make-bytevector 16 -127)))
  86. (bytevector-s8-set! b 0 -126)
  87. (bytevector-u8-set! b 1 246)
  88. (list (bytevector-s8-ref b 0)
  89. (bytevector-u8-ref b 0)
  90. (bytevector-s8-ref b 1)
  91. (bytevector-u8-ref b 1)))))
  92. (pass-if "bytevector->u8-list"
  93. (let ((lst '(1 2 3 128 150 255)))
  94. (equal? lst
  95. (bytevector->u8-list
  96. (let ((b (make-bytevector 6)))
  97. (for-each (lambda (i v)
  98. (bytevector-u8-set! b i v))
  99. (iota 6)
  100. lst)
  101. b)))))
  102. (pass-if "u8-list->bytevector"
  103. (let ((lst '(1 2 3 128 150 255)))
  104. (equal? lst
  105. (bytevector->u8-list (u8-list->bytevector lst)))))
  106. (pass-if-exception "u8-list->bytevector [invalid argument type]"
  107. exception:wrong-type-arg
  108. (u8-list->bytevector 'not-a-list))
  109. (pass-if-exception "u8-list->bytevector [circular list]"
  110. exception:wrong-type-arg
  111. (u8-list->bytevector (circular-list 1 2 3)))
  112. (pass-if "bytevector-uint-{ref,set!} [small]"
  113. (let ((b (make-bytevector 15)))
  114. (bytevector-uint-set! b 0 #x1234
  115. (endianness little) 2)
  116. (equal? (bytevector-uint-ref b 0 (endianness big) 2)
  117. #x3412)))
  118. (pass-if "bytevector-uint-set! [large]"
  119. (let ((b (make-bytevector 16)))
  120. (bytevector-uint-set! b 0 (- (expt 2 128) 3)
  121. (endianness little) 16)
  122. (equal? (bytevector->u8-list b)
  123. '(253 255 255 255 255 255 255 255
  124. 255 255 255 255 255 255 255 255))))
  125. (pass-if "bytevector-uint-{ref,set!} [large]"
  126. (let ((b (make-bytevector 120)))
  127. (bytevector-uint-set! b 0 (- (expt 2 128) 3)
  128. (endianness little) 16)
  129. (equal? (bytevector-uint-ref b 0 (endianness little) 16)
  130. #xfffffffffffffffffffffffffffffffd)))
  131. (pass-if "bytevector-sint-ref [small]"
  132. (let ((b (u8-list->bytevector '(#xff #xf0 #xff))))
  133. (equal? (bytevector-sint-ref b 0 (endianness big) 2)
  134. (bytevector-sint-ref b 1 (endianness little) 2)
  135. -16)))
  136. (pass-if "bytevector-sint-ref [large]"
  137. (let ((b (make-bytevector 50)))
  138. (bytevector-uint-set! b 0 (- (expt 2 128) 3)
  139. (endianness little) 16)
  140. (equal? (bytevector-sint-ref b 0 (endianness little) 16)
  141. -3)))
  142. (pass-if "bytevector-sint-set! [small]"
  143. (let ((b (make-bytevector 3)))
  144. (bytevector-sint-set! b 0 -16 (endianness big) 2)
  145. (bytevector-sint-set! b 1 -16 (endianness little) 2)
  146. (equal? (bytevector->u8-list b)
  147. '(#xff #xf0 #xff))))
  148. (pass-if "equal?"
  149. (let ((bv1 (u8-list->bytevector (iota 123)))
  150. (bv2 (u8-list->bytevector (iota 123))))
  151. (equal? bv1 bv2))))
  152. (with-test-prefix/c&e "2.4 Operations on Integers of Arbitrary Size"
  153. (pass-if "bytevector->sint-list"
  154. (let ((b (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
  155. (equal? (bytevector->sint-list b (endianness little) 2)
  156. '(513 -253 513 513))))
  157. (pass-if "bytevector->uint-list"
  158. (let ((b (u8-list->bytevector '(2 1 255 3 2 1 2 1))))
  159. (equal? (bytevector->uint-list b (endianness big) 2)
  160. '(513 65283 513 513))))
  161. (pass-if "bytevector->uint-list [empty]"
  162. (let ((b (make-bytevector 0)))
  163. (null? (bytevector->uint-list b (endianness big) 2))))
  164. (pass-if-exception "bytevector->sint-list [out-of-range]"
  165. exception:out-of-range
  166. (bytevector->sint-list (make-bytevector 6) (endianness little) -1))
  167. (pass-if-exception "bytevector->uint-list [out-of-range]"
  168. exception:out-of-range
  169. (bytevector->uint-list (make-bytevector 6) (endianness little) 0))
  170. (pass-if-exception "bytevector->uint-list [word size doesn't divide length]"
  171. exception:wrong-type-arg
  172. (bytevector->uint-list (make-bytevector 6) (endianness little) 4))
  173. (pass-if "{sint,uint}-list->bytevector"
  174. (let ((b1 (sint-list->bytevector '(513 -253 513 513)
  175. (endianness little) 2))
  176. (b2 (uint-list->bytevector '(513 65283 513 513)
  177. (endianness little) 2))
  178. (b3 (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
  179. (and (bytevector=? b1 b2)
  180. (bytevector=? b2 b3))))
  181. (pass-if "sint-list->bytevector [limits]"
  182. (bytevector=? (sint-list->bytevector '(-32768 32767)
  183. (endianness big) 2)
  184. (let ((bv (make-bytevector 4)))
  185. (bytevector-u8-set! bv 0 #x80)
  186. (bytevector-u8-set! bv 1 #x00)
  187. (bytevector-u8-set! bv 2 #x7f)
  188. (bytevector-u8-set! bv 3 #xff)
  189. bv)))
  190. (pass-if-exception "sint-list->bytevector [invalid argument type]"
  191. exception:wrong-type-arg
  192. (sint-list->bytevector 'not-a-list (endianness big) 2))
  193. (pass-if-exception "uint-list->bytevector [invalid argument type]"
  194. exception:wrong-type-arg
  195. (uint-list->bytevector 'not-a-list (endianness big) 2))
  196. (pass-if-exception "sint-list->bytevector [circular list]"
  197. exception:wrong-type-arg
  198. (sint-list->bytevector (circular-list 1 2 3) (endianness big)
  199. 2))
  200. (pass-if-exception "uint-list->bytevector [circular list]"
  201. exception:wrong-type-arg
  202. (uint-list->bytevector (circular-list 1 2 3) (endianness big)
  203. 2))
  204. (pass-if-exception "sint-list->bytevector [out-of-range]"
  205. exception:out-of-range
  206. (sint-list->bytevector (list 0 0 (expt 2 16)) (endianness big)
  207. 2))
  208. (pass-if-exception "uint-list->bytevector [out-of-range]"
  209. exception:out-of-range
  210. (uint-list->bytevector '(0 -1) (endianness big) 2)))
  211. (with-test-prefix/c&e "2.5 Operations on 16-Bit Integers"
  212. (pass-if "bytevector-u16-ref"
  213. (let ((b (u8-list->bytevector
  214. '(255 255 255 255 255 255 255 255
  215. 255 255 255 255 255 255 255 253))))
  216. (and (equal? (bytevector-u16-ref b 14 (endianness little))
  217. #xfdff)
  218. (equal? (bytevector-u16-ref b 14 (endianness big))
  219. #xfffd))))
  220. (pass-if "bytevector-s16-ref"
  221. (let ((b (u8-list->bytevector
  222. '(255 255 255 255 255 255 255 255
  223. 255 255 255 255 255 255 255 253))))
  224. (and (equal? (bytevector-s16-ref b 14 (endianness little))
  225. -513)
  226. (equal? (bytevector-s16-ref b 14 (endianness big))
  227. -3))))
  228. (pass-if "bytevector-s16-ref [unaligned]"
  229. (let ((b (u8-list->bytevector '(#xff #xf0 #xff))))
  230. (equal? (bytevector-s16-ref b 1 (endianness little))
  231. -16)))
  232. (pass-if "bytevector-{u16,s16}-ref"
  233. (let ((b (make-bytevector 2)))
  234. (bytevector-u16-set! b 0 44444 (endianness little))
  235. (and (equal? (bytevector-u16-ref b 0 (endianness little))
  236. 44444)
  237. (equal? (bytevector-s16-ref b 0 (endianness little))
  238. (- 44444 65536)))))
  239. (pass-if "bytevector-native-{u16,s16}-{ref,set!}"
  240. (let ((b (make-bytevector 2)))
  241. (bytevector-u16-native-set! b 0 44444)
  242. (and (equal? (bytevector-u16-native-ref b 0)
  243. 44444)
  244. (equal? (bytevector-s16-native-ref b 0)
  245. (- 44444 65536)))))
  246. (pass-if "bytevector-s16-{ref,set!} [unaligned]"
  247. (let ((b (make-bytevector 3)))
  248. (bytevector-s16-set! b 1 -77 (endianness little))
  249. (equal? (bytevector-s16-ref b 1 (endianness little))
  250. -77))))
  251. (with-test-prefix/c&e "2.6 Operations on 32-bit Integers"
  252. (pass-if "bytevector-u32-ref"
  253. (let ((b (u8-list->bytevector
  254. '(255 255 255 255 255 255 255 255
  255. 255 255 255 255 255 255 255 253))))
  256. (and (equal? (bytevector-u32-ref b 12 (endianness little))
  257. #xfdffffff)
  258. (equal? (bytevector-u32-ref b 12 (endianness big))
  259. #xfffffffd))))
  260. (pass-if "bytevector-s32-ref"
  261. (let ((b (u8-list->bytevector
  262. '(255 255 255 255 255 255 255 255
  263. 255 255 255 255 255 255 255 253))))
  264. (and (equal? (bytevector-s32-ref b 12 (endianness little))
  265. -33554433)
  266. (equal? (bytevector-s32-ref b 12 (endianness big))
  267. -3))))
  268. (pass-if "bytevector-{u32,s32}-ref"
  269. (let ((b (make-bytevector 4)))
  270. (bytevector-u32-set! b 0 2222222222 (endianness little))
  271. (and (equal? (bytevector-u32-ref b 0 (endianness little))
  272. 2222222222)
  273. (equal? (bytevector-s32-ref b 0 (endianness little))
  274. (- 2222222222 (expt 2 32))))))
  275. (pass-if "bytevector-{u32,s32}-native-{ref,set!}"
  276. (let ((b (make-bytevector 4)))
  277. (bytevector-u32-native-set! b 0 2222222222)
  278. (and (equal? (bytevector-u32-native-ref b 0)
  279. 2222222222)
  280. (equal? (bytevector-s32-native-ref b 0)
  281. (- 2222222222 (expt 2 32)))))))
  282. (with-test-prefix/c&e "2.7 Operations on 64-bit Integers"
  283. (pass-if "bytevector-u64-ref"
  284. (let ((b (u8-list->bytevector
  285. '(255 255 255 255 255 255 255 255
  286. 255 255 255 255 255 255 255 253))))
  287. (and (equal? (bytevector-u64-ref b 8 (endianness little))
  288. #xfdffffffffffffff)
  289. (equal? (bytevector-u64-ref b 8 (endianness big))
  290. #xfffffffffffffffd))))
  291. (pass-if "bytevector-s64-ref"
  292. (let ((b (u8-list->bytevector
  293. '(255 255 255 255 255 255 255 255
  294. 255 255 255 255 255 255 255 253))))
  295. (and (equal? (bytevector-s64-ref b 8 (endianness little))
  296. -144115188075855873)
  297. (equal? (bytevector-s64-ref b 8 (endianness big))
  298. -3))))
  299. (pass-if "bytevector-{u64,s64}-ref"
  300. (let ((b (make-bytevector 8))
  301. (big 9333333333333333333))
  302. (bytevector-u64-set! b 0 big (endianness little))
  303. (and (equal? (bytevector-u64-ref b 0 (endianness little))
  304. big)
  305. (equal? (bytevector-s64-ref b 0 (endianness little))
  306. (- big (expt 2 64))))))
  307. (pass-if "bytevector-{u64,s64}-native-{ref,set!}"
  308. (let ((b (make-bytevector 8))
  309. (big 9333333333333333333))
  310. (bytevector-u64-native-set! b 0 big)
  311. (and (equal? (bytevector-u64-native-ref b 0)
  312. big)
  313. (equal? (bytevector-s64-native-ref b 0)
  314. (- big (expt 2 64))))))
  315. (pass-if "ref/set! with zero"
  316. (let ((b (make-bytevector 8)))
  317. (bytevector-s64-set! b 0 -1 (endianness big))
  318. (bytevector-u64-set! b 0 0 (endianness big))
  319. (= 0 (bytevector-u64-ref b 0 (endianness big)))))
  320. (pass-if-exception "bignum out of range"
  321. exception:out-of-range
  322. (bytevector-u64-set! (make-bytevector 8) 0 (expt 2 64) (endianness big))))
  323. (with-test-prefix/c&e "2.8 Operations on IEEE-754 Representations"
  324. (pass-if "single, little endian"
  325. ;; http://bugs.gnu.org/11310
  326. (let ((b (make-bytevector 4)))
  327. (bytevector-ieee-single-set! b 0 1.0 (endianness little))
  328. (equal? #vu8(0 0 128 63) b)))
  329. (pass-if "single, big endian"
  330. ;; http://bugs.gnu.org/11310
  331. (let ((b (make-bytevector 4)))
  332. (bytevector-ieee-single-set! b 0 1.0 (endianness big))
  333. (equal? #vu8(63 128 0 0) b)))
  334. (pass-if "bytevector-ieee-single-native-{ref,set!}"
  335. (let ((b (make-bytevector 4))
  336. (number 3.00))
  337. (bytevector-ieee-single-native-set! b 0 number)
  338. (equal? (bytevector-ieee-single-native-ref b 0)
  339. number)))
  340. (pass-if "bytevector-ieee-single-{ref,set!}"
  341. (let ((b (make-bytevector 8))
  342. (number 3.14))
  343. (bytevector-ieee-single-set! b 0 number (endianness little))
  344. (bytevector-ieee-single-set! b 4 number (endianness big))
  345. (equal? (bytevector-ieee-single-ref b 0 (endianness little))
  346. (bytevector-ieee-single-ref b 4 (endianness big)))))
  347. (pass-if "bytevector-ieee-single-{ref,set!} [unaligned]"
  348. (let ((b (make-bytevector 9))
  349. (number 3.14))
  350. (bytevector-ieee-single-set! b 1 number (endianness little))
  351. (bytevector-ieee-single-set! b 5 number (endianness big))
  352. (equal? (bytevector-ieee-single-ref b 1 (endianness little))
  353. (bytevector-ieee-single-ref b 5 (endianness big)))))
  354. (pass-if "double, little endian"
  355. ;; http://bugs.gnu.org/11310
  356. (let ((b (make-bytevector 8)))
  357. (bytevector-ieee-double-set! b 0 1.0 (endianness little))
  358. (equal? #vu8(0 0 0 0 0 0 240 63) b)))
  359. (pass-if "double, big endian"
  360. ;; http://bugs.gnu.org/11310
  361. (let ((b (make-bytevector 8)))
  362. (bytevector-ieee-double-set! b 0 1.0 (endianness big))
  363. (equal? #vu8(63 240 0 0 0 0 0 0) b)))
  364. (pass-if "bytevector-ieee-double-native-{ref,set!}"
  365. (let ((b (make-bytevector 8))
  366. (number 3.14))
  367. (bytevector-ieee-double-native-set! b 0 number)
  368. (equal? (bytevector-ieee-double-native-ref b 0)
  369. number)))
  370. (pass-if "bytevector-ieee-double-{ref,set!}"
  371. (let ((b (make-bytevector 16))
  372. (number 3.14))
  373. (bytevector-ieee-double-set! b 0 number (endianness little))
  374. (bytevector-ieee-double-set! b 8 number (endianness big))
  375. (equal? (bytevector-ieee-double-ref b 0 (endianness little))
  376. (bytevector-ieee-double-ref b 8 (endianness big))))))
  377. ;; Default to the C locale for the following tests.
  378. (when (defined? 'setlocale)
  379. (setlocale LC_ALL "C"))
  380. (with-test-prefix "2.9 Operations on Strings"
  381. (pass-if "string->utf8"
  382. (let* ((str "hello, world")
  383. (utf8 (string->utf8 str)))
  384. (and (bytevector? utf8)
  385. (= (bytevector-length utf8)
  386. (string-length str))
  387. (equal? (string->list str)
  388. (map integer->char (bytevector->u8-list utf8))))))
  389. (pass-if "string->utf8 [latin-1]"
  390. (let* ((str "hé, ça va bien ?")
  391. (utf8 (string->utf8 str)))
  392. (and (bytevector? utf8)
  393. (= (bytevector-length utf8)
  394. (+ 2 (string-length str))))))
  395. (pass-if "string->utf16"
  396. (let* ((str "hello, world")
  397. (utf16 (string->utf16 str)))
  398. (and (bytevector? utf16)
  399. (= (bytevector-length utf16)
  400. (* 2 (string-length str)))
  401. (equal? (string->list str)
  402. (map integer->char
  403. (bytevector->uint-list utf16
  404. (endianness big) 2))))))
  405. (pass-if "string->utf16 [little]"
  406. (let* ((str "hello, world")
  407. (utf16 (string->utf16 str (endianness little))))
  408. (and (bytevector? utf16)
  409. (= (bytevector-length utf16)
  410. (* 2 (string-length str)))
  411. (equal? (string->list str)
  412. (map integer->char
  413. (bytevector->uint-list utf16
  414. (endianness little) 2))))))
  415. (pass-if "string->utf32"
  416. (let* ((str "hello, world")
  417. (utf32 (string->utf32 str)))
  418. (and (bytevector? utf32)
  419. (= (bytevector-length utf32)
  420. (* 4 (string-length str)))
  421. (equal? (string->list str)
  422. (map integer->char
  423. (bytevector->uint-list utf32
  424. (endianness big) 4))))))
  425. (pass-if "string->utf32 [Greek]"
  426. (let* ((str "Ἄνεμοι")
  427. (utf32 (string->utf32 str)))
  428. (and (bytevector? utf32)
  429. (equal? (bytevector->uint-list utf32 (endianness big) 4)
  430. '(#x1f0c #x3bd #x3b5 #x3bc #x3bf #x3b9)))))
  431. (pass-if "string->utf32 [little]"
  432. (let* ((str "hello, world")
  433. (utf32 (string->utf32 str (endianness little))))
  434. (and (bytevector? utf32)
  435. (= (bytevector-length utf32)
  436. (* 4 (string-length str)))
  437. (equal? (string->list str)
  438. (map integer->char
  439. (bytevector->uint-list utf32
  440. (endianness little) 4))))))
  441. (pass-if "utf8->string"
  442. (let* ((utf8 (u8-list->bytevector (map char->integer
  443. (string->list "hello, world"))))
  444. (str (utf8->string utf8)))
  445. (and (string? str)
  446. (= (string-length str)
  447. (bytevector-length utf8))
  448. (equal? (string->list str)
  449. (map integer->char (bytevector->u8-list utf8))))))
  450. (pass-if "utf8->string [latin-1]"
  451. (let* ((utf8 (string->utf8 "hé, ça va bien ?"))
  452. (str (utf8->string utf8)))
  453. (and (string? str)
  454. (= (string-length str)
  455. (- (bytevector-length utf8) 2)))))
  456. (pass-if-equal "utf8->string [replacement character]"
  457. '(104 105 65533)
  458. (map char->integer
  459. (string->list (utf8->string #vu8(104 105 239 191 189)))))
  460. (pass-if-exception "utf8->string [invalid encoding]"
  461. exception:decoding-error
  462. (utf8->string #vu8(104 105 239 191 50)))
  463. (pass-if "utf16->string"
  464. (let* ((utf16 (uint-list->bytevector (map char->integer
  465. (string->list "hello, world"))
  466. (endianness big) 2))
  467. (str (utf16->string utf16)))
  468. (and (string? str)
  469. (= (* 2 (string-length str))
  470. (bytevector-length utf16))
  471. (equal? (string->list str)
  472. (map integer->char
  473. (bytevector->uint-list utf16 (endianness big)
  474. 2))))))
  475. (pass-if "utf16->string [little]"
  476. (let* ((utf16 (uint-list->bytevector (map char->integer
  477. (string->list "hello, world"))
  478. (endianness little) 2))
  479. (str (utf16->string utf16 (endianness little))))
  480. (and (string? str)
  481. (= (* 2 (string-length str))
  482. (bytevector-length utf16))
  483. (equal? (string->list str)
  484. (map integer->char
  485. (bytevector->uint-list utf16 (endianness little)
  486. 2))))))
  487. (pass-if "utf32->string"
  488. (let* ((utf32 (uint-list->bytevector (map char->integer
  489. (string->list "hello, world"))
  490. (endianness big) 4))
  491. (str (utf32->string utf32)))
  492. (and (string? str)
  493. (= (* 4 (string-length str))
  494. (bytevector-length utf32))
  495. (equal? (string->list str)
  496. (map integer->char
  497. (bytevector->uint-list utf32 (endianness big)
  498. 4))))))
  499. (pass-if "utf32->string [little]"
  500. (let* ((utf32 (uint-list->bytevector (map char->integer
  501. (string->list "hello, world"))
  502. (endianness little) 4))
  503. (str (utf32->string utf32 (endianness little))))
  504. (and (string? str)
  505. (= (* 4 (string-length str))
  506. (bytevector-length utf32))
  507. (equal? (string->list str)
  508. (map integer->char
  509. (bytevector->uint-list utf32 (endianness little)
  510. 4)))))))
  511. (with-test-prefix "Datum Syntax"
  512. (pass-if "empty"
  513. (equal? (with-input-from-string "#vu8()" read)
  514. (make-bytevector 0)))
  515. (pass-if "simple"
  516. (equal? (with-input-from-string "#vu8(1 2 3 4 5)" read)
  517. (u8-list->bytevector '(1 2 3 4 5))))
  518. (pass-if ">127"
  519. (equal? (with-input-from-string "#vu8(0 255 127 128)" read)
  520. (u8-list->bytevector '(0 255 127 128))))
  521. (pass-if "self-evaluating?"
  522. (self-evaluating? (make-bytevector 1)))
  523. (pass-if "self-evaluating"
  524. (equal? (eval (with-input-from-string "#vu8(1 2 3 4 5)" read)
  525. (current-module))
  526. (u8-list->bytevector '(1 2 3 4 5))))
  527. (pass-if "quoted"
  528. (equal? (eval (with-input-from-string "'#vu8(1 2 3 4 5)" read)
  529. (current-module))
  530. (u8-list->bytevector '(1 2 3 4 5))))
  531. (pass-if "literal simple"
  532. (equal? #vu8(1 2 3 4 5)
  533. (u8-list->bytevector '(1 2 3 4 5))))
  534. (pass-if "literal >127"
  535. (equal? #vu8(0 255 127 128)
  536. (u8-list->bytevector '(0 255 127 128))))
  537. (pass-if "literal quoted"
  538. (equal? '#vu8(1 2 3 4 5)
  539. (u8-list->bytevector '(1 2 3 4 5))))
  540. (pass-if-exception "incorrect prefix"
  541. exception:read-error
  542. (with-input-from-string "#vi8(1 2 3)" read))
  543. (pass-if-exception "extraneous space"
  544. exception:read-error
  545. (with-input-from-string "#vu8 (1 2 3)" read))
  546. (pass-if-exception "negative integers"
  547. exception:out-of-range
  548. (with-input-from-string "#vu8(-1 -2 -3)" read))
  549. (pass-if-exception "out-of-range integers"
  550. exception:out-of-range
  551. (with-input-from-string "#vu8(0 256)" read)))
  552. (with-test-prefix "Arrays"
  553. (pass-if "array?"
  554. (array? #vu8(1 2 3)))
  555. (pass-if "array-length"
  556. (equal? (iota 16)
  557. (map array-length
  558. (map make-bytevector (iota 16)))))
  559. (pass-if "array-ref"
  560. (let ((bv #vu8(255 127)))
  561. (and (= 255 (array-ref bv 0))
  562. (= 127 (array-ref bv 1)))))
  563. (pass-if-exception "array-ref [index out-of-range]"
  564. exception:out-of-range
  565. (let ((bv #vu8(1 2)))
  566. (array-ref bv 2)))
  567. (pass-if "array-set!"
  568. (let ((bv (make-bytevector 2)))
  569. (array-set! bv 255 0)
  570. (array-set! bv 77 1)
  571. (equal? '(255 77)
  572. (bytevector->u8-list bv))))
  573. (pass-if-exception "array-set! [index out-of-range]"
  574. exception:out-of-range
  575. (let ((bv (make-bytevector 2)))
  576. (array-set! bv 0 2)))
  577. (pass-if-exception "array-set! [value out-of-range]"
  578. exception:out-of-range
  579. (let ((bv (make-bytevector 2)))
  580. (array-set! bv 256 0)))
  581. (pass-if "array-type"
  582. (eq? 'vu8 (array-type #vu8())))
  583. (pass-if "array-contents"
  584. (let ((bv (u8-list->bytevector (iota 10))))
  585. (eq? bv (array-contents bv))))
  586. (pass-if "array-ref"
  587. (let ((bv (u8-list->bytevector (iota 10))))
  588. (equal? (iota 10)
  589. (map (lambda (i) (array-ref bv i))
  590. (iota 10)))))
  591. (pass-if "array-set!"
  592. (let ((bv (make-bytevector 10)))
  593. (for-each (lambda (i)
  594. (array-set! bv i i))
  595. (iota 10))
  596. (equal? (iota 10)
  597. (bytevector->u8-list bv))))
  598. (pass-if "make-typed-array"
  599. (let ((bv (make-typed-array 'vu8 77 33)))
  600. (equal? bv (u8-list->bytevector (make-list 33 77)))))
  601. (pass-if-exception "make-typed-array [out-of-range]"
  602. exception:out-of-range
  603. (make-typed-array 'vu8 256 77)))
  604. (with-test-prefix "uniform-array->bytevector"
  605. (pass-if "bytevector"
  606. (let ((bv #vu8(0 1 128 255)))
  607. (equal? bv (uniform-array->bytevector bv))))
  608. (pass-if "empty bitvector"
  609. (let ((bv (uniform-array->bytevector (make-bitvector 0))))
  610. (equal? bv #vu8())))
  611. (pass-if "bitvector < 8"
  612. (let ((bv (uniform-array->bytevector (make-bitvector 4 #t))))
  613. (= (bytevector-length bv) 4)))
  614. (pass-if "bitvector == 8"
  615. (let ((bv (uniform-array->bytevector (make-bitvector 8 #t))))
  616. (= (bytevector-length bv) 4)))
  617. (pass-if "bitvector > 8"
  618. (let ((bv (uniform-array->bytevector (make-bitvector 9 #t))))
  619. (= (bytevector-length bv) 4)))
  620. (pass-if "bitvector == 32"
  621. (let ((bv (uniform-array->bytevector (make-bitvector 32 #t))))
  622. (= (bytevector-length bv) 4)))
  623. (pass-if "bitvector > 32"
  624. (let ((bv (uniform-array->bytevector (make-bitvector 33 #t))))
  625. (= (bytevector-length bv) 8))))
  626. (with-test-prefix "srfi-4 homogeneous numeric vectors as bytevectors"
  627. ;; This failed prior to Guile 2.0.12.
  628. ;; See <http://bugs.gnu.org/18866>.
  629. (pass-if-equal "bytevector-copy on srfi-4 arrays"
  630. (make-bytevector 8 #xFF)
  631. (bytevector-copy (make-u32vector 2 #xFFFFFFFF))))
  632. ;;; Local Variables:
  633. ;;; eval: (put 'with-test-prefix/c&e 'scheme-indent-function 1)
  634. ;;; End: