-
Notifications
You must be signed in to change notification settings - Fork 23
/
utils_test.go
141 lines (134 loc) · 2.82 KB
/
utils_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
package gotiny
import (
"testing"
)
func TestUint32ToInt32(t *testing.T) {
tests := []struct {
name string
u uint32
want int32
}{
{"max", 4294967295, -2147483648},
{"9=-5", 9, -5},
{"7=-4", 7, -4},
{"5=-3", 5, -3},
{"3=-2", 3, -2},
{"1=-1", 1, -1},
{"0=0", 0, 0},
{"2=1", 2, 1},
{"4=2", 4, 2},
{"6=3", 6, 3},
{"8=4", 8, 4},
{"10=5", 10, 5},
{"12=6", 12, 6},
{"max", 4294967294, 2147483647},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := uint32ToInt32(tt.u); got != tt.want {
t.Errorf("uint32ToInt32(%v) = %v, want %v", tt.u, got, tt.want)
}
})
}
}
func TestUint16ToInt16(t *testing.T) {
tests := []struct {
name string
u uint16
want int16
}{
{"max", 65535, -32768},
{"9=-5", 9, -5},
{"7=-4", 7, -4},
{"5=-3", 5, -3},
{"3=-2", 3, -2},
{"1=-1", 1, -1},
{"0=0", 0, 0},
{"2=1", 2, 1},
{"4=2", 4, 2},
{"6=3", 6, 3},
{"8=4", 8, 4},
{"10=5", 10, 5},
{"12=6", 12, 6},
{"max", 65530, 32765},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := uint16ToInt16(tt.u); got != tt.want {
t.Errorf("uint16ToInt16(%v) = %v, want %v", tt.u, got, tt.want)
}
})
}
}
func TestReverse64Byte(t *testing.T) {
tests := []struct {
name string
u uint64
want uint64
}{
{"all zeros", 0, 0},
{"all ones", 0xffffffffffffffff, 0xffffffffffffffff},
{"simple pattern", 0x1234567890abcdef, 0xefcdab9078563412},
{"random pattern", 0x8765432109876543, 0x4365870921436587},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := reverse64Byte(tt.u)
if got != tt.want {
t.Errorf("reverse64Byte(%x) = %x, want %x", tt.u, got, tt.want)
}
})
}
}
func TestReverse32Byte(t *testing.T) {
tests := []struct {
name string
u uint32
want uint32
}{
{"all zeros", 0, 0},
{"all ones", 0xFFFFFFFF, 0xFFFFFFFF},
{"simple pattern", 0x12345678, 0x78563412},
{"random pattern", 0x87654321, 0x21436587},
{"edge case 1", 0x80000000, 0x00000080},
{"edge case 2", 0x7FFFFFFF, 0xFFFFFF7F},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := reverse32Byte(tt.u)
if got != tt.want {
t.Errorf("reverse32Byte(%x) = %x, want %x", tt.u, got, tt.want)
}
})
}
}
func TestInt64ToUint64(t *testing.T) {
tests := []struct {
name string
want uint64
v int64
}{
{"max", 4294967295, -2147483648},
{"9=-5", 9, -5},
{"7=-4", 7, -4},
{"5=-3", 5, -3},
{"3=-2", 3, -2},
{"1=-1", 1, -1},
{"0=0", 0, 0},
{"2=1", 2, 1},
{"4=2", 4, 2},
{"6=3", 6, 3},
{"8=4", 8, 4},
{"10=5", 10, 5},
{"12=6", 12, 6},
{"max", 4294967294, 2147483647},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := int64ToUint64(tt.v)
if got != tt.want {
t.Errorf("int64ToUint64(%v) = %v, want %v", tt.v, got, tt.want)
}
})
}
}