This repository was archived by the owner on Aug 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbitarray_test.go
More file actions
173 lines (140 loc) · 4.2 KB
/
bitarray_test.go
File metadata and controls
173 lines (140 loc) · 4.2 KB
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
165
166
167
168
169
170
171
172
173
/*
The test module for the BitArray datatype.
This tests:
1. Initializing a new valid BitArray
2. Incorrectly initializing a new BitArray with zero
3. Incorrectly initializing a new BitArray with a value under 8
4. Incorrectly initializing a new BitArray with a value above 8 but not a multiple of 8
5. Filling the BitArray with ones, using AppendBit
6. Filling the BitArray with zeros, using AppendBit
7. Failing to append an invalid bit which isn't a 0 or a 1
8. Failing to append more bits than the BitArray can contain
9. Appending fewer bits than the BitArray's full size
*/
package imagehash
import (
"bytes"
"testing"
)
// Test initializing the bit array, which should contain only zeros
// This not only tests NewBitArray, but also the getter function, GetArray
func TestNewBitArrayInit(t *testing.T) {
numberBits := 32
ba, err := NewBitArray(numberBits)
exp := []byte{0x00, 0x00, 0x00, 0x00}
act := ba.GetArray()
if bytes.Compare(exp, act) != 0 {
t.Errorf("init test [%x] failed: [%x]", exp, act)
} else if err != nil {
t.Errorf("init test failed with error", err)
}
}
// Test initializing a bit array incorrectly with 0 bits
func TestFailNewBitArrayInitZero(t *testing.T) {
numberBits := 0
_, err := NewBitArray(numberBits)
if err == nil {
t.Errorf("zero bits init test didn't fail")
}
}
// Test initializing a bit array incorrectly with the number of bits
// less than 8 but not zero
func TestFailNewBitArrayLessThanEight(t *testing.T) {
numberBits := 5
_, err := NewBitArray(numberBits)
if err == nil {
t.Errorf("less than 8 bits init test didn't fail")
}
}
// Test initializing a bit array incorrectly with the number of bits
// greater than 8 but not a multiple of eight
func TestFailNewBitArrayGreaterThanEight(t *testing.T) {
numberBits := 12
_, err := NewBitArray(numberBits)
if err == nil {
t.Errorf("greater than 8 bits init test didn't fail")
}
}
// Test filing the bit array with ones
func TestAppendOne(t *testing.T) {
numberBits := 32
ba, err := NewBitArray(numberBits)
exp := []byte{0xFF, 0xFF, 0xFF, 0xFF}
// For every bit, append a 1
for i := 0; i < numberBits; i++ {
ba.AppendBit(1)
}
act := ba.GetArray()
if bytes.Compare(exp, act) != 0 {
t.Errorf("AppendOne test [%x] failed: [%x]", exp, act)
} else if err != nil {
t.Errorf("AppendOne test failed with error", err)
}
}
// Test filing the bit array with zeros
func TestAppendZero(t *testing.T) {
numberBits := 32
ba, err := NewBitArray(numberBits)
exp := []byte{0x00, 0x00, 0x00, 0x00}
// For every bit, append a 0
for i := 0; i < numberBits; i++ {
ba.AppendBit(0)
}
act := ba.GetArray()
if bytes.Compare(exp, act) != 0 {
t.Errorf("AppendZero test [%x] failed: [%x]", exp, act)
} else if err != nil {
t.Errorf("AppendZero test failed with error", err)
}
}
// Test appending an invalid bit value other than 0 or 1
func TestAppendInvalidBit(t *testing.T) {
numberBits := 32
ba, err := NewBitArray(numberBits)
// Ensure that the initialization didn't fail
if err != nil {
t.Errorf("AppendInvalidBit test failed init with error", err)
return
}
// Append an invalid value of 24
err = ba.AppendBit(24)
if err == nil {
t.Errorf("AppendInvalidBit test didn't fail")
}
}
// Test filling the array past its capacity, which should fail
func TestOverfilledBitArray(t *testing.T) {
numberBits := 32
fillamount := numberBits + 2
ba, err := NewBitArray(numberBits)
// Ensure that the initialization didn't fail
if err != nil {
t.Errorf("overfill test failed with error", err)
return
}
// Fill up only 13 of the 32 bits
for i := 0; i < fillamount; i++ {
err = ba.AppendBit(1)
}
// If overfilling the bits didn't fail, throw error
if err == nil {
t.Errorf("overfill test didn't fail")
}
}
// Test filling the array below its capacity, which shouldn't fail
func TestPartlyFullBitArray(t *testing.T) {
numberBits := 32
fillamount := 13
ba, err := NewBitArray(numberBits)
exp := []byte{0xFF, 0xF8, 0x00, 0x00}
// Fill up only 13 of the 32 bits
for i := 0; i < fillamount; i++ {
ba.AppendBit(1)
}
act := ba.GetArray()
if bytes.Compare(exp, act) != 0 {
t.Errorf("partial fill test [%x] failed: [%x]", exp, act)
} else if err != nil {
t.Errorf("partial fill test failed with error", err)
}
}