Skip to content

Commit 381c6a3

Browse files
joachimschmidt557andrewrk
authored andcommitted
Correct the isEmpty function
Integrate isEmpty into the tests for std.atomic.Queue Fix wrong test Oops Simpler checking
1 parent c7bcf1a commit 381c6a3

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

std/atomic/queue.zig

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn Queue(comptime T: type) type {
100100
pub fn isEmpty(self: *Self) bool {
101101
const held = self.mutex.acquire();
102102
defer held.release();
103-
return self.head != null;
103+
return self.head == null;
104104
}
105105

106106
pub fn dump(self: *Self) void {
@@ -172,20 +172,25 @@ test "std.atomic.Queue" {
172172
};
173173

174174
if (builtin.single_threaded) {
175+
expect(context.queue.isEmpty());
175176
{
176177
var i: usize = 0;
177178
while (i < put_thread_count) : (i += 1) {
178179
expect(startPuts(&context) == 0);
179180
}
180181
}
182+
expect(!context.queue.isEmpty());
181183
context.puts_done = 1;
182184
{
183185
var i: usize = 0;
184186
while (i < put_thread_count) : (i += 1) {
185187
expect(startGets(&context) == 0);
186188
}
187189
}
190+
expect(context.queue.isEmpty());
188191
} else {
192+
expect(context.queue.isEmpty());
193+
189194
var putters: [put_thread_count]*std.Thread = undefined;
190195
for (putters) |*t| {
191196
t.* = try std.Thread.spawn(&context, startPuts);
@@ -200,6 +205,8 @@ test "std.atomic.Queue" {
200205
_ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
201206
for (getters) |t|
202207
t.wait();
208+
209+
expect(context.queue.isEmpty());
203210
}
204211

205212
if (context.put_sum != context.get_sum) {
@@ -250,54 +257,66 @@ fn startGets(ctx: *Context) u8 {
250257

251258
test "std.atomic.Queue single-threaded" {
252259
var queue = Queue(i32).init();
260+
expect(queue.isEmpty());
253261

254262
var node_0 = Queue(i32).Node{
255263
.data = 0,
256264
.next = undefined,
257265
.prev = undefined,
258266
};
259267
queue.put(&node_0);
268+
expect(!queue.isEmpty());
260269

261270
var node_1 = Queue(i32).Node{
262271
.data = 1,
263272
.next = undefined,
264273
.prev = undefined,
265274
};
266275
queue.put(&node_1);
276+
expect(!queue.isEmpty());
267277

268278
expect(queue.get().?.data == 0);
279+
expect(!queue.isEmpty());
269280

270281
var node_2 = Queue(i32).Node{
271282
.data = 2,
272283
.next = undefined,
273284
.prev = undefined,
274285
};
275286
queue.put(&node_2);
287+
expect(!queue.isEmpty());
276288

277289
var node_3 = Queue(i32).Node{
278290
.data = 3,
279291
.next = undefined,
280292
.prev = undefined,
281293
};
282294
queue.put(&node_3);
295+
expect(!queue.isEmpty());
283296

284297
expect(queue.get().?.data == 1);
298+
expect(!queue.isEmpty());
285299

286300
expect(queue.get().?.data == 2);
301+
expect(!queue.isEmpty());
287302

288303
var node_4 = Queue(i32).Node{
289304
.data = 4,
290305
.next = undefined,
291306
.prev = undefined,
292307
};
293308
queue.put(&node_4);
309+
expect(!queue.isEmpty());
294310

295311
expect(queue.get().?.data == 3);
296312
node_3.next = null;
313+
expect(!queue.isEmpty());
297314

298315
expect(queue.get().?.data == 4);
316+
expect(queue.isEmpty());
299317

300318
expect(queue.get() == null);
319+
expect(queue.isEmpty());
301320
}
302321

303322
test "std.atomic.Queue dump" {

0 commit comments

Comments
 (0)