aboutsummaryrefslogtreecommitdiffstats
path: root/host/tests/sid_t_test.cpp
blob: 85d33c45ed9dd49f79f3c080f0d9a0881b9cafbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Copyright 2014 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include <uhd/exception.hpp>
#include <uhd/types/sid.hpp>
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <sstream>

using uhd::sid_t;

BOOST_AUTO_TEST_CASE(test_sid_t)
{
    uint32_t sid_value = 0x01020310;
    sid_t sid(sid_value);

    BOOST_CHECK_EQUAL(sid.is_set(), true);
    BOOST_CHECK_EQUAL(sid.to_pp_string(), "1.2>3.16");
    BOOST_CHECK_EQUAL(sid.to_pp_string_hex(), "01:02>03:10");
    BOOST_CHECK_EQUAL(sid.get_src(), (uint32_t)0x0102);
    BOOST_CHECK_EQUAL(sid.get_dst(), (uint32_t)0x0310);
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)0x01);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)0x02);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)0x03);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)0x10);
    BOOST_CHECK_EQUAL(sid == sid, true);
    BOOST_CHECK_EQUAL(sid == sid_value, true);

    uint32_t check_sid_val = (uint32_t)sid;
    BOOST_CHECK_EQUAL(check_sid_val, sid_value);

    std::stringstream ss_dec;
    ss_dec << sid;
    BOOST_CHECK_EQUAL(ss_dec.str(), "1.2>3.16");

    std::stringstream ss_hex;
    ss_hex << std::hex << sid;
    BOOST_CHECK_EQUAL(ss_hex.str(), "01:02>03:10");

    sid_t empty_sid;
    BOOST_CHECK_EQUAL(empty_sid.is_set(), false);
    BOOST_CHECK_EQUAL(empty_sid.to_pp_string(), "x.x>x.x");
    BOOST_CHECK_EQUAL(empty_sid.to_pp_string_hex(), "xx:xx>xx:xx");
    BOOST_CHECK_EQUAL(empty_sid == sid, false);
    BOOST_CHECK_EQUAL(empty_sid == sid_value, false);
    BOOST_CHECK_EQUAL((bool)empty_sid, false);

    empty_sid = sid_value; // No longer empty
    BOOST_CHECK_EQUAL(empty_sid.is_set(), true);
    BOOST_CHECK_EQUAL(empty_sid == sid, true);
}

BOOST_AUTO_TEST_CASE(test_sid_t_set)
{
    uint32_t sid_value = 0x0;
    sid_t sid(sid_value);

    sid.set(0x01020304);
    BOOST_CHECK_EQUAL(sid.get(), (uint32_t)0x01020304);
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)0x01);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)0x02);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)0x03);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)0x04);
    BOOST_CHECK_EQUAL(sid.get_dst_xbarport(), (uint32_t)0x0);
    BOOST_CHECK_EQUAL(sid.get_dst_blockport(), (uint32_t)0x4);

    sid.set_src_addr(0x0a);
    BOOST_CHECK_EQUAL(sid.get(), (uint32_t)0x0a020304);
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)0x0a);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)0x02);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)0x03);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)0x04);

    sid.set_src_endpoint(0x0b);
    BOOST_CHECK_EQUAL(sid.get(), (uint32_t)0x0a0b0304);
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)0x0a);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)0x0b);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)0x03);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)0x04);

    sid.set_dst_addr(0x0c);
    BOOST_CHECK_EQUAL(sid.get(), (uint32_t)0x0a0b0c04);
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)0x0a);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)0x0b);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)0x0c);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)0x04);

    sid.set_dst_endpoint(0x0d);
    BOOST_CHECK_EQUAL(sid.get(), (uint32_t)0x0a0b0c0d);
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)0x0a);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)0x0b);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)0x0c);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)0x0d);

    sid.set_dst_xbarport(0xb);
    BOOST_CHECK_EQUAL(sid.get(), (uint32_t)0x0a0b0cbd);
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)0x0a);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)0x0b);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)0x0c);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)0xbd);

    sid.set_dst_blockport(0xc);
    BOOST_CHECK_EQUAL(sid.get(), (uint32_t)0x0a0b0cbc);
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)0x0a);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)0x0b);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)0x0c);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)0xbc);

    const sid_t flipped_sid = sid.reversed();
    BOOST_CHECK_EQUAL(flipped_sid.get(), (uint32_t)0x0cbc0a0b);
    BOOST_CHECK_EQUAL(flipped_sid.reversed(), sid);

    // In-place
    sid.reverse();
    BOOST_CHECK_EQUAL(sid.get(), (uint32_t)0x0cbc0a0b);
}

BOOST_AUTO_TEST_CASE(test_sid_t_from_str)
{
    sid_t sid("1.2>3.4");
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)1);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)2);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)3);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)4);

    sid = "01:02>03:10";
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)1);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)2);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)3);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)16);

    sid = "01:06/03:10";
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)1);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)6);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)3);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)16);

    sid = "01:02:04:10";
    BOOST_CHECK_EQUAL(sid.get_src_addr(), (uint32_t)1);
    BOOST_CHECK_EQUAL(sid.get_src_endpoint(), (uint32_t)2);
    BOOST_CHECK_EQUAL(sid.get_dst_addr(), (uint32_t)4);
    BOOST_CHECK_EQUAL(sid.get_dst_endpoint(), (uint32_t)16);

    BOOST_REQUIRE_THROW(sid_t fail_sid("foobar"), uhd::value_error);
    BOOST_REQUIRE_THROW(sid_t fail_sid("01:02:03:4"), uhd::value_error);
    BOOST_REQUIRE_THROW(sid_t fail_sid("01:02:03:004"), uhd::value_error);
    BOOST_REQUIRE_THROW(sid_t fail_sid("1.2.3.0004"), uhd::value_error);
}