-
Notifications
You must be signed in to change notification settings - Fork 1
/
unsorted_test.go
146 lines (130 loc) · 2.24 KB
/
unsorted_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
package utils
import "testing"
func TestSum(t *testing.T) {
var sumTests = []struct {
input []int
expected int
}{
{
input: []int{1, 2, 3},
expected: 6,
},
{
input: []int{-5, 5},
expected: 0,
},
{
input: []int{5, 5, 5, 5},
expected: 20,
},
}
for _, tt := range sumTests {
res := Sum(tt.input)
if res != tt.expected {
t.Errorf("%d is not equal to %d", res, tt.expected)
}
}
}
func TestRound(t *testing.T) {
var roundTests = []struct {
inputVal float64
inputPlaces int
expected float64
}{
{
inputVal: 69.69696969,
inputPlaces: 2,
expected: 69.70,
},
{
inputVal: 123.4005,
inputPlaces: 1,
expected: 123.4,
},
}
for _, tt := range roundTests {
res := Round(tt.inputVal, tt.inputPlaces)
if res != tt.expected {
t.Errorf("%f is not equal to %f", res, tt.expected)
}
}
}
var packingTests = []struct {
partA uint32
partB uint32
packed uint64
}{
{
partA: 1,
partB: 2,
packed: 4294967298,
},
{
partA: 1,
partB: 4,
packed: 4294967300,
},
{
partA: 7,
partB: 2,
packed: 30064771074,
},
}
func TestSplitUint64(t *testing.T) {
for _, tt := range packingTests {
resA, resB := SplitUint64(tt.packed)
if resA != tt.partA {
t.Errorf("%d is not equal to %d", resA, tt.partA)
}
if resB != tt.partB {
t.Errorf("%d is not equal to %d", resB, tt.partB)
}
}
}
func TestCheckFlag(t *testing.T) {
var tests = []struct {
inputValue uint32
inputFlag uint32
expected bool
}{
{
inputValue: 3,
inputFlag: 1,
expected: true,
},
{
inputValue: 2,
inputFlag: 1,
expected: false,
},
{
inputValue: 16,
inputFlag: 1,
expected: false,
},
{
inputValue: 18,
inputFlag: 2,
expected: true,
},
{
inputValue: 17,
inputFlag: 1,
expected: true,
},
}
for _, tt := range tests {
res := CheckFlag(tt.inputValue, tt.inputFlag)
if res != tt.expected {
t.Errorf("%t is not equal to %t (%d - %d)", res, tt.expected, tt.inputValue, tt.inputFlag)
}
}
}
func TestCombineUint32(t *testing.T) {
for _, tt := range packingTests {
res := CombineUint32(tt.partA, tt.partB)
if res != tt.packed {
t.Errorf("%d is not equal to %d", res, tt.packed)
}
}
}