added new asserts to interval interface

This commit is contained in:
Francisco Paisana 2020-08-13 19:15:07 +01:00
parent 8b174ce2f4
commit 4457bbda7e
5 changed files with 45 additions and 29 deletions

View File

@ -55,7 +55,8 @@ public:
std::string msg = std::string msg =
"ERROR: new size=" + std::to_string(new_size) + " exceeds bitset capacity=" + std::to_string(max_size()); "ERROR: new size=" + std::to_string(new_size) + " exceeds bitset capacity=" + std::to_string(max_size());
THROW_BAD_ACCESS(msg.c_str()); THROW_BAD_ACCESS(msg.c_str());
} else if (new_size == cur_size) { }
if (new_size == cur_size) {
return; return;
} }
cur_size = new_size; 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); assert_within_bounds_(pos, true);
if (val) { if (val) {
@ -75,13 +76,13 @@ public:
} }
} }
void set(size_t pos) noexcept void set(size_t pos)
{ {
assert_within_bounds_(pos, true); assert_within_bounds_(pos, true);
set_(pos); set_(pos);
} }
void reset(size_t pos) noexcept void reset(size_t pos)
{ {
assert_within_bounds_(pos, true); assert_within_bounds_(pos, true);
reset_(pos); 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); assert_within_bounds_(pos, true);
return test_(pos); return test_(pos);
@ -109,7 +110,7 @@ public:
return *this; return *this;
} }
bounded_bitset<N, reversed>& fill(size_t startpos, size_t endpos, bool value = true) noexcept bounded_bitset<N, reversed>& fill(size_t startpos, size_t endpos, bool value = true)
{ {
assert_within_bounds_(startpos, false); assert_within_bounds_(startpos, false);
assert_within_bounds_(endpos, false); assert_within_bounds_(endpos, false);
@ -151,7 +152,7 @@ public:
return false; 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_(start, false);
assert_within_bounds_(stop, false); assert_within_bounds_(stop, false);
@ -227,7 +228,7 @@ public:
return ret; return ret;
} }
std::string to_string() const noexcept std::string to_string() const
{ {
std::string s; std::string s;
s.assign(size(), '0'); s.assign(size(), '0');

View File

@ -22,6 +22,7 @@
#ifndef SRSLTE_INTERVAL_H #ifndef SRSLTE_INTERVAL_H
#define SRSLTE_INTERVAL_H #define SRSLTE_INTERVAL_H
#include "adt_utils.h"
#include <cassert> #include <cassert>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
@ -48,28 +49,24 @@ public:
T length() const { return stop_ - start_; } T length() const { return stop_ - start_; }
void inc_length(int n) void set(T start_point, T stop_point)
{ {
stop_ += n; assert(stop_point >= start_point);
assert(stop_ >= start_);
}
void set_length(T len)
{
assert(len >= 0);
stop_ = start_ + len;
}
void set_start(T start_point)
{
assert(start_point <= stop_);
start_ = start_point; start_ = start_point;
stop_ = stop_point;
} }
void set_stop(T stop_point) void expand_by(T len)
{ {
assert(start_ <= stop_point); // Detect length overflows
stop_ = stop_point; assert(std::is_unsigned<T>::value or (len >= 0 or length() >= -len));
stop_ += len;
}
void expand_to(T len)
{
assert(std::is_unsigned<T>::value or len >= 0);
stop_ = start_ + len;
} }
void displace_by(int offset) void displace_by(int offset)

View File

@ -74,11 +74,29 @@ int test_interval_intersect()
return SRSLTE_SUCCESS; return SRSLTE_SUCCESS;
} }
int test_interval_expand()
{
srslte::interval<uint32_t> I{};
srslte::interval<int> 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() int main()
{ {
TESTASSERT(test_interval_init() == SRSLTE_SUCCESS); TESTASSERT(test_interval_init() == SRSLTE_SUCCESS);
TESTASSERT(test_interval_overlaps() == SRSLTE_SUCCESS); TESTASSERT(test_interval_overlaps() == SRSLTE_SUCCESS);
TESTASSERT(test_interval_contains() == SRSLTE_SUCCESS); TESTASSERT(test_interval_contains() == SRSLTE_SUCCESS);
TESTASSERT(test_interval_intersect() == SRSLTE_SUCCESS); TESTASSERT(test_interval_intersect() == SRSLTE_SUCCESS);
TESTASSERT(test_interval_expand() == SRSLTE_SUCCESS);
return 0; return 0;
} }

View File

@ -538,7 +538,7 @@ bool sf_grid_t::find_ul_alloc(uint32_t L, prb_interval* alloc) const
alloc->displace_to(n); alloc->displace_to(n);
} }
if (not ul_mask.test(n)) { if (not ul_mask.test(n)) {
alloc->inc_length(1); alloc->expand_by(1);
} else if (alloc->length() > 0) { } else if (alloc->length() > 0) {
// avoid edges // avoid edges
if (n < 3) { 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 // Make sure L is allowed by SC-FDMA modulation
while (!srslte_dft_precoding_valid_prb(alloc->length())) { while (!srslte_dft_precoding_valid_prb(alloc->length())) {
alloc->inc_length(-1); alloc->expand_by(-1);
} }
return alloc->length() == L; return alloc->length() == L;
} }

View File

@ -212,7 +212,7 @@ bool ul_metric_rr::find_allocation(uint32_t L, prb_interval* alloc)
alloc->displace_to(n); alloc->displace_to(n);
} }
if (not used_rb->test(n)) { if (not used_rb->test(n)) {
alloc->inc_length(1); alloc->expand_by(1);
} else if (alloc->length() > 0) { } else if (alloc->length() > 0) {
// avoid edges // avoid edges
if (n < 3) { 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 // Make sure L is allowed by SC-FDMA modulation
while (!srslte_dft_precoding_valid_prb(alloc->length())) { while (!srslte_dft_precoding_valid_prb(alloc->length())) {
alloc->inc_length(-1); alloc->expand_by(-1);
} }
return alloc->length() == L; return alloc->length() == L;
} }