MCPcopy Create free account
hub / github.com/DOSNetwork/core / TestIntEndianness

Function TestIntEndianness

group/mod/int_test.go:12–55  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestIntEndianness(t *testing.T) {
13 modulo := big.NewInt(65535)
14 var v int64 = 65500
15 // Let's assume it is bigendian and test that
16 i := new(Int).Init64(v, modulo)
17 assert.Equal(t, i.BO, BigEndian)
18
19 buff1, err := i.MarshalBinary()
20 assert.Nil(t, err)
21 i.BO = BigEndian
22 buff2, err := i.MarshalBinary()
23 assert.Nil(t, err)
24 assert.Equal(t, buff1, buff2)
25
26 // Let's change endianness and check the result
27 i.BO = LittleEndian
28 buff3, err := i.MarshalBinary()
29 assert.Nil(t, err)
30 assert.NotEqual(t, buff2, buff3)
31
32 // let's try LittleEndian function
33 buff4 := i.LittleEndian(0, 32)
34 assert.Equal(t, buff3, buff4)
35 // set endianess but using littleendian should not change anything
36 i.BO = BigEndian
37 assert.Equal(t, buff4, i.LittleEndian(0, 32))
38
39 // Try to reconstruct the int from the buffer
40 i = new(Int).Init64(v, modulo)
41 i2 := NewInt64(0, modulo)
42 buff, _ := i.MarshalBinary()
43 assert.Nil(t, i2.UnmarshalBinary(buff))
44 assert.True(t, i.Equal(i2))
45
46 i.BO = LittleEndian
47 buff, _ = i.MarshalBinary()
48 i2.BO = LittleEndian
49 assert.Nil(t, i2.UnmarshalBinary(buff))
50 assert.True(t, i.Equal(i2))
51
52 i2.BO = BigEndian
53 assert.Nil(t, i2.UnmarshalBinary(buff))
54 assert.False(t, i.Equal(i2))
55}
56func TestIntEndianBytes(t *testing.T) {
57 modulo, err := hex.DecodeString("1000")
58 moduloI := new(big.Int).SetBytes(modulo)

Callers

nothing calls this directly

Calls 6

NewInt64Function · 0.85
Init64Method · 0.80
LittleEndianMethod · 0.80
EqualMethod · 0.45
MarshalBinaryMethod · 0.45
UnmarshalBinaryMethod · 0.45

Tested by

no test coverage detected