bitwise_test.gno
3.96 Kb ยท 158 lines
1package int256
2
3import (
4 "testing"
5)
6
7func TestBitwise_And(t *testing.T) {
8 tests := []struct {
9 x, y, want string
10 }{
11 {"5", "1", "1"}, // 0101 & 0001 = 0001
12 {"-1", "1", "1"}, // 1111 & 0001 = 0001
13 {"-5", "3", "3"}, // 1111...1011 & 0000...0011 = 0000...0011
14 {MAX_UINT256, MAX_UINT256, MAX_UINT256},
15 {TWO_POW_128, TWO_POW_128_MINUS_1, "0"}, // 2^128 & (2^128 - 1) = 0
16 {TWO_POW_128, MAX_UINT256, TWO_POW_128}, // 2^128 & MAX_INT256
17 {MAX_UINT256, TWO_POW_128, TWO_POW_128}, // MAX_INT256 & 2^128
18 }
19
20 for _, tc := range tests {
21 x, _ := FromDecimal(tc.x)
22 y, _ := FromDecimal(tc.y)
23 want, _ := FromDecimal(tc.want)
24
25 got := new(Int).And(x, y)
26
27 if got.Neq(want) {
28 t.Errorf("And(%s, %s) = %s, want %s", x.String(), y.String(), got.String(), want.String())
29 }
30 }
31}
32
33func TestBitwise_Or(t *testing.T) {
34 tests := []struct {
35 x, y, want string
36 }{
37 {"5", "1", "5"}, // 0101 | 0001 = 0101
38 {"-1", "1", "-1"}, // 1111 | 0001 = 1111
39 {"-5", "3", "-5"}, // 1111...1011 | 0000...0011 = 1111...1011
40 {TWO_POW_128, TWO_POW_128_MINUS_1, TWO_POW_129_MINUS_1},
41 {TWO_POW_128, MAX_UINT256, MAX_UINT256},
42 {"0", TWO_POW_128, TWO_POW_128}, // 0 | 2^128 = 2^128
43 {MAX_UINT256, TWO_POW_128, MAX_UINT256}, // MAX_INT256 | 2^128 = MAX_INT256
44 }
45
46 for _, tc := range tests {
47 x, _ := FromDecimal(tc.x)
48 y, _ := FromDecimal(tc.y)
49 want, _ := FromDecimal(tc.want)
50
51 got := new(Int).Or(x, y)
52
53 if got.Neq(want) {
54 t.Errorf(
55 "Or(%s, %s) = %s, want %s",
56 x.String(), y.String(), got.String(), want.String(),
57 )
58 }
59 }
60}
61
62func TestBitwise_Not(t *testing.T) {
63 tests := []struct {
64 x, want string
65 }{
66 {"5", "-6"}, // 0101 -> 1111...1010
67 {"-1", "0"}, // 1111...1111 -> 0000...0000
68 {TWO_POW_128, MINUS_TWO_POW_128_MINUS_1}, // NOT 2^128
69 {TWO_POW_255, MIN_INT256_MINUS_1}, // NOT 2^255
70 }
71
72 for _, tc := range tests {
73 x, _ := FromDecimal(tc.x)
74 want, _ := FromDecimal(tc.want)
75
76 got := new(Int).Not(x)
77
78 if got.Neq(want) {
79 t.Errorf("Not(%s) = %s, want %s", x.String(), got.String(), want.String())
80 }
81 }
82}
83
84func TestBitwise_Xor(t *testing.T) {
85 tests := []struct {
86 x, y, want string
87 }{
88 {"5", "1", "4"}, // 0101 ^ 0001 = 0100
89 {"-1", "1", "-2"}, // 1111...1111 ^ 0000...0001 = 1111...1110
90 {"-5", "3", "-8"}, // 1111...1011 ^ 0000...0011 = 1111...1000
91 {TWO_POW_128, TWO_POW_128, "0"}, // 2^128 ^ 2^128 = 0
92 {MAX_UINT256, TWO_POW_128, MINUS_TWO_POW_128_MINUS_1}, // MAX_INT256 ^ 2^128
93 {TWO_POW_255, MAX_UINT256, MIN_INT256_MINUS_1}, // 2^255 ^ MAX_INT256
94 }
95
96 for _, tt := range tests {
97 x, _ := FromDecimal(tt.x)
98 y, _ := FromDecimal(tt.y)
99 want, _ := FromDecimal(tt.want)
100
101 got := new(Int).Xor(x, y)
102
103 if got.Neq(want) {
104 t.Errorf("Xor(%s, %s) = %s, want %s", x.String(), y.String(), got.String(), want.String())
105 }
106 }
107}
108
109func TestBitwise_Rsh(t *testing.T) {
110 tests := []struct {
111 x string
112 n uint
113 want string
114 }{
115 {"5", 1, "2"}, // 0101 >> 1 = 0010
116 {"42", 3, "5"}, // 00101010 >> 3 = 00000101
117 {TWO_POW_128, 128, "1"},
118 {MAX_UINT256, 255, "1"},
119 {TWO_POW_255, 254, "2"},
120 {MINUS_TWO_POW_128, 128, TWO_POW_128_MINUS_1},
121 }
122
123 for _, tt := range tests {
124 x, _ := FromDecimal(tt.x)
125 want, _ := FromDecimal(tt.want)
126
127 got := new(Int).Rsh(x, tt.n)
128
129 if got.Neq(want) {
130 t.Errorf("Rsh(%s, %d) = %s, want %s", x.String(), tt.n, got.String(), want.String())
131 }
132 }
133}
134
135func TestBitwise_Lsh(t *testing.T) {
136 tests := []struct {
137 x string
138 n uint
139 want string
140 }{
141 {"5", 2, "20"}, // 0101 << 2 = 10100
142 {"42", 5, "1344"}, // 00101010 << 5 = 10101000000
143 {"1", 128, TWO_POW_128}, // 1 << 128 = 2^128
144 {"2", 254, TWO_POW_255},
145 {"1", 255, MIN_INT256}, // 1 << 255 = MIN_INT256 (overflow)
146 }
147
148 for _, tt := range tests {
149 x, _ := FromDecimal(tt.x)
150 want, _ := FromDecimal(tt.want)
151
152 got := new(Int).Lsh(x, tt.n)
153
154 if got.Neq(want) {
155 t.Errorf("Lsh(%s, %d) = %s, want %s", x.String(), tt.n, got.String(), want.String())
156 }
157 }
158}