11/* main.c
22 *
3- * Example program demonstrating libctest usage.
3+ * Example program demonstrating libtdd usage and features .
44 * This program uses all the public features of the library.
55 */
66#include <signal.h>
77#include <stdio.h>
88#include <stdlib.h>
99#include <string.h>
1010
11- #include "ctest .h"
11+ #include "tdd .h"
1212
1313static void * test_errfunc (void * t );
1414static void * test_failfunc (void * t );
@@ -21,30 +21,35 @@ int main(int argc, char* argv[]) {
2121
2222 // suite_add is a variadic function that can be used to add an arbitrary
2323 // number of tests at once
24+ // new tests are added with a function pointer, a name, and an optional
25+ // description; if the description is to be omitted, you may pass NULL
2426 suite_add (s , 2 ,
2527 newtest (& test_timer , "test_timer" ,
2628 "Manual benchmark. Requires timespan to be printed "
2729 "manually." ),
2830 newtest (& bench_func , "bench_func" ,
2931 "Builtin benchmark. Execution timespan is printed "
3032 "automatically below." ));
33+
3134 // suite_addtest is a function that simply appends a test to the list of
3235 // tests in the suite; could be used to programmatically add tests
33- suite_addtest (
34- s , newtest ( & test_errfunc , "test_errfunc" , "Produces an error." ));
36+ suite_addtest (s , newtest ( & test_errfunc , "test_errfunc" , "Raises error." ));
37+
3538 // suite_add can be called multiple times to add groups of tests to the
3639 // test suite
3740 suite_add (s , 2 ,
3841 newtest (& test_failfunc , "test_failfunc" , "Fails immediately." ),
39- newtest (& test_segvfunc , "test_segvfunc" , "Raised SIGSEGV." ));
42+ newtest (& test_segvfunc , "test_segvfunc" , "Raises SIGSEGV." ));
4043
4144 printf ("Running tests ignoring failures.\n" );
45+
4246 suite_run (s , false);
4347 if (s -> finished ) {
4448 printf ("Suite ran all tests.\n" );
4549 } else {
4650 printf ("Suite only ran %d tests.\n" , s -> test_index + 1 );
4751 }
52+ printf ("Suite encountered: %d segmentation faults.\n" , s -> n_segv );
4853 printf ("\n" );
4954
5055 // reset the test suite to demonstrate that it can be rerun with fatal
@@ -56,7 +61,7 @@ int main(int argc, char* argv[]) {
5661 if (s -> finished ) {
5762 printf ("Suite ran all tests.\n" );
5863 } else {
59- printf ("Suite may not have run all tests! \n" );
64+ printf ("Suite only ran %d tests. \n" , s -> test_index + 1 );
6065 }
6166
6267 // print summary statistics
@@ -74,13 +79,13 @@ int main(int argc, char* argv[]) {
7479
7580void * test_errfunc (void * t ) {
7681 test_error (t , "a non-critical error ocurred." );
77- return t ;
82+ return NULL ;
7883}
7984
8085void * test_failfunc (void * t ) {
8186 test_fail (t , "a critical error occurred!" );
8287 printf ("this code should not run unless fatal failures are disabled!\n" );
83- return t ;
88+ return NULL ;
8489}
8590
8691void * test_timer (void * t ) {
@@ -89,17 +94,17 @@ void* test_timer(void* t) {
8994 strcpy (s , "This function is being timed!" );
9095 free (s );
9196 test_done (t );
92- return t ;
97+ return NULL ;
9398}
9499
95100void * bench_func (void * t ) {
96101 char * s = calloc (128 , sizeof (char ));
97102 strcpy (s , "This function is being timed!" );
98103 free (s );
99- return t ;
104+ return NULL ;
100105}
101106
102107void * test_segvfunc (void * t ) {
103108 raise (SIGSEGV );
104- return t ;
109+ return NULL ;
105110}
0 commit comments