Skip to content

Commit 5701f6d

Browse files
committed
ExprEngine: Added ifIntRangeAlwaysFalse and ifIntRangeAlwaysTrue tests
1 parent 229e39e commit 5701f6d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/exprengine.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,6 +2504,9 @@ static std::string execute(const Token *start, const Token *end, Data &data)
25042504
if (auto b = std::dynamic_pointer_cast<ExprEngine::BinOpResult>(condValue)) {
25052505
canBeFalse = b->isEqual(&data, 0);
25062506
canBeTrue = b->isTrue(&data);
2507+
} else if (auto i = std::dynamic_pointer_cast<ExprEngine::IntRange>(condValue)) {
2508+
canBeFalse = i->isEqual(&data, 0);
2509+
canBeTrue = ExprEngine::BinOpResult("!=", i, std::make_shared<ExprEngine::IntRange>("0", 0, 0)).isTrue(&data);
25072510
}
25082511

25092512
Data &thenData(data);

test/testexprengine.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class TestExprEngine : public TestFixture {
6565
TEST_CASE(ifelse1);
6666
TEST_CASE(ifif);
6767
TEST_CASE(ifreturn);
68+
TEST_CASE(ifIntRangeAlwaysFalse);
69+
TEST_CASE(ifIntRangeAlwaysTrue);
6870

6971
TEST_CASE(istream);
7072

@@ -487,6 +489,40 @@ class TestExprEngine : public TestFixture {
487489
expr(code, "=="));
488490
}
489491

492+
void ifIntRangeAlwaysFalse() {
493+
const char code[] = "void foo(unsigned char x) {\n"
494+
" if (x > 0)\n"
495+
" return;\n"
496+
" if (x) {\n" // <-- condition should be "always false".
497+
" x++;\n"
498+
" }\n"
499+
" return x == 0;\n" // <- sat
500+
"}";
501+
ASSERT_EQUALS("(<= $1 0)\n"
502+
"(= $1 0)\n"
503+
"(and (>= $1 0) (<= $1 255))\n"
504+
"(= $1 0)\n"
505+
"z3::sat\n",
506+
expr(code, "=="));
507+
}
508+
509+
void ifIntRangeAlwaysTrue() {
510+
const char code[] = "void foo(unsigned char x) {\n"
511+
" if (x < 1)\n"
512+
" return;\n"
513+
" if (x) {\n" // <-- condition should be "always true".
514+
" x++;\n"
515+
" }\n"
516+
" return x == 0;\n" // <- unsat
517+
"}";
518+
ASSERT_EQUALS("(>= $1 1)\n"
519+
"(distinct $1 0)\n"
520+
"(and (>= $1 0) (<= $1 255))\n"
521+
"(= (+ $1 1) 0)\n"
522+
"z3::unsat\n",
523+
expr(code, "=="));
524+
}
525+
490526
void istream() {
491527
const char code[] = "void foo(const std::string& in) {\n"
492528
" std::istringstream istr(in);\n"

0 commit comments

Comments
 (0)