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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/// Author: Diffblue Ltd.
/// \file Tests for sharing-node utility
#define SN_INTERNAL_CHECKS
#include <testing-utils/use_catch.h>
#include <util/sharing_node.h>
// could be an internal node or a container node
class innert : public sharing_nodet<int, int>
{
public:
friend void sharing_node_internals_test();
};
void sharing_node_internals_test()
{
SECTION("Internal node test")
{
innert internal;
REQUIRE(!internal.data);
internal.write_internal();
REQUIRE(internal.data);
REQUIRE(internal.data.use_count() == 1);
innert internal2(internal);
REQUIRE(internal.use_count() == 2);
REQUIRE(internal2.use_count() == 2);
internal2.write_internal();
REQUIRE(internal.use_count() == 1);
REQUIRE(internal2.use_count() == 1);
}
SECTION("Container node test")
{
innert container;
REQUIRE(!container.data);
container.write_container();
REQUIRE(container.data);
REQUIRE(container.data.use_count() == 1);
innert container2(container);
REQUIRE(container.use_count() == 2);
REQUIRE(container2.use_count() == 2);
container2.write_container();
REQUIRE(container.use_count() == 1);
REQUIRE(container2.use_count() == 1);
}
}
TEST_CASE("Sharing node internals", "[core][util]")
{
sharing_node_internals_test();
}
TEST_CASE("Sharing node", "[core][util]")
{
SECTION("Leaf test")
{
typedef sharing_nodet<int, int> leaft;
// Basic leaf
{
const leaft leaf(1, 2);
REQUIRE(leaf.shares_with(leaf));
REQUIRE(leaf.get_key() == 1);
REQUIRE(leaf.get_value() == 2);
}
// Shared leaf
{
const leaft leaf1(1, 2);
const leaft leaf2(leaf1);
REQUIRE(leaf1.shares_with(leaf2));
REQUIRE(leaf2.get_key() == 1);
REQUIRE(leaf2.get_value() == 2);
}
// Modify shared leaf
{
const leaft leaf1(1, 2);
leaft leaf2(leaf1);
REQUIRE(leaf2.shares_with(leaf1));
leaf2.set_value(3);
REQUIRE(leaf2.get_value() == 3);
REQUIRE(!leaf2.shares_with(leaf1));
}
}
SECTION("Inner node test")
{
typedef sharing_nodet<int, int> innert;
// Empty container
{
const innert c;
REQUIRE(c.empty());
}
// Single container
{
innert c;
const innert &cc = c;
c.place_leaf(1, 2);
c.place_leaf(3, 4);
REQUIRE(!cc.get_container().empty());
REQUIRE(cc.find_leaf(1) != nullptr);
REQUIRE(cc.find_leaf(3) != nullptr);
auto leaf = c.find_leaf(1);
REQUIRE(leaf->get_key() == 1);
REQUIRE(leaf->get_value() == 2);
c.remove_leaf(1);
c.remove_leaf(3);
REQUIRE(cc.get_container().empty());
}
// Shared container
{
innert c1;
c1.place_leaf(1, 2);
c1.place_leaf(3, 4);
innert c2(c1);
auto leaf = c2.find_leaf(1);
leaf->set_value(7);
REQUIRE(!c1.shares_with(c2));
{
auto leaf1 = c1.find_leaf(1);
auto leaf2 = c2.find_leaf(1);
REQUIRE(!leaf1->shares_with(*leaf2));
}
{
auto leaf1 = c1.find_leaf(3);
auto leaf2 = c2.find_leaf(3);
REQUIRE(leaf1->shares_with(*leaf2));
}
}
// Internal node mapping to other internal nodes
{
innert i;
const innert &ci = i;
i.add_child(0);
REQUIRE(!i.empty());
REQUIRE(!i.get_to_map().empty());
REQUIRE(ci.find_child(0) != nullptr);
i.remove_child(0);
REQUIRE(i.get_to_map().empty());
REQUIRE(ci.find_child(0) == nullptr);
}
}
}
|