adt lib,bugfix - fix bounded_bitset resize to clear bits outside of mask correctly

master
Francisco 4 years ago committed by Francisco Paisana
parent 770021e364
commit 7dcb703d06

@ -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<word_t>(0);
buffer[i] = static_cast<word_t>(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_()
{

@ -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;
}

Loading…
Cancel
Save