From 7dcb703d06521a03fdd1a1959ac18fd7b215a45d Mon Sep 17 00:00:00 2001 From: Francisco Date: Thu, 11 Mar 2021 11:09:21 +0000 Subject: [PATCH] adt lib,bugfix - fix bounded_bitset resize to clear bits outside of mask correctly --- lib/include/srslte/adt/bounded_bitset.h | 10 +++--- lib/test/adt/bounded_bitset_test.cc | 44 +++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/lib/include/srslte/adt/bounded_bitset.h b/lib/include/srslte/adt/bounded_bitset.h index 1b4e351ab..3ebc85762 100644 --- a/lib/include/srslte/adt/bounded_bitset.h +++ b/lib/include/srslte/adt/bounded_bitset.h @@ -33,9 +33,9 @@ class bounded_bitset static const size_t bits_per_word = 8 * sizeof(word_t); public: - constexpr bounded_bitset() : buffer(), cur_size(0) {} + constexpr bounded_bitset() = default; - constexpr explicit bounded_bitset(size_t cur_size_) : buffer(), cur_size(cur_size_) {} + constexpr explicit bounded_bitset(size_t cur_size_) : cur_size(cur_size_) {} constexpr size_t max_size() const noexcept { return N; } @@ -54,7 +54,7 @@ public: cur_size = new_size; sanitize_(); for (size_t i = nof_words_(); i < max_nof_words_(); ++i) { - get_word_(i) = static_cast(0); + buffer[i] = static_cast(0); } } @@ -268,8 +268,8 @@ public: } private: - word_t buffer[(N - 1) / bits_per_word + 1]; - size_t cur_size; + word_t buffer[(N - 1) / bits_per_word + 1] = {0}; + size_t cur_size = 0; void sanitize_() { diff --git a/lib/test/adt/bounded_bitset_test.cc b/lib/test/adt/bounded_bitset_test.cc index 1eaf2970b..40f4f7279 100644 --- a/lib/test/adt/bounded_bitset_test.cc +++ b/lib/test/adt/bounded_bitset_test.cc @@ -28,7 +28,6 @@ int test_zero_bitset() TESTASSERT(mask2.max_size() == 25); TESTASSERT(mask2.size() == 23); - TESTASSERT(mask2.size() == 23); TESTASSERT(mask2.count() == 0); TESTASSERT(mask2.none()); TESTASSERT(not mask2.any()); @@ -95,7 +94,7 @@ int test_bitset_bitwise_oper() try { mask2 |= mask; } catch (srslte::bad_type_access& c) { - printf("Received exception \"%s\" as expected\n", c.what()); + printf("Received exception \"%s\" (as expected)\n", c.what()); caught = true; } TESTASSERT(caught); @@ -137,6 +136,46 @@ int test_bitset_print() return SRSLTE_SUCCESS; } +int test_bitset_resize() +{ + { + srslte::bounded_bitset<100> bitset; + TESTASSERT(bitset.none() and bitset.size() == 0); + + bitset.resize(100); + TESTASSERT(bitset.none() and bitset.size() == 100); + bitset.fill(0, 100); + TESTASSERT(bitset.all() and bitset.size() == 100); + + bitset.resize(25); + TESTASSERT(bitset.to_uint64() == 0x1ffffff); + TESTASSERT(bitset.all() and bitset.size() == 25); // keeps the data it had for the non-erased bits + + bitset.resize(100); + TESTASSERT(bitset.count() == 25 and bitset.size() == 100); + } + + { + // TEST: Reverse case + srslte::bounded_bitset<100, true> bitset; + TESTASSERT(bitset.none() and bitset.size() == 0); + + bitset.resize(100); + TESTASSERT(bitset.none() and bitset.size() == 100); + bitset.fill(0, 100); + TESTASSERT(bitset.all() and bitset.size() == 100); + + bitset.resize(25); + TESTASSERT(bitset.to_uint64() == 0x1ffffff); + TESTASSERT(bitset.all() and bitset.size() == 25); // keeps the data it had for the non-erased bits + + bitset.resize(100); + TESTASSERT(bitset.count() == 25 and bitset.size() == 100); + } + + return SRSLTE_SUCCESS; +} + int main() { TESTASSERT(test_zero_bitset() == SRSLTE_SUCCESS); @@ -144,6 +183,7 @@ int main() TESTASSERT(test_bitset_set() == SRSLTE_SUCCESS); TESTASSERT(test_bitset_bitwise_oper() == SRSLTE_SUCCESS); TESTASSERT(test_bitset_print() == SRSLTE_SUCCESS); + TESTASSERT(test_bitset_resize() == SRSLTE_SUCCESS); printf("Success\n"); return 0; }