Skip to content

Commit 7e1ba16

Browse files
author
Daniel Kroening
authored
Merge pull request #874 from thk123/feature/unit-test-framework
Add the Catch unit testing framework
2 parents 947a75c + 760e1fc commit 7e1ba16

File tree

8 files changed

+11520
-61
lines changed

8 files changed

+11520
-61
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ script:
152152
- if [ -e bin/gcc ] ; then export PATH=$PWD/bin:$PATH ; fi ;
153153
COMMAND="env UBSAN_OPTIONS=print_stacktrace=1 make -C regression test" &&
154154
eval ${PRE_COMMAND} ${COMMAND}
155+
- COMMAND="make -C unit CXX=\"$COMPILER\" CXXFLAGS=\"$FLAGS $EXTRA_CXXFLAGS\" -j2" &&
156+
eval ${PRE_COMMAND} ${COMMAND}
157+
- COMMAND="make -C unit test" && eval ${PRE_COMMAND} ${COMMAND}
155158

156159
before_cache:
157160
- ccache -s

CODING_STANDARD

+32-14
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ Whitespaces:
2626
- No whitespaces in blank lines
2727
- Put argument lists on next line (and ident 2 spaces) if too long
2828
- Put parameters on separate lines (and ident 2 spaces) if too long
29-
- No whitespaces around colon for inheritance,
29+
- No whitespaces around colon for inheritance,
3030
put inherited into separate lines in case of multiple inheritance
3131
- The initializer list follows the constructor without a whitespace
3232
around the colon. Break line after the colon if required and indent members.
3333
- if(...), else, for(...), do, and while(...) are always in a separate line
34-
- Break expressions in if, for, while if necessary and align them
34+
- Break expressions in if, for, while if necessary and align them
3535
with the first character following the opening parenthesis
3636
- Use {} instead of ; for the empty statement
37-
- Single line blocks without { } are allowed,
37+
- Single line blocks without { } are allowed,
3838
but put braces around multi-line blocks
39-
- Use blank lines to visually separate logically cohesive code blocks
39+
- Use blank lines to visually separate logically cohesive code blocks
4040
within a function
4141
- Have a newline at the end of a file
4242

4343
Comments:
4444
- Do not use /* */ except for file and function comment blocks
45-
- Each source and header file must start with a comment block
45+
- Each source and header file must start with a comment block
4646
stating the Module name and Author [will be changed when we roll out doxygen]
4747
- Each function in the source file (not the header) is preceded
4848
by a function comment header consisting of a comment block stating
@@ -75,9 +75,9 @@ Comments:
7575
- Use #ifdef DEBUG to guard debug code
7676

7777
Naming:
78-
- Identifiers may use the characters [a-z0-9_] and should start with a
78+
- Identifiers may use the characters [a-z0-9_] and should start with a
7979
lower-case letter (parameters in constructors may start with _).
80-
- Use american spelling for identifiers.
80+
- Use american spelling for identifiers.
8181
- Separate basic words by _
8282
- Avoid abbreviations (e.g. prefer symbol_table to of st).
8383
- User defined type identifiers have to be terminated by 't'. Moreover,
@@ -136,7 +136,7 @@ C++ features:
136136
- Avoid iterators, use ranged for instead
137137
- Avoid allocation with new/delete, use unique_ptr
138138
- Avoid pointers, use references
139-
- Avoid char *, use std::string
139+
- Avoid char *, use std::string
140140
- For numbers, use int, unsigned, long, unsigned long, double
141141
- Use mp_integer, not BigInt
142142
- Use the functions in util for conversions between numbers and strings
@@ -146,13 +146,13 @@ C++ features:
146146
- Use instances of std::size_t for comparison with return values of .size() of
147147
STL containers and algorithms, and use them as indices to arrays or vectors.
148148
- Do not use default values in public functions
149-
- Use assertions to detect programming errors, e.g. whenever you make
149+
- Use assertions to detect programming errors, e.g. whenever you make
150150
assumptions on how your code is used
151-
- Use exceptions only when the execution of the program has to abort
151+
- Use exceptions only when the execution of the program has to abort
152152
because of erroneous user input
153-
- We allow to use 3rd-party libraries directly.
154-
No wrapper matching the coding rules is required.
155-
Allowed libraries are: STL.
153+
- We allow to use 3rd-party libraries directly.
154+
No wrapper matching the coding rules is required.
155+
Allowed libraries are: STL.
156156
- When throwing, omit the brackets, i.e. `throw "error"`.
157157
- Error messages should start with a lower case letter.
158158
- Use the auto keyword if and only if one of the following
@@ -165,12 +165,30 @@ Architecture-specific code:
165165
- Don't include architecture-specific header files without #ifdef ...
166166

