Skip to content

Commit 786e3b8

Browse files
Petr BauchPetr Bauch
authored andcommitted
Added unit test for wrong type in table
Builds a goto program with one assertion and checks that: 1) with the right type in the table the validation succeeds, 2) with the wrong type the validation fails.
1 parent 7145230 commit 786e3b8

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

src/goto-programs/goto_program.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,6 @@ void goto_programt::instructiont::validate(
675675
{
676676
namespacet ns(table);
677677
std::vector<std::vector<std::string>> type_collector;
678-
std::stringstream location_stream;
679-
location_stream << source_location;
680678

681679
auto type_finder = [&](const exprt &e) {
682680
if(e.id() == ID_symbol)
@@ -685,13 +683,14 @@ void goto_programt::instructiont::validate(
685683
const auto &symbol_id = symbol_expr.get_identifier();
686684

687685
if(table.has_symbol(symbol_id))
688-
DATA_CHECK(
686+
DATA_CHECK_WITH_DIAGNOSTICS(
689687
base_type_eq(
690688
symbol_expr.type(), table.lookup_ref(symbol_id).type, ns),
691-
id2string(symbol_id) + " type inconsistency (" +
692-
location_stream.str() + ")\n" + "goto program type: " +
693-
symbol_expr.type().id_string() + "\n " + "symbol table type: " +
694-
table.lookup_ref(symbol_id).type.id_string());
689+
id2string(symbol_id) + " type inconsistency\n" +
690+
"goto program type: " + symbol_expr.type().id_string() + "\n " +
691+
"symbol table type: " +
692+
table.lookup_ref(symbol_id).type.id_string(),
693+
source_location);
695694
}
696695
};
697696

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ SRC += analyses/ai/ai.cpp \
1515
analyses/does_remove_const/does_type_preserve_const_correctness.cpp \
1616
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
1717
goto-programs/goto_trace_output.cpp \
18+
goto-programs/goto_program_symbol_type_table_consistency.cpp \
1819
interpreter/interpreter.cpp \
1920
json/json_parser.cpp \
2021
path_strategies.cpp \
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for goto_program::validate
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#include <goto-programs/goto_function.h>
10+
#include <testing-utils/catch.hpp>
11+
#include <util/arith_tools.h>
12+
13+
SCENARIO(
14+
"Validation of consistent program/table pair (type-wise)",
15+
"[core][goto-programs][validate]")
16+
{
17+
GIVEN("A program with one assertion")
18+
{
19+
symbol_tablet symbol_table;
20+
const typet type1 = signedbv_typet(32);
21+
const typet type2 = signedbv_typet(64);
22+
symbolt symbol;
23+
irep_idt symbol_name = "a";
24+
symbol.name = symbol_name;
25+
symbol_exprt varx(symbol_name, type1);
26+
27+
exprt val10 = from_integer(10, type1);
28+
binary_relation_exprt x_le_10(varx, ID_le, val10);
29+
30+
goto_functiont goto_function;
31+
auto &instructions = goto_function.body.instructions;
32+
instructions.emplace_back(goto_program_instruction_typet::ASSERT);
33+
instructions.back().make_assertion(x_le_10);
34+
35+
WHEN("Symbol table has the right symbol type")
36+
{
37+
symbol.type = type1;
38+
symbol_table.insert(symbol);
39+
40+
THEN("The consistency check succeeds")
41+
{
42+
goto_function.validate(symbol_table, validation_modet::INVARIANT);
43+
44+
REQUIRE(true);
45+
}
46+
}
47+
48+
WHEN("Symbol table has the wrong symbol type")
49+
{
50+
symbol.type = type2;
51+
symbol_table.insert(symbol);
52+
53+
THEN("The consistency check fails")
54+
{
55+
bool caught = false;
56+
try
57+
{
58+
goto_function.validate(symbol_table, validation_modet::EXCEPTION);
59+
}
60+
catch(incorrect_goto_program_exceptiont &e)
61+
{
62+
caught = true;
63+
}
64+
REQUIRE(caught);
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)