Skip to content

Commit 201f40a

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 9ae1f43 commit 201f40a

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
@@ -202,6 +202,7 @@ struct clar_func {
202202
struct clar_suite {
203203
const char *name;
204204
struct clar_func initialize;
205+
struct clar_func reset;
205206
struct clar_func cleanup;
206207
const struct clar_func *tests;
207208
size_t test_count;
@@ -328,6 +329,7 @@ clar_run_test(
328329
const struct clar_suite *suite,
329330
const struct clar_func *test,
330331
const struct clar_func *initialize,
332+
const struct clar_func *reset,
331333
const struct clar_func *cleanup)
332334
{
333335
int runs = test->runs, i = 0;
@@ -353,6 +355,17 @@ clar_run_test(
353355
struct clar_counter start, end;
354356
double elapsed;
355357

358+
if (i > 0 && reset->ptr != NULL) {
359+
reset->ptr();
360+
} else if (i > 0) {
361+
if (_clar.local_cleanup != NULL)
362+
_clar.local_cleanup(_clar.local_cleanup_payload);
363+
if (cleanup->ptr != NULL)
364+
cleanup->ptr();
365+
if (initialize->ptr != NULL)
366+
initialize->ptr();
367+
}
368+
356369
clar_counter_now(&start);
357370
test->ptr();
358371
clar_counter_now(&end);
@@ -481,7 +494,7 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)
481494

482495
_clar.last_report = report;
483496

484-
clar_run_test(suite, &test[i], &suite->initialize, &suite->cleanup);
497+
clar_run_test(suite, &test[i], &suite->initialize, &suite->reset, &suite->cleanup);
485498

486499
if (_clar.exit_on_error && _clar.total_errors)
487500
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)