Skip to content

Commit

Permalink
Fix dangling next pointer 2 (#56)
Browse files Browse the repository at this point in the history
This is same as pull request
[44](#44) but with much cleaner
test implementation.
  • Loading branch information
mitchellh committed Aug 6, 2023
2 parents d79c54c + 0fbc672 commit 432e906
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub fn Intrusive(
if (b.heap.next) |b_next| {
a.heap.next = b_next;
b_next.heap.prev = a;
b.heap.next = null;
}

// If A has a child, then B becomes the leftmost sibling
Expand Down Expand Up @@ -340,3 +341,39 @@ test "heap: million values" {
try testing.expect(h.deleteMin() == null);
try testing.expect(count == NUM_TIMERS);
}

test "heap: dangling next pointer" {
const testing = std.testing;
const Elem = struct {
const Self = @This();
value: usize = 0,
heap: IntrusiveField(Self) = .{},
};

const Heap = Intrusive(Elem, void, (struct {
fn less(ctx: void, a: *Elem, b: *Elem) bool {
_ = ctx;
return a.value < b.value;
}
}).less);

var a: Elem = .{ .value = 2 };
var b: Elem = .{ .value = 4 };
var c: Elem = .{ .value = 5 };
var d: Elem = .{ .value = 1 };
var e: Elem = .{ .value = 3 };

var h: Heap = .{ .context = {} };
h.insert(&a);
h.insert(&b);
h.insert(&c);
h.insert(&d);
h.insert(&e);

try testing.expect(h.deleteMin().?.value == 1);
try testing.expect(h.deleteMin().?.value == 2);
try testing.expect(h.deleteMin().?.value == 3);
try testing.expect(h.deleteMin().?.value == 4);
try testing.expect(h.deleteMin().?.value == 5);
try testing.expect(h.deleteMin() == null);
}

0 comments on commit 432e906

Please sign in to comment.