forked from jandelgado/jled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_example_morse.cpp
85 lines (79 loc) · 2.58 KB
/
test_example_morse.cpp
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
// JLed Unit tests (run on host).
// Tests for the morse code example.
// Copyright 2018 Jan Delgado [email protected]
#include "catch.hpp"
#include <morse.h> // NOLINT
TEST_CASE("calc len of bit array", "[morse_example_bitset]") {
class TestBitset : public Bitset {
public:
static void test() {
REQUIRE(0 == num_bytes(0));
REQUIRE(1 == num_bytes(1));
REQUIRE(1 == num_bytes(7));
REQUIRE(1 == num_bytes(8));
REQUIRE(2 == num_bytes(9));
REQUIRE(2 == num_bytes(16));
REQUIRE(3 == num_bytes(17));
}
};
TestBitset::test();
}
TEST_CASE("set and test bits in the bitset", "[morse_example_bitset]") {
Bitset bf(18); // 3 bytes
REQUIRE(18 == bf.size());
REQUIRE(!bf.test(0));
REQUIRE(!bf.test(10));
REQUIRE(!bf.test(17));
bf.set(0, true);
bf.set(10, true);
bf.set(17, true);
REQUIRE(bf.test(0));
REQUIRE(bf.test(10));
REQUIRE(bf.test(17));
}
TEST_CASE("treepos returns correct position in tree", "[morse_example]") {
class TestMorse : public Morse {
public:
void test() {
REQUIRE(2 == treepos('E'));
REQUIRE(3 == treepos('T'));
REQUIRE(4 == treepos('I'));
REQUIRE(7 == treepos('M'));
REQUIRE(8 == treepos('S'));
REQUIRE(16 == treepos('H'));
REQUIRE(32 == treepos('5'));
}
};
TestMorse().test();
}
TEST_CASE("binary code of character is determined correctly",
"[morse_example]") {
class TestMorse : public Morse {
public:
void test() {
uint16_t code = pos_to_morse_code(treepos('F')); // F = ..-. = 1000
REQUIRE(4 == (code >> 8)); // hi: length in bits
REQUIRE(0b0100 ==
(code & 0xff)); // lo: morse code as bits, reversed
}
};
TestMorse().test();
}
TEST_CASE("string is encoded correctly into sequence", "[morse_example]") {
// A = . - => 1 + 3 + (1) dits
// E = . => 1 dit
// B = - . . . => 3 + 1 + 1 + 1 + (3*1) dits
Morse m("AE B");
// clang-format off
constexpr int expected[]{
1, 0, 1, 1, 1, // A
0, 0, 0, // pause between chars
1, // E
0, 0, 0, 0, 0, 0, 0, // 7 dits between words
1, 1, 1, 0, 1, 0, 1, 0, 1}; // B
// clang-format on
REQUIRE(sizeof(expected) / sizeof(expected[0]) == m.size());
for (auto i = 0u; i < m.size(); i++) {
REQUIRE(expected[i] == m.test(i));
}
}