test-ints.scm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. ;;; Numeric operation tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-ints")
  22. ;;; Fixnum overflow
  23. ;; add and sub
  24. (test-call "536870912" (lambda (a b) (+ a b)) 536870911 1)
  25. (test-call "-536870913" (lambda (a b) (- a b)) -536870912 1)
  26. ;; add/immediate and sub/immediate
  27. (test-call "536870912" (lambda (a) (1+ a)) 536870911)
  28. (test-call "-536870913" (lambda (a) (1- a)) -536870912)
  29. (test-call "536870953" (lambda (a) (+ a 42)) 536870911)
  30. (test-call "-536870954" (lambda (a) (- a 42)) -536870912)
  31. ;; mul
  32. (test-call "391" (lambda (a b) (* a b)) 17 23)
  33. (test-call "536895241" (lambda (a) (* a a)) 23171)
  34. ;; Construct two bignums and add them (vs. previous tests producing
  35. ;; bignum sums from fixnum inputs)
  36. (test-call "1073741824" (lambda (a b) (+ (+ a b) (+ a b))) 536870911 1)
  37. (test-call "536870954" (lambda (a b c) (+ (+ a b) c)) 536870911 1 42)
  38. (test-call "536870954" (lambda (a b c) (+ c (+ a b))) 536870911 1 42)
  39. (test-call "0" (lambda (a b) (- (+ a b) (+ a b))) 536870911 1)
  40. (test-call "536870870" (lambda (a b c) (- (+ a b) c)) 536870911 1 42)
  41. (test-call "-536870870" (lambda (a b c) (- c (+ a b))) 536870911 1 42)
  42. (test-call "1073741824" (lambda (a b c) (* (+ a b) c)) 536870911 1 2)
  43. (test-call "1073741824" (lambda (a b c) (* c (+ a b))) 536870911 1 2)
  44. (test-call "288230376151711744" (lambda (a b) (* (+ a b) (+ a b))) 536870911 1)
  45. ;; Bignum literals
  46. (test-call "288230376151711745" 1+ 288230376151711744)
  47. ;; Integer division
  48. ;; Quotient
  49. (test-call "12" (lambda (a b) (quotient a b)) 123 10)
  50. (test-call "-12" (lambda (a b) (quotient a b)) 123 -10)
  51. (test-call "-12" (lambda (a b) (quotient a b)) -123 10)
  52. (test-call "12" (lambda (a b) (quotient a b)) -123 -10)
  53. (test-call "53687091" (lambda (a b c) (quotient (+ a b) c)) 536870911 1 10)
  54. (test-call "-53687091" (lambda (a b c) (quotient (+ a b) c)) 536870911 1 -10)
  55. (test-call "-53687091" (lambda (a b c) (quotient (+ a b) c)) -536870911 -1 10)
  56. (test-call "53687091" (lambda (a b c) (quotient (+ a b) c)) -536870911 -1 -10)
  57. (test-call "0" (lambda (a b c) (quotient a (+ b c))) 10 536870911 1)
  58. (test-call "0" (lambda (a b c) (quotient a (+ b c))) 10 -536870911 -1)
  59. (test-call "0" (lambda (a b c) (quotient a (+ b c))) -10 536870911 1)
  60. (test-call "0" (lambda (a b c) (quotient a (+ b c))) -10 -536870911 -1)
  61. (with-additional-imports
  62. ((hoot bitwise))
  63. (test-call "536870912" (lambda (a b) (quotient a b)) (ash -1 29) -1))
  64. (test-call "1"
  65. (lambda (a b c d) (quotient (+ a b) (+ c d)))
  66. 536870911 2 536870911 1)
  67. (test-call "-1"
  68. (lambda (a b c d) (quotient (+ a b) (+ c d)))
  69. 536870911 2 -536870911 -1)
  70. (test-call "-1"
  71. (lambda (a b c d) (quotient (+ a b) (+ c d)))
  72. -536870911 -2 536870911 1)
  73. (test-call "1"
  74. (lambda (a b c d) (quotient (+ a b) (+ c d)))
  75. -536870911 -2 -536870911 -1)
  76. ;; Remainder
  77. (test-call "3" (lambda (a b) (remainder a b)) 123 10)
  78. (test-call "3" (lambda (a b) (remainder a b)) 123 -10)
  79. (test-call "-3" (lambda (a b) (remainder a b)) -123 10)
  80. (test-call "-3" (lambda (a b) (remainder a b)) -123 -10)
  81. (test-call "2" (lambda (a b c) (remainder (+ a b) c)) 536870911 1 10)
  82. (test-call "2" (lambda (a b c) (remainder (+ a b) c)) 536870911 1 -10)
  83. (test-call "-2" (lambda (a b c) (remainder (+ a b) c)) -536870911 -1 10)
  84. (test-call "-2" (lambda (a b c) (remainder (+ a b) c)) -536870911 -1 -10)
  85. (test-call "10" (lambda (a b c) (remainder a (+ b c))) 10 536870911 1)
  86. (test-call "10" (lambda (a b c) (remainder a (+ b c))) 10 -536870911 -1)
  87. (test-call "-10" (lambda (a b c) (remainder a (+ b c))) -10 536870911 1)
  88. (test-call "-10" (lambda (a b c) (remainder a (+ b c))) -10 -536870911 -1)
  89. (test-call "1"
  90. (lambda (a b c d) (remainder (+ a b) (+ c d)))
  91. 536870911 2 536870911 1)
  92. (test-call "1"
  93. (lambda (a b c d) (remainder (+ a b) (+ c d)))
  94. 536870911 2 -536870911 -1)
  95. (test-call "-1"
  96. (lambda (a b c d) (remainder (+ a b) (+ c d)))
  97. -536870911 -2 536870911 1)
  98. (test-call "-1"
  99. (lambda (a b c d) (remainder (+ a b) (+ c d)))
  100. -536870911 -2 -536870911 -1)
  101. ;; Modulo
  102. (test-call "3" (lambda (a b) (modulo a b)) 123 10)
  103. (test-call "-7" (lambda (a b) (modulo a b)) 123 -10)
  104. (test-call "7" (lambda (a b) (modulo a b)) -123 10)
  105. (test-call "-3" (lambda (a b) (modulo a b)) -123 -10)
  106. (test-call "2" (lambda (a b c) (modulo (+ a b) c)) 536870911 1 10)
  107. (test-call "-8" (lambda (a b c) (modulo (+ a b) c)) 536870911 1 -10)
  108. (test-call "8" (lambda (a b c) (modulo (+ a b) c)) -536870911 -1 10)
  109. (test-call "-2" (lambda (a b c) (modulo (+ a b) c)) -536870911 -1 -10)
  110. (test-call "10" (lambda (a b c) (modulo a (+ b c))) 10 536870911 1)
  111. (test-call "-536870902" (lambda (a b c) (modulo a (+ b c))) 10 -536870911 -1)
  112. (test-call "536870902" (lambda (a b c) (modulo a (+ b c))) -10 536870911 1)
  113. (test-call "-10" (lambda (a b c) (modulo a (+ b c))) -10 -536870911 -1)
  114. (test-call "1"
  115. (lambda (a b c d) (modulo (+ a b) (+ c d)))
  116. 536870911 2 536870911 1)
  117. (test-call "-536870911"
  118. (lambda (a b c d) (modulo (+ a b) (+ c d)))
  119. 536870911 2 -536870911 -1)
  120. (test-call "536870911"
  121. (lambda (a b c d) (modulo (+ a b) (+ c d)))
  122. -536870911 -2 536870911 1)
  123. (test-call "-1"
  124. (lambda (a b c d) (modulo (+ a b) (+ c d)))
  125. -536870911 -2 -536870911 -1)
  126. (test-call "3" (lambda (x y) (quotient x y)) 10 3)
  127. (test-call "-3" (lambda (x y) (quotient x y)) -10 3)
  128. (test-call "1" (lambda (x y) (remainder x y)) 10 3)
  129. (test-call "-1" (lambda (x y) (remainder x y)) -10 3)
  130. (test-call "1" (lambda (x y) (modulo x y)) 10 3)
  131. (test-call "2" (lambda (x y) (modulo x y)) -10 3)
  132. ;;; abs, floor and ceiling
  133. (test-call "42" (lambda (a) (abs a)) 42)
  134. (test-call "42" (lambda (a) (abs a)) -42)
  135. (test-call "536870912" (lambda (a b) (abs (+ a b))) 536870911 1)
  136. (test-call "536870913" (lambda (a b) (abs (+ a b))) -536870911 -2)
  137. (test-call "23" (lambda (a) (floor a)) 23)
  138. (test-call "23" (lambda (a) (ceiling a)) 23)
  139. (test-call "536870912" (lambda (a b) (floor (+ a b))) 536870911 1)
  140. (test-call "536870912" (lambda (a b) (ceiling (+ a b))) 536870911 1)
  141. (test-call "-536870913" (lambda (a b) (floor (+ a b))) -536870911 -2)
  142. (test-call "-536870913" (lambda (a b) (ceiling (+ a b))) -536870911 -2)
  143. ;; exact-integer-sqrt
  144. (test-call "(2 0)"
  145. (lambda (x)
  146. (call-with-values (lambda () (exact-integer-sqrt x))
  147. (lambda (s r) (list s r))))
  148. 4)
  149. (test-call "(2 1)"
  150. (lambda (x)
  151. (call-with-values (lambda () (exact-integer-sqrt x))
  152. (lambda (s r) (list s r))))
  153. 5)
  154. (test-end* "test-ints")