bytevectors.test 27 KB

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