xref: /openbmc/linux/lib/kunit/kunit-example-test.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1c475c77dSAlan Maguire // SPDX-License-Identifier: GPL-2.0
2c475c77dSAlan Maguire /*
3c475c77dSAlan Maguire  * Example KUnit test to show how to use KUnit.
4c475c77dSAlan Maguire  *
5c475c77dSAlan Maguire  * Copyright (C) 2019, Google LLC.
6c475c77dSAlan Maguire  * Author: Brendan Higgins <brendanhiggins@google.com>
7c475c77dSAlan Maguire  */
8c475c77dSAlan Maguire 
9c475c77dSAlan Maguire #include <kunit/test.h>
10e047c5eaSDavid Gow #include <kunit/static_stub.h>
11c475c77dSAlan Maguire 
12c475c77dSAlan Maguire /*
13c475c77dSAlan Maguire  * This is the most fundamental element of KUnit, the test case. A test case
14c475c77dSAlan Maguire  * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if
15c475c77dSAlan Maguire  * any expectations or assertions are not met, the test fails; otherwise, the
16c475c77dSAlan Maguire  * test passes.
17c475c77dSAlan Maguire  *
18c475c77dSAlan Maguire  * In KUnit, a test case is just a function with the signature
19c475c77dSAlan Maguire  * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores
20c475c77dSAlan Maguire  * information about the current test.
21c475c77dSAlan Maguire  */
example_simple_test(struct kunit * test)22c475c77dSAlan Maguire static void example_simple_test(struct kunit *test)
23c475c77dSAlan Maguire {
24c475c77dSAlan Maguire 	/*
25c475c77dSAlan Maguire 	 * This is an EXPECTATION; it is how KUnit tests things. When you want
26c475c77dSAlan Maguire 	 * to test a piece of code, you set some expectations about what the
27c475c77dSAlan Maguire 	 * code should do. KUnit then runs the test and verifies that the code's
28c475c77dSAlan Maguire 	 * behavior matched what was expected.
29c475c77dSAlan Maguire 	 */
30c475c77dSAlan Maguire 	KUNIT_EXPECT_EQ(test, 1 + 1, 2);
31c475c77dSAlan Maguire }
32c475c77dSAlan Maguire 
33c475c77dSAlan Maguire /*
34c475c77dSAlan Maguire  * This is run once before each test case, see the comment on
35c475c77dSAlan Maguire  * example_test_suite for more information.
36c475c77dSAlan Maguire  */
example_test_init(struct kunit * test)37c475c77dSAlan Maguire static int example_test_init(struct kunit *test)
38c475c77dSAlan Maguire {
39c475c77dSAlan Maguire 	kunit_info(test, "initializing\n");
40c475c77dSAlan Maguire 
41c475c77dSAlan Maguire 	return 0;
42c475c77dSAlan Maguire }
43c475c77dSAlan Maguire 
44c475c77dSAlan Maguire /*
45a5ce66adSDavid Gow  * This is run once after each test case, see the comment on
46a5ce66adSDavid Gow  * example_test_suite for more information.
47a5ce66adSDavid Gow  */
example_test_exit(struct kunit * test)48a5ce66adSDavid Gow static void example_test_exit(struct kunit *test)
49a5ce66adSDavid Gow {
50a5ce66adSDavid Gow 	kunit_info(test, "cleaning up\n");
51a5ce66adSDavid Gow }
52a5ce66adSDavid Gow 
53a5ce66adSDavid Gow 
54a5ce66adSDavid Gow /*
551cdba21dSDaniel Latypov  * This is run once before all test cases in the suite.
561cdba21dSDaniel Latypov  * See the comment on example_test_suite for more information.
571cdba21dSDaniel Latypov  */
example_test_init_suite(struct kunit_suite * suite)581cdba21dSDaniel Latypov static int example_test_init_suite(struct kunit_suite *suite)
591cdba21dSDaniel Latypov {
601cdba21dSDaniel Latypov 	kunit_info(suite, "initializing suite\n");
611cdba21dSDaniel Latypov 
621cdba21dSDaniel Latypov 	return 0;
631cdba21dSDaniel Latypov }
641cdba21dSDaniel Latypov 
651cdba21dSDaniel Latypov /*
66a5ce66adSDavid Gow  * This is run once after all test cases in the suite.
67a5ce66adSDavid Gow  * See the comment on example_test_suite for more information.
68a5ce66adSDavid Gow  */
example_test_exit_suite(struct kunit_suite * suite)69a5ce66adSDavid Gow static void example_test_exit_suite(struct kunit_suite *suite)
70a5ce66adSDavid Gow {
71a5ce66adSDavid Gow 	kunit_info(suite, "exiting suite\n");
72a5ce66adSDavid Gow }
73a5ce66adSDavid Gow 
74a5ce66adSDavid Gow 
75a5ce66adSDavid Gow /*
76d99ea675SDavid Gow  * This test should always be skipped.
77d99ea675SDavid Gow  */
example_skip_test(struct kunit * test)78d99ea675SDavid Gow static void example_skip_test(struct kunit *test)
79d99ea675SDavid Gow {
80d99ea675SDavid Gow 	/* This line should run */
81d99ea675SDavid Gow 	kunit_info(test, "You should not see a line below.");
82d99ea675SDavid Gow 
83d99ea675SDavid Gow 	/* Skip (and abort) the test */
84d99ea675SDavid Gow 	kunit_skip(test, "this test should be skipped");
85d99ea675SDavid Gow 
86d99ea675SDavid Gow 	/* This line should not execute */
87d99ea675SDavid Gow 	KUNIT_FAIL(test, "You should not see this line.");
88d99ea675SDavid Gow }
89d99ea675SDavid Gow 
90d99ea675SDavid Gow /*
91d99ea675SDavid Gow  * This test should always be marked skipped.
92d99ea675SDavid Gow  */
example_mark_skipped_test(struct kunit * test)93d99ea675SDavid Gow static void example_mark_skipped_test(struct kunit *test)
94d99ea675SDavid Gow {
95d99ea675SDavid Gow 	/* This line should run */
96d99ea675SDavid Gow 	kunit_info(test, "You should see a line below.");
97d99ea675SDavid Gow 
98d99ea675SDavid Gow 	/* Skip (but do not abort) the test */
99d99ea675SDavid Gow 	kunit_mark_skipped(test, "this test should be skipped");
100d99ea675SDavid Gow 
101d99ea675SDavid Gow 	/* This line should run */
102d99ea675SDavid Gow 	kunit_info(test, "You should see this line.");
103d99ea675SDavid Gow }
1047b339105SDaniel Latypov 
1057b339105SDaniel Latypov /*
1067b339105SDaniel Latypov  * This test shows off all the types of KUNIT_EXPECT macros.
1077b339105SDaniel Latypov  */
example_all_expect_macros_test(struct kunit * test)1087b339105SDaniel Latypov static void example_all_expect_macros_test(struct kunit *test)
1097b339105SDaniel Latypov {
1103b30fb62SMaíra Canal 	const u32 array1[] = { 0x0F, 0xFF };
1113b30fb62SMaíra Canal 	const u32 array2[] = { 0x1F, 0xFF };
1123b30fb62SMaíra Canal 
1137b339105SDaniel Latypov 	/* Boolean assertions */
1147b339105SDaniel Latypov 	KUNIT_EXPECT_TRUE(test, true);
1157b339105SDaniel Latypov 	KUNIT_EXPECT_FALSE(test, false);
1167b339105SDaniel Latypov 
1177b339105SDaniel Latypov 	/* Integer assertions */
1187b339105SDaniel Latypov 	KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
1197b339105SDaniel Latypov 	KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
1207b339105SDaniel Latypov 	KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
1217b339105SDaniel Latypov 	KUNIT_EXPECT_NE(test, 1, 0); /* check != */
1227b339105SDaniel Latypov 	KUNIT_EXPECT_GT(test, 1, 0); /* check >  */
1237b339105SDaniel Latypov 	KUNIT_EXPECT_LT(test, 0, 1); /* check <  */
1247b339105SDaniel Latypov 
1257b339105SDaniel Latypov 	/* Pointer assertions */
1267b339105SDaniel Latypov 	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
1277b339105SDaniel Latypov 	KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
1287b339105SDaniel Latypov 	KUNIT_EXPECT_PTR_NE(test, test, NULL);
129de82c15dSRicardo Ribalda 	KUNIT_EXPECT_NULL(test, NULL);
130de82c15dSRicardo Ribalda 	KUNIT_EXPECT_NOT_NULL(test, test);
1317b339105SDaniel Latypov 
1327b339105SDaniel Latypov 	/* String assertions */
1337b339105SDaniel Latypov 	KUNIT_EXPECT_STREQ(test, "hi", "hi");
1347b339105SDaniel Latypov 	KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
1357b339105SDaniel Latypov 
1363b30fb62SMaíra Canal 	/* Memory block assertions */
1373b30fb62SMaíra Canal 	KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(array1));
1383b30fb62SMaíra Canal 	KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(array1));
1393b30fb62SMaíra Canal 
1407b339105SDaniel Latypov 	/*
1417b339105SDaniel Latypov 	 * There are also ASSERT variants of all of the above that abort test
1427b339105SDaniel Latypov 	 * execution if they fail. Useful for memory allocations, etc.
1437b339105SDaniel Latypov 	 */
1447b339105SDaniel Latypov 	KUNIT_ASSERT_GT(test, sizeof(char), 0);
1457b339105SDaniel Latypov 
1467b339105SDaniel Latypov 	/*
1477b339105SDaniel Latypov 	 * There are also _MSG variants of all of the above that let you include
1487b339105SDaniel Latypov 	 * additional text on failure.
1497b339105SDaniel Latypov 	 */
1507b339105SDaniel Latypov 	KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
1517b339105SDaniel Latypov 	KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
1527b339105SDaniel Latypov }
1537b339105SDaniel Latypov 
154e047c5eaSDavid Gow /* This is a function we'll replace with static stubs. */
add_one(int i)155e047c5eaSDavid Gow static int add_one(int i)
156e047c5eaSDavid Gow {
157e047c5eaSDavid Gow 	/* This will trigger the stub if active. */
158e047c5eaSDavid Gow 	KUNIT_STATIC_STUB_REDIRECT(add_one, i);
159e047c5eaSDavid Gow 
160e047c5eaSDavid Gow 	return i + 1;
161e047c5eaSDavid Gow }
162e047c5eaSDavid Gow 
163e047c5eaSDavid Gow /* This is used as a replacement for the above function. */
subtract_one(int i)164e047c5eaSDavid Gow static int subtract_one(int i)
165e047c5eaSDavid Gow {
166e047c5eaSDavid Gow 	/* We don't need to trigger the stub from the replacement. */
167e047c5eaSDavid Gow 
168e047c5eaSDavid Gow 	return i - 1;
169e047c5eaSDavid Gow }
170e047c5eaSDavid Gow 
171e047c5eaSDavid Gow /*
172e047c5eaSDavid Gow  * This test shows the use of static stubs.
173e047c5eaSDavid Gow  */
example_static_stub_test(struct kunit * test)174e047c5eaSDavid Gow static void example_static_stub_test(struct kunit *test)
175e047c5eaSDavid Gow {
176e047c5eaSDavid Gow 	/* By default, function is not stubbed. */
177e047c5eaSDavid Gow 	KUNIT_EXPECT_EQ(test, add_one(1), 2);
178e047c5eaSDavid Gow 
179e047c5eaSDavid Gow 	/* Replace add_one() with subtract_one(). */
180e047c5eaSDavid Gow 	kunit_activate_static_stub(test, add_one, subtract_one);
181e047c5eaSDavid Gow 
182e047c5eaSDavid Gow 	/* add_one() is now replaced. */
183e047c5eaSDavid Gow 	KUNIT_EXPECT_EQ(test, add_one(1), 0);
184e047c5eaSDavid Gow 
185e047c5eaSDavid Gow 	/* Return add_one() to normal. */
186e047c5eaSDavid Gow 	kunit_deactivate_static_stub(test, add_one);
187e047c5eaSDavid Gow 	KUNIT_EXPECT_EQ(test, add_one(1), 2);
188e047c5eaSDavid Gow }
189e047c5eaSDavid Gow 
190d273b728SMichal Wajdeczko static const struct example_param {
191d273b728SMichal Wajdeczko 	int value;
192d273b728SMichal Wajdeczko } example_params_array[] = {
193d273b728SMichal Wajdeczko 	{ .value = 2, },
194d273b728SMichal Wajdeczko 	{ .value = 1, },
195d273b728SMichal Wajdeczko 	{ .value = 0, },
196d273b728SMichal Wajdeczko };
197d273b728SMichal Wajdeczko 
example_param_get_desc(const struct example_param * p,char * desc)198d273b728SMichal Wajdeczko static void example_param_get_desc(const struct example_param *p, char *desc)
199d273b728SMichal Wajdeczko {
200d273b728SMichal Wajdeczko 	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "example value %d", p->value);
201d273b728SMichal Wajdeczko }
202d273b728SMichal Wajdeczko 
203d273b728SMichal Wajdeczko KUNIT_ARRAY_PARAM(example, example_params_array, example_param_get_desc);
204d273b728SMichal Wajdeczko 
205d273b728SMichal Wajdeczko /*
206d273b728SMichal Wajdeczko  * This test shows the use of params.
207d273b728SMichal Wajdeczko  */
example_params_test(struct kunit * test)208d273b728SMichal Wajdeczko static void example_params_test(struct kunit *test)
209d273b728SMichal Wajdeczko {
210d273b728SMichal Wajdeczko 	const struct example_param *param = test->param_value;
211d273b728SMichal Wajdeczko 
212d273b728SMichal Wajdeczko 	/* By design, param pointer will not be NULL */
213d273b728SMichal Wajdeczko 	KUNIT_ASSERT_NOT_NULL(test, param);
214d273b728SMichal Wajdeczko 
215d273b728SMichal Wajdeczko 	/* Test can be skipped on unsupported param values */
216d273b728SMichal Wajdeczko 	if (!param->value)
217d273b728SMichal Wajdeczko 		kunit_skip(test, "unsupported param value");
218d273b728SMichal Wajdeczko 
219d273b728SMichal Wajdeczko 	/* You can use param values for parameterized testing */
220d273b728SMichal Wajdeczko 	KUNIT_EXPECT_EQ(test, param->value % param->value, 0);
221d273b728SMichal Wajdeczko }
222d273b728SMichal Wajdeczko 
223d99ea675SDavid Gow /*
224*02c2d0c2SRae Moar  * This test should always pass. Can be used to practice filtering attributes.
225*02c2d0c2SRae Moar  */
example_slow_test(struct kunit * test)226*02c2d0c2SRae Moar static void example_slow_test(struct kunit *test)
227*02c2d0c2SRae Moar {
228*02c2d0c2SRae Moar 	KUNIT_EXPECT_EQ(test, 1 + 1, 2);
229*02c2d0c2SRae Moar }
230*02c2d0c2SRae Moar 
231*02c2d0c2SRae Moar /*
232c475c77dSAlan Maguire  * Here we make a list of all the test cases we want to add to the test suite
233c475c77dSAlan Maguire  * below.
234c475c77dSAlan Maguire  */
235c475c77dSAlan Maguire static struct kunit_case example_test_cases[] = {
236c475c77dSAlan Maguire 	/*
237c475c77dSAlan Maguire 	 * This is a helper to create a test case object from a test case
238c475c77dSAlan Maguire 	 * function; its exact function is not important to understand how to
239c475c77dSAlan Maguire 	 * use KUnit, just know that this is how you associate test cases with a
240c475c77dSAlan Maguire 	 * test suite.
241c475c77dSAlan Maguire 	 */
242c475c77dSAlan Maguire 	KUNIT_CASE(example_simple_test),
243d99ea675SDavid Gow 	KUNIT_CASE(example_skip_test),
244d99ea675SDavid Gow 	KUNIT_CASE(example_mark_skipped_test),
2457b339105SDaniel Latypov 	KUNIT_CASE(example_all_expect_macros_test),
246e047c5eaSDavid Gow 	KUNIT_CASE(example_static_stub_test),
247d273b728SMichal Wajdeczko 	KUNIT_CASE_PARAM(example_params_test, example_gen_params),
248*02c2d0c2SRae Moar 	KUNIT_CASE_SLOW(example_slow_test),
249c475c77dSAlan Maguire 	{}
250c475c77dSAlan Maguire };
251c475c77dSAlan Maguire 
252c475c77dSAlan Maguire /*
253c475c77dSAlan Maguire  * This defines a suite or grouping of tests.
254c475c77dSAlan Maguire  *
255c475c77dSAlan Maguire  * Test cases are defined as belonging to the suite by adding them to
256c475c77dSAlan Maguire  * `kunit_cases`.
257c475c77dSAlan Maguire  *
258c475c77dSAlan Maguire  * Often it is desirable to run some function which will set up things which
259c475c77dSAlan Maguire  * will be used by every test; this is accomplished with an `init` function
260c475c77dSAlan Maguire  * which runs before each test case is invoked. Similarly, an `exit` function
261c475c77dSAlan Maguire  * may be specified which runs after every test case and can be used to for
262c475c77dSAlan Maguire  * cleanup. For clarity, running tests in a test suite would behave as follows:
263c475c77dSAlan Maguire  *
2641cdba21dSDaniel Latypov  * suite.suite_init(suite);
265c475c77dSAlan Maguire  * suite.init(test);
266c475c77dSAlan Maguire  * suite.test_case[0](test);
267c475c77dSAlan Maguire  * suite.exit(test);
268c475c77dSAlan Maguire  * suite.init(test);
269c475c77dSAlan Maguire  * suite.test_case[1](test);
270c475c77dSAlan Maguire  * suite.exit(test);
2711cdba21dSDaniel Latypov  * suite.suite_exit(suite);
272c475c77dSAlan Maguire  * ...;
273c475c77dSAlan Maguire  */
274c475c77dSAlan Maguire static struct kunit_suite example_test_suite = {
275c475c77dSAlan Maguire 	.name = "example",
276c475c77dSAlan Maguire 	.init = example_test_init,
277a5ce66adSDavid Gow 	.exit = example_test_exit,
2781cdba21dSDaniel Latypov 	.suite_init = example_test_init_suite,
279a5ce66adSDavid Gow 	.suite_exit = example_test_exit_suite,
280c475c77dSAlan Maguire 	.test_cases = example_test_cases,
281c475c77dSAlan Maguire };
282c475c77dSAlan Maguire 
283c475c77dSAlan Maguire /*
284c475c77dSAlan Maguire  * This registers the above test suite telling KUnit that this is a suite of
285c475c77dSAlan Maguire  * tests that need to be run.
286c475c77dSAlan Maguire  */
287c475c77dSAlan Maguire kunit_test_suites(&example_test_suite);
288c475c77dSAlan Maguire 
289c475c77dSAlan Maguire MODULE_LICENSE("GPL v2");
290