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