test-fractions.scm 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. (test-call "#t" (lambda (a b c) (< a (/ b c))) 0 1 2)
  64. ;;; abs, floor and ceiling
  65. (test-call "1/2" (lambda (a b) (abs (/ a b))) 1 2)
  66. (test-call "1/2" (lambda (a b) (abs (/ a b))) -1 2)
  67. (test-call "0" (lambda (a b) (floor (/ a b))) 1 2)
  68. (test-call "-1" (lambda (a b) (floor (/ a b))) -1 2)
  69. (test-call "8" (lambda (a b) (floor (/ a b))) 26 3)
  70. (test-call "-9" (lambda (a b) (floor (/ a b))) -26 3)
  71. (test-call "178956970" (lambda (a b c) (floor (/ (+ a b) c))) 536870911 1 3)
  72. (test-call "-178956971" (lambda (a b c) (floor (/ (+ a b) c))) -536870911 -2 3)
  73. (test-call "0" (lambda (a b) (floor (/ (+ a b) (+ a b b)))) 536870911 1)
  74. (test-call "0" (lambda (a b) (floor (/ (+ a b) (+ a b b)))) -536870911 -2)
  75. (test-call "536870912" (lambda (a b c) (floor (/ (* (+ a b) c) c))) 536870911 1 2)
  76. (test-call "-536870913" (lambda (a b c) (floor (/ (* (+ a b) c) c))) -536870911 -2 2)
  77. (test-call "1" (lambda (a b) (ceiling (/ a b))) 1 2)
  78. (test-call "0" (lambda (a b) (ceiling (/ a b))) -1 2)
  79. (test-call "9" (lambda (a b) (ceiling (/ a b))) 26 3)
  80. (test-call "-8" (lambda (a b) (ceiling (/ a b))) -26 3)
  81. (test-call "178956971" (lambda (a b c) (ceiling (/ (+ a b) c))) 536870911 1 3)
  82. (test-call "-178956971" (lambda (a b c) (ceiling (/ (+ a b) c))) -536870911 -2 3)
  83. (test-call "1" (lambda (a b) (ceiling (/ (+ a b) (+ a b b)))) 536870911 1)
  84. (test-call "1" (lambda (a b) (ceiling (/ (+ a b) (+ a b b)))) -536870911 -2)
  85. (test-call "536870912" (lambda (a b c) (ceiling (/ (* (+ a b) c) c))) 536870911 1 2)
  86. (test-call "-536870913" (lambda (a b c) (ceiling (/ (* (+ a b) c) c))) -536870911 -2 2)
  87. (test-end* "test-fractions")