Skip to content

Commit bd167d0

Browse files
author
thk123
committed
Add test cases for simplifying boolean formula
1 parent 077b5ee commit bd167d0

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

unit/util/simplify_expr.cpp

+122
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,125 @@ TEST_CASE("Simplify cast from bool", "[core][util]")
298298
REQUIRE(simp == B);
299299
}
300300
}
301+
302+
TEST_CASE("simplify_expr boolean expressions", "[core][util]")
303+
{
304+
symbol_tablet symbol_table;
305+
namespacet ns{symbol_table};
306+
307+
SECTION("Binary boolean operations")
308+
{
309+
struct test_entryt
310+
{
311+
exprt lhs;
312+
exprt rhs;
313+
exprt expected_value;
314+
};
315+
316+
SECTION("AND")
317+
{
318+
std::vector<test_entryt> test_values = {
319+
{true_exprt(), true_exprt(), true_exprt()},
320+
{true_exprt(), false_exprt(), false_exprt()},
321+
{false_exprt(), true_exprt(), false_exprt()},
322+
{false_exprt(), false_exprt(), false_exprt()},
323+
};
324+
325+
for(const auto &entry : test_values)
326+
{
327+
const exprt result = simplify_expr(and_exprt{entry.lhs, entry.rhs}, ns);
328+
REQUIRE(result == entry.expected_value);
329+
}
330+
}
331+
332+
SECTION("OR")
333+
{
334+
std::vector<test_entryt> test_values = {
335+
{true_exprt(), true_exprt(), true_exprt()},
336+
{true_exprt(), false_exprt(), true_exprt()},
337+
{false_exprt(), true_exprt(), true_exprt()},
338+
{false_exprt(), false_exprt(), false_exprt()},
339+
};
340+
341+
for(const auto &entry : test_values)
342+
{
343+
const exprt result = simplify_expr(or_exprt{entry.lhs, entry.rhs}, ns);
344+
REQUIRE(result == entry.expected_value);
345+
}
346+
}
347+
348+
SECTION("Implies")
349+
{
350+
std::vector<test_entryt> test_values = {
351+
{true_exprt(), true_exprt(), true_exprt()},
352+
{true_exprt(), false_exprt(), false_exprt()},
353+
{false_exprt(), true_exprt(), true_exprt()},
354+
{false_exprt(), false_exprt(), true_exprt()},
355+
};
356+
357+
for(const auto &entry : test_values)
358+
{
359+
const exprt result =
360+
simplify_expr(implies_exprt{entry.lhs, entry.rhs}, ns);
361+
REQUIRE(result == entry.expected_value);
362+
}
363+
}
364+
}
365+
SECTION("Not")
366+
{
367+
REQUIRE(simplify_expr(not_exprt{true_exprt()}, ns) == false_exprt());
368+
REQUIRE(simplify_expr(not_exprt{false_exprt()}, ns) == true_exprt());
369+
}
370+
SECTION("Nested boolean expressions")
371+
{
372+
INFO("((!true) || true) && (false => false))")
373+
REQUIRE(
374+
simplify_expr(
375+
and_exprt{or_exprt{not_exprt{true_exprt{}},
376+
implies_exprt{false_exprt{}, false_exprt{}}},
377+
true_exprt{}},
378+
ns) == true_exprt{});
379+
}
380+
SECTION("Numeric comparisons")
381+
{
382+
struct test_entryt
383+
{
384+
irep_idt comparision;
385+
int lhs;
386+
int rhs;
387+
exprt expected;
388+
};
389+
390+
std::vector<test_entryt> comparisons = {
391+
{ID_lt, -1, 1, true_exprt()},
392+
{ID_lt, 1, 1, false_exprt()},
393+
{ID_lt, 1, -1, false_exprt()},
394+
395+
{ID_le, -1, 1, true_exprt()},
396+
{ID_le, 1, 1, true_exprt()},
397+
{ID_le, 1, -1, false_exprt()},
398+
399+
{ID_ge, -1, 1, false_exprt()},
400+
{ID_ge, 1, 1, true_exprt()},
401+
{ID_ge, 1, -1, true_exprt()},
402+
403+
{ID_gt, -1, 1, false_exprt()},
404+
{ID_gt, 1, 1, false_exprt()},
405+
{ID_gt, 1, -1, true_exprt()},
406+
};
407+
408+
const auto binary_relation_from = [](const test_entryt &entry) {
409+
const signedbv_typet int_type(32);
410+
return binary_relation_exprt{from_integer(entry.lhs, int_type),
411+
entry.comparision,
412+
from_integer(entry.rhs, int_type)};
413+
};
414+
415+
for(const test_entryt &test_entry : comparisons)
416+
{
417+
REQUIRE(
418+
simplify_expr(binary_relation_from(test_entry), ns) ==
419+
test_entry.expected);
420+
}
421+
}
422+
}

0 commit comments

Comments
 (0)