Skip to content

Commit d3d9669

Browse files
committed
introduce a reset function
For multi-run tests (benchmarks), we introduce a `reset` function. By default, between each run of a test, the initialization will be called at startup, and the cleanup will be called at finish. A benchmark may wish to set up multi-run state at the beginning of the invocation (in initialization), and keep a steady state through all test runs. Users can now add a `reset` function so that initialization occurs only at the beginning of all runs.
1 parent c2abd62 commit d3d9669

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clar.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ struct clar_func {
198198
struct clar_suite {
199199
const char *name;
200200
struct clar_func initialize;
201+
struct clar_func reset;
201202
struct clar_func cleanup;
202203
const struct clar_func *tests;
203204
size_t test_count;
@@ -322,6 +323,7 @@ clar_run_test(
322323
const struct clar_suite *suite,
323324
const struct clar_func *test,
324325
const struct clar_func *initialize,
326+
const struct clar_func *reset,
325327
const struct clar_func *cleanup)
326328
{
327329
int runs = test->runs, i = 0;
@@ -345,6 +347,17 @@ clar_run_test(
345347
struct clar_counter start, end;
346348
double elapsed;
347349

350+
if (i > 0 && reset->ptr != NULL) {
351+
reset->ptr();
352+
} else if (i > 0) {
353+
if (_clar.local_cleanup != NULL)
354+
_clar.local_cleanup(_clar.local_cleanup_payload);
355+
if (cleanup->ptr != NULL)
356+
cleanup->ptr();
357+
if (initialize->ptr != NULL)
358+
initialize->ptr();
359+
}
360+
348361
clar_counter_now(&start);
349362
test->ptr();
350363
clar_counter_now(&end);
@@ -469,7 +482,7 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)
469482

470483
_clar.last_report = report;
471484

472-
clar_run_test(suite, &test[i], &suite->initialize, &suite->cleanup);
485+
clar_run_test(suite, &test[i], &suite->initialize, &suite->reset, &suite->cleanup);
473486

474487
if (_clar.exit_on_error && _clar.total_errors)
475488
return;

generate.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def render(self):
3232
for initializer in self.module.initializers:
3333
out += "extern %s;\n" % initializer['declaration']
3434

35+
if self.module.reset:
36+
out += "extern %s;\n" % self.module.reset['declaration']
37+
3538
if self.module.cleanup:
3639
out += "extern %s;\n" % self.module.cleanup['declaration']
3740

@@ -63,12 +66,14 @@ def render(self):
6366
{
6467
"${clean_name}",
6568
${initialize},
69+
${reset},
6670
${cleanup},
6771
${cb_ptr}, ${cb_count}, ${enabled}
6872
}"""
6973
).substitute(
7074
clean_name = name,
7175
initialize = self._render_callback(initializer),
76+
reset = self._render_callback(self.module.reset),
7277
cleanup = self._render_callback(self.module.cleanup),
7378
cb_ptr = "_%s_cb_%s" % (self.module.app_name, self.module.name),
7479
cb_count = len(self.module.callbacks),
@@ -109,6 +114,7 @@ def parse(self, contents):
109114

110115
self.callbacks = []
111116
self.initializers = []
117+
self.reset = None
112118
self.cleanup = None
113119

114120
for (declaration, symbol, short_name, options) in regex.findall(contents):
@@ -156,6 +162,8 @@ def parse(self, contents):
156162

157163
if short_name.startswith('initialize'):
158164
self.initializers.append(data)
165+
elif short_name == 'reset':
166+
self.reset = data
159167
elif short_name == 'cleanup':
160168
self.cleanup = data
161169
else:

0 commit comments

Comments
 (0)