Lines Matching refs: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)
26 * to test a piece of code, you set some expectations about what the
27 * code should do. KUnit then runs the test and verifies that the code's
30 KUNIT_EXPECT_EQ(test, 1 + 1, 2);
34 * This is run once before each test case, see the comment on
37 static int example_test_init(struct kunit *test)
39 kunit_info(test, "initializing\n");
45 * This is run once after each test case, see the comment on
48 static void example_test_exit(struct kunit *test)
50 kunit_info(test, "cleaning up\n");
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)
81 kunit_info(test, "You should not see a line below.");
83 /* Skip (and abort) the test */
84 kunit_skip(test, "this test should be skipped");
87 KUNIT_FAIL(test, "You should not see this line.");
91 * This test should always be marked skipped.
93 static void example_mark_skipped_test(struct kunit *test)
96 kunit_info(test, "You should see a line below.");
98 /* Skip (but do not abort) the test */
99 kunit_mark_skipped(test, "this test should be skipped");
102 kunit_info(test, "You should see this line.");
106 * This test shows off all the types of KUNIT_EXPECT macros.
108 static void example_all_expect_macros_test(struct kunit *test)
114 KUNIT_EXPECT_TRUE(test, true);
115 KUNIT_EXPECT_FALSE(test, false);
118 KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
119 KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
120 KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
121 KUNIT_EXPECT_NE(test, 1, 0); /* check != */
122 KUNIT_EXPECT_GT(test, 1, 0); /* check > */
123 KUNIT_EXPECT_LT(test, 0, 1); /* check < */
126 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
127 KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
128 KUNIT_EXPECT_PTR_NE(test, test, NULL);
129 KUNIT_EXPECT_NULL(test, NULL);
130 KUNIT_EXPECT_NOT_NULL(test, test);
133 KUNIT_EXPECT_STREQ(test, "hi", "hi");
134 KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
137 KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(array1));
138 KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(array1));
141 * There are also ASSERT variants of all of the above that abort test
144 KUNIT_ASSERT_GT(test, sizeof(char), 0);
150 KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
151 KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
182 * This test shows the use of static stubs.
184 static void example_static_stub_test(struct kunit *test)
187 KUNIT_EXPECT_EQ(test, add_one(1), 2);
190 kunit_activate_static_stub(test, add_one, subtract_one);
193 KUNIT_EXPECT_EQ(test, add_one(1), 0);
196 kunit_deactivate_static_stub(test, add_one);
197 KUNIT_EXPECT_EQ(test, add_one(1), 2);
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)
211 KUNIT_EXPECT_EQ(test, add_one(1), 2);
214 kunit_activate_static_stub(test, add_one_fn_ptr, subtract_one);
217 KUNIT_EXPECT_EQ(test, add_one(1), 0);
220 kunit_deactivate_static_stub(test, add_one_fn_ptr);
221 KUNIT_EXPECT_EQ(test, add_one(1), 2);
241 * This test shows the use of params.
243 static void example_params_test(struct kunit *test)
245 const struct example_param *param = test->param_value;
248 KUNIT_ASSERT_NOT_NULL(test, param);
252 kunit_skip(test, "unsupported param value %d", param->value);
255 KUNIT_EXPECT_EQ(test, param->value % param->value, 0);
259 * This test shows the use of test->priv.
261 static void example_priv_test(struct kunit *test)
263 /* unless setup in suite->init(), test->priv is NULL */
264 KUNIT_ASSERT_NULL(test, test->priv);
267 test->priv = kunit_kzalloc(test, 1, GFP_KERNEL);
268 KUNIT_EXPECT_NOT_NULL(test, test->priv);
269 KUNIT_ASSERT_PTR_EQ(test, test->priv, kunit_get_current_test()->priv);
273 * This test should always pass. Can be used to practice filtering attributes.
275 static void example_slow_test(struct kunit *test)
277 KUNIT_EXPECT_EQ(test, 1 + 1, 2);
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.
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)
350 KUNIT_EXPECT_EQ(test, init_add(1, 1), 2);
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