math.unsigned: add uint128 tests (#13213)

pull/13214/head
playX 2022-01-19 12:18:46 +03:00 committed by GitHub
parent 7a2705d8ce
commit c48a9e71f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 1 deletions

View File

@ -307,7 +307,7 @@ pub fn (u Uint128) lsh(n u32) Uint128 {
return s
}
// lsh returns u >> n
// rsh returns u >> n
pub fn (u Uint128) rsh(n u32) Uint128 {
mut s := Uint128{}
if n > 64 {
@ -460,3 +460,7 @@ pub fn (u Uint128) - (v Uint128) Uint128 {
pub fn (u Uint128) * (v Uint128) Uint128 {
return u.mul(v)
}
pub fn (u Uint128) < (v Uint128) bool {
return u.cmp(v) == -1
}

View File

@ -0,0 +1,99 @@
import math.unsigned
fn test_str() {
x := unsigned.uint128_from_dec_str('170141183460469231713240559642174554112') or { panic('') }
assert x.str() == '170141183460469231713240559642174554112'
y := unsigned.uint128_from_64(42)
assert y.str() == '42'
}
fn test_ops() {
x := unsigned.uint128_from_64(18446744073709551615)
y := unsigned.uint128_from_64(18446744073709551615)
z := unsigned.uint128_from_dec_str('340282366920938463426481119284349108225') or {
assert false
panic('')
}
assert (x * y).str() == '340282366920938463426481119284349108225'
assert (x + y).str() == '36893488147419103230'
assert (z / unsigned.uint128_from_64(2)).str() == '170141183460469231713240559642174554112'
assert (unsigned.uint128_from_dec_str('170141183460469231713240559642174554112') or {
panic('')
} - unsigned.uint128_from_64(2)).str() == '170141183460469231713240559642174554110'
assert x == y
assert unsigned.uint128_from_dec_str('340282366920938463426481119284349108225') or {
assert false
panic('')
} > y
}
struct LeadingZeros {
l unsigned.Uint128
r unsigned.Uint128
zeros int
}
fn new(x u64, y u64) unsigned.Uint128 {
return unsigned.Uint128{x, y}
}
fn test_leading_zeros() {
tcs := [
LeadingZeros{
l: new(0x00, 0xf000000000000000)
r: new(0x00, 0x8000000000000000)
zeros: 1
},
LeadingZeros{
l: new(0x00, 0xf000000000000000)
r: new(0x00, 0xc000000000000000)
zeros: 2
},
LeadingZeros{
l: new(0x00, 0xf000000000000000)
r: new(0x00, 0xe000000000000000)
zeros: 3
},
LeadingZeros{
l: new(0x00, 0xffff000000000000)
r: new(0x00, 0xff00000000000000)
zeros: 8
},
LeadingZeros{
l: new(0x00, 0x000000000000ffff)
r: new(0x00, 0x000000000000ff00)
zeros: 56
},
LeadingZeros{
l: new(0xf000000000000000, 0x01)
r: new(0x4000000000000000, 0x00)
zeros: 63
},
LeadingZeros{
l: new(0xf000000000000000, 0x00)
r: new(0x4000000000000000, 0x00)
zeros: 64
},
LeadingZeros{
l: new(0xf000000000000000, 0x00)
r: new(0x8000000000000000, 0x00)
zeros: 65
},
LeadingZeros{
l: new(0x00, 0x00)
r: new(0x00, 0x00)
zeros: 128
},
LeadingZeros{
l: new(0x01, 0x00)
r: new(0x00, 0x00)
zeros: 127
},
]
for tc in tcs {
zeros := tc.l.xor(tc.r).leading_zeros()
assert zeros == tc.zeros
}
}