167167
Output:
168-
- Do not output to cout or cerr directly (except in temporary debug code,
168+
- Do not output to cout or cerr directly (except in temporary debug code,
169169
and then guard #include <iostream> by #ifdef DEBUG)
170170
- Derive from messaget if the class produces output and use the streams provided
171171
(status(), error(), debug(), etc)
172172
- Use '\n' instead of std::endl
173173

174+
Unit tests:
175+
- Unit tests are written using Catch: https://github.com/philsquared/Catch/
176+
- For large classes:
177+
- Create a separate file that contains the tests for each method of each
178+
class
179+
- The file should be named according to
180+
`unit/class/path/class_name/function_name.cpp`
181+
- For small classes:
182+
- Create a separate file that contains the tests for all methods of each
183+
class
184+
- The file should be named according to unit/class/path/class_name.cpp
185+
- Catch supports tagging, tests should be tagged with all the following tags:
186+
- [core] should be used for all tests unless the test takes more than 1
187+
second to run, then it should be tagged with [long]
188+
- [folder_name] of the file being tested
189+
- [class_name] of the class being tested
190+
- [function_name] of the function being tested
191+
174192
You are allowed to break rules if you have a good reason to do so.
175193

176194
Pre-commit hook to run cpplint locally

appveyor.yml

+4
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,7 @@ test_script:
108108
rmdir /s /q goto-instrument\slice08
109109
110110
make test
111+
112+
cd ..
113+
make -C unit all
114+
make -C unit test

unit/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Unit test binaries
2+
miniBDD
3+
sharing_node
4+
string_utils
5+
unit_tests

unit/Makefile

+21-47
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
SRC = cpp_parser.cpp \
2-
cpp_scanner.cpp \
3-
elf_reader.cpp \
4-
float_utils.cpp \
5-
ieee_float.cpp \
6-
json.cpp \
7-
miniBDD.cpp \
8-
osx_fat_reader.cpp \
9-
sharing_map.cpp \
10-
sharing_node.cpp \
11-
smt2_parser.cpp \
12-
string_utils.cpp \
13-
unicode.cpp \
14-
wp.cpp \
1+
.PHONY: all cprover.dir test
2+
3+
SRC = unit_tests.cpp \
4+
catch_example.cpp \
155
# Empty last line
166

17-
INCLUDES= -I ../src/
7+
INCLUDES= -I ../src/ -I.
188

199
include ../src/config.inc
2010
include ../src/common
2111

12+
cprover.dir:
13+
$(MAKE) $(MAKEARGS) -C ../src
14+
2215
LIBS = ../src/ansi-c/ansi-c$(LIBEXT) \
2316
../src/cpp/cpp$(LIBEXT) \
2417
../src/json/json$(LIBEXT) \
@@ -31,52 +24,33 @@ LIBS = ../src/ansi-c/ansi-c$(LIBEXT) \
3124
../src/assembler/assembler$(LIBEXT) \
3225
../src/analyses/analyses$(LIBEXT) \
3326
../src/solvers/solvers$(LIBEXT) \
27+
# Empty last line
3428

35-
CLEANFILES = $(SRC:.cpp=$(EXEEXT))
29+
TESTS = unit_tests$(EXEEXT) \
30+
miniBDD$(EXEEXT) \
31+
string_utils$(EXEEXT) \
32+
sharing_node$(EXEEXT) \
33+
# Empty last line
3634

37-
all: $(SRC:.cpp=$(EXEEXT))
35+
CLEANFILES = $(TESTS)
3836

39-
###############################################################################
37+
all: cprover.dir
38+
$(MAKE) $(MAKEARGS) $(TESTS)
4039

41-
cpp_parser$(EXEEXT): cpp_parser$(OBJEXT)
42-
$(LINKBIN)
40+
test: all
41+
$(foreach test,$(TESTS), (echo Running: $(test); ./$(test)) &&) true
4342

44-
cpp_scanner$(EXEEXT): cpp_scanner$(OBJEXT)
45-
$(LINKBIN)
4643

47-
elf_reader$(EXEEXT): elf_reader$(OBJEXT)
48-
$(LINKBIN)
49-
50-
float_utils$(EXEEXT): float_utils$(OBJEXT)
51-
$(LINKBIN)
52-
53-
ieee_float$(EXEEXT): ieee_float$(OBJEXT)
54-
$(LINKBIN)
44+
###############################################################################
5545

56-
json$(EXEEXT): json$(OBJEXT)
46+
unit_tests$(EXEEXT): $(OBJ)
5747
$(LINKBIN)
5848

5949
miniBDD$(EXEEXT): miniBDD$(OBJEXT)
6050
$(LINKBIN)
6151

62-
osx_fat_reader$(EXEEXT): osx_fat_reader$(OBJEXT)
63-
$(LINKBIN)
64-
65-
smt2_parser$(EXEEXT): smt2_parser$(OBJEXT)
66-
$(LINKBIN)
67-
68-
wp$(EXEEXT): wp$(OBJEXT)
69-
$(LINKBIN)
70-
7152
string_utils$(EXEEXT): string_utils$(OBJEXT)
7253
$(LINKBIN)
7354

74-
sharing_map$(EXEEXT): sharing_map$(OBJEXT)
75-
$(LINKBIN)
76-
7755
sharing_node$(EXEEXT): sharing_node$(OBJEXT)
7856
$(LINKBIN)
79-
80-
unicode$(EXEEXT): unicode$(OBJEXT)
81-
$(LINKBIN)
82-

0 commit comments

Comments
 (0)