STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// Tests for the promises radio.h makes, run against the fake radio.
2// They check the fake (they need fake::inject and fake::last_sent), but the
3// promises they check are the ones the real radio driver must also keep.
4#include <string.h>
5
6#include "radio.h"
7#include "radio_fake.h"
8#include "unity.h"
9
10using namespace radio_link;
11
12void setUp(void) {
13 fake::reset(true);
14}
15void tearDown(void) {
16}
17
19 fake::reset(true);
20 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_probe());
21}
22
24 fake::reset(false);
25 TEST_ASSERT_EQUAL(RadioStatus::ErrNotFound, radio_probe());
26}
27
29 uint8_t buf[16];
30 uint8_t len = 0xAB;
31 TEST_ASSERT_EQUAL(RadioStatus::NoData, radio_receive(buf, sizeof buf, len));
32 TEST_ASSERT_EQUAL_UINT8(0xAB, len); // len must be left alone on NoData
33}
34
36 const uint8_t pkt[] = {1, 2, 3, 4, 5};
37 TEST_ASSERT_TRUE(fake::inject(pkt, sizeof pkt));
38 uint8_t buf[16];
39 uint8_t len = 0;
40 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_receive(buf, sizeof buf, len));
41 TEST_ASSERT_EQUAL_UINT8(sizeof pkt, len);
42 TEST_ASSERT_EQUAL_HEX8_ARRAY(pkt, buf, sizeof pkt);
43 // The packet is used up, so a second receive finds nothing.
44 TEST_ASSERT_EQUAL(RadioStatus::NoData, radio_receive(buf, sizeof buf, len));
45}
46
48 const uint8_t a[] = {0xAA};
49 const uint8_t b[] = {0xBB, 0xBB};
50 TEST_ASSERT_TRUE(fake::inject(a, sizeof a));
51 TEST_ASSERT_TRUE(fake::inject(b, sizeof b));
52 uint8_t buf[16];
53 uint8_t len = 0;
54 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_receive(buf, sizeof buf, len));
55 TEST_ASSERT_EQUAL_UINT8(1, len);
56 TEST_ASSERT_EQUAL_HEX8(0xAA, buf[0]);
57 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_receive(buf, sizeof buf, len));
58 TEST_ASSERT_EQUAL_UINT8(2, len);
59 TEST_ASSERT_EQUAL_HEX8(0xBB, buf[0]);
60}
61
62// The queue holds 8 packets in a ring: the read and write positions go back
63// to slot 0 after slot 7. Push and pop 20 packets one at a time, so both
64// positions wrap around twice, and check every packet comes back intact.
66 uint8_t buf[16];
67 uint8_t len = 0;
68 for (uint8_t i = 0; i < 20; i++) {
69 const uint8_t pkt[] = {i, static_cast<uint8_t>(i + 100)};
70 TEST_ASSERT_TRUE(fake::inject(pkt, sizeof pkt));
71 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_receive(buf, sizeof buf, len));
72 TEST_ASSERT_EQUAL_UINT8(sizeof pkt, len);
73 TEST_ASSERT_EQUAL_HEX8_ARRAY(pkt, buf, sizeof pkt);
74 }
75 TEST_ASSERT_EQUAL(RadioStatus::NoData, radio_receive(buf, sizeof buf, len));
76}
77
78// A full queue must refuse a 9th packet, not write over the oldest one.
80 for (uint8_t i = 0; i < 8; i++) {
81 TEST_ASSERT_TRUE(fake::inject(&i, 1));
82 }
83 const uint8_t extra = 0xEE;
84 TEST_ASSERT_FALSE(fake::inject(&extra, 1));
85 uint8_t buf[16];
86 uint8_t len = 0;
87 for (uint8_t i = 0; i < 8; i++) {
88 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_receive(buf, sizeof buf, len));
89 TEST_ASSERT_EQUAL_HEX8(i, buf[0]);
90 }
91 TEST_ASSERT_EQUAL(RadioStatus::NoData, radio_receive(buf, sizeof buf, len));
92}
93
95 const uint8_t pkt[] = {1};
96 TEST_ASSERT_FALSE(fake::inject(pkt, 0));
97 uint8_t buf[16];
98 uint8_t len = 0;
99 TEST_ASSERT_EQUAL(RadioStatus::NoData, radio_receive(buf, sizeof buf, len));
100}
101
103 const uint8_t pkt[8] = {0};
104 TEST_ASSERT_TRUE(fake::inject(pkt, sizeof pkt));
105 uint8_t small[4];
106 uint8_t len = 0xAB;
107 TEST_ASSERT_EQUAL(RadioStatus::ErrTooLong,
108 radio_receive(small, sizeof small, len));
109 // The too-big packet is thrown away, so it does not block the queue.
110 TEST_ASSERT_EQUAL(RadioStatus::NoData,
111 radio_receive(small, sizeof small, len));
112}
113
115 const uint8_t pkt[] = {9, 8, 7};
116 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_send(pkt, sizeof pkt));
117 uint8_t back[16];
118 const uint8_t n = fake::last_sent(back, sizeof back);
119 TEST_ASSERT_EQUAL_UINT8(sizeof pkt, n);
120 TEST_ASSERT_EQUAL_HEX8_ARRAY(pkt, back, sizeof pkt);
121}
122
124 const uint8_t pkt[] = {1, 2};
125 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_send(pkt, sizeof pkt));
126 fake::reset(true);
127 uint8_t back[16];
128 TEST_ASSERT_EQUAL_UINT8(0, fake::last_sent(back, sizeof back));
129 TEST_ASSERT_EQUAL_UINT(0, fake::sent_count());
130}
131
133 const uint8_t a[] = {1};
134 const uint8_t b[] = {2, 3};
135 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_send(a, sizeof a));
136 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_send(b, sizeof b));
137 TEST_ASSERT_EQUAL_UINT(2, fake::sent_count());
138 uint8_t back[16];
139 TEST_ASSERT_EQUAL_UINT8(sizeof b, fake::last_sent(back, sizeof back));
140 TEST_ASSERT_EQUAL_HEX8_ARRAY(b, back, sizeof b);
141}
142
144 fake::reset(false);
145 const uint8_t pkt[] = {1};
146 TEST_ASSERT_EQUAL(RadioStatus::ErrNotFound, radio_send(pkt, sizeof pkt));
147 TEST_ASSERT_EQUAL_UINT(0, fake::sent_count());
148}
149
152 const uint8_t pkt[] = {1};
153 TEST_ASSERT_EQUAL(RadioStatus::ErrTooLong, radio_send(pkt, sizeof pkt));
154 TEST_ASSERT_EQUAL_UINT(0, fake::sent_count());
155 // reset() puts it back to Ok.
156 fake::reset(true);
157 TEST_ASSERT_EQUAL(RadioStatus::Ok, radio_send(pkt, sizeof pkt));
158}
159
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
*brief create JSON pointer *sa reference_tokens const string_t & a
Definition json.hpp:13877
return reference_tokens back()
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474
int main()
static void test_inject_refuses_when_queue_full(void)
Definition test_main.cpp:79
static void test_send_is_visible_to_the_test(void)
static void test_receive_is_first_in_first_out(void)
Definition test_main.cpp:47
static void test_receive_drops_packet_bigger_than_buffer(void)
static void test_receive_returns_injected_packet_with_its_length(void)
Definition test_main.cpp:35
static void test_inject_refuses_empty_packet(void)
Definition test_main.cpp:94
static void test_reset_forgets_what_was_sent(void)
static void test_receive_keeps_working_after_queue_wraps(void)
Definition test_main.cpp:65
static void test_probe_reports_not_found_when_chip_missing(void)
Definition test_main.cpp:23
void setUp(void)
Definition test_main.cpp:12
void tearDown(void)
Definition test_main.cpp:15
static void test_receive_with_nothing_waiting_returns_no_data(void)
Definition test_main.cpp:28
static void test_send_gives_the_failure_the_test_asks_for(void)
static void test_probe_ok_when_chip_present(void)
Definition test_main.cpp:18
static void test_send_counts_each_packet(void)
static void test_send_fails_when_chip_missing(void)