mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Add unit testing for List
This commit is contained in:
parent
615f8cd443
commit
0558a7bfed
2 changed files with 117 additions and 0 deletions
|
@ -246,6 +246,7 @@ set(MAIN_SRCS
|
||||||
IntSet.cc
|
IntSet.cc
|
||||||
IP.cc
|
IP.cc
|
||||||
IPAddr.cc
|
IPAddr.cc
|
||||||
|
List.cc
|
||||||
Reporter.cc
|
Reporter.cc
|
||||||
NFA.cc
|
NFA.cc
|
||||||
Net.cc
|
Net.cc
|
||||||
|
|
116
src/List.cc
Normal file
116
src/List.cc
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
#include <List.h>
|
||||||
|
#include <3rdparty/doctest.h>
|
||||||
|
|
||||||
|
TEST_CASE("list construction")
|
||||||
|
{
|
||||||
|
List<int> list;
|
||||||
|
CHECK(list.empty());
|
||||||
|
|
||||||
|
List<int> list2(10);
|
||||||
|
CHECK(list2.empty());
|
||||||
|
CHECK(list2.max() == 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("list operation")
|
||||||
|
{
|
||||||
|
List<int> list({ 1, 2, 3 });
|
||||||
|
CHECK(list.size() == 3);
|
||||||
|
CHECK(list.max() == 3);
|
||||||
|
CHECK(list[0] == 1);
|
||||||
|
CHECK(list[1] == 2);
|
||||||
|
CHECK(list[2] == 3);
|
||||||
|
|
||||||
|
// push_back forces a resize of the list here, which grows the list
|
||||||
|
// by a growth factor. That makes the max elements equal to 6.
|
||||||
|
list.push_back(4);
|
||||||
|
CHECK(list.size() == 4);
|
||||||
|
CHECK(list.max() == 6);
|
||||||
|
CHECK(list[3] == 4);
|
||||||
|
|
||||||
|
CHECK(list.front() == 1);
|
||||||
|
CHECK(list.back() == 4);
|
||||||
|
|
||||||
|
list.pop_front();
|
||||||
|
CHECK(list.size() == 3);
|
||||||
|
CHECK(list.front() == 2);
|
||||||
|
|
||||||
|
list.pop_back();
|
||||||
|
CHECK(list.size() == 2);
|
||||||
|
CHECK(list.back() == 3);
|
||||||
|
|
||||||
|
list.push_back(4);
|
||||||
|
CHECK(list.is_member(2));
|
||||||
|
CHECK(list.member_pos(2) == 0);
|
||||||
|
|
||||||
|
list.remove(2);
|
||||||
|
CHECK(list.size() == 2);
|
||||||
|
CHECK(list[0] == 3);
|
||||||
|
CHECK(list[1] == 4);
|
||||||
|
|
||||||
|
// Squash the list down to the existing elements.
|
||||||
|
list.resize();
|
||||||
|
CHECK(list.size() == 2);
|
||||||
|
CHECK(list.max() == 2);
|
||||||
|
|
||||||
|
// Attempt replacing a known position.
|
||||||
|
int old = list.replace(0, 10);
|
||||||
|
CHECK(list.size() == 2);
|
||||||
|
CHECK(list.max() == 2);
|
||||||
|
CHECK(old == 3);
|
||||||
|
CHECK(list[0] == 10);
|
||||||
|
CHECK(list[1] == 4);
|
||||||
|
|
||||||
|
// Attempt replacing an element off the end of the list, which
|
||||||
|
// causes a resize.
|
||||||
|
old = list.replace(3, 5);
|
||||||
|
CHECK(list.size() == 4);
|
||||||
|
CHECK(list.max() == 4);
|
||||||
|
CHECK(old == 0);
|
||||||
|
CHECK(list[0] == 10);
|
||||||
|
CHECK(list[1] == 4);
|
||||||
|
CHECK(list[2] == 0);
|
||||||
|
CHECK(list[3] == 5);
|
||||||
|
|
||||||
|
// Attempt replacing an element with a negative index, which returns the
|
||||||
|
// default value for the list type.
|
||||||
|
old = list.replace(-1, 50);
|
||||||
|
CHECK(list.size() == 4);
|
||||||
|
CHECK(list.max() == 4);
|
||||||
|
CHECK(old == 0);
|
||||||
|
|
||||||
|
list.clear();
|
||||||
|
CHECK(list.size() == 0);
|
||||||
|
CHECK(list.max() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("list iteration")
|
||||||
|
{
|
||||||
|
List<int> list({ 1, 2, 3, 4});
|
||||||
|
|
||||||
|
int index = 1;
|
||||||
|
for ( int v : list )
|
||||||
|
CHECK(v == index++);
|
||||||
|
|
||||||
|
index = 1;
|
||||||
|
for ( auto it = list.begin(); it != list.end(); index++, ++it)
|
||||||
|
CHECK(*it == index);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("plists")
|
||||||
|
{
|
||||||
|
PList<int> list;
|
||||||
|
list.push_back(new int(1));
|
||||||
|
list.push_back(new int(2));
|
||||||
|
list.push_back(new int(3));
|
||||||
|
|
||||||
|
CHECK(*list[0] == 1);
|
||||||
|
|
||||||
|
int* new_val = new int(5);
|
||||||
|
auto old = list.replace(-1, new_val);
|
||||||
|
delete new_val;
|
||||||
|
CHECK(old == nullptr);
|
||||||
|
|
||||||
|
for ( auto v : list )
|
||||||
|
delete v;
|
||||||
|
list.clear();
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue