test-bytevectors.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. ;; FIXME: Enable once we have bignums.
  60. ;; (test-call "4278452994" (lambda (bv) (bytevector-u32-native-ref bv 2))
  61. ;; #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  62. (test-call "50463231" (lambda (bv) (bytevector-s32-native-ref bv 0))
  63. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  64. (test-call "67305985" (lambda (bv) (bytevector-s32-native-ref bv 1))
  65. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  66. (test-call "-16514302" (lambda (bv) (bytevector-s32-native-ref bv 2))
  67. #vu8(#xff #x01 #x02 #x03 #x04 #xff)))
  68. (with-additional-imports
  69. ((only (hoot bytevectors)
  70. bytevector-u64-native-ref bytevector-u64-native-set!
  71. bytevector-s64-native-ref bytevector-s64-native-set!))
  72. (test-call "511" (lambda (bv) (bytevector-u64-native-ref bv 0))
  73. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  74. (test-call "1" (lambda (bv) (bytevector-u64-native-ref bv 1))
  75. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  76. ;; FIXME: Enable once we have bignums.
  77. ;; (test-call "72057594037927936"
  78. ;; (lambda (bv) (bytevector-u64-native-ref bv 2))
  79. ;; #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  80. ;; (test-call "18374967954648334336"
  81. ;; (lambda (bv) (bytevector-u64-native-ref bv 3))
  82. ;; #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  83. (test-call "511" (lambda (bv) (bytevector-s64-native-ref bv 0))
  84. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  85. (test-call "1" (lambda (bv) (bytevector-s64-native-ref bv 1))
  86. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  87. ;; FIXME: Enable once we have bignums.
  88. ;; (test-call "72057594037927936"
  89. ;; (lambda (bv) (bytevector-s64-native-ref bv 2))
  90. ;; #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  91. ;; (test-call "-71776119061217280"
  92. ;; (lambda (bv) (bytevector-s64-native-ref bv 3))
  93. ;; #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  94. (test-call "-65025" (lambda (bv) (bytevector-s64-native-ref bv 0))
  95. #vu8(#xff 1 #xff #xff #xff #xff #xff #xff)))
  96. (with-additional-imports
  97. ((only (hoot bytevectors)
  98. bytevector-ieee-single-native-ref
  99. bytevector-ieee-single-native-set!
  100. bytevector-ieee-double-native-ref
  101. bytevector-ieee-double-native-set!))
  102. (test-call "42.69" (lambda (bv)
  103. (bytevector-ieee-double-native-ref bv 0))
  104. #vu8(184 30 133 235 81 88 69 64))
  105. (test-call "42.689998626708984"
  106. (lambda (bv)
  107. (bytevector-ieee-single-native-ref bv 0))
  108. #vu8(143 194 42 66))
  109. (test-call "85.38"
  110. (lambda (bv)
  111. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  112. (+ f64 f64)))
  113. #vu8(184 30 133 235 81 88 69 64))
  114. (test-call "43.69"
  115. (lambda (bv)
  116. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  117. (+ f64 1.0)))
  118. #vu8(184 30 133 235 81 88 69 64))
  119. (test-call "41.69"
  120. (lambda (bv)
  121. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  122. (- f64 1.0)))
  123. #vu8(184 30 133 235 81 88 69 64))
  124. (test-call "64.035"
  125. (lambda (bv)
  126. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  127. (* f64 1.5)))
  128. #vu8(184 30 133 235 81 88 69 64))
  129. (test-call "21.345"
  130. (lambda (bv)
  131. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  132. (/ f64 2.0)))
  133. #vu8(184 30 133 235 81 88 69 64))
  134. (test-call "-57.31"
  135. (lambda (bv)
  136. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  137. (- f64 100.0)))
  138. #vu8(184 30 133 235 81 88 69 64))
  139. (test-call "57.31"
  140. (lambda (bv)
  141. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  142. (abs (- f64 100.0))))
  143. #vu8(184 30 133 235 81 88 69 64))
  144. (test-call "6.5337584895678535"
  145. (lambda (bv)
  146. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  147. (sqrt (abs f64))))
  148. #vu8(184 30 133 235 81 88 69 64))
  149. (test-call "42.0"
  150. (lambda (bv)
  151. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  152. (floor f64)))
  153. #vu8(184 30 133 235 81 88 69 64))
  154. (test-call "43.0"
  155. (lambda (bv)
  156. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  157. (ceiling f64)))
  158. #vu8(184 30 133 235 81 88 69 64))
  159. (test-call "(-0.9614691168217643 0.2749129633138033 -3.497358237429792)"
  160. (lambda (bv)
  161. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  162. (list (sin f64)
  163. (cos f64)
  164. (tan f64))))
  165. #vu8(184 30 133 235 81 88 69 64))
  166. ;; Not testing fasin, facos for now because apparently Guile doesn't emit those!
  167. (test-call "(1.5473759202633208 0.7853981633974483)"
  168. (lambda (bv)
  169. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  170. (list (atan f64)
  171. (atan f64 f64))))
  172. #vu8(184 30 133 235 81 88 69 64)))
  173. (test-call "#vu8(0 0 0 0 0)"
  174. (lambda () (make-bytevector 5)))
  175. (test-call "#vu8(42 42 42 42 42)"
  176. (lambda () (make-bytevector 5 42)))
  177. (test-call "#vu8(1 2 3 4)"
  178. (lambda () (bytevector 1 2 3 4)))
  179. (test-call "#t" (lambda (a b) (equal? a b)) #vu8() #vu8())
  180. (test-call "#t" (lambda (a b) (equal? a b)) #vu8(1 2) #vu8(1 2))
  181. (test-call "#f" (lambda (a b) (equal? a b)) #vu8() #vu8(1))
  182. (test-call "#f" (lambda (a b) (equal? a b)) #vu8(1 2) #vu8(2 1))
  183. (test-call "#f" (lambda (a b) (equal? a b)) #vu8(1 2 1) #vu8(1 2 3))
  184. (test-end* "test-bytevectors")