-
Notifications
You must be signed in to change notification settings - Fork 1
/
uint128_test.go
164 lines (154 loc) · 4.17 KB
/
uint128_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copied with additions into netipds from net/netip
// Copyright 2020 The Inet.Af AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package netipds
import (
"testing"
)
func TestUint128AddSub(t *testing.T) {
const add1 = 1
const sub1 = -1
tests := []struct {
in uint128
op int // +1 or -1 to add vs subtract
want uint128
}{
{uint128{0, 0}, add1, uint128{0, 1}},
{uint128{0, 1}, add1, uint128{0, 2}},
{uint128{1, 0}, add1, uint128{1, 1}},
{uint128{0, ^uint64(0)}, add1, uint128{1, 0}},
{uint128{^uint64(0), ^uint64(0)}, add1, uint128{0, 0}},
{uint128{0, 0}, sub1, uint128{^uint64(0), ^uint64(0)}},
{uint128{0, 1}, sub1, uint128{0, 0}},
{uint128{0, 2}, sub1, uint128{0, 1}},
{uint128{1, 0}, sub1, uint128{0, ^uint64(0)}},
{uint128{1, 1}, sub1, uint128{1, 0}},
}
for _, tt := range tests {
var got uint128
switch tt.op {
case add1:
got = tt.in.addOne()
case sub1:
got = tt.in.subOne()
default:
panic("bogus op")
}
if got != tt.want {
t.Errorf("%v add %d = %v; want %v", tt.in, tt.op, got, tt.want)
}
}
}
func TestBitsSetFrom(t *testing.T) {
tests := []struct {
bit uint8
want uint128
}{
{0, uint128{^uint64(0), ^uint64(0)}},
{1, uint128{^uint64(0) >> 1, ^uint64(0)}},
{63, uint128{1, ^uint64(0)}},
{64, uint128{0, ^uint64(0)}},
{65, uint128{0, ^uint64(0) >> 1}},
{127, uint128{0, 1}},
{128, uint128{0, 0}},
}
for _, tt := range tests {
var zero uint128
got := zero.bitsSetFrom(tt.bit)
if got != tt.want {
t.Errorf("0.bitsSetFrom(%d) = %064b want %064b", tt.bit, got, tt.want)
}
}
}
func TestBitsClearedFrom(t *testing.T) {
tests := []struct {
bit uint8
want uint128
}{
{0, uint128{0, 0}},
{1, uint128{1 << 63, 0}},
{63, uint128{^uint64(0) &^ 1, 0}},
{64, uint128{^uint64(0), 0}},
{65, uint128{^uint64(0), 1 << 63}},
{127, uint128{^uint64(0), ^uint64(0) &^ 1}},
{128, uint128{^uint64(0), ^uint64(0)}},
}
for _, tt := range tests {
ones := uint128{^uint64(0), ^uint64(0)}
got := ones.bitsClearedFrom(tt.bit)
if got != tt.want {
t.Errorf("ones.bitsClearedFrom(%d) = %064b want %064b", tt.bit, got, tt.want)
}
}
}
func TestShift(t *testing.T) {
const left = "<<"
const right = ">>"
tests := []struct {
in uint128
op string
bits uint8
want uint128
}{
{uint128{0, 0}, left, 0, uint128{0, 0}},
{uint128{0, 1}, left, 0, uint128{0, 1}},
{uint128{0, 0}, left, 1, uint128{0, 0}},
{uint128{0, 1}, left, 1, uint128{0, 2}},
{uint128{0, 1}, left, 63, uint128{0, 1 << 63}},
{uint128{0, 1}, left, 64, uint128{1, 0}},
{uint128{0, 1}, left, 127, uint128{1 << 63, 0}},
{uint128{0, 1}, left, 128, uint128{0, 0}},
{uint128{1, 1}, left, 1, uint128{2, 2}},
{uint128{1, 1 << 63}, left, 1, uint128{3, 0}},
{uint128{0, 0}, right, 0, uint128{0, 0}},
{uint128{0, 0}, right, 1, uint128{0, 0}},
{uint128{0, 1}, right, 1, uint128{0, 0}},
{uint128{0, 2}, right, 1, uint128{0, 1}},
{uint128{1, 0}, right, 1, uint128{0, 1 << 63}},
{uint128{1, 0}, right, 64, uint128{0, 1}},
{uint128{2, 2}, right, 1, uint128{1, 1}},
{uint128{1, 1 << 63}, right, 63, uint128{0, 3}},
{uint128{1 << 63, 0}, right, 64, uint128{0, 1 << 63}},
{uint128{1 << 63, 0}, right, 127, uint128{0, 1}},
{uint128{1 << 63, 0}, right, 128, uint128{0, 0}},
}
for _, tt := range tests {
var got uint128
switch tt.op {
case left:
got = tt.in.shiftLeft(tt.bits)
case right:
got = tt.in.shiftRight(tt.bits)
default:
panic("bogus op")
}
if got != tt.want {
t.Errorf("%v %s %d = %v; want %v", tt.in, tt.op, tt.bits, got, tt.want)
}
}
}
func TestIsBitSet(t *testing.T) {
tests := []struct {
a uint128
i uint8
want uint8
}{
{uint128{0, 0}, 0, 0},
{uint128{0, 1}, 0, 0},
{uint128{1 << 63, 0}, 0, 1},
{uint128{1 << 62, 0}, 1, 1},
{uint128{0, 1 << 63}, 64, 1},
{uint128{0, 1}, 127, 1},
{uint128{0, 2}, 126, 1},
{uint128{^uint64(0), ^uint64(0)}, 0, 1},
{uint128{^uint64(0), ^uint64(0)}, 127, 1},
// i > 127 => false
{uint128{0, 0}, 128, 0},
}
for _, tt := range tests {
if got := tt.a.isBitSet(tt.i); got != tt.want {
t.Errorf("%v.isBitSet(%d) = %v, want %v", tt.a, tt.i, got, tt.want)
}
}
}