///|
test "creating bigint values" {
// From integer literals with 'N' suffix
let big1 = 12345678901234567890N
inspect(big1, content="12345678901234567890")
// From regular integers
let big2 = @bigint.BigInt::from_int(42)
inspect(big2, content="42")
// From Int64 values
let big3 = @bigint.BigInt::from_int64(9223372036854775807L)
inspect(big3, content="9223372036854775807")
// From strings
let big4 = @bigint.BigInt::from_string("123456789012345678901234567890")
inspect(big4, content="123456789012345678901234567890")
// From hexadecimal strings
let big5 = @bigint.BigInt::from_string("1a2b3c4d5e6f", radix=16)
inspect(big5, content="28772997619311")
}///|
test "arithmetic operations" {
let a = 123456789012345678901234567890N
let b = 987654321098765432109876543210N
// Addition
let sum = a + b
inspect(sum, content="1111111110111111111011111111100")
// Subtraction
let diff = b - a
inspect(diff, content="864197532086419753208641975320")
// Multiplication
let product = @bigint.BigInt::from_int(123) * @bigint.BigInt::from_int(456)
inspect(product, content="56088")
// Division
let quotient = @bigint.BigInt::from_int(1000) / @bigint.BigInt::from_int(7)
inspect(quotient, content="142")
// Modulo
let remainder = @bigint.BigInt::from_int(1000) % @bigint.BigInt::from_int(7)
inspect(remainder, content="6")
// Negation
let neg = -a
inspect(neg, content="-123456789012345678901234567890")
}///|
test "comparisons" {
let big = 12345N
let small = 123N
// BigInt to BigInt comparison
inspect(big > small, content="true")
inspect(big == small, content="false")
inspect(small < big, content="true")
// BigInt to Int comparison
inspect(big.equal_int(12345), content="true")
inspect(big.compare_int(12345), content="0")
inspect(big.compare_int(1000), content="1") // greater than
inspect(small.compare_int(200), content="-1") // less than
// BigInt to Int64 comparison
let big64 = @bigint.BigInt::from_int64(9223372036854775807L)
inspect(big64.equal_int64(9223372036854775807L), content="true")
}///|
test "bitwise operations" {
let a = 0b11110000N // 240 in decimal
let b = 0b10101010N // 170 in decimal
// Bitwise AND
let and_result = a & b
inspect(and_result, content="160") // 0b10100000
// Bitwise OR
let or_result = a | b
inspect(or_result, content="250") // 0b11111010
// Bitwise XOR
let xor_result = a ^ b
inspect(xor_result, content="90") // 0b01011010
// Bit length
let big_num = 255N
inspect(big_num.bit_length(), content="8")
// Count trailing zeros
let with_zeros = 1000N // Has trailing zeros in binary
let ctz = with_zeros.ctz()
inspect(ctz >= 0, content="true")
}///|
test "power operations" {
// Basic power
let base = 2N
let exponent = 10N
let power = base.pow(exponent)
inspect(power, content="1024")
// Modular exponentiation (useful for cryptography)
let base2 = 3N
let exp2 = 5N
let modulus = 7N
let mod_power = base2.pow(exp2, modulus~)
inspect(mod_power, content="5") // (3^5) % 7 = 243 % 7 = 5
// Large modular exponentiation (optimized for speed)
let large_base = 123N
let large_exp = 20N
let large_mod = 1000007N
let result = large_base.pow(large_exp, modulus=large_mod)
inspect(result, content="378446") // (123^20) % 1000007
}///|
test "string conversions" {
let big = 255N
// Decimal string
let decimal = big.to_string()
inspect(decimal, content="255")
// Hexadecimal (lowercase)
let hex = big.to_string(radix=16)
inspect(hex, content="ff")
// Parse from hex (radix=16)
let from_radix16 = @bigint.BigInt::from_string("deadbeef", radix=16)
inspect(from_radix16, content="3735928559")
// Round-trip conversion
let original = 98765432109876543210N
let as_string = original.to_string()
let parsed_back = @bigint.BigInt::from_string(as_string)
inspect(original == parsed_back, content="true")
}///|
test "byte conversions" {
let big = 0x123456789abcdefN
// Convert to bytes
let bytes = big.to_octets()
inspect(bytes.length() > 0, content="true")
// Convert from bytes (positive number)
let from_bytes = @bigint.BigInt::from_octets(bytes)
inspect(from_bytes == big, content="true")
// Convert with specific length
let fixed_length = @bigint.BigInt::from_int(255).to_octets(length=4)
inspect(fixed_length.length(), content="4")
// Negative numbers
// let negative = -big
// let neg_bytes = negative.to_octets()
// to_octets does not accept negative numbers
// let neg_from_bytes = @bigint.BigInt::from_octets(neg_bytes, signum=-1)
// inspect(neg_from_bytes == negative, content="true")
}///|
test "type conversions" {
let big = 12345N
// To Int (truncates if too large)
let as_int = big.to_int()
inspect(as_int, content="12345")
// To Int64
let as_int64 = big.to_int64()
inspect(as_int64, content="12345")
// To UInt
let as_uint = big.to_uint()
inspect(as_uint, content="12345")
// To smaller types
let small = 255N
let as_int16 = small.to_int16()
inspect(as_int16, content="255")
let as_uint16 = small.to_uint16()
inspect(as_uint16, content="255")
}///|
test "json serialization" {
let big = 12345678901234567890N
// Convert to JSON (as string to preserve precision)
let json = big.to_json()
@debug.debug_inspect(json, content="String(\"12345678901234567890\")")
// Large numbers that exceed JavaScript's safe integer range
let very_big = @bigint.BigInt::from_string("123456789012345678901234567890")
let big_json = very_big.to_json()
@debug.debug_inspect(
big_json,
content="String(\"123456789012345678901234567890\")",
)
}///|
test "utility functions" {
let zero = 0N
let positive = 42N
let negative = -42N
// Check if zero
inspect(zero.is_zero(), content="true")
inspect(positive.is_zero(), content="false")
// Sign testing through comparison
inspect(positive > zero, content="true")
inspect(negative < zero, content="true")
inspect(zero == zero, content="true")
}type BigInttest {
let a = 9223372036854775807N // Max value of Int64
let b = 1N
inspect(a + b, content="9223372036854775808") // Beyond Int64 range
inspect(-a + -b, content="-9223372036854775808")
}test {
let a = @bigint.BigInt::from_string("42") // 0b101010
let b = @bigint.BigInt::from_string("-12") // ~0b1011 + 1
inspect(a & b, content="32") // 0b100000
let a = @bigint.BigInt::from_string("-8") // ~0b111 + 1
let b = @bigint.BigInt::from_string("-4") // ~0b11 + 1
inspect(a & b, content="-8") // ~0b1011 + 1
}test {
let a = @bigint.BigInt::from_string("42")
let b = @bigint.BigInt::from_string("-12")
inspect(a | b, content="-2")
let c = @bigint.BigInt::from_string("-8")
let d = @bigint.BigInt::from_string("-4")
inspect(c | d, content="-4")
}test {
let a = @bigint.BigInt::from_string("42")
let b = @bigint.BigInt::from_string("-7")
inspect(a ^ b, content="-45")
let a = @bigint.BigInt::from_string("42")
inspect(a ^ a, content="0") // XOR with self gives 0
}test {
let a = @bigint.BigInt::from_string("42")
let b = @bigint.BigInt::from_string("24")
let c = @bigint.BigInt::from_string("-42")
inspect(a.compare(b), content="1") // 42 > 24
inspect(b.compare(a), content="-1") // 24 < 42
inspect(c.compare(a), content="-1") // -42 < 42
inspect(a.compare(a), content="0") // 42 = 42
}test {
let a = @bigint.BigInt::from_string("100")
let b = @bigint.BigInt::from_string("20")
inspect(a / b, content="5")
inspect(-a / b, content="-5")
inspect(a / -b, content="-5")
inspect(-a / -b, content="5")
}test {
let a = 123456789N
let b = 123456789N
let c = -123456789N
inspect(a == b, content="true")
inspect(a == c, content="false")
}test {
let hasher = Hasher(seed=0)
let big = 12345N
hasher.combine(big)
inspect(hasher.finalize(), content="890579181")
}test {
let a = 42N
let b = 5N
inspect(a % b, content="2")
let c = -42N
let d = -5N
inspect(c % d, content="-2")
}test {
let a = 12345678901234567890N
let b = -98765432109876543210N
inspect(a * b, content="-1219326311370217952237463801111263526900")
inspect(a * 0N, content="0")
}test {
inspect(-42N, content="-42")
inspect(-(-42N), content="42")
inspect(-0N, content="0")
}test {
let x = 5N
inspect(x << 2, content="20")
let y = -5N
inspect(y << 2, content="-20")
}test {
let n = @bigint.BigInt::from_string("1024")
inspect(n >> 3, content="128")
let neg = @bigint.BigInt::from_string("-1024")
inspect(neg >> 3, content="-128")
}test {
let a = 12345678901234567890N
let b = 9876543210987654321N
inspect(a - b, content="2469135690246913569")
inspect(-a - b, content="-22222222112222222211")
}test {
let big = 12345678901234567890N
let json = big.to_json()
@debug.debug_inspect(
json,
content=(
#|String("12345678901234567890")
),
)
}test {
let pos = 16N // 10000
inspect(pos.bit_length(), content="5")
let neg = -16N //
inspect(neg.bit_length(), content="4")
let zero = 0N
inspect(zero.bit_length(), content="0")
}test {
let big = 42N
inspect(big.compare_int(24), content="1") // 42 > 24
inspect(big.compare_int(42), content="0") // 42 = 42
inspect(big.compare_int(100), content="-1") // 42 < 100
}test {
let big = 42N
inspect(big.compare_int64(24L), content="1") // 42 > 24
inspect(big.compare_int64(42L), content="0") // 42 = 42
inspect(big.compare_int64(100L), content="-1") // 42 < 100
}test {
inspect(8N.ctz(), content="3") // 0b1000
inspect(12N.ctz(), content="2") // 0b1100
inspect(0N.ctz(), content="0")
}test {
let big = 42N
inspect(big.equal_int(42), content="true")
inspect(big.equal_int(41), content="false")
let large = 9223372036854775808N // Beyond Int64 range
inspect(large.equal_int(42), content="false")
}test {
let big = @bigint.BigInt::from_int64(9223372036854775807L) // Int64 max value
inspect(big.equal_int64(9223372036854775807L), content="true")
inspect(big.equal_int64(42L), content="false")
let overflow = @bigint.BigInt::from_string("9223372036854775808") // Beyond Int64 range
inspect(overflow.equal_int64(9223372036854775807L), content="false")
}test {
let big = @bigint.BigInt::from_int(42)
inspect(big, content="42")
let neg = @bigint.BigInt::from_int(-42)
inspect(neg, content="-42")
}test {
let big = @bigint.BigInt::from_int64(9223372036854775807L) // max value of Int64
inspect(big, content="9223372036854775807")
let neg = @bigint.BigInt::from_int64(-9223372036854775808L) // min value of Int64
inspect(neg, content="-9223372036854775808")
}test {
let bytes = b"\x01\x02\x03" // Represents 0x010203
let positive = @bigint.BigInt::from_octets(bytes)
let negative = @bigint.BigInt::from_octets(bytes, signum=-1)
inspect(positive, content="66051")
inspect(negative, content="-66051")
}test {
let n = 42U
inspect(@bigint.BigInt::from_uint(n), content="42")
}test {
let n = @bigint.BigInt::from_uint64(12345678901234567890UL)
inspect(n, content="12345678901234567890")
let zero = @bigint.BigInt::from_uint64(0UL)
inspect(zero, content="0")
}test {
inspect(0N.is_zero(), content="true")
inspect(42N.is_zero(), content="false")
inspect((-1N).is_zero(), content="false")
}test {
let base = @bigint.BigInt::from_string("3")
let exp = @bigint.BigInt::from_string("4")
inspect(base.pow(exp), content="81")
inspect(base.pow(exp, modulus=@bigint.BigInt::from_string("10")), content="1")
}test {
let big = 2147483648N // 2^31
inspect(big.to_int(), content="-2147483648") // Overflow to Int.min_value
}test {
let n = 42N
inspect(n.to_int16(), content="42")
let neg = -1N
inspect(neg.to_int16(), content="-1")
let big = 32768N // 2^15
inspect(big.to_int16(), content="-32768") // Overflow to Int16.min_value
}test {
let big = 9223372036854775807N // max value of Int64
inspect(big.to_int64(), content="9223372036854775807")
let bigger = big + 1
inspect(bigger.to_int64(), content="-9223372036854775808") // Overflow to Int64.min_value
}test {
let n = @bigint.BigInt::from_string("abcdef", radix=16)
inspect(n.to_octets(length=4), content="b\"\\x00\\xab\\xcd\\xef\"")
let m = @bigint.BigInt::from_string("0")
inspect(m.to_octets(), content="b\"\\x00\"")
}test {
let n = 12345678901234567890N
inspect(n.to_string(), content="12345678901234567890")
inspect(n.to_string(radix=16), content="ab54a98ceb1f0ad2")
let neg = -42N
inspect(neg.to_string(), content="-42")
let zero = 0N
inspect(zero.to_string(), content="0")
}test {
let n = 42N
inspect(n.to_uint(), content="42")
let neg = -1N
inspect(neg.to_uint(), content="4294967295") // 2^32 - 1
}test {
let n = 42N
inspect(n.to_uint16(), content="42")
let neg = -1N
inspect(neg.to_uint16(), content="65535") // 2^16 - 1
}test {
let n = 12345678901234567890N
inspect(n.to_uint64(), content="12345678901234567890")
let neg = -1N
inspect(neg.to_uint64(), content="18446744073709551615") // 2^64 - 1
}Install
Installed by default