From d2fc085b8cee03bf2bfc89ecdc90c1c3c743e12b Mon Sep 17 00:00:00 2001 From: LiviaSun <33578456+ChuyueSun@users.noreply.github.com> Date: Fri, 2 Aug 2024 18:29:50 -0700 Subject: [PATCH] update heap unit tests (#7324) * new heap invariants * change ENSURE to SASSERT for unit test heap * change SASSERT to VERIFY * update heap tests * update * remove one invariant --- src/test/heap.cpp | 3 +-- src/util/heap.h | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/test/heap.cpp b/src/test/heap.cpp index 6a5bc7b9f..25a509dc8 100644 --- a/src/test/heap.cpp +++ b/src/test/heap.cpp @@ -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_set; typedef heap int_heap; -#define N 10000 +#define N 100 static random_gen heap_rand(1); @@ -146,4 +146,3 @@ void tst_heap() { tst2(); } } - diff --git a/src/util/heap.h b/src/util/heap.h index c080c6ebd..71cafeeb8 100644 --- a/src/util/heap.h +++ b/src/util/heap.h @@ -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(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(m_values.size())) { SASSERT(m_value2indices[m_values[idx]] == idx); SASSERT(parent(idx) == 0 || !less_than(m_values[idx], m_values[parent(idx)]));