Skip to content

Commit bdc7a9a

Browse files
author
Thomas Kiley
committed
Add test case for eval on binary operations
1 parent 3a8fdd1 commit bdc7a9a

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

unit/util/interval/eval.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,62 @@ SCENARIO("Unary eval on intervals", "[core][analyses][interval][eval]")
103103
}
104104
}
105105
}
106+
107+
TEST_CASE("binary eval switch", "[core][analyses][interval]")
108+
{
109+
const auto interval_of = [](const int value) {
110+
return constant_interval_exprt(from_integer(value, signedbv_typet(32)));
111+
};
112+
113+
const auto interval_from_to = [](const int lower, const int upper) {
114+
return constant_interval_exprt(
115+
from_integer(lower, signedbv_typet(32)),
116+
from_integer(upper, signedbv_typet(32)));
117+
};
118+
119+
const auto boolean_interval = [](const bool value) {
120+
return constant_interval_exprt::tvt_to_interval(tvt(value));
121+
};
122+
123+
constant_interval_exprt two = interval_of(2);
124+
constant_interval_exprt five = interval_of(5);
125+
constant_interval_exprt eight = interval_of(8);
126+
127+
SECTION("Numeric operations")
128+
{
129+
REQUIRE(five.eval(ID_plus, eight) == interval_of(13));
130+
REQUIRE(five.eval(ID_minus, eight) == interval_of(-3));
131+
REQUIRE(five.eval(ID_mult, eight) == interval_of(40));
132+
REQUIRE(five.eval(ID_div, eight) == interval_of(0));
133+
REQUIRE(five.eval(ID_mod, eight) == interval_from_to(0, 5));
134+
REQUIRE(five.eval(ID_shl, two) == interval_of(20 /* 5 << 2 */));
135+
REQUIRE(five.eval(ID_ashr, two) == interval_of(1 /* 5 >> 2 */));
136+
REQUIRE(five.eval(ID_bitor, eight) == interval_of(13));
137+
REQUIRE(five.eval(ID_bitand, eight) == interval_of(0));
138+
REQUIRE(five.eval(ID_bitxor, two) == interval_of(7));
139+
}
140+
SECTION("Comparisons")
141+
{
142+
REQUIRE(five.eval(ID_lt, eight) == boolean_interval(true));
143+
REQUIRE(five.eval(ID_le, eight) == boolean_interval(true));
144+
REQUIRE(five.eval(ID_gt, eight) == boolean_interval(false));
145+
REQUIRE(five.eval(ID_ge, eight) == boolean_interval(false));
146+
REQUIRE(five.eval(ID_equal, eight) == boolean_interval(false));
147+
REQUIRE(five.eval(ID_notequal, eight) == boolean_interval(true));
148+
}
149+
SECTION("Logical operators")
150+
{
151+
const auto true_interval = boolean_interval(true);
152+
const auto false_interval = boolean_interval(false);
153+
REQUIRE(
154+
true_interval.eval(ID_and, false_interval) == boolean_interval(false));
155+
REQUIRE(
156+
true_interval.eval(ID_or, false_interval) == boolean_interval(true));
157+
REQUIRE(
158+
true_interval.eval(ID_xor, false_interval) == boolean_interval(true));
159+
}
160+
SECTION("Invalid operations")
161+
{
162+
REQUIRE(five.eval(ID_type, eight).is_top());
163+
}
164+
}

0 commit comments

Comments
 (0)