Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
test_eui48.cpp
Go to the documentation of this file.
1#include <cassert>
2#include <cinttypes>
3#include <cstring>
4
6
7#include <jau/basic_types.hpp>
8#include <jau/eui48.hpp>
9#include <jau/darray.hpp>
10
11using namespace jau;
12
13static void test_sub01(const lb_endian_t byte_order, const std::string& mac_str, const jau::darray<std::string>& mac_sub_strs, const jau::darray<jau::snsize_t>& indices) {
14 const EUI48 mac(mac_str);
15 printf("Test EUI48 mac: '%s' -> '%s'\n", mac_str.c_str(), mac.toString().c_str());
16 REQUIRE(mac_str == mac.toString());
17
18 int i=0;
20 PRAGMA_DISABLE_WARNING_RESTRICT
21 // bogus gcc 12.2 'may overlap'
22 jau::for_each_const(mac_sub_strs, [&byte_order, &i, &mac, &indices](const std::string &mac_sub_str) {
23 const EUI48Sub mac_sub(mac_sub_str);
24 printf("EUI48Sub mac02_sub: '%s' -> '%s'\n", mac_sub_str.c_str(), mac_sub.toString().c_str());
25 {
26 // cut-off pre- and post-colon in test string, but leave single colon
27 std::string sub_str(mac_sub_str);
28 if( sub_str.size() == 0 ) {
29 sub_str = ":";
30 } else if( sub_str != ":" ) {
31 if( sub_str.size() > 0 && sub_str[0] == ':' ) {
32 sub_str = sub_str.substr(1, sub_str.size());
33 }
34 if( sub_str.size() > 0 && sub_str[sub_str.size()-1] == ':' ) {
35 sub_str = sub_str.substr(0, sub_str.size()-1);
36 }
37 }
38 REQUIRE(sub_str == mac_sub.toString());
39 }
40 jau::snsize_t idx = mac.indexOf(mac_sub, byte_order);
41 REQUIRE( idx == indices.at(i));
42 if( idx >= 0 ) {
43 REQUIRE( mac.contains(mac_sub) );
44 } else {
45 REQUIRE( !mac.contains(mac_sub) );
46 }
47
48 ++i;
49 } );
51 (void) indices;
52}
53
54static void test_sub02(const std::string& mac_sub_str_exp, const std::string& mac_sub_str, const bool expected_result) {
55 std::string errmsg;
56 EUI48Sub mac_sub;
57 const bool res = EUI48Sub::scanEUI48Sub(mac_sub_str, mac_sub, errmsg);
58 if( res ) {
59 printf("EUI48Sub mac_sub: '%s' -> '%s'\n", mac_sub_str.c_str(), mac_sub.toString().c_str());
60 if( expected_result ) {
61 REQUIRE(mac_sub_str_exp == mac_sub.toString());
62 }
63 } else {
64 printf("EUI48Sub mac_sub: '%s' -> Error '%s'\n", mac_sub_str.c_str(), errmsg.c_str());
65 }
66 REQUIRE( expected_result == res );
67}
68
69TEST_CASE( "EUI48 Test 01", "[datatype][eui48]" ) {
70 EUI48 mac01;
71 INFO_STR("EUI48 size: whole0 "+std::to_string(sizeof(EUI48)));
72 INFO_STR("EUI48 size: whole1 "+std::to_string(sizeof(mac01)));
73 INFO_STR("EUI48 size: data1 "+std::to_string(sizeof(mac01.b)));
74 REQUIRE_MSG("EUI48 struct and data size match", sizeof(EUI48) == sizeof(mac01));
75 REQUIRE_MSG("EUI48 struct and data size match", sizeof(mac01) == sizeof(mac01.b));
76
77 {
78 // index [high=5 ... low=0]
79 const std::string mac02_str = "C0:10:22:A0:10:00";
80 const jau::darray<std::string> mac02_sub_strs = { "C0", "C0:10", ":10:22", "10:22", ":10:22:", "10:22:", "10", "10:00", "00", ":", "", "00:10", mac02_str};
81 const jau::darray<jau::snsize_t> mac02_sub_idxs_le = { 5, 4, 3, 3, 3, 3, 1, 0, 0, 0, 0, -1, 0};
82 const jau::darray<jau::snsize_t> mac02_sub_idxs_be = { 0, 0, 1, 1, 1, 1, 4, 4, 5, 0, 0, -1, 0};
83 test_sub01(lb_endian_t::little, mac02_str, mac02_sub_strs, mac02_sub_idxs_le);
84 test_sub01(lb_endian_t::big, mac02_str, mac02_sub_strs, mac02_sub_idxs_be);
85 }
86
87 {
88 // index [high=5 ... low=0]
89 const std::string mac03_str = "01:02:03:04:05:06";
90 const jau::darray<std::string> mac03_sub_strs = { "01", "01:02", ":03:04", "03:04", ":04:05:", "04:05:", "04", "05:06", "06", ":", "", "06:05", mac03_str};
91 const jau::darray<jau::snsize_t> mac03_sub_idxs_le = { 5, 4, 2, 2, 1, 1, 2, 0, 0, 0, 0, -1, 0};
92 const jau::darray<jau::snsize_t> mac03_sub_idxs_be = { 0, 0, 2, 2, 3, 3, 3, 4, 5, 0, 0, -1, 0};
93 test_sub01(lb_endian_t::little, mac03_str, mac03_sub_strs, mac03_sub_idxs_le);
94 test_sub01(lb_endian_t::big, mac03_str, mac03_sub_strs, mac03_sub_idxs_be);
95 }
96 {
97 const std::string mac_sub_str = "C0:10:22:A0:10:00";
98 test_sub02(mac_sub_str, mac_sub_str, true /* expected_result */);
99 }
100 {
101 const std::string mac_sub_str = "0600106";
102 const std::string dummy;
103 test_sub02(dummy, mac_sub_str, false /* expected_result */);
104 }
105 {
106 EUI48 h("01:02:03:04:05:06");
107 EUI48Sub n("01:02");
108 INFO_STR("EUI48 indexOf: h "+h.toString()+", n "+n.toString());
109 REQUIRE(0 == h.indexOf(n, lb_endian_t::big));
110 REQUIRE(4 == h.indexOf(n, lb_endian_t::little));
111 }
112 {
113 EUI48 h("01:02:03:04:05:06");
114 EUI48Sub n("05:06");
115 INFO_STR("EUI48 indexOf: h "+h.toString()+", n "+n.toString());
116 REQUIRE(4 == h.indexOf(n, lb_endian_t::big));
117 REQUIRE(0 == h.indexOf(n, lb_endian_t::little));
118 }
119}
#define INFO_STR(msg)
Definition: catch2_ext.hpp:75
#define REQUIRE_MSG(MSG,...)
Definition: catch2_ext.hpp:58
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:148
const_reference at(size_type i) const
Like std::vector::at(size_type), immutable reference.
Definition: darray.hpp:814
constexpr UnaryFunction for_each_const(T &data, UnaryFunction f, std::enable_if_t< is_cow_type< T >::value, bool >=true) noexcept
lb_endian_t
Simplified reduced endian type only covering little- and big-endian.
Definition: byte_util.hpp:227
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
#define PRAGMA_DISABLE_WARNING_PUSH
Definition: cpp_pragma.hpp:76
#define PRAGMA_DISABLE_WARNING_POP
Definition: cpp_pragma.hpp:77
int_fast32_t snsize_t
Natural 'ssize_t' alternative using int_fast32_t as its natural sized type.
Definition: int_types.hpp:65
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition: backtrace.hpp:32
A 48 bit EUI-48 sub-identifier, see EUI48.
Definition: eui48.hpp:51
std::string toString() const noexcept
Returns the EUI48 sub-string representation with MSB first (lb_endian::big), less or equal 17 charact...
Definition: eui48.cpp:36
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
Definition: eui48.hpp:324
bool contains(const EUI48Sub &needle) const noexcept
Definition: eui48.hpp:324
uint8_t b[6]
Definition: eui48.hpp:324
jau::snsize_t indexOf(const EUI48Sub &needle, const lb_endian_t byte_order) const noexcept
Definition: eui48.hpp:324
std::string toString() const noexcept
Definition: eui48.cpp:167
static void test_sub01(const lb_endian_t byte_order, const std::string &mac_str, const jau::darray< std::string > &mac_sub_strs, const jau::darray< jau::snsize_t > &indices)
Definition: test_eui48.cpp:13
static void test_sub02(const std::string &mac_sub_str_exp, const std::string &mac_sub_str, const bool expected_result)
Definition: test_eui48.cpp:54
TEST_CASE("EUI48 Test 01", "[datatype][eui48]")
Definition: test_eui48.cpp:69
int printf(const char *format,...)
Operating Systems predefined macros.