test-ints.scm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. ;;; 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. (test-call "1"
  62. (lambda (a b c d) (quotient (+ a b) (+ c d)))
  63. 536870911 2 536870911 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. ;; Remainder
  74. (test-call "3" (lambda (a b) (remainder a b)) 123 10)
  75. (test-call "3" (lambda (a b) (remainder a b)) 123 -10)
  76. (test-call "-3" (lambda (a b) (remainder a b)) -123 10)
  77. (test-call "-3" (lambda (a b) (remainder a b)) -123 -10)
  78. (test-call "2" (lambda (a b c) (remainder (+ a b) c)) 536870911 1 10)
  79. (test-call "2" (lambda (a b c) (remainder (+ a b) c)) 536870911 1 -10)
  80. (test-call "-2" (lambda (a b c) (remainder (+ a b) c)) -536870911 -1 10)
  81. (test-call "-2" (lambda (a b c) (remainder (+ a b) c)) -536870911 -1 -10)
  82. (test-call "10" (lambda (a b c) (remainder a (+ b c))) 10 536870911 1)
  83. (test-call "10" (lambda (a b c) (remainder a (+ b c))) 10 -536870911 -1)
  84. (test-call "-10" (lambda (a b c) (remainder a (+ b c))) -10 536870911 1)
  85. (test-call "-10" (lambda (a b c) (remainder a (+ b c))) -10 -536870911 -1)
  86. (test-call "1"
  87. (lambda (a b c d) (remainder (+ a b) (+ c d)))
  88. 536870911 2 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. ;; Modulo
  99. (test-call "3" (lambda (a b) (modulo a b)) 123 10)
  100. (test-call "-7" (lambda (a b) (modulo a b)) 123 -10)
  101. (test-call "7" (lambda (a b) (modulo a b)) -123 10)
  102. (test-call "-3" (lambda (a b) (modulo a b)) -123 -10)
  103. (test-call "2" (lambda (a b c) (modulo (+ a b) c)) 536870911 1 10)
  104. (test-call "-8" (lambda (a b c) (modulo (+ a b) c)) 536870911 1 -10)
  105. (test-call "8" (lambda (a b c) (modulo (+ a b) c)) -536870911 -1 10)
  106. (test-call "-2" (lambda (a b c) (modulo (+ a b) c)) -536870911 -1 -10)
  107. (test-call "10" (lambda (a b c) (modulo a (+ b c))) 10 536870911 1)
  108. (test-call "-536870902" (lambda (a b c) (modulo a (+ b c))) 10 -536870911 -1)
  109. (test-call "536870902" (lambda (a b c) (modulo a (+ b c))) -10 536870911 1)
  110. (test-call "-10" (lambda (a b c) (modulo a (+ b c))) -10 -536870911 -1)
  111. (test-call "1"
  112. (lambda (a b c d) (modulo (+ a b) (+ c d)))
  113. 536870911 2 536870911 1)
  114. (test-call "-536870911"
  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 "-1"
  121. (lambda (a b c d) (modulo (+ a b) (+ c d)))
  122. -536870911 -2 -536870911 -1)
  123. (test-call "3" (lambda (x y) (quotient x y)) 10 3)
  124. (test-call "-3" (lambda (x y) (quotient x y)) -10 3)
  125. (test-call "1" (lambda (x y) (remainder x y)) 10 3)
  126. (test-call "-1" (lambda (x y) (remainder x y)) -10 3)
  127. (test-call "1" (lambda (x y) (modulo x y)) 10 3)
  128. (test-call "2" (lambda (x y) (modulo x y)) -10 3)
  129. ;;; abs, floor and ceiling
  130. (test-call "42" (lambda (a) (abs a)) 42)
  131. (test-call "42" (lambda (a) (abs a)) -42)
  132. (test-call "536870912" (lambda (a b) (abs (+ a b))) 536870911 1)
  133. (test-call "536870913" (lambda (a b) (abs (+ a b))) -536870911 -2)
  134. (test-call "23" (lambda (a) (floor a)) 23)
  135. (test-call "23" (lambda (a) (ceiling a)) 23)
  136. (test-call "536870912" (lambda (a b) (floor (+ a b))) 536870911 1)
  137. (test-call "536870912" (lambda (a b) (ceiling (+ a b))) 536870911 1)
  138. (test-call "-536870913" (lambda (a b) (floor (+ a b))) -536870911 -2)
  139. (test-call "-536870913" (lambda (a b) (ceiling (+ a b))) -536870911 -2)
  140. ;; exact-integer-sqrt
  141. (test-call "(2 0)"
  142. (lambda (x)
  143. (call-with-values (lambda () (exact-integer-sqrt x))
  144. (lambda (s r) (list s r))))
  145. 4)
  146. (test-call "(2 1)"
  147. (lambda (x)
  148. (call-with-values (lambda () (exact-integer-sqrt x))
  149. (lambda (s r) (list s r))))
  150. 5)
  151. (test-end* "test-ints")