| ... | @@ -230,7 +230,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar | ... | @@ -230,7 +230,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 230 | } else { | 230 | } else { |
| 231 | // The children or grandchildren are the last layer | 231 | // The children or grandchildren are the last layer |
| 232 | const first_child_index = firstChildIndex(index); | 232 | const first_child_index = firstChildIndex(index); |
| 233 | if (first_child_index > self.len) return; | 233 | if (first_child_index >= self.len) return; |
| 234 | | 234 | |
| 235 | const best_descendent = self.bestDescendent(first_child_index, first_grandchild_index, target_order); | 235 | const best_descendent = self.bestDescendent(first_child_index, first_grandchild_index, target_order); |
| 236 | | 236 | |
| ... | @@ -1002,3 +1002,25 @@ test "std.PriorityDequeue: add and remove" { | ... | @@ -1002,3 +1002,25 @@ test "std.PriorityDequeue: add and remove" { |
| 1002 | try expectEqual(@as(usize, 2), queue.removeMax()); | 1002 | try expectEqual(@as(usize, 2), queue.removeMax()); |
| 1003 | try expectEqual(@as(usize, 1), queue.removeMin()); | 1003 | try expectEqual(@as(usize, 1), queue.removeMin()); |
| 1004 | } | 1004 | } |
| | 1005 | |
| | 1006 | var all_cmps_unique = true; |
| | 1007 | |
| | 1008 | test "std.PriorityDeque: don't compare a value to a copy of itself" { |
| | 1009 | var depq = PriorityDequeue(u32, void, struct { |
| | 1010 | fn uniqueLessThan(_: void, a: u32, b: u32) Order { |
| | 1011 | all_cmps_unique = all_cmps_unique and (a != b); |
| | 1012 | return std.math.order(a, b); |
| | 1013 | } |
| | 1014 | }.uniqueLessThan).init(testing.allocator, {}); |
| | 1015 | defer depq.deinit(); |
| | 1016 | |
| | 1017 | try depq.add(1); |
| | 1018 | try depq.add(2); |
| | 1019 | try depq.add(3); |
| | 1020 | try depq.add(4); |
| | 1021 | try depq.add(5); |
| | 1022 | try depq.add(6); |
| | 1023 | |
| | 1024 | _ = depq.removeIndex(2); |
| | 1025 | try expectEqual(all_cmps_unique, true); |
| | 1026 | } |