Modify suite inttypes (#1898)

* Stop using abbreviations

* Put parser tests first

* Fix incorrect code

* Deal with linting

* Change some comments and labels

* Improve test

* Use syntax literals for int/u64 and int/s64

---------

Co-authored-by: sogaiu <983021772@users.noreply.github.com>
This commit is contained in:
sogaiu
2026-10-03 13:39:30 -05:00
committed by GitHub
co-authored by sogaiu
parent fe0e19804e
commit 6e8bb2df15
+121 -124
View File
@@ -21,121 +21,119 @@
(import ./helper :prefix "" :exit true)
(start-suite)
# Disable linting warnings
(setdyn *lint-warn* :none)
# New parser
(assert (= 123:u (int/u64 "123")) "int/u64 parsing 1")
(assert (= 0:u (int/u64 "0")) "int/u64 parsing 2")
(assert (= 0xFFFF_FFFF_FFFF_FFFF:u (int/u64 "0xFFFF_FFFF_FFFF_FFFF")) "int/u64 parsing 3")
# some tests for bigint
(assert (= 123:s (int/s64 "123")) "int/s64 parsing 1")
(assert (= -123:s (int/s64 "-123")) "int/s64 parsing 2")
(assert (= 0:s (int/s64 "0")) "int/s64 parsing 3")
# some tests for inttypes
# 319575c
(def i64 int/s64)
(def u64 int/u64)
(assert-no-error
"create some uint64 bigints"
"int/u64 creation"
(do
# from number
(def a (u64 10))
(def _a 10:u)
(def _b 0x1f_ffff_ffff_ffff:u)
# max double we can convert to int (2^53)
(def b (u64 0x1fffffffffffff))
(def b (u64 (math/pow 2 53)))
(def _c (int/u64 (math/pow 2 53)))
# from string
(def c (u64 "0xffff_ffff_ffff_ffff"))
(def c (u64 "32rvv_vv_vv_vv"))
(def d (u64 "123456789"))))
(def _d (int/u64 "0xffff_ffff_ffff_ffff"))
(def _e (int/u64 "32rvv_vv_vv_vv"))
(def _f (int/u64 "123456789"))))
# Conversion back to an int32
# 88db9751d
(assert (= (int/to-number (u64 0xFaFa)) 0xFaFa))
(assert (= (int/to-number (i64 0xFaFa)) 0xFaFa))
(assert (= (int/to-number (u64 9007199254740991)) 9007199254740991))
(assert (= (int/to-number (i64 9007199254740991)) 9007199254740991))
(assert (= (int/to-number (i64 -9007199254740991)) -9007199254740991))
# New parser
(assert (= (u64 "123") 123:u) "u64 parsing")
(assert (= (u64 "0") 0:u) "u64 parsing")
(assert (= (u64 "0xFFFF_FFFF_FFFF_FFFF") 0xFFFF_FFFF_FFFF_FFFF:u) "u64 parsing")
(assert (= (i64 "123") 123:s) "s64 parsing")
(assert (= (i64 "-123") -123:s) "s64 parsing")
(assert (= (i64 "0") 0:s) "s64 parsing")
(assert (= (int/to-number 0xFaFa:u) 0xFaFa) "int/to-number 1")
(assert (= (int/to-number 0xFaFa:s) 0xFaFa) "int/to-number 2")
(assert (= (int/to-number 9007199254740991:u) 9007199254740991) "int/to-number 3")
(assert (= (int/to-number 9007199254740991:s) 9007199254740991) "int/to-number 4")
(assert (= (int/to-number -9007199254740991:s) -9007199254740991) "int/to-number 5")
(assert-error
"u64 out of bounds for safe integer"
(int/to-number (u64 "9007199254740993"))
"int/u64 out of bounds for safe integer"
(int/to-number 9007199254740993:u))
(assert-error
"s64 out of bounds for safe integer"
(int/to-number (i64 "-9007199254740993"))))
(assert-error
"int/s64 out of bounds for safe integer"
(int/to-number -9007199254740993:s))
(assert-error
"int/to-number fails on non-abstract types"
(int/to-number 1))
(assert-no-error
"create some int64 bigints"
"int/s64 creation"
(do
# from number
(def a (i64 -10))
(def _a -10:s)
(def _b 0x1f_ffff_ffff_ffff:s)
# max double we can convert to int (2^53)
(def b (i64 0x1fffffffffffff))
(def b (i64 (math/pow 2 53)))
(def _c (int/s64 (math/pow 2 53)))
# from string
(def c (i64 "0x7fff_ffff_ffff_ffff"))
(def d (i64 "123456789"))))
(def _d (int/s64 "0x7fff_ffff_ffff_ffff"))
(def _e (int/s64 "123456789"))))
(assert-error
"bad initializers"
(do
# double to big to be converted to uint64 without truncation (2^53 + 1)
(def b (u64 (+ 0xffff_ffff_ffff_ff 1)))
(def b (u64 (+ (math/pow 2 53) 1)))
# double too big (> 2^53) to be converted to uint64 without truncation
(def _a (int/u64 (+ 0xffff_ffff_ffff_ff 1)))
(def _b (int/u64 (+ (math/pow 2 53) 1)))
# out of range 65 bits
(def c (u64 "0x1ffffffffffffffff"))
(def _c (int/u64 "0x1ffffffffffffffff"))
# just to big
(def d (u64 "123456789123456789123456789"))))
(def _d (int/u64 "123456789123456789123456789"))))
(assert (= (:/ (u64 "0xffff_ffff_ffff_ffff") 8 2) (u64 "0xfffffffffffffff"))
"bigint operations 1")
(assert (let [a (u64 0xff)] (= (:+ a a a a) (:* a 2 2)))
"bigint operations 2")
(assert (= (:/ 0xffff_ffff_ffff_ffff:u 8 2) 0xfff_ffff_ffff_ffff:u)
"inttype operations 1")
(assert (let [a 0xff:u] (= (:+ a a a a) (:* a 2 2)))
"inttype operations 2")
# 5ae520a2c
(assert (= (string (i64 -123)) "-123") "i64 prints reasonably")
(assert (= (string (u64 123)) "123") "u64 prints reasonably")
(assert (= (string -123:s) "-123") "int/s64 prints reasonably")
(assert (= (string 123:u) "123") "int/u64 prints reasonably")
# 1db6d0e0b
(assert-error
"trap INT64_MIN / -1"
(:/ (int/s64 "-0x8000_0000_0000_0000") -1))
(:/ -0x8000_0000_0000_0000:s -1))
# int/s64 and int/u64 serialization
# 6aea7c7f7
(assert (deep= (int/to-bytes (u64 0)) @"\x00\x00\x00\x00\x00\x00\x00\x00"))
(assert (deep= (int/to-bytes (i64 1) :le)
@"\x01\x00\x00\x00\x00\x00\x00\x00"))
(assert (deep= (int/to-bytes (i64 1) :be)
@"\x00\x00\x00\x00\x00\x00\x00\x01"))
(assert (deep= (int/to-bytes (i64 -1))
@"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"))
(assert (deep= (int/to-bytes (i64 -5) :be)
@"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFB"))
(assert (deep= (int/to-bytes (u64 1) :le)
@"\x01\x00\x00\x00\x00\x00\x00\x00"))
(assert (deep= (int/to-bytes (u64 1) :be)
@"\x00\x00\x00\x00\x00\x00\x00\x01"))
(assert (deep= (int/to-bytes (u64 300) :be)
@"\x00\x00\x00\x00\x00\x00\x01\x2C"))
(assert (deep= (int/to-bytes 0:u) @"\x00\x00\x00\x00\x00\x00\x00\x00")
"int/to-bytes 1")
(assert (deep= (int/to-bytes 1:s :le) @"\x01\x00\x00\x00\x00\x00\x00\x00")
"int/to-bytes 2")
(assert (deep= (int/to-bytes 1:s :be) @"\x00\x00\x00\x00\x00\x00\x00\x01")
"int/to-bytes 3")
(assert (deep= (int/to-bytes -1:s) @"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF")
"int/to-bytes 4")
(assert (deep= (int/to-bytes -5:s :be) @"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFB")
"int/to-bytes 5")
(assert (deep= (int/to-bytes 1:u :le) @"\x01\x00\x00\x00\x00\x00\x00\x00")
"int/to-bytes 6")
(assert (deep= (int/to-bytes 1:u :be) @"\x00\x00\x00\x00\x00\x00\x00\x01")
"int/to-bytes 7")
(assert (deep= (int/to-bytes 300:u :be) @"\x00\x00\x00\x00\x00\x00\x01\x2C")
"int/to-bytes 8")
# int/s64 int/u64 to existing buffer
# bbb3e16fd
(let [buf1 @""
buf2 @"abcd"]
(assert (deep= (int/to-bytes (i64 1) :le buf1)
@"\x01\x00\x00\x00\x00\x00\x00\x00"))
(assert (deep= buf1 @"\x01\x00\x00\x00\x00\x00\x00\x00"))
(assert (deep= (int/to-bytes (u64 300) :be buf2)
@"abcd\x00\x00\x00\x00\x00\x00\x01\x2C")))
(assert (deep= (int/to-bytes 1:s :le buf1) @"\x01\x00\x00\x00\x00\x00\x00\x00")
"int/to-bytes to buf1")
(assert (deep= buf1 @"\x01\x00\x00\x00\x00\x00\x00\x00")
"buf1 result")
(assert (deep= (int/to-bytes 300:u :be buf2) @"abcd\x00\x00\x00\x00\x00\x00\x01\x2C")
"int/to-bytes to buf2")
(assert (deep= buf2 @"abcd\x00\x00\x00\x00\x00\x00\x01\x2C")
"buf2 result"))
# int/s64 and int/u64 parameter type checking
# 6aea7c7f7
@@ -146,40 +144,39 @@
# 6aea7c7f7
(assert-error
"invalid endianness passed to int/to-bytes"
(int/to-bytes (u64 0) :little))
(int/to-bytes 0:u :little))
# bbb3e16fd
(assert-error
"invalid buffer passed to int/to-bytes"
(int/to-bytes (u64 0) :little :buffer))
(int/to-bytes 0:u :le :buffer))
# Right hand operators
# 4fe005e3c
(assert (= (int/s64 (sum (range 10))) (sum (map int/s64 (range 10))))
"right hand operators 1")
(assert (= (int/s64
(product (range 1 10))) (product (map int/s64 (range 1 10))))
(assert (= (int/s64 (product (range 1 10))) (product (map int/s64 (range 1 10))))
"right hand operators 2")
(assert (= (int/s64 15) (bor 10 (int/s64 5)) (bor (int/s64 10) 5))
(assert (= 15:s (bor 10 5:s) (bor 10:s 5))
"right hand operators 3")
# Integer type checks
# 11067d7a5
(assert (compare= 0 (- (int/u64 "1000") 1000)) "subtract from int/u64")
(assert (compare= 0 (- 1000:u 1000)) "subtract from int/u64")
(assert (odd? (int/u64 "1001")) "odd? 1")
(assert (not (odd? (int/u64 "1000"))) "odd? 2")
(assert (odd? (int/s64 "1001")) "odd? 3")
(assert (not (odd? (int/s64 "1000"))) "odd? 4")
(assert (odd? (int/s64 "-1001")) "odd? 5")
(assert (not (odd? (int/s64 "-1000"))) "odd? 6")
(assert (odd? 1001:u) "odd? 1")
(assert (not (odd? 1000:u)) "odd? 2")
(assert (odd? 1001:s) "odd? 3")
(assert (not (odd? 1000:s)) "odd? 4")
(assert (odd? -1001:s) "odd? 5")
(assert (not (odd? -1000:s)) "odd? 6")
(assert (even? (int/u64 "1000")) "even? 1")
(assert (not (even? (int/u64 "1001"))) "even? 2")
(assert (even? (int/s64 "1000")) "even? 3")
(assert (not (even? (int/s64 "1001"))) "even? 4")
(assert (even? (int/s64 "-1000")) "even? 5")
(assert (not (even? (int/s64 "-1001"))) "even? 6")
(assert (even? 1000:u) "even? 1")
(assert (not (even? 1001:u)) "even? 2")
(assert (even? 1000:s) "even? 3")
(assert (not (even? 1001:s)) "even? 4")
(assert (even? -1000:s) "even? 5")
(assert (not (even? -1001:s)) "even? 6")
# integer type operations
(defn opcheck [int x y]
@@ -217,16 +214,16 @@
(assert (compare= (bnot x) (bnot (int/s64 x))) "int/s64 bnot"))
(loop [x :range [0 10]]
(assert (= (int/u64 "0xFFFF_FFFF_FFFF_FFFF")
(assert (= 0xFFFF_FFFF_FFFF_FFFF:u
(bxor (int/u64 x) (bnot (int/u64 x))))
"int/u64 bnot"))
# Check for issue #1130
# 7e65c2bda
(var d (int/s64 7))
(var d 7:s)
(mod 0 d)
(var d (int/s64 7))
(var d 7:s)
(def result (seq [n :in (range -21 0)] (mod n d)))
(assert (deep= result
(map int/s64 @[0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6]))
@@ -240,13 +237,13 @@
INF (/ 1 0)
MINUS_INF (/ -1 0)
compare-poly-tests
[[(int/s64 3) (int/u64 3) 0]
[(int/s64 -3) (int/u64 3) -1]
[(int/s64 3) (int/u64 2) 1]
[(int/s64 3) 3 0] [(int/s64 3) 4 -1] [(int/s64 3) -9 1]
[(int/u64 3) 3 0] [(int/u64 3) 4 -1] [(int/u64 3) -9 1]
[3 (int/s64 3) 0] [3 (int/s64 4) -1] [3 (int/s64 -5) 1]
[3 (int/u64 3) 0] [3 (int/u64 4) -1] [3 (int/u64 2) 1]
[[3:s 3:u 0]
[-3:s 3:u -1]
[3:s 2:u 1]
[3:s 3 0] [3:s 4 -1] [3:s -9 1]
[3:u 3 0] [3:u 4 -1] [3:u -9 1]
[3 3:s 0] [3 4:s -1] [3 -5:s 1]
[3 3:u 0] [3 4:u -1] [3 2:u 1]
[(int/s64 MAX_INT_64_STRING) (int/u64 MAX_UINT_64_STRING) -1]
[(int/s64 MAX_INT_IN_DBL_STRING)
(scan-number MAX_INT_IN_DBL_STRING) 0]
@@ -254,46 +251,46 @@
(scan-number MAX_INT_IN_DBL_STRING) 0]
[(+ 1 (int/u64 MAX_INT_IN_DBL_STRING))
(scan-number MAX_INT_IN_DBL_STRING) 1]
[(int/s64 0) INF -1] [(int/u64 0) INF -1]
[MINUS_INF (int/u64 0) -1] [MINUS_INF (int/s64 0) -1]
[(int/s64 1) NAN 0] [NAN (int/u64 1) 0]]]
[0:s INF -1] [0:u INF -1]
[MINUS_INF 0:u -1] [MINUS_INF 0:s -1]
[1:s NAN 0] [NAN 1:u 0]]]
(each [x y c] compare-poly-tests
(assert (= c (compare x y))
(string/format "compare polymorphic %q %q %d" x y c))))
# marshal
(def m1 (u64 3141592654))
(def m1 3141592654:u)
(def m2 (unmarshal (marshal m1)))
(assert (= m1 m2) "marshal/unmarshal")
# compare u64/u64
(assert (= (compare (u64 1) (u64 2)) -1) "compare 1")
(assert (= (compare (u64 1) (u64 1)) 0) "compare 2")
(assert (= (compare (u64 2) (u64 1)) +1) "compare 3")
# compare int/u64 int/u64
(assert (= (compare 1:u 2:u) -1) "compare 1")
(assert (= (compare 1:u 1:u) 0) "compare 2")
(assert (= (compare 2:u 1:u) +1) "compare 3")
# compare i64/i64
(assert (= (compare (i64 -1) (i64 +1)) -1) "compare 4")
(assert (= (compare (i64 +1) (i64 +1)) 0) "compare 5")
(assert (= (compare (i64 +1) (i64 -1)) +1) "compare 6")
# compare int/s64 int/s64
(assert (= (compare -1:s +1:s) -1) "compare 4")
(assert (= (compare +1:s +1:s) 0) "compare 5")
(assert (= (compare +1:s -1:s) +1) "compare 6")
# compare u64/i64
(assert (= (compare (u64 1) (i64 2)) -1) "compare 7")
(assert (= (compare (u64 1) (i64 -1)) +1) "compare 8")
(assert (= (compare (u64 0) (i64 -1)) +1) "compare 9")
# compare int/u64 int/s64
(assert (= (compare 1:u 2:s) -1) "compare 7")
(assert (= (compare 1:u -1:s) +1) "compare 8")
(assert (= (compare 0:u -1:s) +1) "compare 9")
# compare i64/u64
(assert (= (compare (i64 1) (u64 2)) -1) "compare 10")
(assert (= (compare (i64 -1) (u64 1)) -1) "compare 11")
(assert (= (compare (i64 -1) (u64 0)) -1) "compare 12")
# compare int/s64 int/u64
(assert (= (compare 1:s 2:u) -1) "compare 10")
(assert (= (compare -1:s 1:u) -1) "compare 11")
(assert (= (compare -1:s 0:u) -1) "compare 12")
# off by 1 error in inttypes
# a3e812b86
(assert (= (int/s64 "-0x8000_0000_0000_0000")
(+ (int/s64 "0x7FFF_FFFF_FFFF_FFFF") 1)) "int types wrap around")
(assert (= (int/s64 "0x7FFF_FFFF_FFFF_FFFF")
(- (int/s64 "-0x8000_0000_0000_0000") 1)) "int types wrap around")
(assert (= -0x8000_0000_0000_0000:s
(+ 0x7FFF_FFFF_FFFF_FFFF:s 1)) "int types wrap around")
(assert (= 0x7FFF_FFFF_FFFF_FFFF:s
(- -0x8000_0000_0000_0000:s 1)) "int types wrap around")
# Issue #1217
(assert (= (- (int/u64 "0xFFFFFFFF") 1) (int/u64 "0xFFFFFFFE")) "u64 subtract")
(assert (= (- 0xFFFF_FFFF:u 1) 0xFFFF_FFFE:u) "int/u64 subtract")
(end-suite)