|
| 1 | +// Copyright 2018 Michael Tautschnig |
| 2 | + |
| 3 | +/// \file Tests that irept memory consumption is fixed |
| 4 | + |
| 5 | +#include <testing-utils/catch.hpp> |
| 6 | +#include <util/irep.h> |
| 7 | + |
| 8 | +SCENARIO("irept_memory", "[core][utils][irept]") |
| 9 | +{ |
| 10 | + GIVEN("Always") |
| 11 | + { |
| 12 | + THEN("An irept is just a pointer") |
| 13 | + { |
| 14 | + REQUIRE(sizeof(irept) == sizeof(void *)); |
| 15 | + } |
| 16 | + |
| 17 | + THEN("The storage size of an irept is fixed") |
| 18 | + { |
| 19 | +#ifdef SHARING |
| 20 | + const std::size_t ref_count_size = sizeof(unsigned); |
| 21 | +#else |
| 22 | + const std::size_t ref_count_size = 0; |
| 23 | +#endif |
| 24 | + |
| 25 | +#ifndef USE_STRING |
| 26 | + const std::size_t data_size = sizeof(dstringt); |
| 27 | + REQUIRE(sizeof(dstringt) == sizeof(unsigned)); |
| 28 | +#else |
| 29 | + const std::size_t data_size = sizeof(std::string); |
| 30 | + REQUIRE(sizeof(std::string) == sizeof(void *)); |
| 31 | +#endif |
| 32 | + |
| 33 | + const std::size_t sub_size = sizeof(std::vector<int>); |
| 34 | +#ifndef _GLIBCXX_DEBUG |
| 35 | + REQUIRE(sizeof(std::vector<int>) == 3 * sizeof(void *)); |
| 36 | +#endif |
| 37 | + |
| 38 | +#ifndef SUB_IS_LIST |
| 39 | + const std::size_t named_size = sizeof(std::map<int, int>); |
| 40 | +# ifndef _GLIBCXX_DEBUG |
| 41 | +# ifdef __APPLE__ |
| 42 | + REQUIRE(sizeof(std::map<int, int>) == 3 * sizeof(void *)); |
| 43 | +# elif defined(_WIN32) |
| 44 | + REQUIRE(sizeof(std::map<int, int>) == 2 * sizeof(void *)); |
| 45 | +# else |
| 46 | + REQUIRE(sizeof(std::map<int, int>) == 6 * sizeof(void *)); |
| 47 | +# endif |
| 48 | +# endif |
| 49 | +#else |
| 50 | + const std::size_t named_size = sizeof(std::list<int>); |
| 51 | +# ifndef _GLIBCXX_DEBUG |
| 52 | + REQUIRE(sizeof(std::list<int>) == 3 * sizeof(void *)); |
| 53 | +# endif |
| 54 | +#endif |
| 55 | + |
| 56 | +#ifdef HASH_CODE |
| 57 | + const std::size_t hash_code_size = sizeof(std::size_t); |
| 58 | +#else |
| 59 | + const std::size_t hash_code_size = 0; |
| 60 | +#endif |
| 61 | + |
| 62 | + REQUIRE( |
| 63 | + sizeof(irept::dt) == |
| 64 | + ref_count_size + data_size + sub_size + 2 * named_size + |
| 65 | + hash_code_size); |
| 66 | + } |
| 67 | + |
| 68 | + THEN("get_nil_irep yields ID_nil") |
| 69 | + { |
| 70 | + REQUIRE(get_nil_irep().id() == ID_nil); |
| 71 | + REQUIRE(get_nil_irep().is_nil()); |
| 72 | + REQUIRE(!get_nil_irep().is_not_nil()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + GIVEN("An initialized irep") |
| 77 | + { |
| 78 | + irept irep("some_id"); |
| 79 | + irept irep_copy(irep); |
| 80 | + irept irep_assign = irep; |
| 81 | + |
| 82 | + irept irep_other("some_other_id"); |
| 83 | + |
| 84 | + THEN("Its id is some_id") |
| 85 | + { |
| 86 | + REQUIRE(irep.id() == "some_id"); |
| 87 | + REQUIRE(irep_copy.id() == "some_id"); |
| 88 | + REQUIRE(irep_assign.id() == "some_id"); |
| 89 | + |
| 90 | + REQUIRE(irep_other.id() == "some_other_id"); |
| 91 | + |
| 92 | + // TODO(tautschnig): id_string() should be deprecated in favour of |
| 93 | + // id2string(...) |
| 94 | + REQUIRE(irep.id_string().size() == 7); |
| 95 | + } |
| 96 | + |
| 97 | + THEN("Swapping works") |
| 98 | + { |
| 99 | + irep.swap(irep_other); |
| 100 | + |
| 101 | + REQUIRE(irep.id() == "some_other_id"); |
| 102 | + REQUIRE(irep_copy.id() == "some_id"); |
| 103 | + REQUIRE(irep_assign.id() == "some_id"); |
| 104 | + |
| 105 | + REQUIRE(irep_other.id() == "some_id"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + GIVEN("An irep") |
| 110 | + { |
| 111 | + irept irep; |
| 112 | + |
| 113 | + THEN("Its id is empty") |
| 114 | + { |
| 115 | + REQUIRE(irep.is_not_nil()); |
| 116 | + REQUIRE(irep.id().empty()); |
| 117 | + } |
| 118 | + |
| 119 | + THEN("Its id can be set") |
| 120 | + { |
| 121 | + irep.id("new_id"); |
| 122 | + REQUIRE(irep.id() == "new_id"); |
| 123 | + } |
| 124 | + |
| 125 | + THEN("find of a non-existent element yields nil") |
| 126 | + { |
| 127 | + REQUIRE(irep.find("no-such-element").is_nil()); |
| 128 | + } |
| 129 | + |
| 130 | + THEN("Adding/removing elements is possible") |
| 131 | + { |
| 132 | + REQUIRE(irep.get_sub().empty()); |
| 133 | + REQUIRE(irep.get_named_sub().empty()); |
| 134 | + REQUIRE(irep.get_comments().empty()); |
| 135 | + |
| 136 | + irept &e = irep.add("a_new_element"); |
| 137 | + REQUIRE(e.id().empty()); |
| 138 | + e.id("some_id"); |
| 139 | + REQUIRE(irep.find("a_new_element").id() == "some_id"); |
| 140 | + |
| 141 | + irept irep2("second_irep"); |
| 142 | + irept &e2 = irep.add("a_new_element", irep2); |
| 143 | + REQUIRE(irep.find("a_new_element").id() == "second_irep"); |
| 144 | + REQUIRE(irep.get_named_sub().size() == 1); |
| 145 | + |
| 146 | + irep.add("#a_comment", irep2); |
| 147 | + REQUIRE(irep.find("#a_comment").id() == "second_irep"); |
| 148 | + REQUIRE(irep.get_comments().size() == 1); |
| 149 | + |
| 150 | + irept bak(irep); |
| 151 | + irep.remove("no_such_id"); |
| 152 | + REQUIRE(bak == irep); |
| 153 | + irep.remove("a_new_element"); |
| 154 | + REQUIRE(bak != irep); |
| 155 | + REQUIRE(irep.find("a_new_element").is_nil()); |
| 156 | + |
| 157 | + irep.move_to_sub(bak); |
| 158 | + REQUIRE(irep.get_sub().size() == 1); |
| 159 | + |
| 160 | + irep.move_to_named_sub("another_entry", irep2); |
| 161 | + REQUIRE(irep.get_sub().size() == 1); |
| 162 | + REQUIRE(irep.get_named_sub().size() == 1); |
| 163 | + REQUIRE(irep.get_comments().size() == 1); |
| 164 | + |
| 165 | + irept irep3; |
| 166 | + irep.move_to_named_sub("#a_comment", irep3); |
| 167 | + REQUIRE(irep.find("#a_comment").id().empty()); |
| 168 | + REQUIRE(irep.get_sub().size() == 1); |
| 169 | + REQUIRE(irep.get_named_sub().size() == 1); |
| 170 | + REQUIRE(irep.get_comments().size() == 1); |
| 171 | + |
| 172 | + irept irep4; |
| 173 | + irep.move_to_named_sub("#another_comment", irep4); |
| 174 | + REQUIRE(irep.get_comments().size() == 2); |
| 175 | + } |
| 176 | + |
| 177 | + THEN("Setting and getting works") |
| 178 | + { |
| 179 | + // TODO(tautschnig): get_string() should be deprecated in favour of |
| 180 | + // id2string(...) |
| 181 | + REQUIRE(irep.get_string("no_such_id").empty()); |
| 182 | + |
| 183 | + REQUIRE(irep.get("no_such_id").empty()); |
| 184 | + // TODO(tautschnig): it's not clear whether we need all of the below |
| 185 | + // variants in the API |
| 186 | + REQUIRE(!irep.get_bool("no_such_id")); |
| 187 | + REQUIRE(irep.get_int("no_such_id") == 0); |
| 188 | + REQUIRE(irep.get_unsigned_int("no_such_id") == 0u); |
| 189 | + REQUIRE(irep.get_size_t("no_such_id") == 0u); |
| 190 | + REQUIRE(irep.get_long_long("no_such_id") == 0); |
| 191 | + |
| 192 | + irep.set("some_id", "some string"); |
| 193 | + REQUIRE(irep.get("some_id") == "some string"); |
| 194 | + irept irep2("second_irep"); |
| 195 | + irep.set("a_new_element", irep2); |
| 196 | + REQUIRE(irep.find("a_new_element").id() == "second_irep"); |
| 197 | + irep.set("numeric_id", 1); |
| 198 | + REQUIRE(irep.get_bool("numeric_id")); |
| 199 | + irep.set("numeric_id", 42); |
| 200 | + REQUIRE(!irep.get_bool("numeric_id")); |
| 201 | + REQUIRE(irep.get_int("numeric_id") == 42); |
| 202 | + REQUIRE(irep.get_unsigned_int("numeric_id") == 42u); |
| 203 | + REQUIRE(irep.get_size_t("numeric_id") == 42u); |
| 204 | + REQUIRE(irep.get_long_long("numeric_id") == 42); |
| 205 | + |
| 206 | + irep.clear(); |
| 207 | + REQUIRE(irep.id().empty()); |
| 208 | + REQUIRE(irep.get_sub().empty()); |
| 209 | + REQUIRE(irep.get_named_sub().empty()); |
| 210 | + REQUIRE(irep.get_comments().empty()); |
| 211 | + |
| 212 | + irep.make_nil(); |
| 213 | + REQUIRE(irep.id() == ID_nil); |
| 214 | + REQUIRE(irep.get_sub().empty()); |
| 215 | + REQUIRE(irep.get_named_sub().empty()); |
| 216 | + REQUIRE(irep.get_comments().empty()); |
| 217 | + } |
| 218 | + |
| 219 | + THEN("Pretty printing works") |
| 220 | + { |
| 221 | + irept sub("sub_id"); |
| 222 | + |
| 223 | + irep.id("our_id"); |
| 224 | + irep.add("some_op", sub); |
| 225 | + irep.add("#comment", sub); |
| 226 | + irep.move_to_sub(sub); |
| 227 | + |
| 228 | + std::string pretty = irep.pretty(); |
| 229 | + REQUIRE( |
| 230 | + pretty == |
| 231 | + "our_id\n" |
| 232 | + " * some_op: sub_id\n" |
| 233 | + " * #comment: sub_id\n" |
| 234 | + " 0: sub_id"); |
| 235 | + } |
| 236 | + |
| 237 | + THEN("Hashing works") |
| 238 | + { |
| 239 | + irep.id("some_id"); |
| 240 | + irep.set("#a_comment", 42); |
| 241 | + |
| 242 | + REQUIRE(irep.hash() != 0); |
| 243 | + REQUIRE(irep.full_hash() != 0); |
| 244 | + REQUIRE(irep.hash() != irep.full_hash()); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + GIVEN("Multiple ireps") |
| 249 | + { |
| 250 | + irept irep1, irep2; |
| 251 | + |
| 252 | + THEN("Comparison works") |
| 253 | + { |
| 254 | + REQUIRE(irep1 == irep2); |
| 255 | + REQUIRE(irep1.full_eq(irep2)); |
| 256 | + |
| 257 | + irep1.id("id1"); |
| 258 | + irep2.id("id2"); |
| 259 | + REQUIRE(irep1 != irep2); |
| 260 | + const bool one_lt_two = irep1 < irep2; |
| 261 | + const bool two_lt_one = irep2 < irep1; |
| 262 | + REQUIRE(one_lt_two != two_lt_one); |
| 263 | + REQUIRE(irep1.ordering(irep2) != irep2.ordering(irep1)); |
| 264 | + REQUIRE(irep1.compare(irep2) != 0); |
| 265 | + |
| 266 | + irep2.id("id1"); |
| 267 | + REQUIRE(irep1 == irep2); |
| 268 | + REQUIRE(irep1.full_eq(irep2)); |
| 269 | + |
| 270 | + irep2.set("#a_comment", 42); |
| 271 | + REQUIRE(irep1 == irep2); |
| 272 | + REQUIRE(!irep1.full_eq(irep2)); |
| 273 | + } |
| 274 | + } |
| 275 | +} |
0 commit comments