Lines Matching refs:test
3 * KUnit test for core test infrastructure.
8 #include <kunit/test.h>
9 #include <kunit/test-bug.h>
20 struct kunit *test = data;
21 struct kunit_try_catch_test_context *ctx = test->priv;
28 struct kunit *test = data;
30 KUNIT_FAIL(test, "Catch should not be called\n");
33 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
35 struct kunit_try_catch_test_context *ctx = test->priv;
39 test,
42 kunit_try_catch_run(try_catch, test);
44 KUNIT_EXPECT_TRUE(test, ctx->function_called);
49 struct kunit *test = data;
50 struct kunit_try_catch_test_context *ctx = test->priv;
54 KUNIT_FAIL(test, "This line should never be reached\n");
59 struct kunit *test = data;
60 struct kunit_try_catch_test_context *ctx = test->priv;
65 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
67 struct kunit_try_catch_test_context *ctx = test->priv;
71 test,
74 kunit_try_catch_run(try_catch, test);
76 KUNIT_EXPECT_TRUE(test, ctx->function_called);
79 static int kunit_try_catch_test_init(struct kunit *test)
83 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
84 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
85 test->priv = ctx;
87 ctx->try_catch = kunit_kmalloc(test,
90 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
102 .name = "kunit-try-catch-test",
108 * Context for testing test managed resources
109 * is_resource_initialized is used to test arbitrary resources
112 struct kunit test;
134 static void kunit_resource_test_init_resources(struct kunit *test)
136 struct kunit_test_resource_context *ctx = test->priv;
138 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
140 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
143 static void kunit_resource_test_alloc_resource(struct kunit *test)
145 struct kunit_test_resource_context *ctx = test->priv;
149 res = kunit_alloc_and_get_resource(&ctx->test,
155 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
156 KUNIT_EXPECT_PTR_EQ(test,
159 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
160 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
165 static inline bool kunit_resource_instance_match(struct kunit *test,
180 static void kunit_resource_test_destroy_resource(struct kunit *test)
182 struct kunit_test_resource_context *ctx = test->priv;
184 &ctx->test,
192 KUNIT_ASSERT_FALSE(test,
193 kunit_destroy_resource(&ctx->test,
197 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
198 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
201 static void kunit_resource_test_remove_resource(struct kunit *test)
203 struct kunit_test_resource_context *ctx = test->priv;
205 &ctx->test,
212 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
217 kunit_remove_resource(test, res);
218 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
220 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
223 kunit_remove_resource(test, res);
224 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
228 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
232 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
235 static void kunit_resource_test_cleanup_resources(struct kunit *test)
238 struct kunit_test_resource_context *ctx = test->priv;
242 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
250 kunit_cleanup(&ctx->test);
252 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
295 res2 = kunit_alloc_and_get_resource(&ctx->test,
322 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
324 struct kunit_test_resource_context *ctx = test->priv;
328 res = kunit_alloc_and_get_resource(&ctx->test,
339 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
340 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
344 kunit_cleanup(&ctx->test);
351 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
352 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
355 static void kunit_resource_test_static(struct kunit *test)
360 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
363 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
365 kunit_cleanup(test);
367 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
370 static void kunit_resource_test_named(struct kunit *test)
375 KUNIT_EXPECT_EQ(test,
376 kunit_add_named_resource(test, NULL, NULL, &res1,
379 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
381 KUNIT_EXPECT_EQ(test,
382 kunit_add_named_resource(test, NULL, NULL, &res1,
386 KUNIT_EXPECT_EQ(test,
387 kunit_add_named_resource(test, NULL, NULL, &res2,
391 found = kunit_find_named_resource(test, "resource_1");
393 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
398 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
401 kunit_cleanup(test);
403 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
412 static void kunit_resource_test_action(struct kunit *test)
416 kunit_add_action(test, increment_int, &num_actions);
417 KUNIT_EXPECT_EQ(test, num_actions, 0);
418 kunit_cleanup(test);
419 KUNIT_EXPECT_EQ(test, num_actions, 1);
422 kunit_cleanup(test);
423 KUNIT_EXPECT_EQ(test, num_actions, 1);
426 kunit_add_action(test, increment_int, &num_actions);
427 kunit_add_action(test, increment_int, &num_actions);
428 kunit_cleanup(test);
429 KUNIT_EXPECT_EQ(test, num_actions, 3);
431 static void kunit_resource_test_remove_action(struct kunit *test)
435 kunit_add_action(test, increment_int, &num_actions);
436 KUNIT_EXPECT_EQ(test, num_actions, 0);
438 kunit_remove_action(test, increment_int, &num_actions);
439 kunit_cleanup(test);
440 KUNIT_EXPECT_EQ(test, num_actions, 0);
442 static void kunit_resource_test_release_action(struct kunit *test)
446 kunit_add_action(test, increment_int, &num_actions);
447 KUNIT_EXPECT_EQ(test, num_actions, 0);
449 kunit_release_action(test, increment_int, &num_actions);
450 KUNIT_EXPECT_EQ(test, num_actions, 1);
452 /* Doesn't run again on test exit. */
453 kunit_cleanup(test);
454 KUNIT_EXPECT_EQ(test, num_actions, 1);
470 static void kunit_resource_test_action_ordering(struct kunit *test)
472 struct kunit_test_resource_context *ctx = test->priv;
474 kunit_add_action(test, action_order_1, ctx);
475 kunit_add_action(test, action_order_2, ctx);
476 kunit_add_action(test, action_order_1, ctx);
477 kunit_add_action(test, action_order_2, ctx);
478 kunit_remove_action(test, action_order_1, ctx);
479 kunit_release_action(test, action_order_2, ctx);
480 kunit_cleanup(test);
483 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 2);
484 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
485 KUNIT_EXPECT_EQ(test, ctx->free_order[2], 1);
488 static int kunit_resource_test_init(struct kunit *test)
493 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
495 test->priv = ctx;
497 kunit_init_test(&ctx->test, "test_test_context", NULL);
502 static void kunit_resource_test_exit(struct kunit *test)
504 struct kunit_test_resource_context *ctx = test->priv;
506 kunit_cleanup(&ctx->test);
527 .name = "kunit-resource-test",
533 static void kunit_log_test(struct kunit *test)
537 suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL);
538 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
540 kunit_log(KERN_INFO, test, "put this in log.");
541 kunit_log(KERN_INFO, test, "this too.");
546 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
547 strstr(test->log, "put this in log."));
548 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
549 strstr(test->log, "this too."));
550 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
552 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
555 KUNIT_EXPECT_NULL(test, test->log);
559 static void kunit_log_newline_test(struct kunit *test)
561 kunit_info(test, "Add newline\n");
562 if (test->log) {
563 KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"),
564 "Missing log line, full log:\n%s", test->log);
565 KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n"));
567 kunit_skip(test, "only useful when debugfs is enabled");
578 .name = "kunit-log-test",
582 static void kunit_status_set_failure_test(struct kunit *test)
586 kunit_init_test(&fake, "fake test", NULL);
588 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
590 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
593 static void kunit_status_mark_skipped_test(struct kunit *test)
597 kunit_init_test(&fake, "fake test", NULL);
600 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
601 KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
603 /* Mark the test as skipped. */
607 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
608 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
622 static void kunit_current_test(struct kunit *test)
625 * kunit_get_current_test() are equivalent to current test.
627 KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
628 KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
631 static void kunit_current_fail_test(struct kunit *test)
635 kunit_init_test(&fake, "fake test", NULL);
636 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
638 /* Set current->kunit_test to fake test. */
641 kunit_fail_current_test("This should make `fake` test fail.");
642 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
645 /* Reset current->kunit_test to current test. */
646 current->kunit_test = test;