authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2019-06-16 22:11:09+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-18 16:31:06-04:00
log381c6a38b145665a22440f7aa816f0ddd9b70ee5
treee95a50c1a22b46ae55a7e15e4bd344e8421f4965
parentc7bcf1a447de08a18f5113c97744233a54bb9af7

Correct the isEmpty function

Integrate isEmpty into the tests for std.atomic.Queue Fix wrong test Oops Simpler checking

1 files changed, 20 insertions(+), 1 deletions(-)

std/atomic/queue.zig+20-1
......@@ -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,12 +172,14 @@ 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;
......@@ -185,7 +187,10 @@ test "std.atomic.Queue" {
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,6 +257,7 @@ fn startGets(ctx: *Context) u8 {
250257
251258test "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,
......@@ -257,6 +265,7 @@ test "std.atomic.Queue single-threaded" {
257265 .prev = undefined,
258266 };
259267 queue.put(&node_0);
268 expect(!queue.isEmpty());
260269
261270 var node_1 = Queue(i32).Node{
262271 .data = 1,
......@@ -264,8 +273,10 @@ test "std.atomic.Queue single-threaded" {
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,
......@@ -273,6 +284,7 @@ test "std.atomic.Queue single-threaded" {
273284 .prev = undefined,
274285 };
275286 queue.put(&node_2);
287 expect(!queue.isEmpty());
276288
277289 var node_3 = Queue(i32).Node{
278290 .data = 3,
......@@ -280,10 +292,13 @@ test "std.atomic.Queue single-threaded" {
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,
......@@ -291,13 +306,17 @@ test "std.atomic.Queue single-threaded" {
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
303322test "std.atomic.Queue dump" {