Skip to content

Commit daa135a

Browse files
committed
Move defaults and handlers variable into a static function.
This change allow a lot of code from utest to be removed from the final binary if not used.
1 parent eed52a0 commit daa135a

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

features/frameworks/utest/source/utest_harness.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,15 @@ namespace
4747
size_t case_failed = 0;
4848
size_t case_failed_before = 0;
4949

50-
handlers_t defaults = default_handlers;
51-
handlers_t handlers = defaults;
50+
handlers_t& defaults() {
51+
static handlers_t _handlers = default_handlers;
52+
return _handlers;
53+
}
54+
55+
handlers_t& handlers() {
56+
static handlers_t _handlers = default_handlers;
57+
return _handlers;
58+
}
5259

5360
location_t location = LOCATION_UNKNOWN;
5461

@@ -110,10 +117,10 @@ bool Harness::run(const Specification& specification)
110117
return false;
111118
test_cases = specification.cases;
112119
test_length = specification.length;
113-
defaults = specification.defaults;
114-
handlers.test_setup = defaults.get_handler(specification.setup_handler);
115-
handlers.test_teardown = defaults.get_handler(specification.teardown_handler);
116-
handlers.test_failure = defaults.get_handler(specification.failure_handler);
120+
defaults() = specification.defaults;
121+
handlers().test_setup = defaults().get_handler(specification.setup_handler);
122+
handlers().test_teardown = defaults().get_handler(specification.teardown_handler);
123+
handlers().test_failure = defaults().get_handler(specification.failure_handler);
117124

118125
test_index_of_case = 0;
119126
test_passed = 0;
@@ -127,16 +134,16 @@ bool Harness::run(const Specification& specification)
127134
int setup_status = 0;
128135
failure_t failure(REASON_NONE, location);
129136

