|
| 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 well-formed function call codes", |
| 15 | + "[core][goto-programs][validate]") |
| 16 | +{ |
| 17 | + GIVEN("A program with one function call") |
| 18 | + { |
| 19 | + symbol_tablet symbol_table; |
| 20 | + const signedbv_typet type1(32); |
| 21 | + const signedbv_typet type2(64); |
| 22 | + const code_typet code_type({}, type1); |
| 23 | + |
| 24 | + symbolt var_symbol; |
| 25 | + irep_idt var_symbol_name = "a"; |
| 26 | + var_symbol.name = var_symbol_name; |
| 27 | + symbol_exprt var_a(var_symbol_name, type1); |
| 28 | + |
| 29 | + symbolt var_symbol2; |
| 30 | + irep_idt var_symbol_name2 = "b"; |
| 31 | + var_symbol2.name = var_symbol_name2; |
| 32 | + symbol_exprt var_b(var_symbol_name2, type2); |
| 33 | + |
| 34 | + symbolt fun_symbol; |
| 35 | + irep_idt fun_symbol_name = "foo"; |
| 36 | + fun_symbol.name = fun_symbol_name; |
| 37 | + symbol_exprt fun_foo(fun_symbol_name, code_type); |
| 38 | + |
| 39 | + goto_functiont goto_function; |
| 40 | + auto &instructions = goto_function.body.instructions; |
| 41 | + instructions.emplace_back(goto_program_instruction_typet::FUNCTION_CALL); |
| 42 | + |
| 43 | + var_symbol.type = type1; |
| 44 | + var_symbol2.type = type2; |
| 45 | + fun_symbol.type = type1; |
| 46 | + symbol_table.insert(var_symbol); |
| 47 | + symbol_table.insert(var_symbol2); |
| 48 | + symbol_table.insert(fun_symbol); |
| 49 | + namespacet ns(symbol_table); |
| 50 | + |
| 51 | + WHEN("Return type matches") |
| 52 | + { |
| 53 | + code_function_callt function_call(var_a, fun_foo, {}); |
| 54 | + instructions.back().make_function_call(function_call); |
| 55 | + REQUIRE(instructions.back().code.get_statement() == ID_function_call); |
| 56 | + |
| 57 | + THEN("The consistency check succeeds") |
| 58 | + { |
| 59 | + goto_function.body.validate(ns, validation_modet::INVARIANT); |
| 60 | + REQUIRE(true); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + WHEN("Return type differs from function type") |
| 65 | + { |
| 66 | + code_function_callt function_call(var_b, fun_foo, {}); |
| 67 | + instructions.back().make_function_call(function_call); |
| 68 | + |
| 69 | + THEN("The consistency check fails") |
| 70 | + { |
| 71 | + bool caught = false; |
| 72 | + try |
| 73 | + { |
| 74 | + goto_function.body.validate(ns, validation_modet::EXCEPTION); |
| 75 | + } |
| 76 | + catch(incorrect_goto_program_exceptiont &e) |
| 77 | + { |
| 78 | + caught = true; |
| 79 | + } |
| 80 | + REQUIRE(caught); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments