test-fractions.scm 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. ;;; 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. (test-call "65537/281474976710656"
  45. (lambda (a b) (+ a b)) (/ 1 (ash 1 32)) (/ 1 (ash 1 48)))
  46. (test-call "1/2" (lambda (a b c) (- a (/ b c))) 1 1 2)
  47. (test-call "-1/2" (lambda (a b c) (- (/ a b) c)) 1 2 1)
  48. (test-call "-1/2" (lambda (a b c) (- (/ a b) (/ c c))) 1 2 1)
  49. (test-call "1610612735/3"
  50. (lambda (a b c d) (- (+ a b) (/ c d)))
  51. 536870911 1 1 3)
  52. (test-call "-1610612735/3"
  53. (lambda (a b c d) (- (/ a b) (+ c d)))
  54. 1 3 536870911 1)
  55. (test-call "-1/6" (lambda (a b c d) (- (/ a b) (/ c d))) 1 2 2 3)
  56. (test-call "-1/2" (lambda (a b) (/ a b)) 1 -2)
  57. (test-call "#t" (lambda (a b c) (< a (/ b c))) 0 1 2)
  58. ;;; abs, floor and ceiling
  59. (test-call "1/2" (lambda (a b) (abs (/ a b))) 1 2)
  60. (test-call "1/2" (lambda (a b) (abs (/ a b))) -1 2)
  61. (test-call "0" (lambda (a b) (floor (/ a b))) 1 2)
  62. (test-call "-1" (lambda (a b) (floor (/ a b))) -1 2)
  63. (test-call "8" (lambda (a b) (floor (/ a b))) 26 3)
  64. (test-call "-9" (lambda (a b) (floor (/ a b))) -26 3)
  65. (test-call "178956970" (lambda (a b c) (floor (/ (+ a b) c))) 536870911 1 3)
  66. (test-call "-178956971" (lambda (a b c) (floor (/ (+ a b) c))) -536870911 -2 3)
  67. (test-call "0" (lambda (a b) (floor (/ (+ a b) (+ a b b)))) 536870911 1)
  68. (test-call "0" (lambda (a b) (floor (/ (+ a b) (+ a b b)))) -536870911 -2)
  69. (test-call "536870912" (lambda (a b c) (floor (/ (* (+ a b) c) c))) 536870911 1 2)
  70. (test-call "-536870913" (lambda (a b c) (floor (/ (* (+ a b) c) c))) -536870911 -2 2)
  71. (test-call "1" (lambda (a b) (ceiling (/ a b))) 1 2)
  72. (test-call "0" (lambda (a b) (ceiling (/ a b))) -1 2)
  73. (test-call "9" (lambda (a b) (ceiling (/ a b))) 26 3)
  74. (test-call "-8" (lambda (a b) (ceiling (/ a b))) -26 3)
  75. (test-call "178956971" (lambda (a b c) (ceiling (/ (+ a b) c))) 536870911 1 3)
  76. (test-call "-178956971" (lambda (a b c) (ceiling (/ (+ a b) c))) -536870911 -2 3)
  77. (test-call "1" (lambda (a b) (ceiling (/ (+ a b) (+ a b b)))) 536870911 1)
  78. (test-call "1" (lambda (a b) (ceiling (/ (+ a b) (+ a b b)))) -536870911 -2)
  79. (test-call "536870912" (lambda (a b c) (ceiling (/ (* (+ a b) c) c))) 536870911 1 2)
  80. (test-call "-536870913" (lambda (a b c) (ceiling (/ (* (+ a b) c) c))) -536870911 -2 2)
  81. (test-end* "test-fractions")