130-
if (handlers.test_setup) {
131-
setup_status = handlers.test_setup(test_length);
137+
if (handlers().test_setup) {
138+
setup_status = handlers().test_setup(test_length);
132139
if (setup_status == STATUS_CONTINUE) setup_status = 0;
133140
else if (setup_status < STATUS_CONTINUE) failure.reason = REASON_TEST_SETUP;
134141
else if (setup_status > signed(test_length)) failure.reason = REASON_CASE_INDEX;
135142
}
136143

137144
if (failure.reason != REASON_NONE) {
138-
if (handlers.test_failure) handlers.test_failure(failure);
139-
if (handlers.test_teardown) handlers.test_teardown(0, 0, failure);
145+
if (handlers().test_failure) handlers().test_failure(failure);
146+
if (handlers().test_teardown) handlers().test_teardown(0, 0, failure);
140147
test_cases = NULL;
141148
exit(1);
142149
return true;
@@ -150,8 +157,8 @@ bool Harness::run(const Specification& specification)
150157
scheduler.post(run_next_case, 0);
151158
if (scheduler.run() != 0) {
152159
failure.reason = REASON_SCHEDULER;
153-
if (handlers.test_failure) handlers.test_failure(failure);
154-
if (handlers.test_teardown) handlers.test_teardown(0, 0, failure);
160+
if (handlers().test_failure) handlers().test_failure(failure);
161+
if (handlers().test_teardown) handlers().test_teardown(0, 0, failure);
155162
test_cases = NULL;
156163
exit(1);
157164
return true;
@@ -167,8 +174,8 @@ void Harness::raise_failure(const failure_reason_t reason)
167174
if (test_cases == NULL) return;
168175

169176
utest::v1::status_t fail_status = STATUS_ABORT;
170-
if (handlers.test_failure) handlers.test_failure(failure_t(reason, location));
171-
if (handlers.case_failure) fail_status = handlers.case_failure(case_current, failure_t(reason, location));
177+
if (handlers().test_failure) handlers().test_failure(failure_t(reason, location));
178+
if (handlers().case_failure) fail_status = handlers().case_failure(case_current, failure_t(reason, location));
172179

173180
{
174181
UTEST_ENTER_CRITICAL_SECTION;
@@ -184,25 +191,25 @@ void Harness::raise_failure(const failure_reason_t reason)
184191
}
185192

186193
if (fail_status == STATUS_ABORT || reason & REASON_CASE_SETUP) {
187-
if (handlers.case_teardown && location != LOCATION_CASE_TEARDOWN) {
194+
if (handlers().case_teardown && location != LOCATION_CASE_TEARDOWN) {
188195
location_t fail_loc(location);
189196
location = LOCATION_CASE_TEARDOWN;
190197

191-
utest::v1::status_t teardown_status = handlers.case_teardown(case_current, case_passed, case_failed, failure_t(reason, fail_loc));
198+
utest::v1::status_t teardown_status = handlers().case_teardown(case_current, case_passed, case_failed, failure_t(reason, fail_loc));
192199
if (teardown_status < STATUS_CONTINUE) raise_failure(REASON_CASE_TEARDOWN);
193200
else if (teardown_status > signed(test_length)) raise_failure(REASON_CASE_INDEX);
194201
else if (teardown_status >= 0) case_index = teardown_status - 1;
195202

196203
// Restore case failure location once we have dealt with case teardown
197204
location = fail_loc;
198-
handlers.case_teardown = NULL;
205+
handlers().case_teardown = NULL;
199206
}
200207
}
201208
if (fail_status == STATUS_ABORT) {
202209
test_failed++;
203210
failure_t fail(reason, location);
204211
location = LOCATION_TEST_TEARDOWN;
205-
if (handlers.test_teardown) handlers.test_teardown(test_passed, test_failed, fail);
212+
if (handlers().test_teardown) handlers().test_teardown(test_passed, test_failed, fail);
206213
exit(test_failed);
207214
die();
208215
}
@@ -218,8 +225,8 @@ void Harness::schedule_next_case()
218225
if (case_control.repeat & REPEAT_SETUP_TEARDOWN || !(case_control.repeat & (REPEAT_ON_TIMEOUT | REPEAT_ON_VALIDATE))) {
219226
location = LOCATION_CASE_TEARDOWN;
220227

221-
if (handlers.case_teardown) {
222-
utest::v1::status_t status = handlers.case_teardown(case_current, case_passed, case_failed,
228+
if (handlers().case_teardown) {
229+
utest::v1::status_t status = handlers().case_teardown(case_current, case_passed, case_failed,
223230
case_failed ? failure_t(REASON_CASES, LOCATION_UNKNOWN) : failure_t(REASON_NONE));
224231
if (status < STATUS_CONTINUE) raise_failure(REASON_CASE_TEARDOWN);
225232
else if (status > signed(test_length)) raise_failure(REASON_CASE_INDEX);
@@ -298,9 +305,9 @@ void Harness::run_next_case()
298305
UTEST_LOG_FUNCTION();
299306
if(case_current < (test_cases + test_length))
300307
{
301-
handlers.case_setup = defaults.get_handler(case_current->setup_handler);
302-
handlers.case_teardown = defaults.get_handler(case_current->teardown_handler);
303-
handlers.case_failure = defaults.get_handler(case_current->failure_handler);
308+
handlers().case_setup = defaults().get_handler(case_current->setup_handler);
309+
handlers().case_teardown = defaults().get_handler(case_current->teardown_handler);
310+
handlers().case_failure = defaults().get_handler(case_current->failure_handler);
304311

305312
if (case_current->is_empty()) {
306313
location = LOCATION_UNKNOWN;
@@ -321,7 +328,7 @@ void Harness::run_next_case()
321328

322329
if (setup_repeat & REPEAT_SETUP_TEARDOWN) {
323330
location = LOCATION_CASE_SETUP;
324-
if (handlers.case_setup && (handlers.case_setup(case_current, test_index_of_case) != STATUS_CONTINUE)) {
331+
if (handlers().case_setup && (handlers().case_setup(case_current, test_index_of_case) != STATUS_CONTINUE)) {
325332
raise_failure(REASON_CASE_SETUP);
326333
schedule_next_case();
327334
return;
@@ -361,9 +368,9 @@ void Harness::run_next_case()
361368
UTEST_LEAVE_CRITICAL_SECTION;
362369
}
363370
}
364-
else if (handlers.test_teardown) {
371+
else if (handlers().test_teardown) {
365372
location = LOCATION_TEST_TEARDOWN;
366-
handlers.test_teardown(test_passed, test_failed, test_failed ? failure_t(REASON_CASES, LOCATION_UNKNOWN) : failure_t(REASON_NONE));
373+
handlers().test_teardown(test_passed, test_failed, test_failed ? failure_t(REASON_CASES, LOCATION_UNKNOWN) : failure_t(REASON_NONE));
367374
test_cases = NULL;
368375
exit(test_failed);
369376
} else {

0 commit comments

Comments
 (0)