test-fractions.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. ;;; Fraction tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-fractions")
  22. ;; Exact fraction arithmetic
  23. (test-call "1/2" (lambda (a b) (/ a b)) 1 2)
  24. (test-call "3/536870912" (lambda (a b c) (/ a (+ b c))) 3 536870911 1)
  25. (test-call "536870912/3" (lambda (a b c) (/ (+ a b) c)) 536870911 1 3)
  26. (test-call "536870912/536870913"
  27. (lambda (a b) (/ (+ a b) (+ a b b)))
  28. 536870911 1)
  29. (test-call "1/2" (lambda (a b) (* a (/ a b))) 1 2)
  30. (test-call "1" (lambda (a b) (* b (/ a b))) 1 2)
  31. (test-call "1/4" (lambda (a b) (* (/ a b) (/ a b))) 2 4)
  32. (test-call "1" (lambda (a b) (* (/ b a) (/ a b))) 1 2)
  33. ;; FIXME:
  34. ;; (test-call "1"
  35. ;; (lambda (a b) (* (/ (+ a b) (+ a b)) (/ (+ a b) (+ a b))))
  36. ;; 536870911 1)
  37. (test-call "3/2" (lambda (a b c) (+ a (/ b c))) 1 1 2)
  38. (test-call "1073741825/2"
  39. (lambda (a b c d) (+ (+ a b) (/ c d)))
  40. 536870911 1 1 2)
  41. (test-call "7/6" (lambda (a b c d) (+ (/ a b) (/ c d))) 1 2 2 3)
  42. (test-call "7/6" (lambda (a b c d) (+ (/ a b) (/ c d))) 2 4 4 6)
  43. ;; Adding fractions whose denominators have a bignum gcd.
  44. (with-additional-imports
  45. ((hoot bitwise))
  46. (test-call "65537/281474976710656"
  47. (lambda (a b) (+ a b)) (/ 1 (ash 1 32)) (/ 1 (ash 1 48))))
  48. (test-call "1/2" (lambda (a b c) (- a (/ b c))) 1 1 2)
  49. (test-call "-1/2" (lambda (a b c) (- (/ a b) c)) 1 2 1)
  50. (test-call "-1/2" (lambda (a b c) (- (/ a b) (/ c c))) 1 2 1)
  51. (test-call "1610612735/3"
  52. (lambda (a b c d) (- (+ a b) (/ c d)))
  53. 536870911 1 1 3)
  54. (test-call "-1610612735/3"
  55. (lambda (a b c d) (- (/ a b) (+ c d)))
  56. 1 3 536870911 1)
  57. (test-call "-1/6" (lambda (a b c d) (- (/ a b) (/ c d))) 1 2 2 3)
  58. (test-call "-1/2" (lambda (a b) (/ a b)) 1 -2)
  59. (with-additional-imports
  60. ((hoot bitwise))
  61. (test-call "2147483648" (lambda (a b) (/ a b)) (ash 1 29) 1/4)
  62. (test-call "1/2147483648" (lambda (a b) (/ a b)) 1/4 (ash 1 29)))
  63. ;; Fraction/flonum arithmetic
  64. (test-call "0.5" (lambda (a b) (+ a b)) 1/4 0.25)
  65. (test-call "0.5" (lambda (a b) (+ a b)) 0.25 1/4)
  66. (test-call "0.0" (lambda (a b) (- a b)) 1/4 0.25)
  67. (test-call "0.0" (lambda (a b) (- a b)) 0.25 1/4)
  68. (test-call "0.5" (lambda (a b) (* a b)) 1/4 2.0)
  69. (test-call "0.5" (lambda (a b) (* a b)) 2.0 1/4)
  70. (test-call "0.125" (lambda (a b) (/ a b)) 1/4 2.0)
  71. (test-call "8.0" (lambda (a b) (/ a b)) 2.0 1/4)
  72. (test-call "#t" (lambda (a b c) (< a (/ b c))) 0 1 2)
  73. ;;; abs, floor and ceiling
  74. (test-call "1/2" (lambda (a b) (abs (/ a b))) 1 2)
  75. (test-call "1/2" (lambda (a b) (abs (/ a b))) -1 2)
  76. (test-call "0" (lambda (a b) (floor (/ a b))) 1 2)
  77. (test-call "-1" (lambda (a b) (floor (/ a b))) -1 2)
  78. (test-call "8" (lambda (a b) (floor (/ a b))) 26 3)
  79. (test-call "-9" (lambda (a b) (floor (/ a b))) -26 3)
  80. (test-call "178956970" (lambda (a b c) (floor (/ (+ a b) c))) 536870911 1 3)
  81. (test-call "-178956971" (lambda (a b c) (floor (/ (+ a b) c))) -536870911 -2 3)
  82. (test-call "0" (lambda (a b) (floor (/ (+ a b) (+ a b b)))) 536870911 1)
  83. (test-call "0" (lambda (a b) (floor (/ (+ a b) (+ a b b)))) -536870911 -2)
  84. (test-call "536870912" (lambda (a b c) (floor (/ (* (+ a b) c) c))) 536870911 1 2)
  85. (test-call "-536870913" (lambda (a b c) (floor (/ (* (+ a b) c) c))) -536870911 -2 2)
  86. (test-call "1" (lambda (a b) (ceiling (/ a b))) 1 2)
  87. (test-call "0" (lambda (a b) (ceiling (/ a b))) -1 2)
  88. (test-call "9" (lambda (a b) (ceiling (/ a b))) 26 3)
  89. (test-call "-8" (lambda (a b) (ceiling (/ a b))) -26 3)
  90. (test-call "178956971" (lambda (a b c) (ceiling (/ (+ a b) c))) 536870911 1 3)
  91. (test-call "-178956971" (lambda (a b c) (ceiling (/ (+ a b) c))) -536870911 -2 3)
  92. (test-call "1" (lambda (a b) (ceiling (/ (+ a b) (+ a b b)))) 536870911 1)
  93. (test-call "1" (lambda (a b) (ceiling (/ (+ a b) (+ a b b)))) -536870911 -2)
  94. (test-call "536870912" (lambda (a b c) (ceiling (/ (* (+ a b) c) c))) 536870911 1 2)
  95. (test-call "-536870913" (lambda (a b c) (ceiling (/ (* (+ a b) c) c))) -536870911 -2 2)
  96. (test-end* "test-fractions")