-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu256_test.go
127 lines (111 loc) · 2.9 KB
/
u256_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package uint256
import (
"testing"
)
func TestSetAllOne(t *testing.T) {
z := Zero()
z.SetAllOne()
if z.ToString() != twoPow256Sub1 {
t.Errorf("Expected all ones, got %s", z.ToString())
}
}
func TestByte(t *testing.T) {
tests := []struct {
input string
position uint64
expected byte
}{
{"0x1000000000000000000000000000000000000000000000000000000000000000", 0, 16},
{"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0, 255},
{"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 255},
}
for i, tt := range tests {
z, _ := FromHex(tt.input)
n := NewUint(tt.position)
result := z.Byte(n)
if result.arr[0] != uint64(tt.expected) {
t.Errorf("Test case %d failed. Input: %s, Position: %d, Expected: %d, Got: %d",
i, tt.input, tt.position, tt.expected, result.arr[0])
}
// check other array elements are 0
if result.arr[1] != 0 || result.arr[2] != 0 || result.arr[3] != 0 {
t.Errorf("Test case %d failed. Non-zero values in upper bytes", i)
}
}
// overflow
z, _ := FromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
n := NewUint(32)
result := z.Byte(n)
if !result.IsZero() {
t.Errorf("Expected zero for position >= 32, got %v", result)
}
}
func TestBitLen(t *testing.T) {
tests := []struct {
input string
expected int
}{
{"0x0", 0},
{"0x1", 1},
{"0xff", 8},
{"0x100", 9},
{"0xffff", 16},
{"0x10000", 17},
{"0xffffffffffffffff", 64},
{"0x10000000000000000", 65},
{"0xffffffffffffffffffffffffffffffff", 128},
{"0x100000000000000000000000000000000", 129},
{"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 256},
}
for i, tt := range tests {
z, _ := FromHex(tt.input)
result := z.BitLen()
if result != tt.expected {
t.Errorf("Test case %d failed. Input: %s, Expected: %d, Got: %d",
i, tt.input, tt.expected, result)
}
}
}
func TestByteLen(t *testing.T) {
tests := []struct {
input string
expected int
}{
{"0x0", 0},
{"0x1", 1},
{"0xff", 1},
{"0x100", 2},
{"0xffff", 2},
{"0x10000", 3},
{"0xffffffffffffffff", 8},
{"0x10000000000000000", 9},
{"0xffffffffffffffffffffffffffffffff", 16},
{"0x100000000000000000000000000000000", 17},
{"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32},
}
for i, tt := range tests {
z, _ := FromHex(tt.input)
result := z.ByteLen()
if result != tt.expected {
t.Errorf("Test case %d failed. Input: %s, Expected: %d, Got: %d",
i, tt.input, tt.expected, result)
}
}
}
func TestClone(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"0x1", "1"},
{"0x100", "256"},
{"0x10000000000000000", "18446744073709551616"},
}
for _, tt := range tests {
z, _ := FromHex(tt.input)
result := z.Clone()
if result.ToString() != tt.expected {
t.Errorf("Test %s failed. Expected %s, got %s", tt.input, tt.expected, result.ToString())
}
}
}