test-bytevectors.scm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ;;; Copyright (C) 2023 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. (test-call "8" (lambda (bv) (bytevector-u8-ref bv 8))
  25. #vu8(0 #xff 2 #xfd 4 #xfb 6 #xf9 8 #xf7))
  26. (test-call "8" (lambda (bv) (bytevector-s8-ref bv 8))
  27. #vu8(0 #xff 2 #xfd 4 #xfb 6 #xf9 8 #xf7))
  28. (test-call "-9" (lambda (bv) (bytevector-s8-ref bv 9))
  29. #vu8(0 #xff 2 #xfd 4 #xfb 6 #xf9 8 #xf7))
  30. (test-call "247" (lambda (bv) (bytevector-u8-ref bv 9))
  31. #vu8(0 #xff 2 #xfd 4 #xfb 6 #xf9 8 #xf7))
  32. (test-call "65280" (lambda (bv) (bytevector-u16-native-ref bv 0))
  33. #vu8(#x00 #xff #xff #x00))
  34. (test-call "65535" (lambda (bv) (bytevector-u16-native-ref bv 1))
  35. #vu8(#x00 #xff #xff #x00))
  36. (test-call "255" (lambda (bv) (bytevector-u16-native-ref bv 2))
  37. #vu8(#x00 #xff #xff #x00))
  38. (test-call "-256" (lambda (bv) (bytevector-s16-native-ref bv 0))
  39. #vu8(#x00 #xff #xff #x00))
  40. (test-call "-1" (lambda (bv) (bytevector-s16-native-ref bv 1))
  41. #vu8(#x00 #xff #xff #x00))
  42. (test-call "255" (lambda (bv) (bytevector-s16-native-ref bv 2))
  43. #vu8(#x00 #xff #xff #x00))
  44. (test-call "50463231" (lambda (bv) (bytevector-u32-native-ref bv 0))
  45. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  46. (test-call "67305985" (lambda (bv) (bytevector-u32-native-ref bv 1))
  47. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  48. ;; FIXME: Enable once we have bignums.
  49. ;; (test-call "4278452994" (lambda (bv) (bytevector-u32-native-ref bv 2))
  50. ;; #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  51. (test-call "50463231" (lambda (bv) (bytevector-s32-native-ref bv 0))
  52. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  53. (test-call "67305985" (lambda (bv) (bytevector-s32-native-ref bv 1))
  54. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  55. (test-call "-16514302" (lambda (bv) (bytevector-s32-native-ref bv 2))
  56. #vu8(#xff #x01 #x02 #x03 #x04 #xff))
  57. (test-call "511" (lambda (bv) (bytevector-u64-native-ref bv 0))
  58. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  59. (test-call "1" (lambda (bv) (bytevector-u64-native-ref bv 1))
  60. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  61. ;; FIXME: Enable once we have bignums.
  62. ;; (test-call "72057594037927936"
  63. ;; (lambda (bv) (bytevector-u64-native-ref bv 2))
  64. ;; #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  65. ;; (test-call "18374967954648334336"
  66. ;; (lambda (bv) (bytevector-u64-native-ref bv 3))
  67. ;; #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  68. (test-call "511" (lambda (bv) (bytevector-s64-native-ref bv 0))
  69. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  70. (test-call "1" (lambda (bv) (bytevector-s64-native-ref bv 1))
  71. #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  72. ;; FIXME: Enable once we have bignums.
  73. ;; (test-call "72057594037927936"
  74. ;; (lambda (bv) (bytevector-s64-native-ref bv 2))
  75. ;; #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  76. ;; (test-call "-71776119061217280"
  77. ;; (lambda (bv) (bytevector-s64-native-ref bv 3))
  78. ;; #vu8(#xff 1 0 0 0 0 0 0 0 1 #xff))
  79. (test-call "-65025" (lambda (bv) (bytevector-s64-native-ref bv 0))
  80. #vu8(#xff 1 #xff #xff #xff #xff #xff #xff))
  81. (test-call "42.69" (lambda (bv)
  82. (bytevector-ieee-double-native-ref bv 0))
  83. #vu8(184 30 133 235 81 88 69 64))
  84. (test-call "42.689998626708984"
  85. (lambda (bv)
  86. (bytevector-ieee-single-native-ref bv 0))
  87. #vu8(143 194 42 66))
  88. (test-call "85.38"
  89. (lambda (bv)
  90. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  91. (+ f64 f64)))
  92. #vu8(184 30 133 235 81 88 69 64))
  93. (test-call "43.69"
  94. (lambda (bv)
  95. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  96. (+ f64 1.0)))
  97. #vu8(184 30 133 235 81 88 69 64))
  98. (test-call "41.69"
  99. (lambda (bv)
  100. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  101. (- f64 1.0)))
  102. #vu8(184 30 133 235 81 88 69 64))
  103. (test-call "64.035"
  104. (lambda (bv)
  105. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  106. (* f64 1.5)))
  107. #vu8(184 30 133 235 81 88 69 64))
  108. (test-call "21.345"
  109. (lambda (bv)
  110. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  111. (/ f64 2.0)))
  112. #vu8(184 30 133 235 81 88 69 64))
  113. (test-call "-57.31"
  114. (lambda (bv)
  115. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  116. (- f64 100.0)))
  117. #vu8(184 30 133 235 81 88 69 64))
  118. (test-call "57.31"
  119. (lambda (bv)
  120. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  121. (abs (- f64 100.0))))
  122. #vu8(184 30 133 235 81 88 69 64))
  123. (test-call "6.5337584895678535"
  124. (lambda (bv)
  125. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  126. (sqrt (abs f64))))
  127. #vu8(184 30 133 235 81 88 69 64))
  128. (test-call "42.0"
  129. (lambda (bv)
  130. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  131. (floor f64)))
  132. #vu8(184 30 133 235 81 88 69 64))
  133. (test-call "43.0"
  134. (lambda (bv)
  135. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  136. (ceiling f64)))
  137. #vu8(184 30 133 235 81 88 69 64))
  138. (test-call "(-0.9614691168217643 0.2749129633138033 -3.497358237429792)"
  139. (lambda (bv)
  140. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  141. (list (sin f64)
  142. (cos f64)
  143. (tan f64))))
  144. #vu8(184 30 133 235 81 88 69 64))
  145. ;; Not testing fasin, facos for now because apparently Guile doesn't emit those!
  146. (test-call "(1.5473759202633208 0.7853981633974483)"
  147. (lambda (bv)
  148. (let ((f64 (bytevector-ieee-double-native-ref bv 0)))
  149. (list (atan f64)
  150. (atan f64 f64))))
  151. #vu8(184 30 133 235 81 88 69 64))
  152. (test-call "#vu8(0 0 0 0 0)"
  153. (lambda () (make-bytevector 5)))
  154. (test-call "#vu8(42 42 42 42 42)"
  155. (lambda () (make-bytevector 5 42)))
  156. (test-call "#vu8(1 2 3 4)"
  157. (lambda () (bytevector 1 2 3 4)))
  158. (test-call "#t" (lambda (a b) (equal? a b)) #vu8() #vu8())
  159. (test-call "#t" (lambda (a b) (equal? a b)) #vu8(1 2) #vu8(1 2))
  160. (test-call "#f" (lambda (a b) (equal? a b)) #vu8() #vu8(1))
  161. (test-call "#f" (lambda (a b) (equal? a b)) #vu8(1 2) #vu8(2 1))
  162. (test-call "#f" (lambda (a b) (equal? a b)) #vu8(1 2 1) #vu8(1 2 3))
  163. (test-end* "test-bytevectors")