This repository has been archived on 2024-01-28. You can view files and clone it, but cannot push or open issues or pull requests.
ecg-prog-filtered/u01/tests.cpp

117 lines
4.3 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include <iostream>
#include "node.h"
int get_node_id(node *n) {
// TODO: ensure the test calling this starts at node_id 0 (currently this is
// undefined based on order the tests are run)
// for the time being, this gets the offset
int id = std::stoi(n->get_name().substr(5));
// this tests if the node id was correctly extracted
REQUIRE(n->get_name() == "node_" + std::to_string(id));
return id;
}
TEST_CASE("Name") {
node *n = new node("test");
REQUIRE(n->get_name() == "test");
n->set_name("foo");
REQUIRE(n->get_name() == "foo");
delete n;
}
TEST_CASE("Number of children") {
node *n = new node("test");
REQUIRE(n->get_nr_children() == 0);
for (std::size_t i = 0; i < 20; i++) {
node *child = new node("child");
n->add_child(child);
}
REQUIRE(n->get_nr_children() == 20);
delete n;
}
TEST_CASE("Get children") {
node *n = new node("test");
REQUIRE(n->get_nr_children() == 0);
for (std::size_t i = 0; i < 5; i++) {
n->add_child(new node("child " + std::to_string(i)));
}
for (std::size_t i = 0; i < 5; i++) {
REQUIRE(n->get_child(i)->get_name() == "child " + std::to_string(i));
}
delete n;
}
TEST_CASE("Global node counting") {
node *n = new node();
const int node_id_start = get_node_id(n);
for (std::size_t i = 0; i < 5; i++) {
n->add_child(new node());
}
n->add_child(new node("explicit name"));
for (std::size_t i = 0; i < 5; i++) {
REQUIRE(n->get_child(i)->get_name() ==
"node_" + std::to_string(node_id_start + 1 + i));
}
REQUIRE(n->get_child(5)->get_name() == "explicit name");
delete n;
}
TEST_CASE("Create complete tree") {
const std::size_t nr_child_nodes = 2;
const unsigned int tree_depth = 4;
node *t = create_complete_tree(nr_child_nodes, tree_depth);
const int node_id_start = get_node_id(t);
int count = 0;
REQUIRE(t->get_name() == "node_" + std::to_string(node_id_start + count++));
// this is very verbose,
// but without a serialisation,
// there is no other good way I could find (that does not also implement the
// logic to test).
REQUIRE(t->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(0)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(0)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(1)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(1)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(0)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(0)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(1)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(1)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(0)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(0)->get_child(0)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(0)->get_child(1)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(1)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(1)->get_child(0)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(1)->get_child(1)->get_nr_children() == nr_child_nodes);
delete t;
}