1
0
mirror of https://github.com/fumiama/unibase2n.git synced 2026-06-27 07:20:32 +08:00

complete all encode & unit test

This commit is contained in:
源文雨
2022-10-01 14:53:17 +08:00
parent a68a04bbe9
commit 08228b7a61
10 changed files with 449 additions and 66 deletions

View File

@@ -11,19 +11,24 @@ func TestUint128be(t *testing.T) {
zero := readuint128be(make([]byte, 16))
one := readuint128be([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})
num := readuint128be(buf[:])
num.add(one)
num.addeq(one)
assert.Equal(t, num, zero)
num.add(readuint128be([]byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}))
num.shr(8)
assert.Equal(t, num, &uint128be{0x001234567890abcd, 0xef1234567890abcd})
num.shr(16)
assert.Equal(t, num, &uint128be{0x0000001234567890, 0xabcdef1234567890})
num.shr(32)
assert.Equal(t, num, &uint128be{0x0000000000000012, 0x34567890abcdef12})
num.shr(1)
num.addeq(readuint128be([]byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}))
num.shreq(8)
assert.Equal(t, num, uint128be{0x001234567890abcd, 0xef1234567890abcd})
assert.Equal(t, num.String(), "0x1234567890abcdef1234567890abcd")
num.shreq(16)
assert.Equal(t, num, uint128be{0x0000001234567890, 0xabcdef1234567890})
num.shreq(32)
assert.Equal(t, num, uint128be{0x0000000000000012, 0x34567890abcdef12})
assert.Equal(t, num.String(), "0x1234567890abcdef12")
num.shreq(1)
// 因为 num.a 低 1 位为 0 所以可以成功
assert.Equal(t, num, &uint128be{0x0000000000000012 >> 1, 0x34567890abcdef12 >> 1})
num.shr(7)
assert.Equal(t, num, uint128be{0x0000000000000012 >> 1, 0x34567890abcdef12 >> 1})
num.shreq(7)
num.write(buf[:])
assert.Equal(t, buf, [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef})
assert.Equal(t, num.String(), "0x1234567890abcdef")
num.shreq(8)
assert.Equal(t, num.String(), "0x1234567890abcd")
}