Lines Matching full:test

3  * Example KUnit test to show how to use KUnit.
9 #include <kunit/test.h>
13 * This is the most fundamental element of KUnit, the test case. A test case
15 * any expectations or assertions are not met, the test fails; otherwise, the
16 * test passes.
18 * In KUnit, a test case is just a function with the signature
20 * information about the current test.
22 static void example_simple_test(struct kunit *test) in example_simple_test() argument
26 * to test a piece of code, you set some expectations about what the in example_simple_test()
27 * code should do. KUnit then runs the test and verifies that the code's in example_simple_test()
30 KUNIT_EXPECT_EQ(test, 1 + 1, 2); in example_simple_test()
34 * This is run once before each test case, see the comment on
37 static int example_test_init(struct kunit *test) in example_test_init() argument
39 kunit_info(test, "initializing\n"); in example_test_init()
45 * This is run once after each test case, see the comment on
48 static void example_test_exit(struct kunit *test) in example_test_exit() argument
50 kunit_info(test, "cleaning up\n"); in example_test_exit()
55 * This is run once before all test cases in the suite.
66 * This is run once after all test cases in the suite.
76 * This test should always be skipped.
78 static void example_skip_test(struct kunit *test) in example_skip_test() argument
81 kunit_info(test, "You should not see a line below."); in example_skip_test()
83 /* Skip (and abort) the test */ in example_skip_test()
84 kunit_skip(test, "this test should be skipped"); in example_skip_test()
87 KUNIT_FAIL(test, "You should not see this line."); in example_skip_test()
91 * This test should always be marked skipped.
93 static void example_mark_skipped_test(struct kunit *test) in example_mark_skipped_test() argument
96 kunit_info(test, "You should see a line below."); in example_mark_skipped_test()
98 /* Skip (but do not abort) the test */ in example_mark_skipped_test()
99 kunit_mark_skipped(test, "this test should be skipped"); in example_mark_skipped_test()
102 kunit_info(test, "You should see this line."); in example_mark_skipped_test()
106 * This test shows off all the types of KUNIT_EXPECT macros.
108 static void example_all_expect_macros_test(struct kunit *test) in example_all_expect_macros_test() argument
114 KUNIT_EXPECT_TRUE(test, true); in example_all_expect_macros_test()
115 KUNIT_EXPECT_FALSE(test, false); in example_all_expect_macros_test()
118 KUNIT_EXPECT_EQ(test, 1, 1); /* check == */ in example_all_expect_macros_test()
119 KUNIT_EXPECT_GE(test, 1, 1); /* check >= */ in example_all_expect_macros_test()
120 KUNIT_EXPECT_LE(test, 1, 1); /* check <= */ in example_all_expect_macros_test()
121 KUNIT_EXPECT_NE(test, 1, 0); /* check != */ in example_all_expect_macros_test()
122 KUNIT_EXPECT_GT(test, 1, 0); /* check > */ in example_all_expect_macros_test()
123 KUNIT_EXPECT_LT(test, 0, 1); /* check < */ in example_all_expect_macros_test()
126 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test); in example_all_expect_macros_test()
127 KUNIT_EXPECT_PTR_EQ(test, NULL, NULL); in example_all_expect_macros_test()
128 KUNIT_EXPECT_PTR_NE(test, test, NULL); in example_all_expect_macros_test()
129 KUNIT_EXPECT_NULL(test, NULL); in example_all_expect_macros_test()
130 KUNIT_EXPECT_NOT_NULL(test, test); in example_all_expect_macros_test()
133 KUNIT_EXPECT_STREQ(test, "hi", "hi"); in example_all_expect_macros_test()
134 KUNIT_EXPECT_STRNEQ(test, "hi", "bye"); in example_all_expect_macros_test()
137 KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(array1)); in example_all_expect_macros_test()
138 KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(array1)); in example_all_expect_macros_test()
141 * There are also ASSERT variants of all of the above that abort test in example_all_expect_macros_test()
144 KUNIT_ASSERT_GT(test, sizeof(char), 0); in example_all_expect_macros_test()
150 KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); in example_all_expect_macros_test()
151 KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!"); in example_all_expect_macros_test()
182 * This test shows the use of static stubs.
184 static void example_static_stub_test(struct kunit *test) in example_static_stub_test() argument
187 KUNIT_EXPECT_EQ(test, add_one(1), 2); in example_static_stub_test()
190 kunit_activate_static_stub(test, add_one, subtract_one); in example_static_stub_test()
193 KUNIT_EXPECT_EQ(test, add_one(1), 0); in example_static_stub_test()
196 kunit_deactivate_static_stub(test, add_one); in example_static_stub_test()
197 KUNIT_EXPECT_EQ(test, add_one(1), 2); in example_static_stub_test()
201 * This test shows the use of static stubs when the function being
208 static void example_static_stub_using_fn_ptr_test(struct kunit *test) in example_static_stub_using_fn_ptr_test() argument
211 KUNIT_EXPECT_EQ(test, add_one(1), 2); in example_static_stub_using_fn_ptr_test()
214 kunit_activate_static_stub(test, add_one_fn_ptr, subtract_one); in example_static_stub_using_fn_ptr_test()
217 KUNIT_EXPECT_EQ(test, add_one(1), 0); in example_static_stub_using_fn_ptr_test()
220 kunit_deactivate_static_stub(test, add_one_fn_ptr); in example_static_stub_using_fn_ptr_test()
221 KUNIT_EXPECT_EQ(test, add_one(1), 2); in example_static_stub_using_fn_ptr_test()
241 * This test shows the use of params.
243 static void example_params_test(struct kunit *test) in example_params_test() argument
245 const struct example_param *param = test->param_value; in example_params_test()
248 KUNIT_ASSERT_NOT_NULL(test, param); in example_params_test()
250 /* Test can be skipped on unsupported param values */ in example_params_test()
252 kunit_skip(test, "unsupported param value %d", param->value); in example_params_test()
255 KUNIT_EXPECT_EQ(test, param->value % param->value, 0); in example_params_test()
259 * This test shows the use of test->priv.
261 static void example_priv_test(struct kunit *test) in example_priv_test() argument
263 /* unless setup in suite->init(), test->priv is NULL */ in example_priv_test()
264 KUNIT_ASSERT_NULL(test, test->priv); in example_priv_test()
267 test->priv = kunit_kzalloc(test, 1, GFP_KERNEL); in example_priv_test()
268 KUNIT_EXPECT_NOT_NULL(test, test->priv); in example_priv_test()
269 KUNIT_ASSERT_PTR_EQ(test, test->priv, kunit_get_current_test()->priv); in example_priv_test()
273 * This test should always pass. Can be used to practice filtering attributes.
275 static void example_slow_test(struct kunit *test) in example_slow_test() argument
277 KUNIT_EXPECT_EQ(test, 1 + 1, 2); in example_slow_test()
281 * Here we make a list of all the test cases we want to add to the test suite
286 * This is a helper to create a test case object from a test case
288 * use KUnit, just know that this is how you associate test cases with a
289 * test suite.
306 * Test cases are defined as belonging to the suite by adding them to
310 * will be used by every test; this is accomplished with an `init` function
311 * which runs before each test case is invoked. Similarly, an `exit` function
312 * may be specified which runs after every test case and can be used to for
313 * cleanup. For clarity, running tests in a test suite would behave as follows:
316 * suite.init(test);
317 * suite.test_case[0](test);
318 * suite.exit(test);
319 * suite.init(test);
320 * suite.test_case[1](test);
321 * suite.exit(test);
335 * This registers the above test suite telling KUnit that this is a suite of
346 * This test should always pass. Can be used to test init suites.
348 static void __init example_init_test(struct kunit *test) in example_init_test() argument
350 KUNIT_EXPECT_EQ(test, init_add(1, 1), 2); in example_init_test()
355 * used in debugfs to retrieve results after test has run
364 * used in debugfs to retrieve results after test has run
372 * This registers the test suite and marks the suite as using init data
377 MODULE_DESCRIPTION("Example KUnit test suite");