From 4457bbda7ef01cbc52bd7a17b4be6d7b0bcc551e Mon Sep 17 00:00:00 2001 From: Francisco Paisana Date: Thu, 13 Aug 2020 19:15:07 +0100 Subject: [PATCH] added new asserts to interval interface --- lib/include/srslte/adt/bounded_bitset.h | 17 ++++++++------- lib/include/srslte/adt/interval.h | 27 +++++++++++------------- lib/test/adt/interval_test.cc | 18 ++++++++++++++++ srsenb/src/stack/mac/scheduler_grid.cc | 4 ++-- srsenb/src/stack/mac/scheduler_metric.cc | 4 ++-- 5 files changed, 43 insertions(+), 27 deletions(-) diff --git a/lib/include/srslte/adt/bounded_bitset.h b/lib/include/srslte/adt/bounded_bitset.h index 74cb5f086..0a5f60c39 100644 --- a/lib/include/srslte/adt/bounded_bitset.h +++ b/lib/include/srslte/adt/bounded_bitset.h @@ -55,7 +55,8 @@ public: std::string msg = "ERROR: new size=" + std::to_string(new_size) + " exceeds bitset capacity=" + std::to_string(max_size()); THROW_BAD_ACCESS(msg.c_str()); - } else if (new_size == cur_size) { + } + if (new_size == cur_size) { return; } cur_size = new_size; @@ -65,7 +66,7 @@ public: } } - void set(size_t pos, bool val) noexcept + void set(size_t pos, bool val) { assert_within_bounds_(pos, true); if (val) { @@ -75,13 +76,13 @@ public: } } - void set(size_t pos) noexcept + void set(size_t pos) { assert_within_bounds_(pos, true); set_(pos); } - void reset(size_t pos) noexcept + void reset(size_t pos) { assert_within_bounds_(pos, true); reset_(pos); @@ -94,7 +95,7 @@ public: } } - bool test(size_t pos) const noexcept + bool test(size_t pos) const { assert_within_bounds_(pos, true); return test_(pos); @@ -109,7 +110,7 @@ public: return *this; } - bounded_bitset& fill(size_t startpos, size_t endpos, bool value = true) noexcept + bounded_bitset& fill(size_t startpos, size_t endpos, bool value = true) { assert_within_bounds_(startpos, false); assert_within_bounds_(endpos, false); @@ -151,7 +152,7 @@ public: return false; } - bool any(size_t start, size_t stop) const noexcept + bool any(size_t start, size_t stop) const { assert_within_bounds_(start, false); assert_within_bounds_(stop, false); @@ -227,7 +228,7 @@ public: return ret; } - std::string to_string() const noexcept + std::string to_string() const { std::string s; s.assign(size(), '0'); diff --git a/lib/include/srslte/adt/interval.h b/lib/include/srslte/adt/interval.h index 90bdca218..7928cc54f 100644 --- a/lib/include/srslte/adt/interval.h +++ b/lib/include/srslte/adt/interval.h @@ -22,6 +22,7 @@ #ifndef SRSLTE_INTERVAL_H #define SRSLTE_INTERVAL_H +#include "adt_utils.h" #include #include #include @@ -48,28 +49,24 @@ public: T length() const { return stop_ - start_; } - void inc_length(int n) + void set(T start_point, T stop_point) { - stop_ += n; - assert(stop_ >= start_); - } - - void set_length(T len) - { - assert(len >= 0); - stop_ = start_ + len; + assert(stop_point >= start_point); + start_ = start_point; + stop_ = stop_point; } - void set_start(T start_point) + void expand_by(T len) { - assert(start_point <= stop_); - start_ = start_point; + // Detect length overflows + assert(std::is_unsigned::value or (len >= 0 or length() >= -len)); + stop_ += len; } - void set_stop(T stop_point) + void expand_to(T len) { - assert(start_ <= stop_point); - stop_ = stop_point; + assert(std::is_unsigned::value or len >= 0); + stop_ = start_ + len; } void displace_by(int offset) diff --git a/lib/test/adt/interval_test.cc b/lib/test/adt/interval_test.cc index 47ba279e2..427ba6afc 100644 --- a/lib/test/adt/interval_test.cc +++ b/lib/test/adt/interval_test.cc @@ -74,11 +74,29 @@ int test_interval_intersect() return SRSLTE_SUCCESS; } +int test_interval_expand() +{ + srslte::interval I{}; + srslte::interval I2{}; + + I.expand_by(5); + TESTASSERT(I.length() == 5); + I.expand_by(-5); + TESTASSERT(I.length() == 0); + + I2.expand_by(3); + TESTASSERT(I2.length() == 3); + // I2.expand_by(-4); + + return SRSLTE_SUCCESS; +} + int main() { TESTASSERT(test_interval_init() == SRSLTE_SUCCESS); TESTASSERT(test_interval_overlaps() == SRSLTE_SUCCESS); TESTASSERT(test_interval_contains() == SRSLTE_SUCCESS); TESTASSERT(test_interval_intersect() == SRSLTE_SUCCESS); + TESTASSERT(test_interval_expand() == SRSLTE_SUCCESS); return 0; } diff --git a/srsenb/src/stack/mac/scheduler_grid.cc b/srsenb/src/stack/mac/scheduler_grid.cc index ff3d8f8fc..0e3803ad7 100644 --- a/srsenb/src/stack/mac/scheduler_grid.cc +++ b/srsenb/src/stack/mac/scheduler_grid.cc @@ -538,7 +538,7 @@ bool sf_grid_t::find_ul_alloc(uint32_t L, prb_interval* alloc) const alloc->displace_to(n); } if (not ul_mask.test(n)) { - alloc->inc_length(1); + alloc->expand_by(1); } else if (alloc->length() > 0) { // avoid edges if (n < 3) { @@ -554,7 +554,7 @@ bool sf_grid_t::find_ul_alloc(uint32_t L, prb_interval* alloc) const // Make sure L is allowed by SC-FDMA modulation while (!srslte_dft_precoding_valid_prb(alloc->length())) { - alloc->inc_length(-1); + alloc->expand_by(-1); } return alloc->length() == L; } diff --git a/srsenb/src/stack/mac/scheduler_metric.cc b/srsenb/src/stack/mac/scheduler_metric.cc index 7cbb020c0..a605586fd 100644 --- a/srsenb/src/stack/mac/scheduler_metric.cc +++ b/srsenb/src/stack/mac/scheduler_metric.cc @@ -212,7 +212,7 @@ bool ul_metric_rr::find_allocation(uint32_t L, prb_interval* alloc) alloc->displace_to(n); } if (not used_rb->test(n)) { - alloc->inc_length(1); + alloc->expand_by(1); } else if (alloc->length() > 0) { // avoid edges if (n < 3) { @@ -228,7 +228,7 @@ bool ul_metric_rr::find_allocation(uint32_t L, prb_interval* alloc) // Make sure L is allowed by SC-FDMA modulation while (!srslte_dft_precoding_valid_prb(alloc->length())) { - alloc->inc_length(-1); + alloc->expand_by(-1); } return alloc->length() == L; }