PX4 Firmware
PX4 Autopilot Software http://px4.io
test_List.cpp
Go to the documentation of this file.
1
/****************************************************************************
2
*
3
* Copyright (C) 2019 PX4 Development Team. All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
*
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in
13
* the documentation and/or other materials provided with the
14
* distribution.
15
* 3. Neither the name PX4 nor the names of its contributors may be
16
* used to endorse or promote products derived from this software
17
* without specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
* POSSIBILITY OF SUCH DAMAGE.
31
*
32
****************************************************************************/
33
34
/**
35
* @file test_List.cpp
36
* Tests the List container.
37
*/
38
39
#include <
unit_test.h
>
40
#include <
containers/List.hpp
>
41
#include <float.h>
42
#include <math.h>
43
44
class
testContainer
:
public
ListNode
<testContainer *>
45
{
46
public
:
47
int
i
{0};
48
};
49
50
class
ListTest
:
public
UnitTest
51
{
52
public
:
53
virtual
bool
run_tests();
54
55
bool
test_add();
56
bool
test_remove();
57
bool
test_range_based_for();
58
59
};
60
61
bool
ListTest::run_tests
()
62
{
63
ut_run_test
(test_add);
64
ut_run_test
(test_remove);
65
ut_run_test
(test_range_based_for);
66
67
return
(_tests_failed == 0);
68
}
69
70
bool
ListTest::test_add
()
71
{
72
List<testContainer *>
list1;
73
74
// size should be 0 initially
75
ut_compare
(
"size initially 0"
, list1.size(), 0);
76
ut_assert_true
(list1.empty());
77
78
// insert 100
79
for
(
int
i
= 0;
i
< 100;
i
++) {
80
testContainer
*t =
new
testContainer
();
81
t->
i
=
i
;
82
list1.add(t);
83
84
ut_compare
(
"size increasing with i"
, list1.size(),
i
+ 1);
85
ut_assert_true
(!list1.empty());
86
}
87
88
// verify full size (100)
89
ut_assert_true
(list1.size() == 100);
90
91
int
i
= 0;
92
93
for
(
auto
t : list1) {
94
// verify all elements were inserted in order
95
ut_compare
(
"stored i"
, i, t->i);
96
i++;
97
}
98
99
// delete all elements
100
list1.clear();
101
102
// verify list has been cleared
103
ut_assert_true
(list1.empty());
104
ut_compare
(
"size 0"
, list1.size(), 0);
105
106
return
true
;
107
}
108
109
bool
ListTest::test_remove
()
110
{
111
List<testContainer *>
list1;
112
113
// size should be 0 initially
114
ut_compare
(
"size initially 0"
, list1.size(), 0);
115
ut_assert_true
(list1.empty());
116
117
// insert 100
118
for
(
int
i
= 0;
i
< 100;
i
++) {
119
testContainer
*t =
new
testContainer
();
120
t->
i
=
i
;
121
list1.add(t);
122
123
ut_compare
(
"size increasing with i"
, list1.size(),
i
+ 1);
124
ut_assert_true
(!list1.empty());
125
}
126
127
// verify full size (100)
128
ut_assert_true
(list1.size() == 100);
129
130
// test removing elements
131
for
(
int
remove_i = 0; remove_i < 100; remove_i++) {
132
133
// find node with i == remove_i
134
testContainer
*removed =
nullptr
;
135
136
for
(
auto
t : list1) {
137
if
(t->i == remove_i) {
138
ut_assert_true
(list1.remove(t));
139
removed = t;
140
}
141
}
142
143
delete
removed;
144
145
// iterate list again to verify removal
146
for
(
auto
t : list1) {
147
ut_assert_true
(t->i != remove_i);
148
}
149
150
ut_assert_true
(list1.size() == 100 - remove_i - 1);
151
}
152
153
// list should now be empty
154
ut_assert_true
(list1.empty());
155
ut_compare
(
"size 0"
, list1.size(), 0);
156
157
// delete all elements (should be safe on empty list)
158
list1.clear();
159
160
// verify list has been cleared
161
ut_assert_true
(list1.empty());
162
ut_compare
(
"size 0"
, list1.size(), 0);
163
164
return
true
;
165
}
166
167
bool
ListTest::test_range_based_for
()
168
{
169
List<testContainer *>
list1;
170
171
// size should be 0 initially
172
ut_compare
(
"size initially 0"
, list1.size(), 0);
173
ut_assert_true
(list1.empty());
174
175
// insert 100 elements in order
176
for
(
int
i
= 99;
i
>= 0;
i
--) {
177
testContainer
*t =
new
testContainer
();
178
t->
i
=
i
;
179
180
list1.add(t);
181
182
ut_assert_true
(!list1.empty());
183
}
184
185
// first element should be 99 (first added)
186
ut_compare
(
"first 0"
, list1.getHead()->i, 99);
187
188
// verify all elements were inserted in order
189
int
i
= 99;
190
auto
t1
= list1.getHead();
191
192
while
(
t1
!=
nullptr
) {
193
ut_compare
(
"check count"
, i,
t1
->i);
194
t1
=
t1
->getSibling();
195
i--;
196
}
197
198
// verify full size (100)
199
ut_compare
(
"size check"
, list1.size(), 100);
200
201
i = 99;
202
203
for
(
auto
t2
: list1) {
204
ut_compare
(
"range based for i"
, i,
t2
->i);
205
i--;
206
}
207
208
// verify full size (100)
209
ut_compare
(
"size check"
, list1.size(), 100);
210
211
// delete all elements
212
list1.clear();
213
214
// verify list has been cleared
215
ut_assert_true
(list1.empty());
216
ut_compare
(
"size check"
, list1.size(), 0);
217
218
return
true
;
219
}
220
221
ut_declare_test_c
(
test_List
,
ListTest
)
ListTest::run_tests
virtual bool run_tests()
Override to run your unit tests.
Definition:
test_List.cpp:61
ListTest::test_add
bool test_add()
Definition:
test_List.cpp:70
ut_declare_test_c
#define ut_declare_test_c(test_function, test_class)
Definition:
unit_test.h:40
ListNode
Definition:
List.hpp:45
List.hpp
An intrusive linked list.
UnitTest
Base class to be used for unit tests.
Definition:
unit_test.h:54
t1
static struct hrt_call t1
Definition:
hrt_test.cpp:54
List
Definition:
List.hpp:59
test_List
int test_List(int argc, char *argv[])
testContainer
Definition:
test_IntrusiveQueue.cpp:39
ListTest
Definition:
test_List.cpp:50
ListTest::test_range_based_for
bool test_range_based_for()
Definition:
test_List.cpp:167
ListTest::test_remove
bool test_remove()
Definition:
test_List.cpp:109
testContainer::i
int i
Definition:
test_IntrusiveQueue.cpp:42
ut_assert_true
#define ut_assert_true(test)
To assert specifically to true.
Definition:
unit_test.h:127
unit_test.h
t2
t2
Definition:
calcH_YAW312.c:3
ut_compare
#define ut_compare(message, v1, v2)
Used to compare two integer values within a unit test.
Definition:
unit_test.h:150
ut_run_test
#define ut_run_test(test)
Runs a single unit test.
Definition:
unit_test.h:96
src
systemcmds
tests
test_List.cpp
Generated by
1.8.13