Lines Matching defs:test
3 * Base unit test (KUnit) API.
9 #include <kunit/test.h>
71 struct kunit *test = is_test ? test_or_suite : NULL;
74 * We do not log the test suite results as doing so would
75 * mean debugfs display would consist of the test suite
76 * description and status prior to individual test results.
86 kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s",
130 static void kunit_print_string_stream(struct kunit *test,
141 kunit_err(test,
144 kunit_err(test, "%s", fragment->fragment);
146 kunit_err(test, "\n");
148 kunit_err(test, "%s", buf);
149 kunit_kfree(test, buf);
153 static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
157 kunit_set_failure(test);
159 stream = alloc_string_stream(test, GFP_KERNEL);
170 kunit_print_string_stream(test, stream);
175 static void __noreturn kunit_abort(struct kunit *test)
177 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
180 * Throw could not abort from test.
185 WARN_ONCE(true, "Throw could not abort from test!\n");
188 void kunit_do_assertion(struct kunit *test,
203 kunit_fail(test, assert);
208 kunit_abort(test);
212 void kunit_init_test(struct kunit *test, const char *name, char *log)
214 spin_lock_init(&test->lock);
215 INIT_LIST_HEAD(&test->resources);
216 test->name = name;
217 test->log = log;
218 if (test->log)
219 test->log[0] = '\0';
220 test->success = true;
225 * Initializes and runs test case. Does not clean up or do post validations.
227 static void kunit_run_case_internal(struct kunit *test,
234 ret = suite->init(test);
236 kunit_err(test, "failed to initialize: %d\n", ret);
237 kunit_set_failure(test);
242 test_case->run_case(test);
245 static void kunit_case_internal_cleanup(struct kunit *test)
247 kunit_cleanup(test);
251 * Performs post validations and cleanup after a test case was run.
254 static void kunit_run_case_cleanup(struct kunit *test,
258 suite->exit(test);
260 kunit_case_internal_cleanup(test);
264 struct kunit *test;
272 struct kunit *test = ctx->test;
277 current->kunit_test = test;
285 kunit_run_case_internal(test, suite, test_case);
287 kunit_run_case_cleanup(test, suite);
293 struct kunit *test = ctx->test;
295 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
298 kunit_set_failure(test);
304 kunit_err(test, "test case timed out\n");
306 * Unknown internal error occurred preventing test case from
310 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
317 * Test case was run, but aborted. It is the test case's business as to
320 kunit_run_case_cleanup(test, suite);
324 * Performs all logic to run a test case. It also catches most errors that
325 * occur in a test case and reports them as failures.
332 struct kunit test;
334 kunit_init_test(&test, test_case->name, test_case->log);
335 try_catch = &test.try_catch;
338 &test,
341 context.test = &test;
346 test_case->success = test.success;
348 kunit_print_ok_not_ok(&test, true, test_case->success,
405 int kunit_add_resource(struct kunit *test,
424 spin_lock(&test->lock);
425 list_add_tail(&res->node, &test->resources);
427 spin_unlock(&test->lock);
433 int kunit_add_named_resource(struct kunit *test,
445 existing = kunit_find_named_resource(test, name);
453 return kunit_add_resource(test, init, free, res, data);
457 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
470 ret = kunit_add_resource(test, init, free, res, data);
483 void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
485 spin_lock(&test->lock);
487 spin_unlock(&test->lock);
492 int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match,
495 struct kunit_resource *res = kunit_find_resource(test, match,
501 kunit_remove_resource(test, res);
531 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
538 return kunit_alloc_resource(test,
546 void kunit_kfree(struct kunit *test, const void *ptr)
550 res = kunit_find_resource(test, kunit_resource_instance_match,
557 kunit_remove_resource(test, res);
564 void kunit_cleanup(struct kunit *test)
569 * test->resources is a stack - each allocation must be freed in the
578 spin_lock(&test->lock);
579 if (list_empty(&test->resources)) {
580 spin_unlock(&test->lock);
583 res = list_last_entry(&test->resources,
588 * resource, and this can't happen if the test->lock
591 spin_unlock(&test->lock);
592 kunit_remove_resource(test, res);