squall/test/List.cpp

177 lines
4.6 KiB
C++
Raw Normal View History

2020-11-01 17:45:45 -06:00
#include "storm/List.hpp"
#include "test/Test.hpp"
2020-09-16 01:28:32 -05:00
struct TestListNode : TSLinkedNode<TestListNode> {
uint32_t index = 0;
};
2025-11-15 22:00:31 -06:00
struct TestExplicitListNode {
uint32_t index = 0;
TSLink<TestExplicitListNode> m_explicitLink;
};
2020-09-16 01:28:32 -05:00
TEST_CASE("TSList", "[list]") {
SECTION("constructs correctly") {
STORM_LIST(TestListNode) list;
REQUIRE(list.Head() == nullptr);
}
2025-11-15 22:00:31 -06:00
SECTION("constructs copy correctly") {
STORM_LIST(TestListNode) list;
auto node = new TestListNode();
list.LinkToHead(node);
STORM_LIST(TestListNode) listCopy(list);
// List copy constructor does not transfer ownership of nodes. It merely copies the link
// offset from the source list and initializes the list terminator.
REQUIRE(list.Head() == node);
REQUIRE(listCopy.Head() == nullptr);
}
2020-09-16 01:28:32 -05:00
}
TEST_CASE("TSList::Head", "[list]") {
SECTION("returns nullptr for uninitialized list") {
STORM_LIST(TestListNode) list;
REQUIRE(list.Head() == nullptr);
}
}
2025-10-24 01:42:16 -07:00
TEST_CASE("TSList::IsEmpty", "[list]") {
SECTION("returns true if just initialized") {
STORM_LIST(TestListNode) list;
CHECK(list.IsEmpty());
}
SECTION("returns false if there are linked items") {
STORM_LIST(TestListNode) list;
list.NewNode(STORM_LIST_TAIL, 0, 0);
CHECK_FALSE(list.IsEmpty());
}
SECTION("returns true after clearing a populated list") {
STORM_LIST(TestListNode) list;
list.NewNode(STORM_LIST_TAIL, 0, 0);
list.Clear();
CHECK(list.IsEmpty());
}
}
2020-09-16 01:28:32 -05:00
TEST_CASE("TSList::LinkToHead", "[list]") {
SECTION("links node to head correctly") {
STORM_LIST(TestListNode) list;
auto node = new TestListNode();
list.LinkToHead(node);
REQUIRE(list.Head() == node);
2020-09-16 23:12:09 -05:00
delete node;
2020-09-16 01:28:32 -05:00
}
2020-09-21 22:47:23 -05:00
SECTION("links multiple nodes to head correctly") {
STORM_LIST(TestListNode) list;
auto node1 = new TestListNode();
list.LinkToHead(node1);
auto node2 = new TestListNode();
list.LinkToHead(node2);
REQUIRE(list.Head() == node2);
REQUIRE(list.Tail() == node1);
delete node1;
delete node2;
}
SECTION("links node to head after linking node to tail correctly") {
STORM_LIST(TestListNode) list;
auto node1 = new TestListNode();
list.LinkToTail(node1);
auto node2 = new TestListNode();
list.LinkToHead(node2);
REQUIRE(list.Head() == node2);
REQUIRE(list.Tail() == node1);
delete node1;
delete node2;
}
}
TEST_CASE("TSList::LinkToTail", "[list]") {
2020-09-21 22:47:23 -05:00
SECTION("links node to tail correctly") {
STORM_LIST(TestListNode) list;
auto node = new TestListNode();
list.LinkToTail(node);
REQUIRE(list.Tail() == node);
delete node;
}
SECTION("links multiple nodes to tail correctly") {
2020-09-21 22:47:23 -05:00
STORM_LIST(TestListNode) list;
auto node1 = new TestListNode();
list.LinkToTail(node1);
2020-09-21 22:47:23 -05:00
auto node2 = new TestListNode();
list.LinkToTail(node2);
REQUIRE(list.Head() == node1);
REQUIRE(list.Tail() == node2);
delete node1;
delete node2;
}
SECTION("links node to tail after linking node to head correctly") {
2020-09-21 22:47:23 -05:00
STORM_LIST(TestListNode) list;
auto node1 = new TestListNode();
list.LinkToHead(node1);
auto node2 = new TestListNode();
list.LinkToTail(node2);
2020-09-21 22:47:23 -05:00
REQUIRE(list.Head() == node1);
REQUIRE(list.Tail() == node2);
2020-09-21 22:47:23 -05:00
delete node1;
delete node2;
}
2020-09-16 01:28:32 -05:00
}
TEST_CASE("TSList::Tail", "[list]") {
SECTION("returns nullptr for uninitialized list") {
STORM_LIST(TestListNode) list;
REQUIRE(list.Tail() == nullptr);
}
}
2025-11-15 22:00:31 -06:00
TEST_CASE("TSExplicitList", "[list]") {
SECTION("constructs correctly") {
STORM_EXPLICIT_LIST(TestExplicitListNode, m_explicitLink) list;
REQUIRE(list.Head() == nullptr);
}
SECTION("constructs copy correctly") {
STORM_EXPLICIT_LIST(TestExplicitListNode, m_explicitLink) list;
auto node = new TestExplicitListNode();
list.LinkToHead(node);
STORM_EXPLICIT_LIST(TestExplicitListNode, m_explicitLink) listCopy(list);
// Explicit list copy constructor does not transfer ownership of nodes. It merely calls
// the copy constructor on the list base class, which only copies the link offset from
// the source list and initializes the list terminator.
REQUIRE(list.Head() == node);
REQUIRE(listCopy.Head() == nullptr);
}
}