Skip to content

Commit

Permalink
introduce a reset function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ethomson committed Feb 26, 2025
1 parent 9ae1f43 commit 201f40a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion clar.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ struct clar_func {
struct clar_suite {
const char *name;
struct clar_func initialize;
struct clar_func reset;
struct clar_func cleanup;
const struct clar_func *tests;
size_t test_count;
Expand Down Expand Up @@ -328,6 +329,7 @@ clar_run_test(
const struct clar_suite *suite,
const struct clar_func *test,
const struct clar_func *initialize,
const struct clar_func *reset,
const struct clar_func *cleanup)
{
int runs = test->runs, i = 0;
Expand All @@ -353,6 +355,17 @@ clar_run_test(
struct clar_counter start, end;
double elapsed;

if (i > 0 && reset->ptr != NULL) {
reset->ptr();
} else if (i > 0) {
if (_clar.local_cleanup != NULL)
_clar.local_cleanup(_clar.local_cleanup_payload);
if (cleanup->ptr != NULL)
cleanup->ptr();
if (initialize->ptr != NULL)
initialize->ptr();
}

clar_counter_now(&start);
test->ptr();
clar_counter_now(&end);
Expand Down Expand Up @@ -481,7 +494,7 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)

_clar.last_report = report;

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

if (_clar.exit_on_error && _clar.total_errors)
return;
Expand Down
8 changes: 8 additions & 0 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def render(self):
for initializer in self.module.initializers:
out += "extern %s;\n" % initializer['declaration']

if self.module.reset:
out += "extern %s;\n" % self.module.reset['declaration']

if self.module.cleanup:
out += "extern %s;\n" % self.module.cleanup['declaration']

Expand Down Expand Up @@ -63,12 +66,14 @@ def render(self):
{
"${clean_name}",
${initialize},
${reset},
${cleanup},
${cb_ptr}, ${cb_count}, ${enabled}
}"""
).substitute(
clean_name = name,
initialize = self._render_callback(initializer),
reset = self._render_callback(self.module.reset),
cleanup = self._render_callback(self.module.cleanup),
cb_ptr = "_%s_cb_%s" % (self.module.app_name, self.module.name),
cb_count = len(self.module.callbacks),
Expand Down Expand Up @@ -109,6 +114,7 @@ def parse(self, contents):

self.callbacks = []
self.initializers = []
self.reset = None
self.cleanup = None

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

if short_name.startswith('initialize'):
self.initializers.append(data)
elif short_name == 'reset':
self.reset = data
elif short_name == 'cleanup':
self.cleanup = data
else:
Expand Down

0 comments on commit 201f40a

Please sign in to comment.