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",
113 * Context for testing test managed resources
114 * is_resource_initialized is used to test arbitrary resources
117 struct kunit test;
139 static void kunit_resource_test_init_resources(struct kunit *test)
141 struct kunit_test_resource_context *ctx = test->priv;
143 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
145 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
148 static void kunit_resource_test_alloc_resource(struct kunit *test)
150 struct kunit_test_resource_context *ctx = test->priv;
154 res = kunit_alloc_and_get_resource(&ctx->test,
160 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
161 KUNIT_EXPECT_PTR_EQ(test,
164 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
165 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
170 static inline bool kunit_resource_instance_match(struct kunit *test,
185 static void kunit_resource_test_destroy_resource(struct kunit *test)
187 struct kunit_test_resource_context *ctx = test->priv;
189 &ctx->test,
197 KUNIT_ASSERT_FALSE(test,
198 kunit_destroy_resource(&ctx->test,
202 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
203 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
206 static void kunit_resource_test_remove_resource(struct kunit *test)
208 struct kunit_test_resource_context *ctx = test->priv;
210 &ctx->test,
217 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
222 kunit_remove_resource(test, res);
223 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
225 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
228 kunit_remove_resource(test, res);
229 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
233 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
237 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
240 static void kunit_resource_test_cleanup_resources(struct kunit *test)
243 struct kunit_test_resource_context *ctx = test->priv;
247 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
255 kunit_cleanup(&ctx->test);
257 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
300 res2 = kunit_alloc_and_get_resource(&ctx->test,
327 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
329 struct kunit_test_resource_context *ctx = test->priv;
333 res = kunit_alloc_and_get_resource(&ctx->test,
344 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
345 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
349 kunit_cleanup(&ctx->test);
356 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
357 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
360 static void kunit_resource_test_static(struct kunit *test)
365 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
368 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
370 kunit_cleanup(test);
372 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
375 static void kunit_resource_test_named(struct kunit *test)
380 KUNIT_EXPECT_EQ(test,
381 kunit_add_named_resource(test, NULL, NULL, &res1,
384 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
386 KUNIT_EXPECT_EQ(test,
387 kunit_add_named_resource(test, NULL, NULL, &res1,
391 KUNIT_EXPECT_EQ(test,
392 kunit_add_named_resource(test, NULL, NULL, &res2,
396 found = kunit_find_named_resource(test, "resource_1");
398 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
403 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
406 kunit_cleanup(test);
408 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
417 static void kunit_resource_test_action(struct kunit *test)
421 kunit_add_action(test, increment_int, &num_actions);
422 KUNIT_EXPECT_EQ(test, num_actions, 0);
423 kunit_cleanup(test);
424 KUNIT_EXPECT_EQ(test, num_actions, 1);
427 kunit_cleanup(test);
428 KUNIT_EXPECT_EQ(test, num_actions, 1);
431 kunit_add_action(test, increment_int, &num_actions);
432 kunit_add_action(test, increment_int, &num_actions);
433 kunit_cleanup(test);
434 KUNIT_EXPECT_EQ(test, num_actions, 3);
436 static void kunit_resource_test_remove_action(struct kunit *test)
440 kunit_add_action(test, increment_int, &num_actions);
441 KUNIT_EXPECT_EQ(test, num_actions, 0);
443 kunit_remove_action(test, increment_int, &num_actions);
444 kunit_cleanup(test);
445 KUNIT_EXPECT_EQ(test, num_actions, 0);
447 static void kunit_resource_test_release_action(struct kunit *test)
451 kunit_add_action(test, increment_int, &num_actions);
452 KUNIT_EXPECT_EQ(test, num_actions, 0);
454 kunit_release_action(test, increment_int, &num_actions);
455 KUNIT_EXPECT_EQ(test, num_actions, 1);
457 /* Doesn't run again on test exit. */
458 kunit_cleanup(test);
459 KUNIT_EXPECT_EQ(test, num_actions, 1);
475 static void kunit_resource_test_action_ordering(struct kunit *test)
477 struct kunit_test_resource_context *ctx = test->priv;
479 kunit_add_action(test, action_order_1, ctx);
480 kunit_add_action(test, action_order_2, ctx);
481 kunit_add_action(test, action_order_1, ctx);
482 kunit_add_action(test, action_order_2, ctx);
483 kunit_remove_action(test, action_order_1, ctx);
484 kunit_release_action(test, action_order_2, ctx);
485 kunit_cleanup(test);
488 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 2);
489 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
490 KUNIT_EXPECT_EQ(test, ctx->free_order[2], 1);
493 static int kunit_resource_test_init(struct kunit *test)
498 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
500 test->priv = ctx;
502 kunit_init_test(&ctx->test, "test_test_context", NULL);
507 static void kunit_resource_test_exit(struct kunit *test)
509 struct kunit_test_resource_context *ctx = test->priv;
511 kunit_cleanup(&ctx->test);
532 .name = "kunit-resource-test",
540 * build this code if this test is built-in.
547 static void kunit_log_test(struct kunit *test)
553 suite.log = kunit_alloc_string_stream(test, GFP_KERNEL);
554 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
557 kunit_log(KERN_INFO, test, "put this in log.");
558 kunit_log(KERN_INFO, test, "this too.");
563 KUNIT_EXPECT_TRUE(test, test->log->append_newlines);
565 full_log = string_stream_get_string(test->log);
566 kunit_add_action(test, kfree_wrapper, full_log);
567 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
569 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
573 kunit_add_action(test, kfree_wrapper, full_log);
574 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
576 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
579 KUNIT_EXPECT_NULL(test, test->log);
583 static void kunit_log_newline_test(struct kunit *test)
587 kunit_info(test, "Add newline\n");
588 if (test->log) {
589 full_log = string_stream_get_string(test->log);
590 kunit_add_action(test, kfree_wrapper, full_log);
591 KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"),
593 KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n"));
595 kunit_skip(test, "only useful when debugfs is enabled");
599 static void kunit_log_test(struct kunit *test)
601 kunit_skip(test, "Log tests only run when built-in");
604 static void kunit_log_newline_test(struct kunit *test)
606 kunit_skip(test, "Log tests only run when built-in");
617 .name = "kunit-log-test",
621 static void kunit_status_set_failure_test(struct kunit *test)
625 kunit_init_test(&fake, "fake test", NULL);
627 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
629 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
632 static void kunit_status_mark_skipped_test(struct kunit *test)
636 kunit_init_test(&fake, "fake test", NULL);
639 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
640 KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
642 /* Mark the test as skipped. */
646 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
647 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
661 static void kunit_current_test(struct kunit *test)
664 * kunit_get_current_test() are equivalent to current test.
666 KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
667 KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
670 static void kunit_current_fail_test(struct kunit *test)
674 kunit_init_test(&fake, "fake test", NULL);
675 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
677 /* Set current->kunit_test to fake test. */
680 kunit_fail_current_test("This should make `fake` test fail.");
681 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
684 /* Reset current->kunit_test to current test. */
685 current->kunit_test = test;
699 static void kunit_device_test(struct kunit *test)
704 test_device = kunit_device_register(test, "my_device");
705 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
710 KUNIT_EXPECT_EQ(test, action_was_run, 0);
712 kunit_device_unregister(test, test_device);
714 KUNIT_EXPECT_EQ(test, action_was_run, 1);
717 static void kunit_device_cleanup_test(struct kunit *test)
722 test_device = kunit_device_register(test, "my_device");
723 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
728 KUNIT_EXPECT_EQ(test, action_was_run, 0);
731 kunit_cleanup(test);
733 KUNIT_EXPECT_EQ(test, action_was_run, 1);
744 struct kunit *test = kunit_get_current_test();
745 struct driver_test_state *state = (struct driver_test_state *)test->priv;
753 struct kunit *test = kunit_get_current_test();
754 struct driver_test_state *state = (struct driver_test_state *)test->priv;
760 static void kunit_device_driver_test(struct kunit *test)
764 struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);
766 test->priv = test_state;
767 test_driver = kunit_driver_create(test, "my_driver");
770 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_driver);
775 test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
778 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
781 KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
786 KUNIT_EXPECT_EQ(test, test_state->action_was_run, 0);
788 kunit_device_unregister(test, test_device);
792 KUNIT_ASSERT_TRUE(test, test_state->driver_device_removed);
794 // We're going to test this again.
799 test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
802 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
805 KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);