bitwise-tests.scm 1.6 KB

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