Skip to content

Commit 954060e

Browse files
Add unit test for has_subtype
1 parent 3dd3877 commit 954060e

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SRC += unit_tests.cpp \
3131
solvers/refinement/string_constraint_generator_valueof/is_digit_with_radix.cpp \
3232
solvers/refinement/string_constraint_instantiation/instantiate_not_contains.cpp \
3333
solvers/refinement/string_refinement/concretize_array.cpp \
34+
solvers/refinement/string_refinement/has_subtype.cpp \
3435
solvers/refinement/string_refinement/substitute_array_list.cpp \
3536
solvers/refinement/string_refinement/union_find_replace.cpp \
3637
util/expr_cast/expr_cast.cpp \
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)