test-bytevectors.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. ;;; Copyright (C) 2023, 2024, 2025 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. (with-additional-imports ((scheme inexact))
  142. (test-call "6.5337584895678535"
  143. (lambda (bv)
  144. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  145. (sqrt (abs f64))))
  146. #vu8(184 30 133 235 81 88 69 64)))
  147. (test-call "42.0"
  148. (lambda (bv)
  149. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  150. (floor f64)))
  151. #vu8(184 30 133 235 81 88 69 64))
  152. (test-call "43.0"
  153. (lambda (bv)
  154. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  155. (ceiling f64)))
  156. #vu8(184 30 133 235 81 88 69 64))
  157. (with-additional-imports ((scheme inexact))
  158. (test-call "(-0.9614691168217643 0.2749129633138033 -3.497358237429792)"
  159. (lambda (bv)
  160. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  161. (list (sin f64)
  162. (cos f64)
  163. (tan f64))))
  164. #vu8(184 30 133 235 81 88 69 64))
  165. ;; Not testing fasin, facos for now because apparently Guile doesn't emit those!
  166. (test-call "(1.5473759202633208 0.7853981633974483)"
  167. (lambda (bv)
  168. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  169. (list (atan f64)
  170. (atan f64 f64))))
  171. #vu8(184 30 133 235 81 88 69 64))))
  172. (with-additional-imports ((only (hoot bytevectors)
  173. bytevector-uint-ref
  174. bytevector-uint-set!
  175. bytevector-sint-ref
  176. bytevector-sint-set!
  177. endianness))
  178. (test-call "15715755"
  179. (lambda (bv) (bytevector-uint-ref bv 0 (endianness little) 3))
  180. #vu8(171 205 239))
  181. (test-call "-1061461"
  182. (lambda (bv) (bytevector-sint-ref bv 0 (endianness little) 3))
  183. #vu8(171 205 239))
  184. (test-call "#vu8(239 205 171)"
  185. (lambda ()
  186. (let ((bv (make-bytevector 3)))
  187. (bytevector-uint-set! bv 0 #xabcdef (endianness little) 3)
  188. bv)))
  189. (test-call "#vu8(17 50 180)"
  190. (lambda ()
  191. (let ((bv (make-bytevector 3)))
  192. (bytevector-sint-set! bv 0 #x-4bcdef (endianness little) 3)
  193. bv))))
  194. ;; Big endian
  195. (with-additional-imports ((only (hoot bytevectors)
  196. bytevector-u16-ref
  197. bytevector-u16-set!
  198. bytevector-s16-ref
  199. bytevector-s16-set!
  200. bytevector-u32-ref
  201. bytevector-u32-set!
  202. bytevector-s32-ref
  203. bytevector-s32-set!
  204. bytevector-u64-ref
  205. bytevector-u64-set!
  206. bytevector-s64-ref
  207. bytevector-s64-set!
  208. bytevector-uint-ref
  209. bytevector-uint-set!
  210. bytevector-sint-ref
  211. bytevector-sint-set!
  212. bytevector-ieee-single-ref
  213. bytevector-ieee-single-set!
  214. bytevector-ieee-double-ref
  215. bytevector-ieee-double-set!
  216. endianness))
  217. (test-call "12345"
  218. (lambda (bv) (bytevector-u16-ref bv 0 (endianness big)))
  219. #vu8(48 57))
  220. (test-call "-12345"
  221. (lambda (bv) (bytevector-s16-ref bv 0 (endianness big)))
  222. #vu8(207 199))
  223. (test-call "1234567"
  224. (lambda (bv) (bytevector-u32-ref bv 0 (endianness big)))
  225. #vu8(0 18 214 135))
  226. (test-call "-1234567"
  227. (lambda (bv) (bytevector-s32-ref bv 0 (endianness big)))
  228. #vu8(255 237 41 121))
  229. (test-call "123456789123456789"
  230. (lambda (bv) (bytevector-u64-ref bv 0 (endianness big)))
  231. #vu8(1 182 155 75 172 208 95 21))
  232. (test-call "-123456789123456789"
  233. (lambda (bv) (bytevector-s64-ref bv 0 (endianness big)))
  234. #vu8(254 73 100 180 83 47 160 235))
  235. (test-call "11259375"
  236. (lambda (bv) (bytevector-uint-ref bv 0 (endianness big) 3))
  237. #vu8(171 205 239))
  238. (test-call "-5517841"
  239. (lambda (bv) (bytevector-sint-ref bv 0 (endianness big) 3))
  240. #vu8(171 205 239))
  241. (test-call "1.2999999523162842"
  242. (lambda (bv) (bytevector-ieee-single-ref bv 0 (endianness big)))
  243. #vu8(63 166 102 102))
  244. (test-call "1.3"
  245. (lambda (bv) (bytevector-ieee-double-ref bv 0 (endianness big)))
  246. #vu8(63 244 204 204 204 204 204 205))
  247. (test-call "#vu8(171 205)"
  248. (lambda ()
  249. (let ((bv (make-bytevector 2)))
  250. (bytevector-u16-set! bv 0 #xabcd (endianness big))
  251. bv)))
  252. (test-call "#vu8(180 51)"
  253. (lambda ()
  254. (let ((bv (make-bytevector 2)))
  255. (bytevector-s16-set! bv 0 #x-4bcd (endianness big))
  256. bv)))
  257. (test-call "#vu8(171 205 239 171)"
  258. (lambda ()
  259. (let ((bv (make-bytevector 4)))
  260. (bytevector-u32-set! bv 0 #xabcdefab (endianness big))
  261. bv)))
  262. (test-call "#vu8(180 50 16 85)"
  263. (lambda ()
  264. (let ((bv (make-bytevector 4)))
  265. (bytevector-s32-set! bv 0 #x-4bcdefab (endianness big))
  266. bv)))
  267. (test-call "#vu8(171 205 239 171 205 239 171 205)"
  268. (lambda ()
  269. (let ((bv (make-bytevector 8)))
  270. (bytevector-u64-set! bv 0 #xabcdefabcdefabcd (endianness big))
  271. bv)))
  272. (test-call "#vu8(180 50 16 84 50 16 84 51)"
  273. (lambda ()
  274. (let ((bv (make-bytevector 8)))
  275. (bytevector-s64-set! bv 0 #x-4bcdefabcdefabcd (endianness big))
  276. bv)))
  277. (test-call "#vu8(171 205 239)"
  278. (lambda ()
  279. (let ((bv (make-bytevector 3)))
  280. (bytevector-uint-set! bv 0 #xabcdef (endianness big) 3)
  281. bv)))
  282. (test-call "#vu8(180 50 17)"
  283. (lambda ()
  284. (let ((bv (make-bytevector 3)))
  285. (bytevector-sint-set! bv 0 #x-4bcdef (endianness big) 3)
  286. bv)))
  287. (test-call "#vu8(63 166 102 102)"
  288. (lambda ()
  289. (let ((bv (make-bytevector 4)))
  290. (bytevector-ieee-single-set! bv 0 1.3 (endianness big))
  291. bv)))
  292. (test-call "#vu8(63 244 204 204 204 204 204 205)"
  293. (lambda ()
  294. (let ((bv (make-bytevector 8)))
  295. (bytevector-ieee-double-set! bv 0 1.3 (endianness big))
  296. bv))))
  297. (test-call "#vu8(0 0 0 0 0)"
  298. (lambda () (make-bytevector 5)))
  299. (test-call "#vu8(42 42 42 42 42)"
  300. (lambda () (make-bytevector 5 42)))
  301. (test-call "#vu8(1 2 3 4)"
  302. (lambda () (bytevector 1 2 3 4)))
  303. (test-call "#t" (lambda (a b) (equal? a b)) #vu8() #vu8())
  304. (test-call "#t" (lambda (a b) (equal? a b)) #vu8(1 2) #vu8(1 2))
  305. (test-call "#f" (lambda (a b) (equal? a b)) #vu8() #vu8(1))
  306. (test-call "#f" (lambda (a b) (equal? a b)) #vu8(1 2) #vu8(2 1))
  307. (test-call "#f" (lambda (a b) (equal? a b)) #vu8(1 2 1) #vu8(1 2 3))
  308. (test-end* "test-bytevectors")