r6rs-arithmetic-flonums.test 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. ;;; arithmetic-flonums.test --- Test suite for R6RS (rnrs arithmetic flonums)
  2. ;; Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (define-module (test-suite test-r6rs-arithmetic-flonums)
  18. :use-module ((rnrs arithmetic flonums) :version (6))
  19. :use-module ((rnrs conditions) :version (6))
  20. :use-module ((rnrs exceptions) :version (6))
  21. :use-module (test-suite lib))
  22. (define fake-pi 3.14159265)
  23. (define (reasonably-close? x y) (< (abs (- x y)) 0.0000001))
  24. (with-test-prefix "flonum?"
  25. (pass-if "flonum? is #t on flonum"
  26. (flonum? 1.5))
  27. (pass-if "flonum? is #f on complex"
  28. (not (flonum? 1.5+0.0i)))
  29. (pass-if "flonum? is #f on exact integer"
  30. (not (flonum? 3))))
  31. (with-test-prefix "real->flonum"
  32. (pass-if "simple"
  33. (flonum? (real->flonum 3))))
  34. (with-test-prefix "fl=?"
  35. (pass-if "fl=? is #t for eqv inputs"
  36. (fl=? 3.0 3.0 3.0))
  37. (pass-if "fl=? is #f for non-eqv inputs"
  38. (not (fl=? 1.5 0.0 3.0)))
  39. (pass-if "+inf.0 is fl= to itself"
  40. (fl=? +inf.0 +inf.0))
  41. (pass-if "0.0 and -0.0 are fl="
  42. (fl=? 0.0 -0.0)))
  43. (with-test-prefix "fl<?"
  44. (pass-if "fl<? is #t for monotonically < inputs"
  45. (fl<? 1.0 2.0 3.0))
  46. (pass-if "fl<? is #f for non-monotonically < inputs"
  47. (not (fl<? 2.0 2.0 1.4))))
  48. (with-test-prefix "fl<=?"
  49. (pass-if "fl<=? is #t for monotonically < or = inputs"
  50. (fl<=? 1.0 1.2 1.2))
  51. (pass-if "fl<=? is #f non-monotonically < or = inputs"
  52. (not (fl<=? 2.0 1.0 0.9))))
  53. (with-test-prefix "fl>?"
  54. (pass-if "fl>? is #t for monotonically > inputs"
  55. (fl>? 3.0 2.0 1.0))
  56. (pass-if "fl>? is #f for non-monotonically > inputs"
  57. (not (fl>? 1.0 1.0 1.2))))
  58. (with-test-prefix "fl>=?"
  59. (pass-if "fl>=? is #t for monotonically > or = inputs"
  60. (fl>=? 3.0 2.0 2.0))
  61. (pass-if "fl>=? is #f for non-monotonically > or = inputs"
  62. (not (fl>=? 1.0 1.2 1.2))))
  63. (with-test-prefix "flinteger?"
  64. (pass-if "flinteger? is #t on integer flomnums"
  65. (flinteger? 1.0))
  66. (pass-if "flinteger? is #f on non-integer flonums"
  67. (not (flinteger? 1.5))))
  68. (with-test-prefix "flzero?"
  69. (pass-if "flzero? is #t for 0.0 and -0.0"
  70. (and (flzero? 0.0) (flzero? -0.0)))
  71. (pass-if "flzero? is #f for non-zero flonums"
  72. (not (flzero? 1.0))))
  73. (with-test-prefix "flpositive?"
  74. (pass-if "flpositive? is #t on positive flonum"
  75. (flpositive? 1.0))
  76. (pass-if "flpositive? is #f on negative flonum"
  77. (not (flpositive? -1.0)))
  78. (pass-if "0.0 and -0.0 are not flpositive"
  79. (and (not (flpositive? 0.0)) (not (flpositive? -0.0)))))
  80. (with-test-prefix "flnegative?"
  81. (pass-if "flnegative? is #t on negative flonum"
  82. (flnegative? -1.0))
  83. (pass-if "flnegative? is #f on positive flonum"
  84. (not (flnegative? 1.0)))
  85. (pass-if "0.0 and -0.0 are not flnegative"
  86. (and (not (flnegative? 0.0)) (not (flnegative? -0.0)))))
  87. (with-test-prefix "flodd?"
  88. (pass-if "&assertion raised on non-integer flonum"
  89. (guard (condition ((assertion-violation? condition) #t) (else #f))
  90. (begin (flodd? 1.5) #f)))
  91. (pass-if "flodd? is #t on odd flonums"
  92. (flodd? 3.0))
  93. (pass-if "flodd? is #f on even flonums"
  94. (not (flodd? 2.0))))
  95. (with-test-prefix "fleven?"
  96. (pass-if "&assertion raised on non-integer flonum"
  97. (guard (condition ((assertion-violation? condition) #t) (else #f))
  98. (begin (fleven? 1.5) #f)))
  99. (pass-if "fleven? is #t on even flonums"
  100. (fleven? 2.0))
  101. (pass-if "fleven? is #f on odd flonums"
  102. (not (fleven? 3.0))))
  103. (with-test-prefix "flfinite?"
  104. (pass-if "flfinite? is #t on non-infinite flonums"
  105. (flfinite? 2.0))
  106. (pass-if "flfinite? is #f on infinities"
  107. (and (not (flfinite? +inf.0)) (not (flfinite? -inf.0))))
  108. (pass-if "flfinite? is #f on NaNs"
  109. (not (flfinite? +nan.0))))
  110. (with-test-prefix "flinfinite?"
  111. (pass-if "flinfinite? is #t on infinities"
  112. (and (flinfinite? +inf.0) (flinfinite? -inf.0)))
  113. (pass-if "flinfinite? is #f on non-infinite flonums"
  114. (not (flinfinite? 2.0))))
  115. (with-test-prefix "flnan?"
  116. (pass-if "flnan? is #t on NaN and -NaN"
  117. (and (flnan? +nan.0) (flnan? -nan.0)))
  118. (pass-if "flnan? is #f on non-NaN values"
  119. (not (flnan? 1.5))))
  120. (with-test-prefix "flmax"
  121. (pass-if "simple" (fl=? (flmax 1.0 3.0 2.0) 3.0)))
  122. (with-test-prefix "flmin"
  123. (pass-if "simple" (fl=? (flmin -1.0 0.0 2.0) -1.0)))
  124. (with-test-prefix "fl+"
  125. (pass-if "simple" (fl=? (fl+ 2.141 1.0 0.1) 3.241))
  126. (pass-if "zero args" (fl=? (fl+) 0.0)))
  127. (with-test-prefix "fl*"
  128. (pass-if "simple" (fl=? (fl* 1.0 2.0 3.0 1.5) 9.0))
  129. (pass-if "zero args" (fl=? (fl*) 1.0)))
  130. (with-test-prefix "fl-"
  131. (pass-if "unary fl- negates argument" (fl=? (fl- 2.0) -2.0))
  132. (pass-if "simple" (fl=? (fl- 10.5 6.0 0.5) 4.0)))
  133. (with-test-prefix "fl/"
  134. (pass-if "unary fl/ returns multiplicative inverse" (fl=? (fl/ 10.0) 0.1))
  135. (pass-if "simple" (fl=? (fl/ 10.0 2.0 2.0) 2.5)))
  136. (with-test-prefix "flabs"
  137. (pass-if "simple" (and (fl=? (flabs -1.0) 1.0) (fl=? (flabs 1.23) 1.23))))
  138. (with-test-prefix "fldiv-and-mod"
  139. (pass-if "simple"
  140. (call-with-values (lambda () (fldiv-and-mod 5.0 2.0))
  141. (lambda (div mod) (fl=? div 2.0) (fl=? mod 1.0)))))
  142. (with-test-prefix "fldiv"
  143. (pass-if "simple" (fl=? (fldiv 5.0 2.0) 2.0)))
  144. (with-test-prefix "flmod"
  145. (pass-if "simple" (fl=? (flmod 5.0 2.0) 1.0)))
  146. (with-test-prefix "fldiv0-and-mod0"
  147. (pass-if "simple"
  148. (call-with-values (lambda () (fldiv0-and-mod0 -123.0 10.0))
  149. (lambda (div mod)
  150. (and (fl=? div -12.0) (fl=? mod -3.0))))))
  151. (with-test-prefix "fldiv0"
  152. (pass-if "simple" (fl=? (fldiv0 -123.0 10.0) -12.0)))
  153. (with-test-prefix "flmod0"
  154. (pass-if "simple" (fl=? (flmod0 -123.0 10.0) -3.0)))
  155. (with-test-prefix "flnumerator"
  156. (pass-if "simple" (fl=? (flnumerator 0.5) 1.0))
  157. (pass-if "infinities"
  158. (and (fl=? (flnumerator +inf.0) +inf.0)
  159. (fl=? (flnumerator -inf.0) -inf.0)))
  160. (pass-if "negative zero" (eqv? (flnumerator -0.0) -0.0)))
  161. (with-test-prefix "fldenominator"
  162. (pass-if "simple" (fl=? (fldenominator 0.5) 2.0))
  163. (pass-if "infinities"
  164. (and (fl=? (fldenominator +inf.0) 1.0)
  165. (fl=? (fldenominator -inf.0) 1.0)))
  166. (pass-if "zero" (fl=? (fldenominator 0.0) 1.0)))
  167. (with-test-prefix "flfloor"
  168. (pass-if "simple"
  169. (and (fl=? (flfloor -4.3) -5.0)
  170. (fl=? (flfloor 3.5) 3.0))))
  171. (with-test-prefix "flceiling"
  172. (pass-if "simple"
  173. (and (fl=? (flceiling -4.3) -4.0)
  174. (fl=? (flceiling 3.5) 4.0))))
  175. (with-test-prefix "fltruncate"
  176. (pass-if "simple"
  177. (and (fl=? (fltruncate -4.3) -4.0)
  178. (fl=? (fltruncate 3.5) 3.0))))
  179. (with-test-prefix "flround"
  180. (pass-if "simple"
  181. (and (fl=? (flround -4.3) -4.0)
  182. (fl=? (flround 3.5) 4.0))))
  183. (with-test-prefix "flexp"
  184. (pass-if "infinities"
  185. (and (fl=? (flexp +inf.0) +inf.0)
  186. (fl=? (flexp -inf.0) 0.0))))
  187. (with-test-prefix "fllog"
  188. (pass-if "unary fllog returns natural log"
  189. (reasonably-close? (fllog 2.718281828459045) 1.0))
  190. (pass-if "infinities"
  191. (and (fl=? (fllog +inf.0) +inf.0)
  192. (flnan? (fllog -inf.0))))
  193. (pass-if "negative argument"
  194. (flnan? (fllog -1.0)))
  195. (pass-if "zero" (fl=? (fllog 0.0) -inf.0))
  196. (pass-if "negative zero" (fl=? (fllog -0.0) -inf.0))
  197. (pass-if "negative zero with base" (fl=? (fllog -0.0 0.5) +inf.0))
  198. (pass-if "binary fllog returns log in specified base"
  199. (fl=? (fllog 8.0 2.0) 3.0)))
  200. (with-test-prefix "flsin"
  201. (pass-if "simple"
  202. (and (reasonably-close? (flsin (/ fake-pi 2)) 1.0)
  203. (reasonably-close? (flsin (/ fake-pi 6)) 0.5))))
  204. (with-test-prefix "flcos"
  205. (pass-if "simple"
  206. (and (fl=? (flcos 0.0) 1.0) (reasonably-close? (flcos (/ fake-pi 3)) 0.5))))
  207. (with-test-prefix "fltan"
  208. (pass-if "simple"
  209. (and (reasonably-close? (fltan (/ fake-pi 4)) 1.0)
  210. (reasonably-close? (fltan (/ (* 3 fake-pi) 4)) -1.0))))
  211. (with-test-prefix "flasin"
  212. (pass-if "simple"
  213. (and (reasonably-close? (flasin 1.0) (/ fake-pi 2))
  214. (reasonably-close? (flasin 0.5) (/ fake-pi 6))))
  215. (pass-if "out of range"
  216. (flnan? (flasin 2.0))))
  217. (with-test-prefix "flacos"
  218. (pass-if "simple"
  219. (and (fl=? (flacos 1.0) 0.0)
  220. (reasonably-close? (flacos 0.5) (/ fake-pi 3))))
  221. (pass-if "out of range"
  222. (flnan? (flacos 2.0))))
  223. (with-test-prefix "flatan"
  224. (pass-if "unary flatan"
  225. (and (reasonably-close? (flatan 1.0) (/ fake-pi 4))
  226. (reasonably-close? (flatan -1.0) (/ fake-pi -4))))
  227. (pass-if "infinities"
  228. (and (reasonably-close? (flatan -inf.0) -1.5707963267949)
  229. (reasonably-close? (flatan +inf.0) 1.5707963267949)))
  230. (pass-if "binary flatan"
  231. (and (reasonably-close? (flatan 3.5 3.5) (/ fake-pi 4)))))
  232. (with-test-prefix "flsqrt"
  233. (pass-if "simple" (fl=? (flsqrt 4.0) 2.0))
  234. (pass-if "negative" (flnan? (flsqrt -1.0)))
  235. (pass-if "infinity" (fl=? (flsqrt +inf.0) +inf.0))
  236. (pass-if "negative zero" (fl=? (flsqrt -0.0) -0.0)))
  237. (with-test-prefix "flexpt"
  238. (pass-if "simple" (fl=? (flexpt 2.0 3.0) 8.0))
  239. (pass-if "negative squared" (fl=? (flexpt -2.0 2.0) 4.0))
  240. (pass-if "negative cubed" (fl=? (flexpt -2.0 3.0) -8.0))
  241. (pass-if "negative to non-integer power" (flnan? (flexpt -2.0 2.5))))
  242. (with-test-prefix "fixnum->flonum"
  243. (pass-if "simple" (fl=? (fixnum->flonum 100) 100.0)))