use of span type for universal array views

master
Francisco Paisana 4 years ago
parent a9d882c1f1
commit 76a62909c1

@ -0,0 +1,125 @@
/*
* Copyright 2013-2020 Software Radio Systems Limited
*
* This file is part of srsLTE.
*
* srsLTE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsLTE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* A copy of the GNU Affero General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#ifndef SRSLTE_SPAN_H
#define SRSLTE_SPAN_H
#include "srslte/common/common.h"
#include <algorithm>
#include <array>
#include <cassert>
namespace srslte {
template <typename T>
class span
{
public:
using element_type = T;
using value_type = typename std::remove_cv<T>::type;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = element_type*;
using const_pointer = const element_type*;
using reference = element_type&;
using const_reference = const element_type&;
using iterator = pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
constexpr span() noexcept = default;
constexpr span(pointer ptr_, size_type N_) noexcept : ptr(ptr_), len(N_) {}
template <std::size_t N>
constexpr span(element_type (&arr)[N]) noexcept : ptr(arr), len(N)
{}
template <std::size_t N>
constexpr span(std::array<value_type, N>& arr) noexcept : ptr(arr.data()), len(N)
{}
template <std::size_t N>
constexpr span(const std::array<value_type, N>& arr) noexcept : ptr(arr.data()), len(N)
{}
constexpr span(const std::initializer_list<T>& lst) :
ptr(lst.begin() == lst.end() ? (T*)nullptr : lst.begin()),
len(lst.size())
{}
constexpr span(byte_buffer_t& buffer) : ptr(buffer.msg), len(buffer.N_bytes) {}
constexpr span(const byte_buffer_t& buffer) : ptr(buffer.msg), len(buffer.N_bytes) {}
constexpr span(unique_byte_buffer_t& buffer) : ptr(buffer->msg), len(buffer->N_bytes) {}
constexpr span(const unique_byte_buffer_t& buffer) : ptr(buffer->msg), len(buffer->N_bytes) {}
template <typename Container>
constexpr span(Container& cont) : ptr(cont.data()), len(cont.size())
{}
template <typename OtherElementType>
constexpr span(const span<OtherElementType>& other) noexcept : ptr(other.ptr), len(other.size())
{}
~span() noexcept = default;
span& operator=(const span& other) noexcept = default;
constexpr size_type size() const noexcept { return len; }
reference operator[](size_type idx) const
{
assert(idx < len && "index out of bounds!");
return ptr[idx];
}
constexpr bool empty() const noexcept { return size() == 0; }
constexpr reference front() const { return *data(); }
constexpr reference back() const { return *(data() + size() - 1); }
constexpr pointer data() const noexcept { return ptr; }
constexpr iterator begin() const noexcept { return data(); }
constexpr iterator end() const noexcept { return data() + size(); }
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
bool equals(span rhs) const { return (len == rhs.len) ? std::equal(begin(), end(), rhs.begin()) : false; }
// slicing operations
span<element_type> subspan(size_type offset, size_type count) const
{
assert(count <= len && "size out of bounds!");
return {data() + offset, count};
}
constexpr span<element_type> first(size_type count) const { return subspan(0, count); }
constexpr span<element_type> last(size_type count) const { return subspan(size() - count, count); }
private:
pointer ptr = nullptr;
size_type len = 0;
};
template <typename T>
inline bool operator==(span<T> lhs, span<T> rhs)
{
return lhs.equals(rhs);
}
template <typename T>
inline bool operator!=(span<T> lhs, span<T> rhs)
{
return not lhs.equals(rhs);
}
using byte_span = span<uint8_t>;
} // namespace srslte
#endif // SRSLTE_SPAN_H

@ -26,3 +26,7 @@ add_executable(scope_exit_test scope_exit_test.cc)
target_link_libraries(scope_exit_test srslte_common)
add_test(scope_exit_test scope_exit_test)
add_executable(span_test span_test.cc)
target_link_libraries(span_test srslte_common)
add_test(span_test span_test)

@ -0,0 +1,141 @@
/*
* Copyright 2013-2020 Software Radio Systems Limited
*
* This file is part of srsLTE.
*
* srsLTE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsLTE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* A copy of the GNU Affero General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#include "srslte/adt/span.h"
#include "srslte/common/test_common.h"
#include <array>
#include <vector>
int test_span_access()
{
std::vector<int> values{1, 2, 3, 4, 5, 6, 7};
{
srslte::span<int> view{values};
// access operators
TESTASSERT(view.size() == 7);
TESTASSERT(view[0] == 1);
TESTASSERT(view.front() == 1);
TESTASSERT(view.back() == 7);
TESTASSERT(*view.begin() == 1);
TESTASSERT(*view.rbegin() == 7);
// slicing
TESTASSERT(view.first(7) == view);
TESTASSERT(view.last(7) == view);
TESTASSERT(view.first(4).size());
TESTASSERT(view.first(4)[0] == 1);
TESTASSERT(view.last(4)[0] == 4);
}
TESTASSERT(values.size() == 7);
return SRSLTE_SUCCESS;
}
int test_span_conversion()
{
std::vector<int> values{2, 3, 4, 5, 6, 7, 8};
std::array<int, 7> values2{2, 3, 4, 5, 6, 7, 8};
{
// TEST: changing individual values
srslte::span<int> v{values}, v2{values2};
TESTASSERT(v == v2);
v[0] = 3;
TESTASSERT(v != v2);
}
TESTASSERT(values[0] == 3);
TESTASSERT(values.size() == 7);
TESTASSERT(values2.size() == 7);
{
// TEST: const context
const srslte::span<int> v{values}, v2{values2};
TESTASSERT(v != v2);
TESTASSERT(v[0] == 3);
TESTASSERT(v2[0] == 2);
TESTASSERT(v.last(v.size() - 1) == v2.last(v2.size() - 1));
}
values[0] = 2;
{
// TEST: raw arrays
int carray[] = {2, 3, 4, 5, 6, 7, 8};
srslte::span<int> v{values}, v2{carray};
TESTASSERT(v == v2);
TESTASSERT(v2.size() == v.size());
}
{
// TEST: initializer lists
srslte::span<const int> v{{1, 2, 3, 4, 5, 6, 7}};
TESTASSERT(v.size() == 7);
TESTASSERT(v[0] == 1);
}
return SRSLTE_SUCCESS;
}
int test_byte_buffer_conversion()
{
auto foo = [](srslte::byte_span buffer) { return buffer.size() == 5 and buffer[4] == 4; };
auto cfoo = [](const srslte::byte_span buffer) { return buffer.size() == 5 and buffer[4] == 4; };
srslte::byte_buffer_t pdu;
pdu.N_bytes = 5;
pdu.msg[0] = 0;
pdu.msg[1] = 1;
pdu.msg[2] = 2;
pdu.msg[3] = 3;
pdu.msg[4] = 4;
{
srslte::byte_span v{pdu};
TESTASSERT(v.size() == 5);
TESTASSERT(v[0] == 0);
TESTASSERT(v[2] == 2);
TESTASSERT(v[4] == 4);
}
const srslte::byte_buffer_t& pdu2 = pdu;
{
const srslte::byte_span v{pdu2};
TESTASSERT(v.size() == 5);
TESTASSERT(v[0] == 0);
TESTASSERT(v[2] == 2);
TESTASSERT(v[4] == 4);
}
TESTASSERT(foo(pdu));
TESTASSERT(cfoo(pdu));
return SRSLTE_SUCCESS;
}
int main()
{
TESTASSERT(test_span_access() == SRSLTE_SUCCESS);
TESTASSERT(test_span_conversion() == SRSLTE_SUCCESS);
TESTASSERT(test_byte_buffer_conversion() == SRSLTE_SUCCESS);
printf("Success\n");
return SRSLTE_SUCCESS;
}

@ -375,10 +375,10 @@ int test_s1ap_mobility(mobility_test_params test_params)
/* Receive MeasReport from UE (correct if PCI=2) */
if (test_params.fail_at == mobility_test_params::test_event::wrong_measreport) {
uint8_t meas_report[] = {0x08, 0x10, 0x38, 0x74, 0x00, 0x0D, 0xBC, 0x80}; // PCI == 3
test_helpers::copy_msg_to_buffer(pdu, meas_report, sizeof(meas_report));
test_helpers::copy_msg_to_buffer(pdu, meas_report);
} else {
uint8_t meas_report[] = {0x08, 0x10, 0x38, 0x74, 0x00, 0x09, 0xBC, 0x80}; // PCI == 2
test_helpers::copy_msg_to_buffer(pdu, meas_report, sizeof(meas_report));
test_helpers::copy_msg_to_buffer(pdu, meas_report);
}
tester.rrc.write_pdu(tester.rnti, 1, std::move(pdu));
tester.tic();
@ -395,7 +395,7 @@ int test_s1ap_mobility(mobility_test_params test_params)
if (test_params.fail_at == mobility_test_params::test_event::concurrent_ho) {
s1ap.last_ho_required = {};
uint8_t meas_report[] = {0x08, 0x10, 0x38, 0x74, 0x00, 0x09, 0xBC, 0x80}; // PCI == 2
test_helpers::copy_msg_to_buffer(pdu, meas_report, sizeof(meas_report));
test_helpers::copy_msg_to_buffer(pdu, meas_report);
tester.rrc.write_pdu(tester.rnti, 1, std::move(pdu));
tester.tic();
TESTASSERT(s1ap.last_ho_required.rrc_container == nullptr);
@ -430,7 +430,7 @@ int test_s1ap_mobility(mobility_test_params test_params)
0x01, 0x48, 0x04, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0xa0, 0x07, 0xa0,
0x10, 0x00, 0x01, 0x00, 0x05, 0x00, 0xa7, 0xd0, 0xc1, 0xf6, 0xaf, 0x3e, 0x12, 0xcc,
0x86, 0x0d, 0x30, 0x00, 0x0b, 0x5a, 0x02, 0x17, 0x86, 0x00, 0x05, 0xa0, 0x20};
test_helpers::copy_msg_to_buffer(pdu, ho_cmd_rrc_container, sizeof(ho_cmd_rrc_container));
test_helpers::copy_msg_to_buffer(pdu, ho_cmd_rrc_container);
TESTASSERT(s1ap.last_enb_status.rnti != tester.rnti);
tester.rrc.ho_preparation_complete(tester.rnti, true, std::move(pdu));
TESTASSERT(s1ap.last_enb_status.status_present);
@ -458,10 +458,10 @@ int test_intraenb_mobility(mobility_test_params test_params)
/* Receive MeasReport from UE (correct if PCI=2) */
if (test_params.fail_at == mobility_test_params::test_event::wrong_measreport) {
uint8_t meas_report[] = {0x08, 0x10, 0x38, 0x74, 0x00, 0x0D, 0xBC, 0x80}; // PCI == 3
test_helpers::copy_msg_to_buffer(pdu, meas_report, sizeof(meas_report));
test_helpers::copy_msg_to_buffer(pdu, meas_report);
} else {
uint8_t meas_report[] = {0x08, 0x10, 0x38, 0x74, 0x00, 0x09, 0xBC, 0x80}; // PCI == 2
test_helpers::copy_msg_to_buffer(pdu, meas_report, sizeof(meas_report));
test_helpers::copy_msg_to_buffer(pdu, meas_report);
}
tester.rrc.write_pdu(tester.rnti, 1, std::move(pdu));
tester.tic();
@ -481,7 +481,7 @@ int test_intraenb_mobility(mobility_test_params test_params)
if (test_params.fail_at == mobility_test_params::test_event::concurrent_ho) {
tester.pdcp.last_sdu = {};
uint8_t meas_report[] = {0x08, 0x10, 0x38, 0x74, 0x00, 0x09, 0xBC, 0x80}; // PCI == 2
test_helpers::copy_msg_to_buffer(pdu, meas_report, sizeof(meas_report));
test_helpers::copy_msg_to_buffer(pdu, meas_report);
tester.rrc.write_pdu(tester.rnti, 1, std::move(pdu));
tester.tic();
TESTASSERT(tester.pdcp.last_sdu.sdu == nullptr);
@ -523,7 +523,7 @@ int test_intraenb_mobility(mobility_test_params test_params)
/* Test Case: Terminate first Handover. No extra messages should be sent DL. SR/CQI resources match recfg message */
uint8_t recfg_complete[] = {0x10, 0x00};
test_helpers::copy_msg_to_buffer(pdu, recfg_complete, sizeof(recfg_complete));
test_helpers::copy_msg_to_buffer(pdu, recfg_complete);
tester.rrc.write_pdu(tester.rnti, rb_id_t::RB_ID_SRB2, std::move(pdu));
TESTASSERT(tester.pdcp.last_sdu.sdu == nullptr);
sched_interface::ue_cfg_t& ue_cfg = tester.mac.ue_db[tester.rnti];
@ -536,7 +536,7 @@ int test_intraenb_mobility(mobility_test_params test_params)
/* Test Case: The RRC should be able to start a new handover */
uint8_t meas_report[] = {0x08, 0x10, 0x38, 0x74, 0x00, 0x05, 0xBC, 0x80}; // PCI == 1
test_helpers::copy_msg_to_buffer(pdu, meas_report, sizeof(meas_report));
test_helpers::copy_msg_to_buffer(pdu, meas_report);
tester.rrc.write_pdu(tester.rnti, 1, std::move(pdu));
tester.tic();
TESTASSERT(tester.s1ap.last_ho_required.rrc_container == nullptr);

@ -23,7 +23,8 @@
#define SRSENB_TEST_HELPERS_H
#include "srsenb/test/common/dummy_classes.h"
#include <srslte/common/log_filter.h>
#include "srslte/adt/span.h"
#include "srslte/common/log_filter.h"
using namespace srsenb;
using namespace asn1::rrc;
@ -203,9 +204,9 @@ int parse_default_cfg(rrc_cfg_t* rrc_cfg, srsenb::all_args_t& args)
}
template <typename ASN1Type>
bool unpack_asn1(ASN1Type& asn1obj, const srslte::unique_byte_buffer_t& pdu)
bool unpack_asn1(ASN1Type& asn1obj, const srslte::byte_span pdu)
{
asn1::cbit_ref bref{pdu->msg, pdu->N_bytes};
asn1::cbit_ref bref{pdu.data(), (uint32_t)pdu.size()};
if (asn1obj.unpack(bref) != asn1::SRSASN_SUCCESS) {
srslte::logmap::get("TEST")->error("Failed to unpack ASN1 type\n");
return false;
@ -213,12 +214,12 @@ bool unpack_asn1(ASN1Type& asn1obj, const srslte::unique_byte_buffer_t& pdu)
return true;
}
void copy_msg_to_buffer(srslte::unique_byte_buffer_t& pdu, uint8_t* msg, size_t nof_bytes)
void copy_msg_to_buffer(srslte::unique_byte_buffer_t& pdu, srslte::byte_span msg)
{
srslte::byte_buffer_pool* pool = srslte::byte_buffer_pool::get_instance();
pdu = srslte::allocate_unique_buffer(*pool, true);
memcpy(pdu->msg, msg, nof_bytes);
pdu->N_bytes = nof_bytes;
memcpy(pdu->msg, msg.data(), msg.size());
pdu->N_bytes = msg.size();
}
int bring_rrc_to_reconf_state(srsenb::rrc& rrc, srslte::timer_handler& timers, uint16_t rnti)
@ -227,7 +228,7 @@ int bring_rrc_to_reconf_state(srsenb::rrc& rrc, srslte::timer_handler& timers, u
// Send RRCConnectionRequest
uint8_t rrc_conn_request[] = {0x40, 0x12, 0xf6, 0xfb, 0xe2, 0xc6};
copy_msg_to_buffer(pdu, rrc_conn_request, sizeof(rrc_conn_request));
copy_msg_to_buffer(pdu, rrc_conn_request);
rrc.write_pdu(rnti, 0, std::move(pdu));
timers.step_all();
rrc.tti_clock();
@ -236,7 +237,7 @@ int bring_rrc_to_reconf_state(srsenb::rrc& rrc, srslte::timer_handler& timers, u
uint8_t rrc_conn_setup_complete[] = {0x20, 0x00, 0x40, 0x2e, 0x90, 0x50, 0x49, 0xe8, 0x06, 0x0e, 0x82, 0xa2,
0x17, 0xec, 0x13, 0xe2, 0x0f, 0x00, 0x02, 0x02, 0x5e, 0xdf, 0x7c, 0x58,
0x05, 0xc0, 0xc0, 0x00, 0x08, 0x04, 0x03, 0xa0, 0x23, 0x23, 0xc0};
copy_msg_to_buffer(pdu, rrc_conn_setup_complete, sizeof(rrc_conn_setup_complete));
copy_msg_to_buffer(pdu, rrc_conn_setup_complete);
rrc.write_pdu(rnti, 1, std::move(pdu));
timers.step_all();
rrc.tti_clock();
@ -266,7 +267,7 @@ int bring_rrc_to_reconf_state(srsenb::rrc& rrc, srslte::timer_handler& timers, u
// Send SecurityModeComplete
uint8_t sec_mode_complete[] = {0x28, 0x00};
copy_msg_to_buffer(pdu, sec_mode_complete, sizeof(sec_mode_complete));
copy_msg_to_buffer(pdu, sec_mode_complete);
rrc.write_pdu(rnti, 1, std::move(pdu));
timers.step_all();
rrc.tti_clock();
@ -274,14 +275,14 @@ int bring_rrc_to_reconf_state(srsenb::rrc& rrc, srslte::timer_handler& timers, u
// send UE cap info
uint8_t ue_cap_info[] = {0x38, 0x01, 0x01, 0x0c, 0x98, 0x00, 0x00, 0x18, 0x00, 0x0f,
0x30, 0x20, 0x80, 0x00, 0x01, 0x00, 0x0e, 0x01, 0x00, 0x00};
copy_msg_to_buffer(pdu, ue_cap_info, sizeof(ue_cap_info));
copy_msg_to_buffer(pdu, ue_cap_info);
rrc.write_pdu(rnti, 1, std::move(pdu));
timers.step_all();
rrc.tti_clock();
// RRCConnectionReconfiguration was sent. Send RRCConnectionReconfigurationComplete
uint8_t rrc_conn_reconf_complete[] = {0x10, 0x00};
copy_msg_to_buffer(pdu, rrc_conn_reconf_complete, sizeof(rrc_conn_reconf_complete));
copy_msg_to_buffer(pdu, rrc_conn_reconf_complete);
rrc.write_pdu(rnti, 1, std::move(pdu));
timers.step_all();
rrc.tti_clock();

Loading…
Cancel
Save