lib/std/lang/gen/bitset/tests.rad 6.8 KiB raw
1
//! Tests for the bitset module.
2
3
use std::testing;
4
5
/// Test [`super::init`] and basic set/contains operations.
6
@test fn testInit() throws (testing::TestError) {
7
    let mut bits: [u32; 4] = undefined;
8
    let mut bs = super::init(&mut bits[..]);
9
10
    // `init` should zero-initialize, so all bits start unset.
11
    try testing::expect(not super::contains(&bs, 0));
12
    try testing::expect(not super::contains(&bs, 31));
13
    try testing::expect(not super::contains(&bs, 32));
14
    try testing::expect(not super::contains(&bs, 127));
15
16
    // Set various bits including word boundaries.
17
    super::set(&mut bs, 0);
18
    super::set(&mut bs, 31);
19
    super::set(&mut bs, 32);
20
    super::set(&mut bs, 127);
21
22
    try testing::expect(super::contains(&bs, 0));
23
    try testing::expect(super::contains(&bs, 31));
24
    try testing::expect(super::contains(&bs, 32));
25
    try testing::expect(super::contains(&bs, 127));
26
27
    // Other bits still unset.
28
    try testing::expect(not super::contains(&bs, 1));
29
    try testing::expect(not super::contains(&bs, 30));
30
    try testing::expect(not super::contains(&bs, 33));
31
    try testing::expect(not super::contains(&bs, 126));
32
}
33
34
/// Test clear operation.
35
@test fn testClear() throws (testing::TestError) {
36
    let mut bits: [u32; 2] = [0; 2];
37
    let mut bs = super::new(&mut bits[..]);
38
39
    super::set(&mut bs, 0);
40
    super::set(&mut bs, 31);
41
    super::set(&mut bs, 32);
42
43
    try testing::expect(super::contains(&bs, 0));
44
    try testing::expect(super::contains(&bs, 31));
45
    try testing::expect(super::contains(&bs, 32));
46
47
    super::clear(&mut bs, 31);
48
49
    try testing::expect(super::contains(&bs, 0));
50
    try testing::expect(not super::contains(&bs, 31));
51
    try testing::expect(super::contains(&bs, 32));
52
}
53
54
/// Test population count.
55
@test fn testCount() throws (testing::TestError) {
56
    let mut bits: [u32; 2] = [0; 2];
57
    let mut bs = super::new(&mut bits[..]);
58
59
    try testing::expect(super::count(&bs) == 0);
60
61
    super::set(&mut bs, 0);
62
    try testing::expect(super::count(&bs) == 1);
63
64
    super::set(&mut bs, 31);
65
    super::set(&mut bs, 32);
66
    super::set(&mut bs, 63);
67
    try testing::expect(super::count(&bs) == 4);
68
69
    super::clear(&mut bs, 31);
70
    try testing::expect(super::count(&bs) == 3);
71
}
72
73
/// Test union operation.
74
@test fn testUnion() throws (testing::TestError) {
75
    let mut bits_a: [u32; 2] = [0; 2];
76
    let mut bits_b: [u32; 2] = [0; 2];
77
    let mut a = super::new(&mut bits_a[..]);
78
    let mut b = super::new(&mut bits_b[..]);
79
80
    super::set(&mut a, 0);
81
    super::set(&mut a, 10);
82
83
    super::set(&mut b, 10);
84
    super::set(&mut b, 20);
85
86
    super::union_(&mut a, &b);
87
88
    try testing::expect(super::contains(&a, 0));
89
    try testing::expect(super::contains(&a, 10));
90
    try testing::expect(super::contains(&a, 20));
91
    try testing::expect(super::count(&a) == 3);
92
}
93
94
/// Test subtract operation.
95
@test fn testSubtract() throws (testing::TestError) {
96
    let mut bits_a: [u32; 2] = [0; 2];
97
    let mut bits_b: [u32; 2] = [0; 2];
98
    let mut a = super::new(&mut bits_a[..]);
99
    let mut b = super::new(&mut bits_b[..]);
100
101
    super::set(&mut a, 0);
102
    super::set(&mut a, 10);
103
    super::set(&mut a, 20);
104
105
    super::set(&mut b, 10);
106
    super::set(&mut b, 30);
107
108
    super::subtract(&mut a, &b);
109
110
    try testing::expect(super::contains(&a, 0));
111
    try testing::expect(not super::contains(&a, 10));
112
    try testing::expect(super::contains(&a, 20));
113
    try testing::expect(super::count(&a) == 2);
114
}
115
116
/// Test equality check.
117
@test fn testEq() throws (testing::TestError) {
118
    let mut bits_a: [u32; 2] = [0; 2];
119
    let mut bits_b: [u32; 2] = [0; 2];
120
    let mut a = super::new(&mut bits_a[..]);
121
    let mut b = super::new(&mut bits_b[..]);
122
123
    // Both empty.
124
    try testing::expect(super::eq(&a, &b));
125
126
    super::set(&mut a, 5);
127
    try testing::expect(not super::eq(&a, &b));
128
129
    super::set(&mut b, 5);
130
    try testing::expect(super::eq(&a, &b));
131
132
    super::set(&mut a, 32);
133
    super::set(&mut b, 32);
134
    try testing::expect(super::eq(&a, &b));
135
136
    super::set(&mut b, 33);
137
    try testing::expect(not super::eq(&a, &b));
138
}
139
140
/// Test copy operation.
141
@test fn testCopy() throws (testing::TestError) {
142
    let mut bits_a: [u32; 2] = [0; 2];
143
    let mut bits_b: [u32; 2] = [0; 2];
144
    let mut a = super::new(&mut bits_a[..]);
145
    let mut b = super::new(&mut bits_b[..]);
146
147
    super::set(&mut a, 0);
148
    super::set(&mut a, 31);
149
    super::set(&mut a, 63);
150
151
    super::copy(&mut b, &a);
152
153
    try testing::expect(super::eq(&a, &b));
154
    try testing::expect(super::contains(&b, 0));
155
    try testing::expect(super::contains(&b, 31));
156
    try testing::expect(super::contains(&b, 63));
157
}
158
159
/// Test clearAll operation.
160
@test fn testClearAll() throws (testing::TestError) {
161
    let mut bits: [u32; 2] = [0; 2];
162
    let mut bs = super::new(&mut bits[..]);
163
164
    super::set(&mut bs, 0);
165
    super::set(&mut bs, 31);
166
    super::set(&mut bs, 32);
167
    super::set(&mut bs, 63);
168
    try testing::expect(super::count(&bs) == 4);
169
170
    super::clearAll(&mut bs);
171
    try testing::expect(super::count(&bs) == 0);
172
    try testing::expect(not super::contains(&bs, 0));
173
    try testing::expect(not super::contains(&bs, 31));
174
}
175
176
/// Test iteration over set bits.
177
@test fn testIter() throws (testing::TestError) {
178
    let mut bits: [u32; 2] = [0; 2];
179
    let mut bs = super::new(&mut bits[..]);
180
181
    super::set(&mut bs, 3);
182
    super::set(&mut bs, 31);
183
    super::set(&mut bs, 32);
184
    super::set(&mut bs, 50);
185
186
    let mut it = super::iter(&bs);
187
    let mut count: u32 = 0;
188
    let mut sum: u32 = 0;
189
190
    while let n = super::iterNext(&mut it) {
191
        count += 1;
192
        sum += n;
193
    }
194
195
    try testing::expect(count == 4);
196
    try testing::expect(sum == 3 + 31 + 32 + 50);
197
}
198
199
/// Test iteration on empty bitset.
200
@test fn testIterEmpty() throws (testing::TestError) {
201
    let mut bits: [u32; 2] = [0; 2];
202
    let mut bs = super::new(&mut bits[..]);
203
    let mut it = super::iter(&bs);
204
    let result = super::iterNext(&mut it);
205
206
    try testing::expect(result == nil);
207
}
208
209
/// Test [`super::wordsFor`] calculation.
210
@test fn testWordsFor() throws (testing::TestError) {
211
    try testing::expect(super::wordsFor(0) == 0);
212
    try testing::expect(super::wordsFor(1) == 1);
213
    try testing::expect(super::wordsFor(31) == 1);
214
    try testing::expect(super::wordsFor(32) == 1);
215
    try testing::expect(super::wordsFor(33) == 2);
216
    try testing::expect(super::wordsFor(64) == 2);
217
    try testing::expect(super::wordsFor(65) == 3);
218
}
219
220
/// Test out-of-bounds access is safe.
221
@test fn testOutOfBounds() throws (testing::TestError) {
222
    let mut bits: [u32; 1] = [0; 1];
223
    let mut bs = super::new(&mut bits[..]);
224
225
    // Setting beyond length should be ignored.
226
    super::set(&mut bs, 100);
227
    try testing::expect(not super::contains(&bs, 100));
228
229
    // Clearing beyond length should be safe.
230
    super::clear(&mut bs, 100);
231
232
    // Contains beyond length should return false.
233
    try testing::expect(not super::contains(&bs, 100));
234
}