forked from the-aerospace-corporation/satcat5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ptp_header.cc
98 lines (89 loc) · 3.27 KB
/
test_ptp_header.cc
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
//////////////////////////////////////////////////////////////////////////
// Copyright 2024 The Aerospace Corporation.
// This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
//////////////////////////////////////////////////////////////////////////
// Test cases for "ptp::Header" and related classes.
#include <hal_test/catch.hpp>
#include <satcat5/io_core.h>
#include <satcat5/ptp_header.h>
using satcat5::ptp::ClockInfo;
using satcat5::ptp::Header;
using satcat5::ptp::PortId;
using satcat5::ptp::DEFAULT_CLOCK;
using satcat5::ptp::VERY_GOOD_CLOCK;
bool equal(const Header& a, const Header& b) {
return (a.type == b.type)
&& (a.version == b.version)
&& (a.length == b.length)
&& (a.domain == b.domain)
&& (a.sdo_id == b.sdo_id)
&& (a.flags == b.flags)
&& (a.correction == b.correction)
&& (a.subtype == b.subtype)
&& (a.src_port == b.src_port)
&& (a.seq_id == b.seq_id)
&& (a.control == b.control)
&& (a.log_interval == b.log_interval);
}
bool equal(const ClockInfo& a, const ClockInfo& b) {
return (a.grandmasterPriority1 == b.grandmasterPriority1)
&& (a.grandmasterClass == b.grandmasterClass)
&& (a.grandmasterAccuracy == b.grandmasterAccuracy)
&& (a.grandmasterVariance == b.grandmasterVariance)
&& (a.grandmasterPriority2 == b.grandmasterPriority2)
&& (a.grandmasterIdentity == b.grandmasterIdentity)
&& (a.stepsRemoved == b.stepsRemoved)
&& (a.timeSource == b.timeSource);
}
TEST_CASE("ptp_header") {
// Most tests need a buffer for write + readback.
u8 buffer[256];
satcat5::io::ArrayWrite wr(buffer, sizeof(buffer));
SECTION("PortId") {
// Write test values into the working buffer.
const PortId TEST = {1234, 5678};
wr.write_obj(TEST);
REQUIRE(wr.write_finalize());
satcat5::io::ArrayRead rd(buffer, wr.written_len());
// Read and compare each one.
PortId tmp;
rd.read_obj(tmp);
CHECK(tmp == TEST);
CHECK(rd.get_read_ready() == 0);
}
SECTION("Header") {
// Write test values into the working buffer.
const Header TEST = {1, 2, 3, 4, 5, 6, 7, 8, {9, 10}, 11, 12, 13};
wr.write_obj(TEST);
REQUIRE(wr.write_finalize());
satcat5::io::ArrayRead rd(buffer, wr.written_len());
// Read and compare each one.
Header tmp;
rd.read_obj(tmp);
CHECK(equal(tmp, TEST));
CHECK(rd.get_read_ready() == 0);
}
SECTION("ClockInfo") {
// Write test values into the working buffer.
wr.write_obj(DEFAULT_CLOCK);
wr.write_obj(VERY_GOOD_CLOCK);
REQUIRE(wr.write_finalize());
satcat5::io::ArrayRead rd(buffer, wr.written_len());
// Read and compare each one.
ClockInfo tmp;
rd.read_obj(tmp);
CHECK(equal(tmp, DEFAULT_CLOCK));
rd.read_obj(tmp);
CHECK(equal(tmp, VERY_GOOD_CLOCK));
CHECK(rd.get_read_ready() == 0);
}
SECTION("ReadEmpty") {
satcat5::io::ArrayRead rd(buffer, 0);
ClockInfo tmp1;
Header tmp2;
PortId tmp3;
CHECK_FALSE(tmp1.read_from(&rd));
CHECK_FALSE(tmp2.read_from(&rd));
CHECK_FALSE(tmp3.read_from(&rd));
}
}