bitset: Keep safe tests outside unsafe

d1a3f60be6ea478f5e92c163ec07a4a1e6927ba4a5a962fff54eba064d298d18
Bitset operations over local arrays require no raw memory access. Run those tests as safe functions and initialize the clear test with set bits so it still proves that clearAll writes every word.

Retain unsafe only for the arena-backed session test, where raw static storage crosses the allocator boundary.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent 719e243e
lib/std/lang/gen/bitset/tests.rad +12 -12
3 3
use std::testing;
4 4
use std::lang::alloc;
5 5
6 6
7 7
/// Test word initialization and basic set/contains operations.
8 -
@test unsafe fn testInit() throws (testing::TestError) {
9 -
    let mut bits: [u32; 4] = undefined;
8 +
@test fn testInit() throws (testing::TestError) {
9 +
    let mut bits: [u32; 4] = [0xffffffff; 4];
10 10
    let bs = &mut bits[..];
11 11
    super::clearAll(bs);
12 12
13 13
    // All bits start unset after clearing the words.
14 14
    try testing::expect(not super::contains(bs, 0));
33 33
    try testing::expect(not super::contains(bs, 33));
34 34
    try testing::expect(not super::contains(bs, 126));
35 35
}
36 36
37 37
/// Test clear operation.
38 -
@test unsafe fn testClear() throws (testing::TestError) {
38 +
@test fn testClear() throws (testing::TestError) {
39 39
    let mut bits: [u32; 2] = [0; 2];
40 40
    let bs = &mut bits[..];
41 41
42 42
    super::put(bs, 0);
43 43
    super::put(bs, 31);
53 53
    try testing::expect(not super::contains(bs, 31));
54 54
    try testing::expect(super::contains(bs, 32));
55 55
}
56 56
57 57
/// Test population count.
58 -
@test unsafe fn testCount() throws (testing::TestError) {
58 +
@test fn testCount() throws (testing::TestError) {
59 59
    let mut bits: [u32; 2] = [0; 2];
60 60
    let bs = &mut bits[..];
61 61
62 62
    try testing::expect(super::count(bs) == 0);
63 63
72 72
    super::clear(bs, 31);
73 73
    try testing::expect(super::count(bs) == 3);
74 74
}
75 75
76 76
/// Test union operation.
77 -
@test unsafe fn testUnion() throws (testing::TestError) {
77 +
@test fn testUnion() throws (testing::TestError) {
78 78
    let mut bits_a: [u32; 2] = [0; 2];
79 79
    let mut bits_b: [u32; 2] = [0; 2];
80 80
    let a = &mut bits_a[..];
81 81
    let b = &mut bits_b[..];
82 82
93 93
    try testing::expect(super::contains(a, 20));
94 94
    try testing::expect(super::count(a) == 3);
95 95
}
96 96
97 97
/// Test subtract operation.
98 -
@test unsafe fn testSubtract() throws (testing::TestError) {
98 +
@test fn testSubtract() throws (testing::TestError) {
99 99
    let mut bits_a: [u32; 2] = [0; 2];
100 100
    let mut bits_b: [u32; 2] = [0; 2];
101 101
    let a = &mut bits_a[..];
102 102
    let b = &mut bits_b[..];
103 103
115 115
    try testing::expect(super::contains(a, 20));
116 116
    try testing::expect(super::count(a) == 2);
117 117
}
118 118
119 119
/// Test equality check.
120 -
@test unsafe fn testEq() throws (testing::TestError) {
120 +
@test fn testEq() throws (testing::TestError) {
121 121
    let mut bits_a: [u32; 2] = [0; 2];
122 122
    let mut bits_b: [u32; 2] = [0; 2];
123 123
    let a = &mut bits_a[..];
124 124
    let b = &mut bits_b[..];
125 125
139 139
    super::put(b, 33);
140 140
    try testing::expect(not super::eq(a, b));
141 141
}
142 142
143 143
/// Test copy operation.
144 -
@test unsafe fn testCopy() throws (testing::TestError) {
144 +
@test fn testCopy() throws (testing::TestError) {
145 145
    let mut bits_a: [u32; 2] = [0; 2];
146 146
    let mut bits_b: [u32; 2] = [0; 2];
147 147
    let a = &mut bits_a[..];
148 148
    let b = &mut bits_b[..];
149 149
158 158
    try testing::expect(super::contains(b, 31));
159 159
    try testing::expect(super::contains(b, 63));
160 160
}
161 161
162 162
/// Test clearAll operation.
163 -
@test unsafe fn testClearAll() throws (testing::TestError) {
163 +
@test fn testClearAll() throws (testing::TestError) {
164 164
    let mut bits: [u32; 2] = [0; 2];
165 165
    let bs = &mut bits[..];
166 166
167 167
    super::put(bs, 0);
168 168
    super::put(bs, 31);
175 175
    try testing::expect(not super::contains(bs, 0));
176 176
    try testing::expect(not super::contains(bs, 31));
177 177
}
178 178
179 179
/// Test iteration over set bits.
180 -
@test unsafe fn testIter() throws (testing::TestError) {
180 +
@test fn testIter() throws (testing::TestError) {
181 181
    let mut bits: [u32; 2] = [0; 2];
182 182
    let bs = &mut bits[..];
183 183
184 184
    super::put(bs, 3);
185 185
    super::put(bs, 31);
198 198
    try testing::expect(count == 4);
199 199
    try testing::expect(sum == 3 + 31 + 32 + 50);
200 200
}
201 201
202 202
/// Test iteration on empty bitset.
203 -
@test unsafe fn testIterEmpty() throws (testing::TestError) {
203 +
@test fn testIterEmpty() throws (testing::TestError) {
204 204
    let mut bits: [u32; 2] = [0; 2];
205 205
    let bs = &mut bits[..];
206 206
    let mut it = super::iter(bs);
207 207
    let result = super::iterNext(&mut it, bs);
208 208
220 220
    try testing::expect(super::wordsFor(65) == 3);
221 221
    try testing::expect(super::wordsFor(0xFFFFFFFF) == 134217728);
222 222
}
223 223
224 224
/// Test out-of-bounds access is safe.
225 -
@test unsafe fn testOutOfBounds() throws (testing::TestError) {
225 +
@test fn testOutOfBounds() throws (testing::TestError) {
226 226
    let mut bits: [u32; 1] = [0; 1];
227 227
    let bs = &mut bits[..];
228 228
229 229
    // Setting beyond length should be ignored.
230 230
    super::put(bs, 100);