test-division.scm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ;;; Copyright (C) 2023 Robin Templeton
  2. ;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Tests for the various forms of division.
  18. ;;;
  19. ;;; Code:
  20. (use-modules (ice-9 format)
  21. (srfi srfi-64)
  22. (test utils))
  23. (test-begin "test-division")
  24. ;; quotient, remainder and modulus with a flonum argument
  25. (test-call "12.0" (lambda (a b) (quotient a b)) 123.0 10.0)
  26. (test-call "12.0" (lambda (a b) (quotient a b)) 123.0 10)
  27. (test-call "12.0" (lambda (a b) (quotient a b)) 123 10.0)
  28. (test-call "53687091.0" (lambda (a b) (quotient a b)) 536870912.0 10.0)
  29. (test-call "53687091.0" (lambda (a b) (quotient a b)) 536870912.0 10)
  30. (test-call "53687091.0" (lambda (a b) (quotient a b)) 536870912 10.0)
  31. (test-call "3.0" (lambda (a b) (remainder a b)) 123.0 10.0)
  32. (test-call "3.0" (lambda (a b) (remainder a b)) 123.0 10)
  33. (test-call "3.0" (lambda (a b) (remainder a b)) 123 10.0)
  34. (test-call "2.0" (lambda (a b) (remainder a b)) 536870912.0 10.0)
  35. (test-call "2.0" (lambda (a b) (remainder a b)) 536870912.0 10)
  36. (test-call "2.0" (lambda (a b) (remainder a b)) 536870912 10.0)
  37. (test-call "3.0" (lambda (a b) (modulo a b)) 123.0 10.0)
  38. (test-call "3.0" (lambda (a b) (modulo a b)) 123.0 10)
  39. (test-call "3.0" (lambda (a b) (modulo a b)) 123 10.0)
  40. (test-call "2.0" (lambda (a b) (modulo a b)) 536870912.0 10.0)
  41. (test-call "2.0" (lambda (a b) (modulo a b)) 536870912.0 10)
  42. (test-call "2.0" (lambda (a b) (modulo a b)) 536870912 10.0)
  43. ;; Checks the different-sign adjustment in $mod's fixnum-fixnum case,
  44. ;; currently used only for modulo with a flonum argument (which calls
  45. ;; $mod directly, bypassing the fixnum fast path in `(hoot compile)').
  46. (test-call "-7.0" (lambda (a b) (modulo a b)) 123.0 -10.0)
  47. (test-call "-7.0" (lambda (a b) (modulo a b)) 123.0 -10)
  48. (test-call "-7.0" (lambda (a b) (modulo a b)) 123 -10.0)
  49. (test-call "7.0" (lambda (a b) (modulo a b)) -123.0 10.0)
  50. (test-call "7.0" (lambda (a b) (modulo a b)) -123.0 10)
  51. (test-call "7.0" (lambda (a b) (modulo a b)) -123 10.0)
  52. ;; truncating division
  53. (test-call "(2 1)"
  54. (lambda (a b) (call-with-values
  55. (lambda () (truncate/ a b))
  56. (lambda x x)))
  57. 5 2)
  58. (test-call "(-2 -1)"
  59. (lambda (a b) (call-with-values
  60. (lambda () (truncate/ a b))
  61. (lambda x x)))
  62. -5 2)
  63. (test-call "(-2 1)"
  64. (lambda (a b) (call-with-values
  65. (lambda () (truncate/ a b))
  66. (lambda x x)))
  67. 5 -2)
  68. (test-call "(2 -1)"
  69. (lambda (a b) (call-with-values
  70. (lambda () (truncate/ a b))
  71. (lambda x x)))
  72. -5 -2)
  73. (test-call "(2.0 -1.0)"
  74. (lambda (a b) (call-with-values
  75. (lambda () (truncate/ a b))
  76. (lambda x x)))
  77. -5.0 -2.0)
  78. (test-call "(2.0 -1.0)"
  79. (lambda (a b) (call-with-values
  80. (lambda () (truncate/ a b))
  81. (lambda x x)))
  82. -5.0 -2)
  83. (test-call "(2.0 -1.0)"
  84. (lambda (a b) (call-with-values
  85. (lambda () (truncate/ a b))
  86. (lambda x x)))
  87. -5 -2.0)
  88. (test-call "2" (lambda (a b) (truncate-quotient a b)) 5 2)
  89. (test-call "-2" (lambda (a b) (truncate-quotient a b)) -5 2)
  90. (test-call "-2" (lambda (a b) (truncate-quotient a b)) 5 -2)
  91. (test-call "2" (lambda (a b) (truncate-quotient a b)) -5 -2)
  92. (test-call "2.0" (lambda (a b) (truncate-quotient a b)) -5.0 -2.0)
  93. (test-call "2.0" (lambda (a b) (truncate-quotient a b)) -5.0 -2)
  94. (test-call "2.0" (lambda (a b) (truncate-quotient a b)) -5 -2.0)
  95. (test-call "1" (lambda (a b) (truncate-remainder a b)) 5 2)
  96. (test-call "-1" (lambda (a b) (truncate-remainder a b)) -5 2)
  97. (test-call "1" (lambda (a b) (truncate-remainder a b)) 5 -2)
  98. (test-call "-1" (lambda (a b) (truncate-remainder a b)) -5 -2)
  99. (test-call "-1.0" (lambda (a b) (truncate-remainder a b)) -5.0 -2.0)
  100. (test-call "-1.0" (lambda (a b) (truncate-remainder a b)) -5.0 -2)
  101. (test-call "-1.0" (lambda (a b) (truncate-remainder a b)) -5 -2.0)
  102. ;; flooring division
  103. (test-call "(2 1)" (lambda (a b) (call-with-values
  104. (lambda () (floor/ a b))
  105. (lambda x x)))
  106. 5 2)
  107. (test-call "(-3 1)" (lambda (a b) (call-with-values
  108. (lambda () (floor/ a b))
  109. (lambda x x)))
  110. -5 2)
  111. (test-call "(-3 -1)" (lambda (a b) (call-with-values
  112. (lambda () (floor/ a b))
  113. (lambda x x)))
  114. 5 -2)
  115. (test-call "(2 -1)" (lambda (a b) (call-with-values
  116. (lambda () (floor/ a b))
  117. (lambda x x)))
  118. -5 -2)
  119. (test-call "(2.0 -1.0)" (lambda (a b) (call-with-values
  120. (lambda () (floor/ a b))
  121. (lambda x x)))
  122. -5.0 -2.0)
  123. (test-call "(2.0 -1.0)" (lambda (a b) (call-with-values
  124. (lambda () (floor/ a b))
  125. (lambda x x)))
  126. -5.0 -2)
  127. (test-call "(2.0 -1.0)" (lambda (a b) (call-with-values
  128. (lambda () (floor/ a b))
  129. (lambda x x)))
  130. -5 -2.0)
  131. (test-call "2" (lambda (a b) (floor-quotient a b)) 5 2)
  132. (test-call "-3" (lambda (a b) (floor-quotient a b)) -5 2)
  133. (test-call "-3" (lambda (a b) (floor-quotient a b)) 5 -2)
  134. (test-call "2" (lambda (a b) (floor-quotient a b)) -5 -2)
  135. (test-call "2.0" (lambda (a b) (floor-quotient a b)) -5.0 -2.0)
  136. (test-call "2.0" (lambda (a b) (floor-quotient a b)) -5.0 -2)
  137. (test-call "2.0" (lambda (a b) (floor-quotient a b)) -5 -2.0)
  138. (test-call "1" (lambda (a b) (floor-remainder a b)) 5 2)
  139. (test-call "1" (lambda (a b) (floor-remainder a b)) -5 2)
  140. (test-call "-1" (lambda (a b) (floor-remainder a b)) 5 -2)
  141. (test-call "-1" (lambda (a b) (floor-remainder a b)) -5 -2)
  142. (test-call "-1.0" (lambda (a b) (floor-remainder a b)) -5.0 -2.0)
  143. (test-call "-1.0" (lambda (a b) (floor-remainder a b)) -5.0 -2)
  144. (test-call "-1.0" (lambda (a b) (floor-remainder a b)) -5 -2.0)
  145. (with-additional-imports ((only (hoot numbers)
  146. ceiling/
  147. ceiling-quotient
  148. ceiling-remainder
  149. euclidean/
  150. euclidean-quotient
  151. euclidean-remainder))
  152. (test-call "13" (lambda (a b) (ceiling-quotient a b)) 123 10)
  153. (test-call "-7" (lambda (a b) (ceiling-remainder a b)) 123 10)
  154. (test-call "(13 -7)"
  155. (lambda (a b) (call-with-values (lambda () (ceiling/ a b)) list))
  156. 123 10)
  157. (test-call "(-12 3)"
  158. (lambda (a b) (call-with-values (lambda () (ceiling/ a b)) list))
  159. 123 -10)
  160. (test-call "(-12 -3)"
  161. (lambda (a b) (call-with-values (lambda () (ceiling/ a b)) list))
  162. -123 10)
  163. (test-call "(13 7)"
  164. (lambda (a b) (call-with-values (lambda () (ceiling/ a b)) list))
  165. -123 -10)
  166. (test-call "(2.0 3.799999999999997)"
  167. (lambda (a b) (call-with-values (lambda () (ceiling/ a b)) list))
  168. -123.2 -63.5)
  169. ;; FIXME: There's something wrong with fractions.
  170. ;; (test-call "(-3 22/21)"
  171. ;; (lambda (a b) (call-with-values (lambda () (ceiling/ a b)) list))
  172. ;; 13/3 -10/7)
  173. (test-call "12" (lambda (a b) (euclidean-quotient a b)) 123 10)
  174. (test-call "3" (lambda (a b) (euclidean-remainder a b)) 123 10)
  175. (test-call "(12 3)"
  176. (lambda (a b) (call-with-values (lambda () (euclidean/ a b)) list))
  177. 123 10)
  178. (test-call "(-12 3)"
  179. (lambda (a b) (call-with-values (lambda () (euclidean/ a b)) list))
  180. 123 -10)
  181. (test-call "(-13 7)"
  182. (lambda (a b) (call-with-values (lambda () (euclidean/ a b)) list))
  183. -123 10)
  184. (test-call "(13 7)"
  185. (lambda (a b) (call-with-values (lambda () (euclidean/ a b)) list))
  186. -123 -10)
  187. (test-call "(2.0 3.799999999999997)"
  188. (lambda (a b) (call-with-values (lambda () (euclidean/ a b)) list))
  189. -123.2 -63.5)
  190. ;; (test-call "(-3 22/21)"
  191. ;; (lambda (a b) (call-with-values (lambda () (euclidean/ a b)) list))
  192. ;; 16/3 -10/7)
  193. )
  194. (test-end* "test-division")