Lines Matching defs:test

3  * Base unit test (KUnit) API.
10 #include <kunit/test.h>
11 #include <kunit/test-bug.h>
27 * Hook to fail the current test and print an error message to the log.
77 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
97 static void kunit_print_test_stats(struct kunit *test,
103 kunit_log(KERN_INFO, test,
106 test->name,
173 /* Currently supported test levels */
183 * We do not log the test suite header as doing so would
184 * mean debugfs display would consist of the test suite
185 * header prior to individual test results.
198 static void kunit_print_ok_not_ok(struct kunit *test,
209 * When test is NULL assume that results are from the suite
212 WARN(!test && test_level, "suite test level can't be %u!\n", test_level);
215 * We do not log the test suite results as doing so would
216 * mean debugfs display would consist of an incorrect test
221 if (!test)
227 kunit_log(KERN_INFO, test,
281 static void kunit_print_string_stream(struct kunit *test,
292 kunit_err(test,
295 kunit_err(test, "%s", fragment->fragment);
297 kunit_err(test, "\n");
299 kunit_err(test, "%s", buf);
300 kunit_kfree(test, buf);
304 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
310 kunit_set_failure(test);
312 stream = alloc_string_stream(test, GFP_KERNEL);
324 kunit_print_string_stream(test, stream);
329 void __noreturn __kunit_abort(struct kunit *test)
331 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
334 * Throw could not abort from test.
339 WARN_ONCE(true, "Throw could not abort from test!\n");
343 void __kunit_do_failed_assertion(struct kunit *test,
357 kunit_fail(test, loc, type, assert, assert_format, &message);
363 void kunit_init_test(struct kunit *test, const char *name, char *log)
365 spin_lock_init(&test->lock);
366 INIT_LIST_HEAD(&test->resources);
367 test->name = name;
368 test->log = log;
369 if (test->log)
370 test->log[0] = '\0';
371 test->status = KUNIT_SUCCESS;
372 test->status_comment[0] = '\0';
376 /* Only warn when a test takes more than twice the threshold */
387 static void kunit_run_case_check_speed(struct kunit *test,
401 kunit_warn(test,
407 * Initializes and runs test case. Does not clean up or do post validations.
409 static void kunit_run_case_internal(struct kunit *test,
418 ret = suite->init(test);
420 kunit_err(test, "failed to initialize: %d\n", ret);
421 kunit_set_failure(test);
428 test_case->run_case(test);
432 kunit_run_case_check_speed(test, test_case, timespec64_sub(end, start));
435 static void kunit_case_internal_cleanup(struct kunit *test)
437 kunit_cleanup(test);
441 * Performs post validations and cleanup after a test case was run.
444 static void kunit_run_case_cleanup(struct kunit *test,
448 suite->exit(test);
450 kunit_case_internal_cleanup(test);
454 struct kunit *test;
462 struct kunit *test = ctx->test;
466 current->kunit_test = test;
473 kunit_run_case_internal(test, suite, test_case);
479 struct kunit *test = ctx->test;
482 current->kunit_test = test;
484 kunit_run_case_cleanup(test, suite);
490 struct kunit *test = ctx->test;
491 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
494 kunit_set_failure(test);
502 kunit_err(test, "test case cleanup timed out\n");
504 * Unknown internal error occurred preventing test case from
508 kunit_err(test, "internal error occurred during test case cleanup: %d\n",
514 kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n");
521 struct kunit *test = ctx->test;
522 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
525 kunit_set_failure(test);
531 kunit_err(test, "test case timed out\n");
533 * Unknown internal error occurred preventing test case from
537 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
545 * Performs all logic to run a test case. It also catches most errors that
546 * occur in a test case and reports them as failures.
550 struct kunit *test)
555 try_catch = &test->try_catch;
558 test,
561 context.test = test;
568 test,
573 /* Propagate the parameter result to the test case. */
574 if (test->status == KUNIT_FAILURE)
576 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
653 struct kunit test = { .param_value = NULL, .param_index = 0 };
656 kunit_init_test(&test, test_case->name, test_case->log);
659 test.status = KUNIT_SKIPPED;
660 kunit_update_stats(&param_stats, test.status);
662 /* Non-parameterised test. */
664 kunit_run_case_catch_errors(suite, test_case, &test);
665 kunit_update_stats(&param_stats, test.status);
669 test.param_value = test_case->generate_params(NULL, param_desc);
671 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
673 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
676 while (test.param_value) {
677 kunit_run_case_catch_errors(suite, test_case, &test);
681 "param-%d", test.param_index);
684 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE_PARAM,
685 test.status,
686 test.param_index + 1,
688 test.status_comment);
692 test.param_value = test_case->generate_params(test.param_value, param_desc);
693 test.param_index++;
695 kunit_update_stats(&param_stats, test.status);
701 kunit_print_test_stats(&test, param_stats);
703 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE, test_case->status,
706 test.status_comment);
853 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
862 if (kunit_add_action_or_reset(test, (kunit_action_t *)kfree, data) != 0)
869 void kunit_kfree(struct kunit *test, const void *ptr)
874 kunit_release_action(test, (kunit_action_t *)kfree, (void *)ptr);
878 void kunit_cleanup(struct kunit *test)
884 * test->resources is a stack - each allocation must be freed in the
893 spin_lock_irqsave(&test->lock, flags);
894 if (list_empty(&test->resources)) {
895 spin_unlock_irqrestore(&test->lock, flags);
898 res = list_last_entry(&test->resources,
903 * resource, and this can't happen if the test->lock
906 spin_unlock_irqrestore(&test->lock, flags);
907 kunit_remove_resource(test, res);