bitwise-tests.scm 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees
  3. ; Lost: (ARITHMETIC-SHIFT 5 27) => -402653184 [wanted 671088640.]
  4. ; Lost: (ARITHMETIC-SHIFT 5 28) => 268435456 [wanted 1342177280.]
  5. (define (testit name proc x y z)
  6. (let ((result (proc x y)))
  7. (if (not (= result z))
  8. (begin (display "Lost: ")
  9. (write `(,name ,x ,y))
  10. (display " => ")
  11. (write result)
  12. (display " [wanted ")
  13. (write z)
  14. (display "]")
  15. (newline)))))
  16. (define most-positive-fixnum
  17. (let ((n (arithmetic-shift 2 27))) (+ n (- n 1))))
  18. (define (test-left-shifts x)
  19. (let ((crossover (arithmetic-shift 2 27)))
  20. (do ((y 0 (+ y 1))
  21. (z x (* z (if (>= z crossover) 2. 2))))
  22. ((= y 34))
  23. (testit 'arithmetic-shift arithmetic-shift x y z))))
  24. (test-left-shifts 5)
  25. (test-left-shifts -5)
  26. (define (test-right-shifts x)
  27. (do ((y 0 (- y 1))
  28. (z x (quotient z 2)))
  29. ((= y -34))
  30. (testit 'arithmetic-shift arithmetic-shift x y z)))
  31. (test-right-shifts (* 5 (expt 2 36)))
  32. (test-right-shifts (* -5 (expt 2 36)))
  33. (define (bit1? x)
  34. (if (< x 0)
  35. (even? (quotient (- -1 x) 2))
  36. (odd? (quotient x 2))))
  37. (define (try-truth-table name proc predicate)
  38. (do ((x -4 (+ x 1)))
  39. ((= x 4))
  40. (do ((y -4 (+ y 1)))
  41. ((= y 4))
  42. (testit name proc x y
  43. (+ (if (predicate (odd? x) (odd? y)) 1 0)
  44. (if (predicate (bit1? x) (bit1? y)) 2 0)
  45. (if (predicate (negative? x) (negative? y)) -4 0))))))
  46. (try-truth-table 'bitwise-and bitwise-and (lambda (a b) (and a b)))
  47. (try-truth-table 'bitwise-ior bitwise-ior (lambda (a b) (or a b)))