test-bytevectors.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Bytevector tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-bytevectors")
  22. (test-call "10" (lambda (bv) (bytevector-length bv))
  23. #vu8(0 1 2 3 4 5 6 7 8 9))
  24. (with-additional-imports
  25. ((only (hoot bytevectors)
  26. bytevector-s8-ref bytevector-s8-set!))
  27. (test-call "8" (lambda (bv) (bytevector-u8-ref bv 8))
  28. #vu8(0 #xff 2 #xfd 4 #xfb 6 #xf9 8 #xf7))
  29. (test-call "8" (lambda (bv) (bytevector-s8-ref bv 8))
  30. #vu8(0 #xff 2 #xfd 4 #xfb 6 #xf9 8 #xf7))
  31. (test-call "-9" (lambda (bv) (bytevector-s8-ref bv 9))
  32. #vu8(0 #xff 2 #xfd 4 #xfb 6 #xf9 8 #xf7))
  33. (test-call "247" (lambda (bv) (bytevector-u8-ref bv 9))
  34. #vu8(0 #xff 2 #xfd 4 #xfb 6 #xf9 8 #xf7)))
  35. (with-additional-imports
  36. ((only (hoot bytevectors)
  37. bytevector-u16-native-ref bytevector-u16-native-set!
  38. bytevector-s16-native-ref bytevector-s16-native-set!))
  39. (test-call "65280" (lambda (bv) (bytevector-u16-native-ref bv 0))
  40. #vu8(#x00 #xff #xff #x00))
  41. (test-call "65535" (lambda (bv) (bytevector-u16-native-ref bv 1))
  42. #vu8(#x00 #xff #xff #x00))
  43. (test-call "255" (lambda (bv) (bytevector-u16-native-ref bv 2))
  44. #vu8(#x00 #xff #xff #x00))
  45. (test-call "-256" (lambda (bv) (bytevector-s16-native-ref bv 0))
  46. #vu8(#x00 #xff #xff #x00))
  47. (test-call "-1" (lambda (bv) (bytevector-s16-native-ref bv 1))
  48. #vu8(#x00 #xff #xff #x00))
  49. (test-call "255" (lambda (bv) (bytevector-s16-native-ref bv 2))
  50. #vu8(#x00 #xff #xff #x00)))
  51. (with-additional-imports
  52. ((only (hoot bytevectors)
  53. bytevector-u32-native-ref bytevector-u32-native-set!
  54. bytevector-s32-native-ref bytevector-s32-native-set!))
  55. (test-call "50463231" (lambda (bv) (bytevector-u32-native-ref bv 0))
  56. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  57. (test-call "67305985" (lambda (bv) (bytevector-u32-native-ref bv 1))
  58. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  59. (test-call "4278452994" (lambda (bv) (bytevector-u32-native-ref bv 2))
  60. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  61. (test-call "50463231" (lambda (bv) (bytevector-s32-native-ref bv 0))
  62. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  63. (test-call "67305985" (lambda (bv) (bytevector-s32-native-ref bv 1))
  64. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  65. (test-call "-16514302" (lambda (bv) (bytevector-s32-native-ref bv 2))
  66. #vu8(#xff #x01 #x02 #x03 #x04 #xff)))
  67. (with-additional-imports
  68. ((only (hoot bytevectors)
  69. bytevector-u64-native-ref bytevector-u64-native-set!
  70. bytevector-s64-native-ref bytevector-s64-native-set!))
  71. (test-call "511" (lambda (bv) (bytevector-u64-native-ref bv 0))
  72. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  73. (test-call "1" (lambda (bv) (bytevector-u64-native-ref bv 1))
  74. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  75. (test-call "72057594037927936"
  76. (lambda (bv) (bytevector-u64-native-ref bv 2))
  77. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  78. (test-call "18374967954648334336"
  79. (lambda (bv) (bytevector-u64-native-ref bv 3))
  80. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  81. (test-call "511" (lambda (bv) (bytevector-s64-native-ref bv 0))
  82. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  83. (test-call "1" (lambda (bv) (bytevector-s64-native-ref bv 1))
  84. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  85. (test-call "72057594037927936"
  86. (lambda (bv) (bytevector-s64-native-ref bv 2))
  87. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  88. (test-call "-71776119061217280"
  89. (lambda (bv) (bytevector-s64-native-ref bv 3))
  90. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  91. (test-call "-65025" (lambda (bv) (bytevector-s64-native-ref bv 0))
  92. #vu8(#xff 1 #xff #xff #xff #xff #xff #xff)))
  93. (with-additional-imports
  94. ((only (hoot bytevectors)
  95. bytevector-ieee-single-native-ref
  96. bytevector-ieee-single-native-set!
  97. bytevector-ieee-double-native-ref
  98. bytevector-ieee-double-native-set!))
  99. (test-call "42.69" (lambda (bv)
  100. (bytevector-ieee-double-native-ref bv 0))
  101. #vu8(184 30 133 235 81 88 69 64))
  102. (test-call "42.689998626708984"
  103. (lambda (bv)
  104. (bytevector-ieee-single-native-ref bv 0))
  105. #vu8(143 194 42 66))
  106. (test-call "85.38"
  107. (lambda (bv)
  108. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  109. (+ f64 f64)))
  110. #vu8(184 30 133 235 81 88 69 64))
  111. (test-call "43.69"
  112. (lambda (bv)
  113. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  114. (+ f64 1.0)))
  115. #vu8(184 30 133 235 81 88 69 64))
  116. (test-call "41.69"
  117. (lambda (bv)
  118. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  119. (- f64 1.0)))
  120. #vu8(184 30 133 235 81 88 69 64))
  121. (test-call "64.035"
  122. (lambda (bv)
  123. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  124. (* f64 1.5)))
  125. #vu8(184 30 133 235 81 88 69 64))
  126. (test-call "21.345"
  127. (lambda (bv)
  128. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  129. (/ f64 2.0)))
  130. #vu8(184 30 133 235 81 88 69 64))
  131. (test-call "-57.31"
  132. (lambda (bv)
  133. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  134. (- f64 100.0)))
  135. #vu8(184 30 133 235 81 88 69 64))
  136. (test-call "57.31"
  137. (lambda (bv)
  138. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  139. (abs (- f64 100.0))))
  140. #vu8(184 30 133 235 81 88 69 64))
  141. (test-call "6.5337584895678535"
  142. (lambda (bv)
  143. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  144. (sqrt (abs f64))))
  145. #vu8(184 30 133 235 81 88 69 64))
  146. (test-call "42.0"
  147. (lambda (bv)
  148. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  149. (floor f64)))
  150. #vu8(184 30 133 235 81 88 69 64))
  151. (test-call "43.0"
  152. (lambda (bv)
  153. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  154. (ceiling f64)))
  155. #vu8(184 30 133 235 81 88 69 64))
  156. (test-call "(-0.9614691168217643 0.2749129633138033 -3.497358237429792)"
  157. (lambda (bv)
  158. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  159. (list (sin f64)
  160. (cos f64)
  161. (tan f64))))
  162. #vu8(184 30 133 235 81 88 69 64))
  163. ;; Not testing fasin, facos for now because apparently Guile doesn't emit those!
  164. (test-call "(1.5473759202633208 0.7853981633974483)"
  165. (lambda (bv)
  166. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  167. (list (atan f64)
  168. (atan f64 f64))))
  169. #vu8(184 30 133 235 81 88 69 64)))
  170. (with-additional-imports ((only (hoot bytevectors)
  171. bytevector-uint-ref
  172. bytevector-uint-set!
  173. bytevector-sint-ref
  174. bytevector-sint-set!
  175. endianness))
  176. (test-call "15715755"
  177. (lambda (bv) (bytevector-uint-ref bv 0 (endianness little) 3))
  178. #vu8(171 205 239))
  179. (test-call "-1061461"
  180. (lambda (bv) (bytevector-sint-ref bv 0 (endianness little) 3))
  181. #vu8(171 205 239))
  182. (test-call "#vu8(239 205 171)"
  183. (lambda ()
  184. (let ((bv (make-bytevector 3)))
  185. (bytevector-uint-set! bv 0 #xabcdef (endianness little) 3)
  186. bv)))
  187. (test-call "#vu8(17 50 180)"
  188. (lambda ()
  189. (let ((bv (make-bytevector 3)))
  190. (bytevector-sint-set! bv 0 #x-4bcdef (endianness little) 3)
  191. bv))))
  192. ;; Big endian
  193. (with-additional-imports ((only (hoot bytevectors)
  194. bytevector-u16-ref
  195. bytevector-u16-set!
  196. bytevector-s16-ref
  197. bytevector-s16-set!
  198. bytevector-u32-ref
  199. bytevector-u32-set!
  200. bytevector-s32-ref
  201. bytevector-s32-set!
  202. bytevector-u64-ref
  203. bytevector-u64-set!
  204. bytevector-s64-ref
  205. bytevector-s64-set!
  206. bytevector-uint-ref
  207. bytevector-uint-set!
  208. bytevector-sint-ref
  209. bytevector-sint-set!
  210. bytevector-ieee-single-ref
  211. bytevector-ieee-single-set!
  212. bytevector-ieee-double-ref
  213. bytevector-ieee-double-set!
  214. endianness))
  215. (test-call "12345"
  216. (lambda (bv) (bytevector-u16-ref bv 0 (endianness big)))
  217. #vu8(48 57))
  218. (test-call "-12345"
  219. (lambda (bv) (bytevector-s16-ref bv 0 (endianness big)))
  220. #vu8(207 199))
  221. (test-call "1234567"
  222. (lambda (bv) (bytevector-u32-ref bv 0 (endianness big)))
  223. #vu8(0 18 214 135))
  224. (test-call "-1234567"
  225. (lambda (bv) (bytevector-s32-ref bv 0 (endianness big)))
  226. #vu8(255 237 41 121))
  227. (test-call "123456789123456789"
  228. (lambda (bv) (bytevector-u64-ref bv 0 (endianness big)))
  229. #vu8(1 182 155 75 172 208 95 21))
  230. (test-call "-123456789123456789"
  231. (lambda (bv) (bytevector-s64-ref bv 0 (endianness big)))
  232. #vu8(254 73 100 180 83 47 160 235))
  233. (test-call "11259375"
  234. (lambda (bv) (bytevector-uint-ref bv 0 (endianness big) 3))
  235. #vu8(171 205 239))
  236. (test-call "-5517841"
  237. (lambda (bv) (bytevector-sint-ref bv 0 (endianness big) 3))
  238. #vu8(171 205 239))
  239. (test-call "1.2999999523162842"
  240. (lambda (bv) (bytevector-ieee-single-ref bv 0 (endianness big)))
  241. #vu8(63 166 102 102))
  242. (test-call "1.3"
  243. (lambda (bv) (bytevector-ieee-double-ref bv 0 (endianness big)))
  244. #vu8(63 244 204 204 204 204 204 205))
  245. (test-call "#vu8(171 205)"
  246. (lambda ()
  247. (let ((bv (make-bytevector 2)))
  248. (bytevector-u16-set! bv 0 #xabcd (endianness big))
  249. bv)))
  250. (test-call "#vu8(180 51)"
  251. (lambda ()
  252. (let ((bv (make-bytevector 2)))
  253. (bytevector-s16-set! bv 0 #x-4bcd (endianness big))
  254. bv)))
  255. (test-call "#vu8(171 205 239 171)"
  256. (lambda ()
  257. (let ((bv (make-bytevector 4)))
  258. (bytevector-u32-set! bv 0 #xabcdefab (endianness big))
  259. bv)))
  260. (test-call "#vu8(180 50 16 85)"
  261. (lambda ()
  262. (let ((bv (make-bytevector 4)))
  263. (bytevector-s32-set! bv 0 #x-4bcdefab (endianness big))
  264. bv)))
  265. (test-call "#vu8(171 205 239 171 205 239 171 205)"
  266. (lambda ()
  267. (let ((bv (make-bytevector 8)))
  268. (bytevector-u64-set! bv 0 #xabcdefabcdefabcd (endianness big))
  269. bv)))
  270. (test-call "#vu8(180 50 16 84 50 16 84 51)"
  271. (lambda ()
  272. (let ((bv (make-bytevector 8)))
  273. (bytevector-s64-set! bv 0 #x-4bcdefabcdefabcd (endianness big))
  274. bv)))
  275. (test-call "#vu8(171 205 239)"
  276. (lambda ()
  277. (let ((bv (make-bytevector 3)))
  278. (bytevector-uint-set! bv 0 #xabcdef (endianness big) 3)
  279. bv)))
  280. (test-call "#vu8(180 50 17)"
  281. (lambda ()
  282. (let ((bv (make-bytevector 3)))
  283. (bytevector-sint-set! bv 0 #x-4bcdef (endianness big) 3)
  284. bv)))
  285. (test-call "#vu8(63 166 102 102)"
  286. (lambda ()
  287. (let ((bv (make-bytevector 4)))
  288. (bytevector-ieee-single-set! bv 0 1.3 (endianness big))
  289. bv)))
  290. (test-call "#vu8(63 244 204 204 204 204 204 205)"
  291. (lambda ()
  292. (let ((bv (make-bytevector 8)))
  293. (bytevector-ieee-double-set! bv 0 1.3 (endianness big))
  294. bv))))
  295. (test-call "#vu8(0 0 0 0 0)"
  296. (lambda () (make-bytevector 5)))
  297. (test-call "#vu8(42 42 42 42 42)"
  298. (lambda () (make-bytevector 5 42)))
  299. (test-call "#vu8(1 2 3 4)"
  300. (lambda () (bytevector 1 2 3 4)))
  301. (test-call "#t" (lambda (a b) (equal? a b)) #vu8() #vu8())
  302. (test-call "#t" (lambda (a b) (equal? a b)) #vu8(1 2) #vu8(1 2))
  303. (test-call "#f" (lambda (a b) (equal? a b)) #vu8() #vu8(1))
  304. (test-call "#f" (lambda (a b) (equal? a b)) #vu8(1 2) #vu8(2 1))
  305. (test-call "#f" (lambda (a b) (equal? a b)) #vu8(1 2 1) #vu8(1 2 3))
  306. (test-end* "test-bytevectors")