Skip to content

Commit 1f5c6d4

Browse files
committed
C++: Add examples.
1 parent ab071b1 commit 1f5c6d4

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

cpp/ql/test/README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ We are keen to have unit tests for all of our QL code.
1010

1111
Every query in `cpp/ql/src` (outside of `cpp/ql/src/experimental`) should have a test in the corresponding subdirectory of `cpp/ql/test/query-tests`. At a minimum, each query test shall contain one case that should be detected by the query, and one related case that should not.
1212

13-
TODO: example?
13+
For example a simple test for the "Memory is never freed" (`cpp/memory-never-freed`) query might contain the following cases:
14+
```
15+
int *array1, *array2;
16+
17+
array1 = (int *)malloc(sizeof(int) * 100); // BAD: never freed
18+
19+
array2 = (int *)malloc(sizeof(int) * 100); // GOOD
20+
free(array2);
21+
```
1422

1523
Features of the QL libraries in `cpp/ql/src` should also have test coverage, in `cpp/ql/test/library-tests`.
1624

@@ -20,14 +28,25 @@ The contents of `cpp/ql/test` should be original - nothing should be copied from
2028
- ISO/IEC Programming languages - C (all versions)
2129
- ISO/IEC Programming languages - C++ (all versions)
2230

23-
TODO: example
24-
2531
In addition, C/C++ and QL code may be copied from other queries and tests in this repository.
2632

33+
For example the test above for the "Memory is never freed" (`cpp/memory-never-freed`) query requires the following declarations, taken from Programming languages — C (November 2018):
34+
```
35+
void *malloc(size_t size);
36+
void free(void *ptr);
37+
```
38+
We also need to write our own definition of `size_t`. Any unsigned integral type will do for our purposes:
39+
```
40+
typedef unsigned int size_t;
41+
```
42+
2743
## Including files
2844

2945
Standard and third party library header files should not be included in tests by means of `#include` or similar mechanisms. This is because the tests should be independent of platform and library versions installed on the running machine. Standard library declarations may be inserted directly where necessary (see the rules in the section above), but it is generally better to avoid using the standard library at all when possible.
3046

31-
`#include` may be used to include files from the same directory within `cpp/ql/test`.
32-
33-
TODO: example
47+
`#include` may be used to include files from the same directory within `cpp/ql/test`. For example the test for "Include header files only" (`cpp/include-non-header`) includes other files in the test directory:
48+
```
49+
#include "test.H"
50+
#include "test.xpm"
51+
#include "test2.c"
52+
```

0 commit comments

Comments
 (0)