Skip to content

Commit d7e727b

Browse files
committed
Report timings for all tests
Instead of separating the benchmark tests from "normal" tests, store timings for all successful tests and let the user decide which tests they'd like to run via the -s option.
1 parent fca2e97 commit d7e727b

File tree

4 files changed

+34
-47
lines changed

4 files changed

+34
-47
lines changed

clar.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static struct {
133133
int exit_on_error;
134134
int report_suite_names;
135135

136-
int run_benchmarks;
136+
int report_benchmarks;
137137
double timing_start;
138138
double timing_end;
139139

@@ -157,7 +157,6 @@ static struct {
157157
struct clar_func {
158158
const char *name;
159159
void (*ptr)(void);
160-
int is_bench;
161160
};
162161

163162
struct clar_suite {
@@ -251,6 +250,8 @@ clar_run_test(
251250
const struct clar_func *cleanup)
252251
{
253252
_clar.test_status = CL_TEST_OK;
253+
_clar.timing_start = 0.0;
254+
_clar.timing_end = 0.0;
254255
_clar.trampoline_enabled = 1;
255256

256257
CL_TRACE(CL_TRACE__TEST__BEGIN);
@@ -327,22 +328,19 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)
327328
if (filter && strncmp(test[i].name, filter, matchlen))
328329
continue;
329330

330-
if (test[i].is_bench != _clar.run_benchmarks)
331-
continue;
332-
333331
_clar.active_test = test[i].name;
334332
clar_run_test(&test[i], &suite->initialize, &suite->cleanup);
335333

336334
if (_clar.exit_on_error && _clar.total_errors)
337335
return;
338336

339-
if (test[i].is_bench) {
340-
clar_store_timing();
341-
}
337+
clar_store_timing();
342338
}
343339

344-
puts("");
345-
clar_report_timings();
340+
if (_clar.report_benchmarks) {
341+
puts("");
342+
clar_report_timings();
343+
}
346344

347345
_clar.active_test = NULL;
348346
CL_TRACE(CL_TRACE__SUITE_END);
@@ -360,7 +358,7 @@ clar_usage(const char *arg)
360358
printf(" -q \tOnly report tests that had an error\n");
361359
printf(" -Q \tQuit as soon as a test fails\n");
362360
printf(" -l \tPrint suite names\n");
363-
printf(" -b \tRun benchmarks instead of tests\n");
361+
printf(" -b \tReport test benchmarks\n");
364362
exit(-1);
365363
}
366364

@@ -420,7 +418,7 @@ clar_parse_args(int argc, char **argv)
420418
}
421419

422420
case 'b':
423-
_clar.run_benchmarks = 1;
421+
_clar.report_benchmarks = 1;
424422
break;
425423

426424
case 'q':
@@ -509,7 +507,13 @@ clar_test(int argc, char **argv)
509507

510508
static void clar_store_timing(void)
511509
{
512-
struct clar_timing *timing = calloc(1, sizeof(struct clar_timing));
510+
struct clar_timing *timing;
511+
512+
/* Failed tests jump over the timing code */
513+
if (_clar.timing_end == 0)
514+
return;
515+
516+
timing = calloc(1, sizeof(struct clar_timing));
513517

514518
if (_clar.timings == NULL)
515519
_clar.timings = timing;

generate.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, module):
1818
def _render_callback(self, cb):
1919
if not cb:
2020
return ' { NULL, NULL }'
21-
return ' { "%s", &%s, %d }' % (cb['short_name'], cb['symbol'], cb['bench'])
21+
return ' { "%s", &%s }' % (cb['short_name'], cb['symbol'])
2222

2323
class DeclarationTemplate(Template):
2424
def render(self):
@@ -79,13 +79,21 @@ def _replacer(match):
7979

8080
return re.sub(SKIP_COMMENTS_REGEX, _replacer, text)
8181

82-
def _append_callbacks(self, callbacks, is_bench):
83-
for (declaration, symbol, short_name) in callbacks:
82+
def parse(self, contents):
83+
TEST_FUNC_REGEX = r"^(void\s+(test_%s__(\w+))\s*\(\s*void\s*\))\s*\{"
84+
85+
contents = self._skip_comments(contents)
86+
regex = re.compile(TEST_FUNC_REGEX % self.name, re.MULTILINE)
87+
88+
self.callbacks = []
89+
self.initialize = None
90+
self.cleanup = None
91+
92+
for (declaration, symbol, short_name) in regex.findall(contents):
8493
data = {
8594
"short_name" : short_name,
8695
"declaration" : declaration,
87-
"symbol" : symbol,
88-
"bench" : is_bench,
96+
"symbol" : symbol
8997
}
9098

9199
if short_name == 'initialize':
@@ -95,21 +103,6 @@ def _append_callbacks(self, callbacks, is_bench):
95103
else:
96104
self.callbacks.append(data)
97105

98-
def parse(self, contents):
99-
TEST_FUNC_REGEX = r"^(void\s+(test_%s__(\w+))\s*\(\s*void\s*\))\s*\{"
100-
BENCH_FUNC_REGEX = r"^(void\s+(bench_%s__(\w+))\s*\(\s*void\s*\))\s*\{"
101-
102-
contents = self._skip_comments(contents)
103-
test_regex = re.compile(TEST_FUNC_REGEX % self.name, re.MULTILINE)
104-
bench_regex = re.compile(BENCH_FUNC_REGEX % self.name, re.MULTILINE)
105-
106-
self.callbacks = []
107-
self.initialize = None
108-
self.cleanup = None
109-
110-
self._append_callbacks(test_regex.findall(contents), False)
111-
self._append_callbacks(bench_regex.findall(contents), True)
112-
113106
return self.callbacks != []
114107

115108
def refresh(self, path):

test/main.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
int global_test_counter = 0;
21-
int global_is_bench = 0;
2221

2322
#ifdef _WIN32
2423
int __cdecl main(int argc, char *argv[])
@@ -35,10 +34,7 @@ int main(int argc, char *argv[])
3534
ret = clar_test(argc, argv);
3635

3736
/* Your custom cleanup here */
38-
if (global_is_bench)
39-
cl_assert_equal_i(3, global_test_counter);
40-
else
41-
cl_assert_equal_i(8, global_test_counter);
37+
cl_assert_equal_i(11, global_test_counter);
4238

4339
return ret;
4440
}

test/sample.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,34 +83,28 @@ void test_sample__ptr(void)
8383
cl_assert_equal_p(&actual, actual);
8484
}
8585

86-
void bench_sample__loop(void)
86+
void test_sample__bench_loop(void)
8787
{
8888
int i;
8989

90-
global_is_bench = 1;
91-
9290
for (i = 0; i < 1000000; i++) {
9391
}
9492
}
9593

96-
void bench_sample__loop2(void)
94+
void test_sample__bench_loop2(void)
9795
{
9896
int i;
9997

100-
global_is_bench = 1;
101-
10298
for (i = 0; i < 1000000; i++) {
10399
int dummy = i*1000;
104100
dummy = dummy;
105101
}
106102
}
107103

108-
void bench_sample__reset_timer(void)
104+
void test_sample__bench_reset_timer(void)
109105
{
110106
int i;
111107

112-
global_is_bench = 1;
113-
114108
for (i = 0; i < 100000000; i++) {
115109
int dummy = i*1000;
116110
dummy = dummy;

0 commit comments

Comments
 (0)