Lines Matching refs:test

3  * KUnit test for core test infrastructure.
9 #include <kunit/test.h>
10 #include <kunit/test-bug.h>
25 struct kunit *test = data;
26 struct kunit_try_catch_test_context *ctx = test->priv;
33 struct kunit *test = data;
35 KUNIT_FAIL(test, "Catch should not be called\n");
38 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
40 struct kunit_try_catch_test_context *ctx = test->priv;
44 test,
47 kunit_try_catch_run(try_catch, test);
49 KUNIT_EXPECT_TRUE(test, ctx->function_called);
54 struct kunit *test = data;
55 struct kunit_try_catch_test_context *ctx = test->priv;
59 KUNIT_FAIL(test, "This line should never be reached\n");
64 struct kunit *test = data;
65 struct kunit_try_catch_test_context *ctx = test->priv;
70 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
72 struct kunit_try_catch_test_context *ctx = test->priv;
76 test,
79 kunit_try_catch_run(try_catch, test);
81 KUNIT_EXPECT_TRUE(test, ctx->function_called);
84 static int kunit_try_catch_test_init(struct kunit *test)
88 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
89 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
90 test->priv = ctx;
92 ctx->try_catch = kunit_kmalloc(test,
95 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
107 .name = "kunit-try-catch-test",
116 struct kunit *test = data;
121 KUNIT_FAIL(test, "This line should never be reached\n");
124 static void kunit_test_fault_null_dereference(struct kunit *test)
126 struct kunit_try_catch_test_context *ctx = test->priv;
130 test,
133 kunit_try_catch_run(try_catch, test);
135 KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR);
136 KUNIT_EXPECT_TRUE(test, ctx->function_called);
155 * Context for testing test managed resources
156 * is_resource_initialized is used to test arbitrary resources
159 struct kunit test;
181 static void kunit_resource_test_init_resources(struct kunit *test)
183 struct kunit_test_resource_context *ctx = test->priv;
185 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
187 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
190 static void kunit_resource_test_alloc_resource(struct kunit *test)
192 struct kunit_test_resource_context *ctx = test->priv;
196 res = kunit_alloc_and_get_resource(&ctx->test,
202 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
203 KUNIT_EXPECT_PTR_EQ(test,
206 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
207 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
212 static inline bool kunit_resource_instance_match(struct kunit *test,
227 static void kunit_resource_test_destroy_resource(struct kunit *test)
229 struct kunit_test_resource_context *ctx = test->priv;
231 &ctx->test,
239 KUNIT_ASSERT_FALSE(test,
240 kunit_destroy_resource(&ctx->test,
244 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
245 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
248 static void kunit_resource_test_remove_resource(struct kunit *test)
250 struct kunit_test_resource_context *ctx = test->priv;
252 &ctx->test,
259 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
264 kunit_remove_resource(test, res);
265 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
267 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
270 kunit_remove_resource(test, res);
271 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
275 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
279 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
282 static void kunit_resource_test_cleanup_resources(struct kunit *test)
285 struct kunit_test_resource_context *ctx = test->priv;
289 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
297 kunit_cleanup(&ctx->test);
299 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
342 res2 = kunit_alloc_and_get_resource(&ctx->test,
369 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
371 struct kunit_test_resource_context *ctx = test->priv;
375 res = kunit_alloc_and_get_resource(&ctx->test,
386 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
387 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
391 kunit_cleanup(&ctx->test);
398 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
399 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
402 static void kunit_resource_test_static(struct kunit *test)
407 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
410 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
412 kunit_cleanup(test);
414 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
417 static void kunit_resource_test_named(struct kunit *test)
422 KUNIT_EXPECT_EQ(test,
423 kunit_add_named_resource(test, NULL, NULL, &res1,
426 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
428 KUNIT_EXPECT_EQ(test,
429 kunit_add_named_resource(test, NULL, NULL, &res1,
433 KUNIT_EXPECT_EQ(test,
434 kunit_add_named_resource(test, NULL, NULL, &res2,
438 found = kunit_find_named_resource(test, "resource_1");
440 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
445 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
448 kunit_cleanup(test);
450 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
459 static void kunit_resource_test_action(struct kunit *test)
463 kunit_add_action(test, increment_int, &num_actions);
464 KUNIT_EXPECT_EQ(test, num_actions, 0);
465 kunit_cleanup(test);
466 KUNIT_EXPECT_EQ(test, num_actions, 1);
469 kunit_cleanup(test);
470 KUNIT_EXPECT_EQ(test, num_actions, 1);
473 kunit_add_action(test, increment_int, &num_actions);
474 kunit_add_action(test, increment_int, &num_actions);
475 kunit_cleanup(test);
476 KUNIT_EXPECT_EQ(test, num_actions, 3);
478 static void kunit_resource_test_remove_action(struct kunit *test)
482 kunit_add_action(test, increment_int, &num_actions);
483 KUNIT_EXPECT_EQ(test, num_actions, 0);
485 kunit_remove_action(test, increment_int, &num_actions);
486 kunit_cleanup(test);
487 KUNIT_EXPECT_EQ(test, num_actions, 0);
489 static void kunit_resource_test_release_action(struct kunit *test)
493 kunit_add_action(test, increment_int, &num_actions);
494 KUNIT_EXPECT_EQ(test, num_actions, 0);
496 kunit_release_action(test, increment_int, &num_actions);
497 KUNIT_EXPECT_EQ(test, num_actions, 1);
499 /* Doesn't run again on test exit. */
500 kunit_cleanup(test);
501 KUNIT_EXPECT_EQ(test, num_actions, 1);
517 static void kunit_resource_test_action_ordering(struct kunit *test)
519 struct kunit_test_resource_context *ctx = test->priv;
521 kunit_add_action(test, action_order_1, ctx);
522 kunit_add_action(test, action_order_2, ctx);
523 kunit_add_action(test, action_order_1, ctx);
524 kunit_add_action(test, action_order_2, ctx);
525 kunit_remove_action(test, action_order_1, ctx);
526 kunit_release_action(test, action_order_2, ctx);
527 kunit_cleanup(test);
530 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 2);
531 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
532 KUNIT_EXPECT_EQ(test, ctx->free_order[2], 1);
535 static int kunit_resource_test_init(struct kunit *test)
540 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
542 test->priv = ctx;
544 kunit_init_test(&ctx->test, "test_test_context", NULL);
549 static void kunit_resource_test_exit(struct kunit *test)
551 struct kunit_test_resource_context *ctx = test->priv;
553 kunit_cleanup(&ctx->test);
574 .name = "kunit-resource-test",
582 * build this code if this test is built-in.
589 static void kunit_log_test(struct kunit *test)
595 suite.log = kunit_alloc_string_stream(test, GFP_KERNEL);
596 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
599 kunit_log(KERN_INFO, test, "put this in log.");
600 kunit_log(KERN_INFO, test, "this too.");
605 KUNIT_EXPECT_TRUE(test, test->log->append_newlines);
607 full_log = string_stream_get_string(test->log);
608 kunit_add_action(test, kfree_wrapper, full_log);
609 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
611 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
615 kunit_add_action(test, kfree_wrapper, full_log);
616 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
618 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
621 KUNIT_EXPECT_NULL(test, test->log);
625 static void kunit_log_newline_test(struct kunit *test)
629 kunit_info(test, "Add newline\n");
630 if (test->log) {
631 full_log = string_stream_get_string(test->log);
632 kunit_add_action(test, kfree_wrapper, full_log);
633 KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"),
635 KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n"));
637 kunit_skip(test, "only useful when debugfs is enabled");
641 static void kunit_log_test(struct kunit *test)
643 kunit_skip(test, "Log tests only run when built-in");
646 static void kunit_log_newline_test(struct kunit *test)
648 kunit_skip(test, "Log tests only run when built-in");
659 .name = "kunit-log-test",
663 static void kunit_status_set_failure_test(struct kunit *test)
667 kunit_init_test(&fake, "fake test", NULL);
669 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
671 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
674 static void kunit_status_mark_skipped_test(struct kunit *test)
678 kunit_init_test(&fake, "fake test", NULL);
681 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
682 KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
684 /* Mark the test as skipped. */
688 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
689 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
703 static void kunit_current_test(struct kunit *test)
706 * kunit_get_current_test() are equivalent to current test.
708 KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
709 KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
712 static void kunit_current_fail_test(struct kunit *test)
716 kunit_init_test(&fake, "fake test", NULL);
717 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
719 /* Set current->kunit_test to fake test. */
722 kunit_fail_current_test("This should make `fake` test fail.");
723 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
726 /* Reset current->kunit_test to current test. */
727 current->kunit_test = test;
741 static void kunit_device_test(struct kunit *test)
746 test_device = kunit_device_register(test, "my_device");
747 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
752 KUNIT_EXPECT_EQ(test, action_was_run, 0);
754 kunit_device_unregister(test, test_device);
756 KUNIT_EXPECT_EQ(test, action_was_run, 1);
759 static void kunit_device_cleanup_test(struct kunit *test)
764 test_device = kunit_device_register(test, "my_device");
765 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
770 KUNIT_EXPECT_EQ(test, action_was_run, 0);
773 kunit_cleanup(test);
775 KUNIT_EXPECT_EQ(test, action_was_run, 1);
786 struct kunit *test = kunit_get_current_test();
787 struct driver_test_state *state = (struct driver_test_state *)test->priv;
795 struct kunit *test = kunit_get_current_test();
796 struct driver_test_state *state = (struct driver_test_state *)test->priv;
802 static void kunit_device_driver_test(struct kunit *test)
806 struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);
808 test->priv = test_state;
809 test_driver = kunit_driver_create(test, "my_driver");
812 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_driver);
817 test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
820 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
823 KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
828 KUNIT_EXPECT_EQ(test, test_state->action_was_run, 0);
830 kunit_device_unregister(test, test_device);
834 KUNIT_ASSERT_TRUE(test, test_state->driver_device_removed);
836 // We're going to test this again.
841 test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
844 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
847 KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);