forked from the-aerospace-corporation/satcat5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_list.cc
90 lines (79 loc) · 2.23 KB
/
test_list.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
//////////////////////////////////////////////////////////////////////////
// Copyright 2024 The Aerospace Corporation.
// This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
//////////////////////////////////////////////////////////////////////////
// Test cases for the linked-list template classes.
#include <hal_test/catch.hpp>
#include <satcat5/list.h>
// Generic linked-list object for testing ListCore and List.
class TestItem {
public:
friend satcat5::util::ListCore;
explicit TestItem(unsigned val) : m_next(0), m_value(val) {}
private:
TestItem* m_next;
unsigned m_value;
};
TEST_CASE("list.h") {
satcat5::util::List<TestItem> list;
TestItem a(1), b(2), c(3), d(4);
SECTION("add_safe") {
list.add(&a);
list.add(&b);
list.add(&c);
CHECK(list.len() == 3);
list.add_safe(&b);
CHECK(list.len() == 3);
list.add_safe(&d);
CHECK(list.len() == 4);
}
SECTION("contains") {
list.add(&a);
list.add(&c);
CHECK(list.contains(&a));
CHECK_FALSE(list.contains(&b));
CHECK(list.contains(&c));
CHECK_FALSE(list.contains(&d));
}
SECTION("has_loop3") {
list.add(&a);
list.add(&b);
list.add(&c);
CHECK(list.len() == 3);
CHECK_FALSE(list.has_loop());
list.add(&b);
CHECK(list.has_loop());
}
SECTION("has_loop4") {
list.add(&a);
list.add(&b);
list.add(&c);
list.add(&d);
CHECK(list.len() == 4);
CHECK_FALSE(list.has_loop());
list.add(&d);
CHECK(list.has_loop());
}
SECTION("push_back") {
list.push_back(&a);
list.push_back(&b);
CHECK(list.pop_front() == &a);
CHECK(list.pop_front() == &b);
CHECK(list.pop_front() == 0);
}
SECTION("push_front") {
list.push_front(&a);
list.push_front(&b);
CHECK(list.pop_front() == &b);
CHECK(list.pop_front() == &a);
CHECK(list.pop_front() == 0);
}
SECTION("remove") {
list.add(&a);
list.add(&b);
list.add(&c);
CHECK(list.contains(&b));
list.remove(&b);
CHECK_FALSE(list.contains(&b));
}
}