test-complex.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;;; Copyright (C) 2023, 2024 David Thompson <dave@spritely.institute>
  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. ;;; Complex number tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-complex")
  22. (test-call "42" (lambda (x y) (make-rectangular x y)) 42 0)
  23. (test-call "1.0+2.0i" (lambda (x y) (make-rectangular x y)) 1 2)
  24. (test-call "0" (lambda (x y) (make-polar x y)) 0 3)
  25. (test-call "42" (lambda (x y) (make-polar x y)) 42 0)
  26. (test-call "+nan.0+nan.0i" (lambda (x y) (make-polar x y)) +inf.0 +inf.0)
  27. (test-call "4.322418446945118+6.731767878463172i" (lambda (x y) (make-polar x y)) 8.0 1.0)
  28. ;; Addition
  29. (test-call "4.0+2.0i" (lambda (x y) (+ x y)) 1+2i 3)
  30. (test-call "4.0+2.0i" (lambda (x y) (+ x y)) 3 1+2i)
  31. (with-additional-imports
  32. ((hoot bitwise))
  33. (test-call "536870913.0+2.0i" (lambda (x y) (+ x y)) 1+2i (ash 1 29))
  34. (test-call "536870913.0+2.0i" (lambda (x y) (+ x y)) (ash 1 29) 1+2i))
  35. (test-call "4.0+2.0i" (lambda (x y) (+ x y)) 1+2i 3.0)
  36. (test-call "4.0+2.0i" (lambda (x y) (+ x y)) 3.0 1+2i)
  37. (test-call "1.25+2.0i" (lambda (x y) (+ x y)) 1+2i 1/4)
  38. (test-call "1.25+2.0i" (lambda (x y) (+ x y)) 1/4 1+2i)
  39. (test-call "7.0+7.0i" (lambda (x y) (+ x y)) 3+4i 4+3i)
  40. ;; Subtraction
  41. (test-call "0.0+2.0i" (lambda (x y) (- x y)) 1+2i 1)
  42. (test-call "0.0-2.0i" (lambda (x y) (- x y)) 1 1+2i)
  43. (with-additional-imports
  44. ((hoot bitwise))
  45. (test-call "-536870911.0+2.0i" (lambda (x y) (- x y)) 1+2i (ash 1 29))
  46. (test-call "536870911.0-2.0i" (lambda (x y) (- x y)) (ash 1 29) 1+2i))
  47. (test-call "0.0+2.0i" (lambda (x y) (- x y)) 1+2i 1.0)
  48. (test-call "0.0-2.0i" (lambda (x y) (- x y)) 1.0 1+2i)
  49. (test-call "0.75+2.0i" (lambda (x y) (- x y)) 1+2i 1/4)
  50. (test-call "-0.75-2.0i" (lambda (x y) (- x y)) 1/4 1+2i)
  51. (test-call "-1.0+1.0i" (lambda (x y) (- x y)) 3+4i 4+3i)
  52. ;; Multiplication
  53. (test-call "6.0+8.0i" (lambda (x y) (* x y)) 3+4i 2)
  54. (test-call "6.0+8.0i" (lambda (x y) (* x y)) 2 3+4i)
  55. (with-additional-imports
  56. ((hoot bitwise))
  57. (test-call "536870912.0+1073741824.0i" (lambda (x y) (* x y)) 1+2i (ash 1 29))
  58. (test-call "536870912.0+1073741824.0i" (lambda (x y) (* x y)) (ash 1 29) 1+2i))
  59. (test-call "6.0+8.0i" (lambda (x y) (* x y)) 3+4i 2.0)
  60. (test-call "6.0+8.0i" (lambda (x y) (* x y)) 2.0 3+4i)
  61. (test-call "0.25+0.5i" (lambda (x y) (* x y)) 1+2i 1/4)
  62. (test-call "0.25+0.5i" (lambda (x y) (* x y)) 1/4 1+2i)
  63. (test-call "-5.0+10.0i" (lambda (x y) (* x y)) 1+2i 3+4i)
  64. ;; Division
  65. (test-call "1.5+2.0i" (lambda (x y) (/ x y)) 3+4i 2)
  66. (test-call "0.24-0.32i" (lambda (x y) (/ x y)) 2 3+4i)
  67. (with-additional-imports
  68. ((hoot bitwise))
  69. (test-call "1.0+1.0i"
  70. (lambda (x y) (/ x y))
  71. (make-rectangular (ash 1 29) (ash 1 29))
  72. (ash 1 29))
  73. (test-call "0.5-0.5i"
  74. (lambda (x y) (/ x y))
  75. (ash 1 29)
  76. (make-rectangular (ash 1 29) (ash 1 29))))
  77. (test-call "1.5+2.0i" (lambda (x y) (/ x y)) 3+4i 2.0)
  78. (test-call "0.24-0.32i" (lambda (x y) (/ x y)) 2.0 3+4i)
  79. (test-call "4.0+8.0i" (lambda (x y) (/ x y)) 1+2i 1/4)
  80. (test-call "0.05-0.1i" (lambda (x y) (/ x y)) 1/4 1+2i)
  81. (test-call "1.75+0.25i" (lambda (x y) (/ x y)) 3+4i 2+2i)
  82. ;; =
  83. (test-call "#t" (lambda (x y) (= x y)) 1 1+0.0i)
  84. (test-call "#t" (lambda (x y) (= x y)) 18446744073709551616 18446744073709551616+0.0i)
  85. (test-call "#t" (lambda (x y) (= x y)) 0.5 0.5+0.0i)
  86. (test-call "#t" (lambda (x y) (= x y)) 1/2 0.5+0.0i)
  87. (test-call "#t" (lambda (x y) (= x y)) 0.5+0.0i 0.5+0.0i)
  88. (test-call "#f" (lambda (x y) (= x y)) 1 1+0.1i)
  89. (test-call "#f" (lambda (x y) (= x y)) 18446744073709551616 18446744073709551616+0.1i)
  90. (test-call "#f" (lambda (x y) (= x y)) 0.5 0.5+0.1i)
  91. (test-call "#f" (lambda (x y) (= x y)) 1/2 0.5+0.1i)
  92. (test-call "#f" (lambda (x y) (= x y)) 0.5+0.0i 0.5+0.1i)
  93. (test-call "#f" (lambda (x y) (= x y)) 0.5+0.0i 0.4+0.0i)
  94. ;; eqv?
  95. (test-call "#f" (lambda (x y) (eqv? x y)) 1 1+0.0i)
  96. (test-call "#f" (lambda (x y) (eqv? x y)) 18446744073709551616 18446744073709551616+0.0i)
  97. (test-call "#f" (lambda (x y) (eqv? x y)) 0.5 0.5+0.0i)
  98. (test-call "#f" (lambda (x y) (eqv? x y)) 1/2 0.5+0.0i)
  99. (test-call "#t" (lambda (x y) (eqv? x y)) 0.5+0.0i 0.5+0.0i)
  100. (test-call "#f" (lambda (x y) (eqv? x y)) 0.5+0.0i 0.5+0.1i)
  101. (test-call "#f" (lambda (x y) (eqv? x y)) 0.5+0.0i 0.4+0.0i)
  102. (test-call "#t" (lambda (x y) (eqv? x y)) +nan.0+nan.0i +nan.0+nan.0i)
  103. (test-call "#t" (lambda (x y) (eqv? x y)) +nan.0+1i +nan.0+1i)
  104. (test-call "#t" (lambda (x y) (eqv? x y)) 1+nan.0i 1+nan.0i)
  105. (test-end* "test-complex")