Skip to content

Commit

Permalink
update heap unit tests (#7324)
Browse files Browse the repository at this point in the history
* new heap invariants

* change ENSURE to SASSERT for unit test heap

* change SASSERT to VERIFY

* update heap tests

* update

* remove one invariant
  • Loading branch information
ChuyueSun committed Aug 3, 2024
1 parent fce4b36 commit d2fc085
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/test/heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct lt_proc { bool operator()(int v1, int v2) const { return v1 < v2; } };
//struct int_hash_proc { unsigned operator()(int v) const { std::cout << "hash " << v << "\n"; VERIFY(v >= 0); return v; }};
//typedef int_hashtable<int_hash_proc, default_eq<int> > int_set;
typedef heap<lt_proc> int_heap;
#define N 10000
#define N 100

static random_gen heap_rand(1);

Expand Down Expand Up @@ -146,4 +146,3 @@ void tst_heap() {
tst2();
}
}

13 changes: 13 additions & 0 deletions src/util/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ class heap : private LT {
}

bool check_invariant_core(int idx) const {

// Check that m_values starts with a dummy value at index 0.
if (m_values.empty() || m_values[0] != -1) {
return false;
}

// // All indices in m_value2indices that are not used in m_values should be 0
// for (int val = 0; val < static_cast<int>(m_value2indices.size()); ++val) {
// if (std::find(m_values.begin(), m_values.end(), val) == m_values.end() && m_value2indices[val] != 0) {
// return false; // Unused indices should have a 0 value
// }
// }

if (idx < static_cast<int>(m_values.size())) {
SASSERT(m_value2indices[m_values[idx]] == idx);
SASSERT(parent(idx) == 0 || !less_than(m_values[idx], m_values[parent(idx)]));
Expand Down

0 comments on commit d2fc085

Please sign in to comment.