|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: Unit tests for union_find_replacet in |
| 4 | + solvers/refinement/string_refinement.cpp |
| 5 | +
|
| 6 | + Author: DiffBlue Limited. All rights reserved. |
| 7 | +
|
| 8 | +\*******************************************************************/ |
| 9 | + |
| 10 | +#include <testing-utils/catch.hpp> |
| 11 | + |
| 12 | +#include <util/arith_tools.h> |
| 13 | +#include <util/c_types.h> |
| 14 | +#include <util/std_types.h> |
| 15 | +#include <util/std_expr.h> |
| 16 | +#include <solvers/refinement/string_refinement.h> |
| 17 | + |
| 18 | +SCENARIO("union_find_replace", "[core][solvers][refinement][string_refinement]") |
| 19 | +{ |
| 20 | + GIVEN("An empty dictionary") |
| 21 | + { |
| 22 | + union_find_replacet dict; |
| 23 | + pointer_typet char_pointer_type = pointer_type(unsignedbv_typet(16)); |
| 24 | + const symbol_exprt a("a", char_pointer_type); |
| 25 | + const symbol_exprt b("b", char_pointer_type); |
| 26 | + const symbol_exprt c("c", char_pointer_type); |
| 27 | + const symbol_exprt d("d", char_pointer_type); |
| 28 | + const symbol_exprt e("e", char_pointer_type); |
| 29 | + const symbol_exprt f("f", char_pointer_type); |
| 30 | + |
| 31 | + WHEN("Relations a=b, a=c, d=b, e=f are added") |
| 32 | + { |
| 33 | + dict.make_union(a, b); |
| 34 | + dict.make_union(a, c); |
| 35 | + dict.make_union(d, b); |
| 36 | + dict.make_union(e, f); |
| 37 | + THEN("find(d)=find(c), but find(e)!=find(a)") |
| 38 | + { |
| 39 | + REQUIRE(dict.find(d) == dict.find(c)); // transitive equality |
| 40 | + REQUIRE(dict.find(a) == dict.find(a)); // trivial equality |
| 41 | + REQUIRE(dict.find(b) == dict.find(d)); // rhs only symbol |
| 42 | + REQUIRE(dict.find(b) == dict.find(c)); // rhs only transitive |
| 43 | + REQUIRE(dict.find(e) != dict.find(a)); // transitive not equal |
| 44 | + REQUIRE(dict.find(f) != dict.find(a)); // transitive not equal |
| 45 | + } |
| 46 | + |
| 47 | + GIVEN("Expressions a+e, a+d, c+f, c+d") |
| 48 | + { |
| 49 | + plus_exprt a_plus_e(a, e); |
| 50 | + plus_exprt a_plus_d(a, d); |
| 51 | + plus_exprt c_plus_f(c, f); |
| 52 | + plus_exprt c_plus_d(c, d); |
| 53 | + WHEN("We use the dictionary for replacement") |
| 54 | + { |
| 55 | + dict.replace_expr(a_plus_e); |
| 56 | + dict.replace_expr(a_plus_d); |
| 57 | + dict.replace_expr(c_plus_f); |
| 58 | + dict.replace_expr(c_plus_d); |
| 59 | + THEN("a+e=c+f but a+e!=c+d") |
| 60 | + { |
| 61 | + REQUIRE(a_plus_e == c_plus_f); |
| 62 | + REQUIRE(a_plus_e != c_plus_d); |
| 63 | + REQUIRE(a_plus_d == c_plus_d); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + THEN("Introducing cycles does not cause infinite loops or exceptions") |
| 69 | + { |
| 70 | + dict.make_union(c, d); |
| 71 | + REQUIRE(dict.find(d) == dict.find(c)); |
| 72 | + REQUIRE(dict.find(e) != dict.find(a)); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments