blob: eb243debe0a60cdba5ba0bcf6384a541e7ffad82 [file] [log] [blame]
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04001
2Unit test
3
E. Scott Daniels8dd46412019-04-16 20:47:54 +00004This directory contains the unit test support for the RMr
5library. The basic test is run with the follwing command:
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04006
E. Scott Daniels8dd46412019-04-16 20:47:54 +00007 ksh unit_test.ksh
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04008
E. Scott Daniels8dd46412019-04-16 20:47:54 +00009To run a specific test (e.g. ring_test.c) run:
10 ksh unit_test.ksh ring_test.c
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040011
E. Scott Daniels8dd46412019-04-16 20:47:54 +000012The script runs the unit test(s) given, and if they pass then
13runs an analysis on the .gcov files generated to generate
14coverage information. By default, pass/fail of the test is
15based only on the success or failure of the unit tests which
16are testing functionality. The unit test script can report
17an overall failure if coverage is below the indicated threshold
18when given the strict option (-s).
19
20The analysis of .gcov files generates output shown below which
21is thought to be more straight forward than the typical stuff
22gcov produces:
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040023
24unit_test.ksh ring_test.c
25ring_test.c --------------------------------------
26[OK] 100% uta_ring_insert
27[OK] 100% uta_ring_extract
28[OK] 100% uta_ring_free
29[LOW] 76% uta_mk_ring
30[PASS] 91% ../src/common/src/ring_static.c
31
32
E. Scott Daniels8dd46412019-04-16 20:47:54 +000033The output shows, for each function, the coverage (column 2) and an
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040034interpretation (ok or low) wthin an overall pass or fail.
35
36
E. Scott Daniels8dd46412019-04-16 20:47:54 +000037Because of the static nature of the RMr library, tests with the
38intent of providing coverage information, as opposed just to providing
39functional verification, are a bit trickier. To that end, the test
40files in this directory are organised with three file name formats:
41
42 test_*.c tools for testing, not tests
43
44 *_test.c main test programmes which can be compiled in
45 a stand-alone manner (e.g. gcc foo_test.c)
46
47 *_static_test.c Test functions which are real tests and are
48 included by one or more stand-alone driver.
49
50The unit_test script will search only for *_test.c and will ignore
51*_static_test.c files when building it's list for testing.
52
53
54Use the command 'unit_test.ksh -?' to see the usage information
55and complete set of options available.
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040056
57
E. Scott Daniels5efb1e62019-05-02 17:09:35 +000058Merging .gcov files
59As some unit test programmes may not be able/designed to cover all
60of a module completely, the script will merge each .gcov prooduced
61with the prior, matching, file after each pass. The result is a
62final .gcov file that shows the coverage of the module over all
63tests. This allows a true coverage value to be determined while
64permitting more simple tests to be used without the requirement of
65each test to cover everything.
66
67
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040068Discounting
69The unit test script makes a discount pass on low coverage files in
70attempt to discount the coverage rate by ignoring what are considered
71to be difficult to reach blocks in the code. Currently, these blocks
72are limited to what appear to be tests for memory allocation, failure
73and/or nil pointer handling. If code blocks of this sort are found,
74they are not counted against the coverage for the module. If the -v
75option is given, an augmented coverage listing is saved in .dcov which
76shows the discounted lines with a string of equal signs (====) rather
77than the gcov hash string (###).
78
E. Scott Daniels8dd46412019-04-16 20:47:54 +000079The discount check is applied only if an entire module has a lower
80than accepted coverage rate, and can be forced for all modules with
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040081the -f option.
82
83To illustrate, the following code checks the return from the system
84library strdup() call which is very unlikely to fail under test without
E. Scott Daniels8dd46412019-04-16 20:47:54 +000085going to extremes and substituting for the system lib. Thus, the
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040086block which checks for a nil pointer has been discounted:
87
88 -: 354:
89 1: 355: dbuf = strdup( buf );
90 1: 356: if( dbuf == NULL ) {
91 =====: 357: errno = ENOMEM;
92 =====: 358: return 0;
93 -: 359: }
94
E. Scott Daniels5efb1e62019-05-02 17:09:35 +000095A final discount pass is made on all merged .gcov files after all unit
96tests are finished. This final pass should indicate the true discounted
97coverage
98
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040099
100Target Coverage
101By default, a target coverage of 80% is used. For some modules this may
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000102be impossible to achieve, so to prevent always failing these modules
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400103may be listed in the .targets file with their expected minimum coverage.
104Module names need to be qualified (e.g. ../src/common/src/foo.c.
105
106
E. Scott Daniels5efb1e62019-05-02 17:09:35 +0000107Sonar XML Files
108If the -x option is given on the unit test script, then two XML files
109will be generated for Sonar. These are gcov.xml and dcov.xml and are
110the coverage information as reflected in the merged .gcov files and in
111the .dcov files produced during the final pass. The XML files should
112be uploaded to Sonar only after a complete unit test run is made, otherwise
113the coverage information may be lacking (reflecting only the last test
114executed and not a full complement of tests).
115
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400116-----------------------------------------------------------------------
117A note about ksh (A.K.A Korn shell, or kshell)
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000118Ksh is preferred for more complex scripts such as the unit test
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400119script as it does not have some of the limitations that bash
E. Scott Daniels8dd46412019-04-16 20:47:54 +0000120(and other knock-offs) have.