ref: d75f265a0a025a22be6a15612971144b1b3d44a3
dir: /test/number-boundaries.lsp/
; NUMBER BOUNDARIES ------------------------------------------------------------
(define-macro (half-max-signed numtype)
(list 'ash (list numtype 1)
(list '- (list '* 8 (list 'sizeof (list 'quote numtype))) 2)))
(define-macro (high-border-signed numtype)
(list '+ (list '- (list 'half-max-signed numtype) 1)
(list 'half-max-signed numtype)))
(define-macro (low-border-signed numtype)
(list '- -1 (list 'high-border-signed numtype)))
(define-macro (low-border numtype)
(list 'if (list '< (list numtype -1) 1)
(list 'low-border-signed numtype)
(list numtype 0)))
(define-macro (high-border numtype)
(list 'lognot (list 'low-border numtype)))
;(list numtype (list 'lognot (list 'low-border numtype))))
(define-macro (number-borders numtype)
(list 'cons (list 'low-border numtype)
(list 'high-border numtype)))
; TESTS ------------------------------------------------------------------------
(princ "---\n")
(princ "int8 : " (number-borders int8) "\n")
(princ "int16 : " (number-borders int16) "\n")
(princ "int32 : " (number-borders int32) "\n")
(princ "int64 : " (number-borders int64) "\n")
(princ "uint8 : " (number-borders uint8) "\n")
(princ "uint16 : " (number-borders uint16) "\n")
(princ "uint32 : " (number-borders uint32) "\n")
(princ "uint64 : " (number-borders uint64) "\n")
(princ "---\n")
(assert (= 128 (+ (high-border int8) 1)))
(assert (= -129 (- (low-border int8) 1)))
(assert (= 32768 (+ (high-border int16) 1)))
(assert (= -32769 (- (low-border int16) 1)))
(assert (= 2147483648 (+ (high-border int32) 1)))
(assert (= -2147483649 (- (low-border int32) 1)))
(assert (= 9223372036854775808 (+ (high-border int64) 1)))
;(assert (= -9223372036854775809 (- (low-border int64) 1))) ;OVERFLOW
(assert (= 256 (+ (high-border uint8) 1)))
(assert (= -1 (- (low-border uint8) 1)))
(assert (= 65536 (+ (high-border uint16) 1)))
(assert (= -1 (- (low-border uint16) 1)))
(assert (= 4294967296 (+ (high-border uint32) 1)))
(assert (= -1 (- (low-border uint32) 1)))
;(assert (= 18446744073709551616 (+ (high-border uint64) 1))) ;OVERFLOW
(assert (= -1 (- (low-border uint64) 1)))
(princ "all tests pass\n\n")
#t