|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: Unit tests for has_subtype 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/c_types.h> |
| 13 | +#include <solvers/refinement/string_refinement.h> |
| 14 | +#include <java_bytecode/java_types.h> |
| 15 | + |
| 16 | +// Curryfied version of type comparison. |
| 17 | +// Useful for the predicate argument of has_subtype |
| 18 | +static std::function<bool(const typet &)> is_type(const typet &t1) |
| 19 | +{ |
| 20 | + auto f = [&](const typet &t2) { return t1 == t2; }; |
| 21 | + return f; |
| 22 | +} |
| 23 | + |
| 24 | +SCENARIO("has_subtype", "[core][solvers][refinement][string_refinement]") |
| 25 | +{ |
| 26 | + const typet char_type = java_char_type(); |
| 27 | + const typet int_type = java_int_type(); |
| 28 | + const typet bool_type = java_boolean_type(); |
| 29 | + |
| 30 | + REQUIRE(has_subtype(char_type, is_type(char_type))); |
| 31 | + REQUIRE_FALSE(has_subtype(char_type, is_type(int_type))); |
| 32 | + |
| 33 | + GIVEN("a struct with char and int fields") |
| 34 | + { |
| 35 | + struct_typet struct_type; |
| 36 | + struct_type.components().emplace_back("char_field", char_type); |
| 37 | + struct_type.components().emplace_back("int_field", int_type); |
| 38 | + THEN("char and int are subtypes") |
| 39 | + { |
| 40 | + REQUIRE(has_subtype(struct_type, is_type(char_type))); |
| 41 | + REQUIRE(has_subtype(struct_type, is_type(int_type))); |
| 42 | + } |
| 43 | + THEN("bool is not a subtype") |
| 44 | + { |
| 45 | + REQUIRE_FALSE(has_subtype(struct_type, is_type(bool_type))); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + GIVEN("a pointer to char") |
| 50 | + { |
| 51 | + pointer_typet ptr_type = pointer_type(char_type); |
| 52 | + THEN("char is a subtype") |
| 53 | + { |
| 54 | + REQUIRE(has_subtype(ptr_type, is_type(char_type))); |
| 55 | + } |
| 56 | + THEN("int is not a subtype") |
| 57 | + { |
| 58 | + REQUIRE_FALSE(has_subtype(ptr_type, is_type(int_type))); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments