Skip to content

Commit 6fbd1cf

Browse files
author
Daniel Kroening
authored
Merge pull request #1111 from thk123/feature/unit-test-utility
Improvements to the unit tests directory
2 parents f2b7681 + e3f8d4a commit 6fbd1cf

File tree

7 files changed

+224
-21
lines changed

7 files changed

+224
-21
lines changed

src/util/symbol.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ class auxiliary_symbolt:public symbolt
145145
is_file_local=true;
146146
is_auxiliary=true;
147147
}
148+
149+
auxiliary_symbolt(const irep_idt &name, const typet &type):
150+
auxiliary_symbolt()
151+
{
152+
this->name=name;
153+
this->base_name=name;
154+
this->type=type;
155+
}
148156
};
149157

150158
/*! \brief Symbol table entry of function parameter

unit/Makefile

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
.PHONY: all cprover.dir test
22

3-
SRC = unit_tests.cpp \
4-
analyses/does_remove_const/does_expr_lose_const.cpp \
5-
analyses/does_remove_const/does_type_preserve_const_correctness.cpp \
6-
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
7-
catch_example.cpp \
3+
# Source files for test utilities
4+
SRC = src/expr/require_expr.cpp \
5+
src/ansi-c/c_to_expr.cpp \
86
# Empty last line
97

8+
# Test source files
9+
SRC += unit_tests.cpp \
10+
analyses/does_remove_const/does_expr_lose_const.cpp \
11+
analyses/does_remove_const/does_type_preserve_const_correctness.cpp \
12+
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
13+
catch_example.cpp \
14+
# Empty last line
15+
1016
INCLUDES= -I ../src/ -I.
1117

1218
include ../src/config.inc
@@ -15,19 +21,21 @@ include ../src/common
1521
cprover.dir:
1622
$(MAKE) $(MAKEARGS) -C ../src
1723

18-
LIBS += ../src/ansi-c/ansi-c$(LIBEXT) \
19-
../src/cpp/cpp$(LIBEXT) \
20-
../src/json/json$(LIBEXT) \
21-
../src/linking/linking$(LIBEXT) \
22-
../src/util/util$(LIBEXT) \
23-
../src/big-int/big-int$(LIBEXT) \
24-
../src/goto-programs/goto-programs$(LIBEXT) \
25-
../src/pointer-analysis/pointer-analysis$(LIBEXT) \
26-
../src/langapi/langapi$(LIBEXT) \
27-
../src/assembler/assembler$(LIBEXT) \
28-
../src/analyses/analyses$(LIBEXT) \
29-
../src/solvers/solvers$(LIBEXT) \
30-
# Empty last line
24+
CPROVER_LIBS =../src/ansi-c/ansi-c$(LIBEXT) \
25+
../src/cpp/cpp$(LIBEXT) \
26+
../src/json/json$(LIBEXT) \
27+
../src/linking/linking$(LIBEXT) \
28+
../src/util/util$(LIBEXT) \
29+
../src/big-int/big-int$(LIBEXT) \
30+
../src/goto-programs/goto-programs$(LIBEXT) \
31+
../src/pointer-analysis/pointer-analysis$(LIBEXT) \
32+
../src/langapi/langapi$(LIBEXT) \
33+
../src/assembler/assembler$(LIBEXT) \
34+
../src/analyses/analyses$(LIBEXT) \
35+
../src/solvers/solvers$(LIBEXT) \
36+
# Empty last line
37+
38+
OBJ += $(CPROVER_LIBS)
3139

3240
TESTS = unit_tests$(EXEEXT) \
3341
miniBDD$(EXEEXT) \
@@ -49,11 +57,11 @@ test: all
4957
unit_tests$(EXEEXT): $(OBJ)
5058
$(LINKBIN)
5159

52-
miniBDD$(EXEEXT): miniBDD$(OBJEXT)
60+
miniBDD$(EXEEXT): miniBDD$(OBJEXT) $(CPROVER_LIBS)
5361
$(LINKBIN)
5462

55-
string_utils$(EXEEXT): string_utils$(OBJEXT)
63+
string_utils$(EXEEXT): string_utils$(OBJEXT) $(CPROVER_LIBS)
5664
$(LINKBIN)
5765

58-
sharing_node$(EXEEXT): sharing_node$(OBJEXT)
66+
sharing_node$(EXEEXT): sharing_node$(OBJEXT) $(CPROVER_LIBS)
5967
$(LINKBIN)

unit/src/ansi-c/c_to_expr.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Utility for converting strings in to exprt, throwing a CATCH exception
11+
/// if this fails in any way.
12+
///
13+
#include "c_to_expr.h"
14+
15+
#include <catch.hpp>
16+
17+
c_to_exprt::c_to_exprt():
18+
message_handler(
19+
std::unique_ptr<message_handlert>(new ui_message_handlert()))
20+
{
21+
language.set_message_handler(*message_handler);
22+
}
23+
24+
/// Take an input string that should be a valid C rhs expression
25+
/// \param input_string: The string to convert
26+
/// \param ns: The global namespace
27+
/// \return: A constructed expr representing the string
28+
exprt c_to_exprt::operator()(
29+
const std::string &input_string, const namespacet &ns)
30+
{
31+
exprt expr;
32+
bool result=language.to_expr(input_string, "", expr, ns);
33+
REQUIRE(!result);
34+
return expr;
35+
}

unit/src/ansi-c/c_to_expr.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Utility for converting strings in to exprt, throwing a CATCH exception
11+
/// if this fails in any way.
12+
13+
#ifndef CPROVER_SRC_ANSI_C_C_TO_EXPR_H
14+
#define CPROVER_SRC_ANSI_C_C_TO_EXPR_H
15+
16+
#include <memory>
17+
18+
#include <util/expr.h>
19+
#include <util/message.h>
20+
#include <util/ui_message.h>
21+
#include <util/namespace.h>
22+
#include <ansi-c/ansi_c_language.h>
23+
24+
class c_to_exprt
25+
{
26+
public:
27+
c_to_exprt();
28+
exprt operator()(const std::string &input_string, const namespacet &ns);
29+
30+
private:
31+
std::unique_ptr<message_handlert> message_handler;
32+
ansi_c_languaget language;
33+
};
34+
35+
#endif // CPROVER_SRC_ANSI_C_C_TO_EXPR_H

unit/src/expr/require_expr.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Helper functions for requiring specific expressions
11+
/// If the expression is of the wrong type, throw a CATCH exception
12+
/// Also checks associated properties and returns a casted version of the
13+
/// expression.
14+
15+
#include "require_expr.h"
16+
17+
#include <catch.hpp>
18+
#include <util/arith_tools.h>
19+
20+
/// Verify a given exprt is an index_exprt with a a constant value equal to the
21+
/// expected value
22+
/// \param expr: The expression.
23+
/// \param expected_index: The constant value that should be the index.
24+
/// \return The expr cast to an index_exprt
25+
index_exprt require_expr::require_index(const exprt &expr, int expected_index)
26+
{
27+
REQUIRE(expr.id()==ID_index);
28+
const index_exprt &index_expr=to_index_expr(expr);
29+
REQUIRE(index_expr.index().id()==ID_constant);
30+
const constant_exprt &index_value=to_constant_expr(index_expr.index());
31+
mp_integer index_integer_value;
32+
to_integer(index_value, index_integer_value);
33+
REQUIRE(index_integer_value==expected_index);
34+
35+
return index_expr;
36+
}
37+
38+
/// Verify a given exprt is an index_exprt with a nil value as its index
39+
/// \param expr: The expression.
40+
/// \return The expr cast to an index_exprt
41+
index_exprt require_expr::require_top_index(const exprt &expr)
42+
{
43+
REQUIRE(expr.id()==ID_index);
44+
const index_exprt &index_expr=to_index_expr(expr);
45+
REQUIRE(index_expr.index().id()==ID_nil);
46+
return index_expr;
47+
}
48+
49+
/// Verify a given exprt is an member_exprt with a component name equal to the
50+
/// component_identifier
51+
/// \param expr: The expression.
52+
/// \param component_identifier: The name of the component that should be being
53+
/// accessed.
54+
/// \return The expr cast to a member_exprt.
55+
member_exprt require_expr::require_member(
56+
const exprt &expr, const irep_idt &component_identifier)
57+
{
58+
REQUIRE(expr.id()==ID_member);
59+
const member_exprt &member_expr=to_member_expr(expr);
60+
REQUIRE(member_expr.get_component_name()==component_identifier);
61+
return member_expr;
62+
}
63+
64+
/// Verify a given exprt is an symbol_exprt with a identifier name equal to the
65+
/// symbol_name.
66+
/// \param expr: The expression.
67+
/// \param symbol_name: The intended identifier of the symbol
68+
/// \return The expr cast to a symbol_exprt
69+
symbol_exprt require_expr::require_symbol(
70+
const exprt &expr, const irep_idt &symbol_name)
71+
{
72+
REQUIRE(expr.id()==ID_symbol);
73+
const symbol_exprt &symbol_expr=to_symbol_expr(expr);
74+
REQUIRE(symbol_expr.get_identifier()==symbol_name);
75+
return symbol_expr;
76+
}

unit/src/expr/require_expr.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Helper functions for requiring specific expressions
11+
/// If the expression is of the wrong type, throw a CATCH exception
12+
/// Also checks associated properties and returns a casted version of the
13+
/// expression.
14+
15+
#ifndef CPROVER_SRC_EXPR_REQUIRE_EXPR_H
16+
#define CPROVER_SRC_EXPR_REQUIRE_EXPR_H
17+
18+
#include <util/std_expr.h>
19+
20+
// NOLINTNEXTLINE(readability/namespace)
21+
namespace require_expr
22+
{
23+
index_exprt require_index(const exprt &expr, int expected_index);
24+
index_exprt require_top_index(const exprt &expr);
25+
26+
member_exprt require_member(
27+
const exprt &expr, const irep_idt &component_identifier);
28+
29+
symbol_exprt require_symbol(
30+
const exprt &expr, const irep_idt &symbol_name);
31+
}
32+
33+
#endif // CPROVER_SRC_EXPR_REQUIRE_EXPR_H

unit/unit_tests.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@
88

99
#define CATCH_CONFIG_MAIN
1010
#include "catch.hpp"
11+
#include <util/irep.h>
12+
13+
// Debug printer for irept
14+
std::ostream &operator<<(std::ostream &os, const irept &value)
15+
{
16+
os << value.pretty();
17+
return os;
18+
}

0 commit comments

Comments
 (0)