shithub: femtolisp

Download patch

ref: 4379daad09cb2fb5866c6eed2a5f226bcce56816
parent: ced312368fdb879811e25a807fb00782c1f25a45
author: mag <mag-one@autistici.org>
date: Wed Jun 7 19:29:32 EDT 2023

testing number boundaries

--- /dev/null
+++ b/test/number-boundaries.lsp
@@ -1,0 +1,59 @@
+
+; 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
+