r6rs-arithmetic-fixnums.test 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ;;; arithmetic-fixnums.test --- Test suite for R6RS (rnrs arithmetic bitwise)
  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-fixnums)
  18. :use-module ((rnrs arithmetic fixnums) :version (6))
  19. :use-module ((rnrs conditions) :version (6))
  20. :use-module ((rnrs exceptions) :version (6))
  21. :use-module (test-suite lib))
  22. (with-test-prefix "fixnum-width"
  23. (pass-if-equal "consistent with least-fixnum"
  24. (- (expt 2 (- (fixnum-width) 1)))
  25. (least-fixnum))
  26. (pass-if-equal "consistent with greatest-fixnum"
  27. (- (expt 2 (- (fixnum-width) 1)) 1)
  28. (greatest-fixnum)))
  29. (with-test-prefix "fixnum?"
  30. (pass-if "fixnum? is #t for fixnums" (fixnum? 0))
  31. (pass-if "fixnum? is #f for non-fixnums" (not (fixnum? 'foo)))
  32. (pass-if "fixnum? is #f for non-fixnum numbers"
  33. (and (not (fixnum? 1.0)) (not (fixnum? (+ (greatest-fixnum) 1))))))
  34. (with-test-prefix "fx=?"
  35. (pass-if "fx=? is #t for eqv inputs" (fx=? 3 3 3))
  36. (pass-if "fx=? is #f for non-eqv inputs" (not (fx=? 1 2 3))))
  37. (with-test-prefix "fx>?"
  38. (pass-if "fx>? is #t for monotonically > inputs" (fx>? 3 2 1))
  39. (pass-if "fx>? is #f for non-monotonically > inputs" (not (fx>? 1 2 3))))
  40. (with-test-prefix "fx<?"
  41. (pass-if "fx<? is #t for monotonically < inputs" (fx<? 1 2 3))
  42. (pass-if "fx<? is #t for non-monotonically < inputs" (not (fx<? 3 2 1))))
  43. (with-test-prefix "fx>=?"
  44. (pass-if "fx>=? is #t for monotonically > or = inputs" (fx>=? 3 2 2 1))
  45. (pass-if "fx>=? is #f for non-monotonically > or = inputs"
  46. (not (fx>=? 1 2 3))))
  47. (with-test-prefix "fx<=?"
  48. (pass-if "fx<=? is #t for monotonically < or = inputs" (fx<=? 1 2 2 3))
  49. (pass-if "fx<=? is #f for non-monotonically < or = inputs"
  50. (not (fx<=? 3 2 1))))
  51. (with-test-prefix "fxzero?"
  52. (pass-if "fxzero? is #t for zero" (fxzero? 0))
  53. (pass-if "fxzero? is #f for non-zero fixnums"
  54. (and (not (fxzero? 1)) (not (fxzero? -1)))))
  55. (with-test-prefix "fxpositive?"
  56. (pass-if "fxpositive? is #t for positive fixnums" (fxpositive? 1))
  57. (pass-if "fxpositive? is #f for non-positive fixnums"
  58. (and (not (fxpositive? -1))
  59. (not (fxpositive? 0)))))
  60. (with-test-prefix "fxnegative?"
  61. (pass-if "fxnegative? is #t for negative fixnums" (fxnegative? -1))
  62. (pass-if "fxnegative? is #f for non-negative fixnums"
  63. (and (not (fxnegative? 1))
  64. (not (fxnegative? 0)))))
  65. (with-test-prefix "fxodd?"
  66. (pass-if "fxodd? is #t for odd fixnums" (fxodd? 1))
  67. (pass-if "fxodd? is #f for even fixnums" (not (fxodd? 2))))
  68. (with-test-prefix "fxeven?"
  69. (pass-if "fxeven? is #t for even fixnums" (fxeven? 2))
  70. (pass-if "fxeven? is #f for odd fixnums" (not (fxeven? 1))))
  71. (with-test-prefix "fxmax" (pass-if "simple" (fx=? (fxmax 1 3 2) 3)))
  72. (with-test-prefix "fxmin" (pass-if "simple" (fx=? (fxmin -1 0 2) -1)))
  73. (with-test-prefix "fx+"
  74. (pass-if "simple" (fx=? (fx+ 1 2) 3))
  75. (pass-if "&implementation-restriction on non-fixnum result"
  76. (guard (condition ((implementation-restriction-violation? condition) #t)
  77. (else #f))
  78. (begin (fx+ (greatest-fixnum) 1) #f))))
  79. (with-test-prefix "fx*"
  80. (pass-if "simple" (fx=? (fx* 2 3) 6))
  81. (pass-if "&implementation-restriction on non-fixnum result"
  82. (guard (condition ((implementation-restriction-violation? condition) #t)
  83. (else #f))
  84. (begin (fx* (greatest-fixnum) 2) #f))))
  85. (with-test-prefix "fx-"
  86. (pass-if "unary fx- negates argument" (fx=? (fx- 1) -1))
  87. (pass-if "simple" (fx=? (fx- 3 2) 1))
  88. (pass-if "&assertion on non-fixnum result"
  89. (guard (condition ((assertion-violation? condition) #t) (else #f))
  90. (fx- (least-fixnum) 1))))
  91. (with-test-prefix "fxdiv-and-mod"
  92. (pass-if "simple"
  93. (call-with-values (lambda () (fxdiv-and-mod 123 10))
  94. (lambda (d m)
  95. (and (fx=? d 12) (fx=? m 3))))))
  96. (with-test-prefix "fxdiv" (pass-if "simple" (fx=? (fxdiv -123 10) -13)))
  97. (with-test-prefix "fxmod" (pass-if "simple" (fx=? (fxmod -123 10) 7)))
  98. (with-test-prefix "fxdiv0-and-mod0"
  99. (pass-if "simple"
  100. (call-with-values (lambda () (fxdiv0-and-mod0 -123 10))
  101. (lambda (d m)
  102. (and (fx=? d -12) (fx=? m -3))))))
  103. (with-test-prefix "fxdiv0" (pass-if "simple" (fx=? (fxdiv0 -123 10) -12)))
  104. (with-test-prefix "fxmod0" (pass-if "simple" (fx=? (fxmod0 -123 10) -3)))
  105. ;; Without working div and mod implementations and without any example results
  106. ;; from the spec, I have no idea what the results of these functions should
  107. ;; be. -juliang
  108. ;; UPDATE: div and mod implementations are now working properly -mhw
  109. (with-test-prefix "fx+/carry" (pass-if "simple" (throw 'unresolved)))
  110. (with-test-prefix "fx-/carry" (pass-if "simple" (throw 'unresolved)))
  111. (with-test-prefix "fx*/carry" (pass-if "simple" (throw 'unresolved)))
  112. (with-test-prefix "fxnot" (pass-if "simple" (fx=? (fxnot 3) -4)))
  113. (with-test-prefix "fxand" (pass-if "simple" (fx=? (fxand 5 6) 4)))
  114. (with-test-prefix "fxior" (pass-if "simple" (fx=? (fxior 2 4) 6)))
  115. (with-test-prefix "fxxor" (pass-if "simple" (fx=? (fxxor 5 4) 1)))
  116. (with-test-prefix "fxif" (pass-if "simple" (fx=? (fxif 5 3 4) 1)))
  117. (with-test-prefix "fxbit-count"
  118. (pass-if "simple" (fx=? (fxbit-count 5) 2))
  119. (pass-if "negative" (fx=? (fxbit-count -5) -2)))
  120. (with-test-prefix "fxlength" (pass-if "simple" (fx=? (fxlength 5) 3)))
  121. (with-test-prefix "fxfirst-bit-set"
  122. (pass-if "simple"
  123. (and (eqv? (fxfirst-bit-set 1) 0)
  124. (eqv? (fxfirst-bit-set -4) 2)))
  125. (pass-if "fxfirst-bit-set is -1 on zero"
  126. (and (eqv? (fxfirst-bit-set 0) -1))))
  127. (with-test-prefix "fxbit-set?"
  128. (pass-if "fxbit-set? is #t on index of set bit" (fxbit-set? 5 2))
  129. (pass-if "fxbit-set? is #f on index of unset bit" (not (fxbit-set? 5 1))))
  130. (with-test-prefix "fxcopy-bit" (pass-if "simple" (fx=? (fxcopy-bit 2 2 1) 6)))
  131. (with-test-prefix "fxbit-field"
  132. (pass-if "simple" (fx=? (fxbit-field 50 1 4) 1)))
  133. (with-test-prefix "fxcopy-bit-field"
  134. (pass-if "simple" (fx=? (fxcopy-bit-field 255 2 6 10) 235)))
  135. (with-test-prefix "fxarithmetic-shift"
  136. (pass-if "simple"
  137. (and (fx=? (fxarithmetic-shift -6 -1) -3)
  138. (fx=? (fxarithmetic-shift -5 -1) -3)
  139. (fx=? (fxarithmetic-shift -4 -1) -2)
  140. (fx=? (fxarithmetic-shift -3 -1) -2)
  141. (fx=? (fxarithmetic-shift -2 -1) -1)
  142. (fx=? (fxarithmetic-shift -1 -1) -1))))
  143. (with-test-prefix "fxarithmetic-shift-left"
  144. (pass-if "simple" (fx=? (fxarithmetic-shift-left -6 1) -12)))
  145. (with-test-prefix "fxarithmetic-shift-right"
  146. (pass-if "simple" (fx=? (fxarithmetic-shift-right -6 1) -3)))
  147. (with-test-prefix "fxrotate-bit-field"
  148. (pass-if "simple" (fx=? (fxrotate-bit-field 227 2 6 2) 203)))
  149. (with-test-prefix "fxreverse-bit-field"
  150. (pass-if "simple" (fx=? (fxreverse-bit-field 82 1 4) 88)))