€• Œsphinx.addnodes”Œdocument”“”)�”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)�”}”(hhh]”(hŒ pending_xref”“”)�”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…”�”}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ)/translations/zh_CN/dev-tools/kunit/usage”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)�”}”(hhh]”hŒChinese (Traditional)”…”�”}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ)/translations/zh_TW/dev-tools/kunit/usage”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒItalian”…”�”}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ)/translations/it_IT/dev-tools/kunit/usage”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒJapanese”…”�”}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ)/translations/ja_JP/dev-tools/kunit/usage”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒKorean”…”�”}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ)/translations/ko_KR/dev-tools/kunit/usage”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒPortuguese (Brazilian)”…”�”}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ)/translations/pt_BR/dev-tools/kunit/usage”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒSpanish”…”�”}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ)/translations/sp_SP/dev-tools/kunit/usage”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒcomment”“”)�”}”(hŒ SPDX-License-Identifier: GPL-2.0”h]”hŒ SPDX-License-Identifier: GPL-2.0”…”�”}”hh·sbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1hµhhh²hh³ŒC/var/lib/git/docbuild/linux/Documentation/dev-tools/kunit/usage.rst”h´KubhŒsection”“”)�”}”(hhh]”(hŒtitle”“”)�”}”(hŒ Writing Tests”h]”hŒ Writing Tests”…”�”}”(hhÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhÊh²hh³hÇh´KubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ Test Cases”h]”hŒ Test Cases”…”�”}”(hhàh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhÝh²hh³hÇh´KubhŒ paragraph”“”)�”}”(hŒáThe fundamental unit in KUnit is the test case. A test case is a function with the signature ``void (*)(struct kunit *test)``. It calls the function under test and then sets *expectations* for what should happen. For example:”h]”(hŒ]The fundamental unit in KUnit is the test case. A test case is a function with the signature ”…”�”}”(hhðh²hh³Nh´NubhŒliteral”“”)�”}”(hŒ ``void (*)(struct kunit *test)``”h]”hŒvoid (*)(struct kunit *test)”…”�”}”(hhúh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhhðubhŒ1. It calls the function under test and then sets ”…”�”}”(hhðh²hh³Nh´NubhŒemphasis”“”)�”}”(hŒ*expectations*”h]”hŒ expectations”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hhðubhŒ% for what should happen. For example:”…”�”}”(hhðh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K hhÝh²hubhŒ literal_block”“”)�”}”(hŒ™void example_test_success(struct kunit *test) { } void example_test_failure(struct kunit *test) { KUNIT_FAIL(test, "This test never passes."); }”h]”hŒ™void example_test_success(struct kunit *test) { } void example_test_failure(struct kunit *test) { KUNIT_FAIL(test, "This test never passes."); }”…”�”}”hj(sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆŒforce”‰Œlanguage”Œc”Œhighlight_args”}”uh1j&h³hÇh´K hhÝh²hubhï)�”}”(hXAIn the above example, ``example_test_success`` always passes because it does nothing; no expectations are set, and therefore all expectations pass. On the other hand ``example_test_failure`` always fails because it calls ``KUNIT_FAIL``, which is a special expectation that logs a message and causes the test case to fail.”h]”(hŒIn the above example, ”…”�”}”(hj;h²hh³Nh´Nubhù)�”}”(hŒ``example_test_success``”h]”hŒexample_test_success”…”�”}”(hjCh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj;ubhŒx always passes because it does nothing; no expectations are set, and therefore all expectations pass. On the other hand ”…”�”}”(hj;h²hh³Nh´Nubhù)�”}”(hŒ``example_test_failure``”h]”hŒexample_test_failure”…”�”}”(hjUh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj;ubhŒ always fails because it calls ”…”�”}”(hj;h²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_FAIL``”h]”hŒ KUNIT_FAIL”…”�”}”(hjgh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj;ubhŒV, which is a special expectation that logs a message and causes the test case to fail.”…”�”}”(hj;h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KhhÝh²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ Expectations”h]”hŒ Expectations”…”�”}”(hj‚h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjh²hh³hÇh´Kubhï)�”}”(hXIAn *expectation* specifies that we expect a piece of code to do something in a test. An expectation is called like a function. A test is made by setting expectations about the behavior of a piece of code under test. When one or more expectations fail, the test case fails and information about the failure is logged. For example:”h]”(hŒAn ”…”�”}”(hj�h²hh³Nh´Nubj )�”}”(hŒ *expectation*”h]”hŒ expectation”…”�”}”(hj˜h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hj�ubhX9 specifies that we expect a piece of code to do something in a test. An expectation is called like a function. A test is made by setting expectations about the behavior of a piece of code under test. When one or more expectations fail, the test case fails and information about the failure is logged. For example:”…”�”}”(hj�h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K hjh²hubj')�”}”(hŒ…void add_test_basic(struct kunit *test) { KUNIT_EXPECT_EQ(test, 1, add(1, 0)); KUNIT_EXPECT_EQ(test, 2, add(1, 1)); }”h]”hŒ…void add_test_basic(struct kunit *test) { KUNIT_EXPECT_EQ(test, 1, add(1, 0)); KUNIT_EXPECT_EQ(test, 2, add(1, 1)); }”…”�”}”hj°sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´K&hjh²hubhï)�”}”(hXýIn the above example, ``add_test_basic`` makes a number of assertions about the behavior of a function called ``add``. The first parameter is always of type ``struct kunit *``, which contains information about the current test context. The second parameter, in this case, is what the value is expected to be. The last value is what the value actually is. If ``add`` passes all of these expectations, the test case, ``add_test_basic`` will pass; if any one of these expectations fails, the test case will fail.”h]”(hŒIn the above example, ”…”�”}”(hj¿h²hh³Nh´Nubhù)�”}”(hŒ``add_test_basic``”h]”hŒadd_test_basic”…”�”}”(hjÇh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj¿ubhŒF makes a number of assertions about the behavior of a function called ”…”�”}”(hj¿h²hh³Nh´Nubhù)�”}”(hŒ``add``”h]”hŒadd”…”�”}”(hjÙh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj¿ubhŒ(. The first parameter is always of type ”…”�”}”(hj¿h²hh³Nh´Nubhù)�”}”(hŒ``struct kunit *``”h]”hŒstruct kunit *”…”�”}”(hjëh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj¿ubhŒ·, which contains information about the current test context. The second parameter, in this case, is what the value is expected to be. The last value is what the value actually is. If ”…”�”}”(hj¿h²hh³Nh´Nubhù)�”}”(hŒ``add``”h]”hŒadd”…”�”}”(hjýh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj¿ubhŒ2 passes all of these expectations, the test case, ”…”�”}”(hj¿h²hh³Nh´Nubhù)�”}”(hŒ``add_test_basic``”h]”hŒadd_test_basic”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj¿ubhŒL will pass; if any one of these expectations fails, the test case will fail.”…”�”}”(hj¿h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K.hjh²hubhï)�”}”(hŒëA test case *fails* when any expectation is violated; however, the test will continue to run, and try other expectations until the test case ends or is otherwise terminated. This is as opposed to *assertions* which are discussed later.”h]”(hŒ A test case ”…”�”}”(hj'h²hh³Nh´Nubj )�”}”(hŒ*fails*”h]”hŒfails”…”�”}”(hj/h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hj'ubhŒ± when any expectation is violated; however, the test will continue to run, and try other expectations until the test case ends or is otherwise terminated. This is as opposed to ”…”�”}”(hj'h²hh³Nh´Nubj )�”}”(hŒ *assertions*”h]”hŒ assertions”…”�”}”(hjAh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hj'ubhŒ which are discussed later.”…”�”}”(hj'h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K6hjh²hubhï)�”}”(hŒWTo learn about more KUnit expectations, see Documentation/dev-tools/kunit/api/test.rst.”h]”hŒWTo learn about more KUnit expectations, see Documentation/dev-tools/kunit/api/test.rst.”…”�”}”(hjYh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K;hjh²hubhŒnote”“”)�”}”(hŒYA single test case should be short, easy to understand, and focused on a single behavior.”h]”hï)�”}”(hŒYA single test case should be short, easy to understand, and focused on a single behavior.”h]”hŒYA single test case should be short, easy to understand, and focused on a single behavior.”…”�”}”(hjmh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K>hjiubah}”(h]”h ]”h"]”h$]”h&]”uh1jghjh²hh³hÇh´Nubhï)�”}”(hŒ¸For example, if we want to rigorously test the ``add`` function above, create additional tests cases which would test each property that an ``add`` function should have as shown below:”h]”(hŒ/For example, if we want to rigorously test the ”…”�”}”(hj�h²hh³Nh´Nubhù)�”}”(hŒ``add``”h]”hŒadd”…”�”}”(hj‰h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj�ubhŒV function above, create additional tests cases which would test each property that an ”…”�”}”(hj�h²hh³Nh´Nubhù)�”}”(hŒ``add``”h]”hŒadd”…”�”}”(hj›h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj�ubhŒ% function should have as shown below:”…”�”}”(hj�h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KAhjh²hubj')�”}”(hXêvoid add_test_basic(struct kunit *test) { KUNIT_EXPECT_EQ(test, 1, add(1, 0)); KUNIT_EXPECT_EQ(test, 2, add(1, 1)); } void add_test_negative(struct kunit *test) { KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); } void add_test_max(struct kunit *test) { KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); } void add_test_overflow(struct kunit *test) { KUNIT_EXPECT_EQ(test, INT_MIN, add(INT_MAX, 1)); }”h]”hXêvoid add_test_basic(struct kunit *test) { KUNIT_EXPECT_EQ(test, 1, add(1, 0)); KUNIT_EXPECT_EQ(test, 2, add(1, 1)); } void add_test_negative(struct kunit *test) { KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); } void add_test_max(struct kunit *test) { KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); } void add_test_overflow(struct kunit *test) { KUNIT_EXPECT_EQ(test, INT_MIN, add(INT_MAX, 1)); }”…”�”}”hj³sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´KEhjh²hubeh}”(h]”Œ expectations”ah ]”h"]”Œ expectations”ah$]”h&]”uh1hÈhhÝh²hh³hÇh´KubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ Assertions”h]”hŒ Assertions”…”�”}”(hjÍh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjÊh²hh³hÇh´K^ubhï)�”}”(hŒ“An assertion is like an expectation, except that the assertion immediately terminates the test case if the condition is not satisfied. For example:”h]”hŒ“An assertion is like an expectation, except that the assertion immediately terminates the test case if the condition is not satisfied. For example:”…”�”}”(hjÛh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K`hjÊh²hubj')�”}”(hXÊstatic void test_sort(struct kunit *test) { int *a, i, r = 1; a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a); for (i = 0; i < TEST_LEN; i++) { r = (r * 725861) % 6599; a[i] = r; } sort(a, TEST_LEN, sizeof(*a), cmpint, NULL); for (i = 0; i < TEST_LEN-1; i++) KUNIT_EXPECT_LE(test, a[i], a[i + 1]); }”h]”hXÊstatic void test_sort(struct kunit *test) { int *a, i, r = 1; a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a); for (i = 0; i < TEST_LEN; i++) { r = (r * 725861) % 6599; a[i] = r; } sort(a, TEST_LEN, sizeof(*a), cmpint, NULL); for (i = 0; i < TEST_LEN-1; i++) KUNIT_EXPECT_LE(test, a[i], a[i + 1]); }”…”�”}”hjésbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´KchjÊh²hubhï)�”}”(hŒ¸In this example, we need to be able to allocate an array to test the ``sort()`` function. So we use ``KUNIT_ASSERT_NOT_ERR_OR_NULL()`` to abort the test if there's an allocation error.”h]”(hŒEIn this example, we need to be able to allocate an array to test the ”…”�”}”(hjøh²hh³Nh´Nubhù)�”}”(hŒ ``sort()``”h]”hŒsort()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjøubhŒ function. So we use ”…”�”}”(hjøh²hh³Nh´Nubhù)�”}”(hŒ"``KUNIT_ASSERT_NOT_ERR_OR_NULL()``”h]”hŒKUNIT_ASSERT_NOT_ERR_OR_NULL()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjøubhŒ4 to abort the test if there’s an allocation error.”…”�”}”(hjøh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KshjÊh²hubjh)�”}”(hŒ×In other test frameworks, ``ASSERT`` macros are often implemented by calling ``return`` so they only work from the test function. In KUnit, we stop the current kthread on failure, so you can call them from anywhere.”h]”hï)�”}”(hŒ×In other test frameworks, ``ASSERT`` macros are often implemented by calling ``return`` so they only work from the test function. In KUnit, we stop the current kthread on failure, so you can call them from anywhere.”h]”(hŒIn other test frameworks, ”…”�”}”(hj.h²hh³Nh´Nubhù)�”}”(hŒ ``ASSERT``”h]”hŒASSERT”…”�”}”(hj6h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj.ubhŒ) macros are often implemented by calling ”…”�”}”(hj.h²hh³Nh´Nubhù)�”}”(hŒ ``return``”h]”hŒreturn”…”�”}”(hjHh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj.ubhŒ€ so they only work from the test function. In KUnit, we stop the current kthread on failure, so you can call them from anywhere.”…”�”}”(hj.h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Kxhj*ubah}”(h]”h ]”h"]”h$]”h&]”uh1jghjÊh²hh³hÇh´Nubjh)�”}”(hX&Warning: There is an exception to the above rule. You shouldn't use assertions in the suite's exit() function, or in the free function for a resource. These run when a test is shutting down, and an assertion here prevents further cleanup code from running, potentially leading to a memory leak.”h]”hï)�”}”(hX&Warning: There is an exception to the above rule. You shouldn't use assertions in the suite's exit() function, or in the free function for a resource. These run when a test is shutting down, and an assertion here prevents further cleanup code from running, potentially leading to a memory leak.”h]”hX*Warning: There is an exception to the above rule. You shouldn’t use assertions in the suite’s exit() function, or in the free function for a resource. These run when a test is shutting down, and an assertion here prevents further cleanup code from running, potentially leading to a memory leak.”…”�”}”(hjjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K}hjfubah}”(h]”h ]”h"]”h$]”h&]”uh1jghjÊh²hh³hÇh´Nubeh}”(h]”Œ assertions”ah ]”h"]”Œ assertions”ah$]”h&]”uh1hÈhhÝh²hh³hÇh´K^ubeh}”(h]”Œ test-cases”ah ]”h"]”Œ test cases”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒCustomizing error messages”h]”hŒCustomizing error messages”…”�”}”(hj‘h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjŽh²hh³hÇh´Kƒubhï)�”}”(hŒÈEach of the ``KUNIT_EXPECT`` and ``KUNIT_ASSERT`` macros have a ``_MSG`` variant. These take a format string and arguments to provide additional context to the automatically generated error messages.”h]”(hŒ Each of the ”…”�”}”(hjŸh²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_EXPECT``”h]”hŒ KUNIT_EXPECT”…”�”}”(hj§h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjŸubhŒ and ”…”�”}”(hjŸh²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_ASSERT``”h]”hŒ KUNIT_ASSERT”…”�”}”(hj¹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjŸubhŒ macros have a ”…”�”}”(hjŸh²hh³Nh´Nubhù)�”}”(hŒ``_MSG``”h]”hŒ_MSG”…”�”}”(hjËh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjŸubhŒ€ variant. These take a format string and arguments to provide additional context to the automatically generated error messages.”…”�”}”(hjŸh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K…hjŽh²hubj')�”}”(hXchar some_str[41]; generate_sha1_hex_string(some_str); /* Before. Not easy to tell why the test failed. */ KUNIT_EXPECT_EQ(test, strlen(some_str), 40); /* After. Now we see the offending string. */ KUNIT_EXPECT_EQ_MSG(test, strlen(some_str), 40, "some_str='%s'", some_str);”h]”hXchar some_str[41]; generate_sha1_hex_string(some_str); /* Before. Not easy to tell why the test failed. */ KUNIT_EXPECT_EQ(test, strlen(some_str), 40); /* After. Now we see the offending string. */ KUNIT_EXPECT_EQ_MSG(test, strlen(some_str), 40, "some_str='%s'", some_str);”…”�”}”hjãsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´K‰hjŽh²hubhï)�”}”(hŒ_Alternatively, one can take full control over the error message by using ``KUNIT_FAIL()``, e.g.”h]”(hŒIAlternatively, one can take full control over the error message by using ”…”�”}”(hjòh²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_FAIL()``”h]”hŒ KUNIT_FAIL()”…”�”}”(hjúh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjòubhŒ, e.g.”…”�”}”(hjòh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K”hjŽh²hubj')�”}”(hŒÌ/* Before */ KUNIT_EXPECT_EQ(test, some_setup_function(), 0); /* After: full control over the failure message. */ if (some_setup_function()) KUNIT_FAIL(test, "Failed to setup thing for testing");”h]”hŒÌ/* Before */ KUNIT_EXPECT_EQ(test, some_setup_function(), 0); /* After: full control over the failure message. */ if (some_setup_function()) KUNIT_FAIL(test, "Failed to setup thing for testing");”…”�”}”hjsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´K—hjŽh²hubeh}”(h]”Œcustomizing-error-messages”ah ]”h"]”Œcustomizing error messages”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KƒubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒSuppressing warning backtraces”h]”hŒSuppressing warning backtraces”…”�”}”(hj,h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj)h²hh³hÇh´K¡ubhï)�”}”(hŒàSome unit tests trigger warning backtraces either intentionally or as a side effect. Such backtraces are normally undesirable since they distract from the actual test and may result in the impression that there is a problem.”h]”hŒàSome unit tests trigger warning backtraces either intentionally or as a side effect. Such backtraces are normally undesirable since they distract from the actual test and may result in the impression that there is a problem.”…”�”}”(hj:h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K£hj)h²hubhï)�”}”(hŒûBacktraces can be suppressed with **task-scoped suppression**: while suppression is active on the current task, the backtrace and stack dump from ``WARN*()``, ``WARN_ON*()``, and related macros on that task are suppressed. Two API forms are available.”h]”(hŒ"Backtraces can be suppressed with ”…”�”}”(hjHh²hh³Nh´NubhŒstrong”“”)�”}”(hŒ**task-scoped suppression**”h]”hŒtask-scoped suppression”…”�”}”(hjRh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jPhjHubhŒU: while suppression is active on the current task, the backtrace and stack dump from ”…”�”}”(hjHh²hh³Nh´Nubhù)�”}”(hŒ ``WARN*()``”h]”hŒWARN*()”…”�”}”(hjdh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjHubhŒ, ”…”�”}”(hjHh²hh³Nh´Nubhù)�”}”(hŒ``WARN_ON*()``”h]”hŒ WARN_ON*()”…”�”}”(hjvh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjHubhŒN, and related macros on that task are suppressed. Two API forms are available.”…”�”}”(hjHh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K§hj)h²hubhŒ bullet_list”“”)�”}”(hhh]”hŒ list_item”“”)�”}”(hŒwScoped suppression is the simplest form. Wrap the code that triggers warnings in a ``kunit_warning_suppress()`` block: ”h]”hï)�”}”(hŒvScoped suppression is the simplest form. Wrap the code that triggers warnings in a ``kunit_warning_suppress()`` block:”h]”(hŒSScoped suppression is the simplest form. Wrap the code that triggers warnings in a ”…”�”}”(hj™h²hh³Nh´Nubhù)�”}”(hŒ``kunit_warning_suppress()``”h]”hŒkunit_warning_suppress()”…”�”}”(hj¡h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj™ubhŒ block:”…”�”}”(hj™h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K¬hj•ubah}”(h]”h ]”h"]”h$]”h&]”uh1j“hj�h²hh³hÇh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œbullet”Œ-”uh1jŽh³hÇh´K¬hj)h²hubj')�”}”(hŒÃstatic void some_test(struct kunit *test) { kunit_warning_suppress(test) { trigger_backtrace(); KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1); } }”h]”hŒÃstatic void some_test(struct kunit *test) { kunit_warning_suppress(test) { trigger_backtrace(); KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1); } }”…”�”}”hjÇsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´K¯hj)h²hubjh)�”}”(hŒsThe warning count must be checked inside the block; the suppression handle is not accessible after the block exits.”h]”hï)�”}”(hŒsThe warning count must be checked inside the block; the suppression handle is not accessible after the block exits.”h]”hŒsThe warning count must be checked inside the block; the suppression handle is not accessible after the block exits.”…”�”}”(hjÚh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KºhjÖubah}”(h]”h ]”h"]”h$]”h&]”uh1jghj)h²hh³hÇh´Nubj�)�”}”(hhh]”j”)�”}”(hŒ…Direct functions return an explicit handle pointer. Use them when the handle needs to be retained or passed across helper functions: ”h]”hï)�”}”(hŒ„Direct functions return an explicit handle pointer. Use them when the handle needs to be retained or passed across helper functions:”h]”hŒ„Direct functions return an explicit handle pointer. Use them when the handle needs to be retained or passed across helper functions:”…”�”}”(hjõh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K½hjñubah}”(h]”h ]”h"]”h$]”h&]”uh1j“hjîh²hh³hÇh´Nubah}”(h]”h ]”h"]”h$]”h&]”jÅjÆuh1jŽh³hÇh´K½hj)h²hubj')�”}”(hXstatic void some_test(struct kunit *test) { struct kunit_suppressed_warning *w; w = kunit_start_suppress_warning(test); trigger_backtrace(); kunit_end_suppress_warning(test, w); KUNIT_EXPECT_EQ(test, kunit_suppressed_warning_count(w), 1); }”h]”hXstatic void some_test(struct kunit *test) { struct kunit_suppressed_warning *w; w = kunit_start_suppress_warning(test); trigger_backtrace(); kunit_end_suppress_warning(test, w); KUNIT_EXPECT_EQ(test, kunit_suppressed_warning_count(w), 1); }”…”�”}”hjsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´KÀhj)h²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ Test Suites”h]”hŒ Test Suites”…”�”}”(hj!h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjh²hh³hÇh´KÎubhï)�”}”(hXŸWe need many test cases covering all the unit's behaviors. It is common to have many similar tests. In order to reduce duplication in these closely related tests, most unit testing frameworks (including KUnit) provide the concept of a *test suite*. A test suite is a collection of test cases for a unit of code with optional setup and teardown functions that run before/after the whole suite and/or every test case.”h]”(hŒíWe need many test cases covering all the unit’s behaviors. It is common to have many similar tests. In order to reduce duplication in these closely related tests, most unit testing frameworks (including KUnit) provide the concept of a ”…”�”}”(hj/h²hh³Nh´Nubj )�”}”(hŒ *test suite*”h]”hŒ test suite”…”�”}”(hj7h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hj/ubhŒ¨. A test suite is a collection of test cases for a unit of code with optional setup and teardown functions that run before/after the whole suite and/or every test case.”…”�”}”(hj/h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KÐhjh²hubjh)�”}”(hŒ@A test case will only run if it is associated with a test suite.”h]”hï)�”}”(hjQh]”hŒ@A test case will only run if it is associated with a test suite.”…”�”}”(hjSh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KØhjOubah}”(h]”h ]”h"]”h$]”h&]”uh1jghjh²hh³hÇh´Nubhï)�”}”(hŒ For example:”h]”hŒ For example:”…”�”}”(hjfh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KÚhjh²hubj')�”}”(hXëstatic struct kunit_case example_test_cases[] = { KUNIT_CASE(example_test_foo), KUNIT_CASE(example_test_bar), KUNIT_CASE(example_test_baz), {} }; static struct kunit_suite example_test_suite = { .name = "example", .init = example_test_init, .exit = example_test_exit, .suite_init = example_suite_init, .suite_exit = example_suite_exit, .test_cases = example_test_cases, }; kunit_test_suite(example_test_suite);”h]”hXëstatic struct kunit_case example_test_cases[] = { KUNIT_CASE(example_test_foo), KUNIT_CASE(example_test_bar), KUNIT_CASE(example_test_baz), {} }; static struct kunit_suite example_test_suite = { .name = "example", .init = example_test_init, .exit = example_test_exit, .suite_init = example_suite_init, .suite_exit = example_suite_exit, .test_cases = example_test_cases, }; kunit_test_suite(example_test_suite);”…”�”}”hjtsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´KÜhjh²hubhï)�”}”(hXãIn the above example, the test suite ``example_test_suite`` would first run ``example_suite_init``, then run the test cases ``example_test_foo``, ``example_test_bar``, and ``example_test_baz``. Each would have ``example_test_init`` called immediately before it and ``example_test_exit`` called immediately after it. Finally, ``example_suite_exit`` would be called after everything else. ``kunit_test_suite(example_test_suite)`` registers the test suite with the KUnit test framework.”h]”(hŒ%In the above example, the test suite ”…”�”}”(hjƒh²hh³Nh´Nubhù)�”}”(hŒ``example_test_suite``”h]”hŒexample_test_suite”…”�”}”(hj‹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjƒubhŒ would first run ”…”�”}”(hjƒh²hh³Nh´Nubhù)�”}”(hŒ``example_suite_init``”h]”hŒexample_suite_init”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjƒubhŒ, then run the test cases ”…”�”}”(hjƒh²hh³Nh´Nubhù)�”}”(hŒ``example_test_foo``”h]”hŒexample_test_foo”…”�”}”(hj¯h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjƒubhŒ, ”…”�”}”(hjƒh²hh³Nh´Nubhù)�”}”(hŒ``example_test_bar``”h]”hŒexample_test_bar”…”�”}”(hjÁh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjƒubhŒ, and ”…”�”}”(hjƒh²hh³Nh´Nubhù)�”}”(hŒ``example_test_baz``”h]”hŒexample_test_baz”…”�”}”(hjÓh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjƒubhŒ. Each would have ”…”�”}”(hjƒh²hh³Nh´Nubhù)�”}”(hŒ``example_test_init``”h]”hŒexample_test_init”…”�”}”(hjåh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjƒubhŒ" called immediately before it and ”…”�”}”(hjƒh²hh³Nh´Nubhù)�”}”(hŒ``example_test_exit``”h]”hŒexample_test_exit”…”�”}”(hj÷h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjƒubhŒ' called immediately after it. Finally, ”…”�”}”(hjƒh²hh³Nh´Nubhù)�”}”(hŒ``example_suite_exit``”h]”hŒexample_suite_exit”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjƒubhŒ( would be called after everything else. ”…”�”}”(hjƒh²hh³Nh´Nubhù)�”}”(hŒ(``kunit_test_suite(example_test_suite)``”h]”hŒ$kunit_test_suite(example_test_suite)”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjƒubhŒ8 registers the test suite with the KUnit test framework.”…”�”}”(hjƒh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Kïhjh²hubjh)�”}”(hŒèThe ``exit`` and ``suite_exit`` functions will run even if ``init`` or ``suite_init`` fail. Make sure that they can handle any inconsistent state which may result from ``init`` or ``suite_init`` encountering errors or exiting early.”h]”hï)�”}”(hŒèThe ``exit`` and ``suite_exit`` functions will run even if ``init`` or ``suite_init`` fail. Make sure that they can handle any inconsistent state which may result from ``init`` or ``suite_init`` encountering errors or exiting early.”h]”(hŒThe ”…”�”}”(hj7h²hh³Nh´Nubhù)�”}”(hŒ``exit``”h]”hŒexit”…”�”}”(hj?h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj7ubhŒ and ”…”�”}”(hj7h²hh³Nh´Nubhù)�”}”(hŒ``suite_exit``”h]”hŒ suite_exit”…”�”}”(hjQh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj7ubhŒ functions will run even if ”…”�”}”(hj7h²hh³Nh´Nubhù)�”}”(hŒ``init``”h]”hŒinit”…”�”}”(hjch²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj7ubhŒ or ”…”�”}”(hj7h²hh³Nh´Nubhù)�”}”(hŒ``suite_init``”h]”hŒ suite_init”…”�”}”(hjuh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj7ubhŒS fail. Make sure that they can handle any inconsistent state which may result from ”…”�”}”(hj7h²hh³Nh´Nubhù)�”}”(hŒ``init``”h]”hŒinit”…”�”}”(hj‡h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj7ubhŒ or ”…”�”}”(hj7h²hh³Nh´Nubhù)�”}”(hŒ``suite_init``”h]”hŒ suite_init”…”�”}”(hj™h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj7ubhŒ& encountering errors or exiting early.”…”�”}”(hj7h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Køhj3ubah}”(h]”h ]”h"]”h$]”h&]”uh1jghjh²hh³hÇh´Nubhï)�”}”(hŒù``kunit_test_suite(...)`` is a macro which tells the linker to put the specified test suite in a special linker section so that it can be run by KUnit either after ``late_init``, or when the test module is loaded (if the test was built as a module).”h]”(hù)�”}”(hŒ``kunit_test_suite(...)``”h]”hŒkunit_test_suite(...)”…”�”}”(hj»h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj·ubhŒ‹ is a macro which tells the linker to put the specified test suite in a special linker section so that it can be run by KUnit either after ”…”�”}”(hj·h²hh³Nh´Nubhù)�”}”(hŒ ``late_init``”h]”hŒ late_init”…”�”}”(hjÍh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj·ubhŒH, or when the test module is loaded (if the test was built as a module).”…”�”}”(hj·h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Kýhjh²hubhï)�”}”(hŒEFor more information, see Documentation/dev-tools/kunit/api/test.rst.”h]”hŒEFor more information, see Documentation/dev-tools/kunit/api/test.rst.”…”�”}”(hjåh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mhjh²hubhŒtarget”“”)�”}”(hŒ.. _kunit-on-non-uml:”h]”h}”(h]”h ]”h"]”h$]”h&]”Œrefid”Œkunit-on-non-uml”uh1jóh´Mhjh²hh³hÇubeh}”(h]”Œ test-suites”ah ]”h"]”Œ test suites”ah$]”h&]”uh1hÈhj)h²hh³hÇh´KÎubeh}”(h]”Œsuppressing-warning-backtraces”ah ]”h"]”Œsuppressing warning backtraces”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´K¡ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ%Writing Tests For Other Architectures”h]”hŒ%Writing Tests For Other Architectures”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjh²hh³hÇh´Mubhï)�”}”(hŒùIt is better to write tests that run on UML to tests that only run under a particular architecture. It is better to write tests that run under QEMU or another easy to obtain (and monetarily free) software environment to a specific piece of hardware.”h]”hŒùIt is better to write tests that run on UML to tests that only run under a particular architecture. It is better to write tests that run under QEMU or another easy to obtain (and monetarily free) software environment to a specific piece of hardware.”…”�”}”(hj"h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M hjh²hubhï)�”}”(hXÀNevertheless, there are still valid reasons to write a test that is architecture or hardware specific. For example, we might want to test code that really belongs in ``arch/some-arch/*``. Even so, try to write the test so that it does not depend on physical hardware. Some of our test cases may not need hardware, only few tests actually require the hardware to test it. When hardware is not available, instead of disabling tests, we can skip them.”h]”(hŒ¦Nevertheless, there are still valid reasons to write a test that is architecture or hardware specific. For example, we might want to test code that really belongs in ”…”�”}”(hj0h²hh³Nh´Nubhù)�”}”(hŒ``arch/some-arch/*``”h]”hŒarch/some-arch/*”…”�”}”(hj8h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj0ubhX. Even so, try to write the test so that it does not depend on physical hardware. Some of our test cases may not need hardware, only few tests actually require the hardware to test it. When hardware is not available, instead of disabling tests, we can skip them.”…”�”}”(hj0h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mhjh²hubhï)�”}”(hŒ¥Now that we have narrowed down exactly what bits are hardware specific, the actual procedure for writing and running the tests is same as writing normal KUnit tests.”h]”hŒ¥Now that we have narrowed down exactly what bits are hardware specific, the actual procedure for writing and running the tests is same as writing normal KUnit tests.”…”�”}”(hjPh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mhjh²hubhŒ important”“”)�”}”(hŒvWe may have to reset hardware state. If this is not possible, we may only be able to run one test case per invocation.”h]”hï)�”}”(hŒvWe may have to reset hardware state. If this is not possible, we may only be able to run one test case per invocation.”h]”hŒvWe may have to reset hardware state. If this is not possible, we may only be able to run one test case per invocation.”…”�”}”(hjdh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mhj`ubah}”(h]”h ]”h"]”h$]”h&]”uh1j^hjh²hh³hÇh´Nubh¶)�”}”(hŒ`TODO(brendanhiggins@google.com): Add an actual example of an architecture- dependent KUnit test.”h]”hŒ`TODO(brendanhiggins@google.com): Add an actual example of an architecture- dependent KUnit test.”…”�”}”hjxsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1hµhjh²hh³hÇh´Mubeh}”(h]”(Œ%writing-tests-for-other-architectures”jeh ]”h"]”(Œ%writing tests for other architectures”Œkunit-on-non-uml”eh$]”h&]”uh1hÈhhÊh²hh³hÇh´MŒexpect_referenced_by_name”}”jŒjõsŒexpect_referenced_by_id”}”jjõsubeh}”(h]”Œ writing-tests”ah ]”h"]”Œ writing tests”ah$]”h&]”uh1hÈhhh²hh³hÇh´KubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒCommon Patterns”h]”hŒCommon Patterns”…”�”}”(hjžh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj›h²hh³hÇh´M!ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒIsolating Behavior”h]”hŒIsolating Behavior”…”�”}”(hj¯h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj¬h²hh³hÇh´M$ubhï)�”}”(hXýUnit testing limits the amount of code under test to a single unit. It controls what code gets run when the unit under test calls a function. Where a function is exposed as part of an API such that the definition of that function can be changed without affecting the rest of the code base. In the kernel, this comes from two constructs: classes, which are structs that contain function pointers provided by the implementer, and architecture-specific functions, which have definitions selected at compile time.”h]”hXýUnit testing limits the amount of code under test to a single unit. It controls what code gets run when the unit under test calls a function. Where a function is exposed as part of an API such that the definition of that function can be changed without affecting the rest of the code base. In the kernel, this comes from two constructs: classes, which are structs that contain function pointers provided by the implementer, and architecture-specific functions, which have definitions selected at compile time.”…”�”}”(hj½h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M&hj¬h²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒClasses”h]”hŒClasses”…”�”}”(hjÎh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjËh²hh³hÇh´M/ubhï)�”}”(hX[Classes are not a construct that is built into the C programming language; however, it is an easily derived concept. Accordingly, in most cases, every project that does not use a standardized object oriented library (like GNOME's GObject) has their own slightly different way of doing object oriented programming; the Linux kernel is no exception.”h]”hX]Classes are not a construct that is built into the C programming language; however, it is an easily derived concept. Accordingly, in most cases, every project that does not use a standardized object oriented library (like GNOME’s GObject) has their own slightly different way of doing object oriented programming; the Linux kernel is no exception.”…”�”}”(hjÜh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M1hjËh²hubhï)�”}”(hX^The central concept in kernel object oriented programming is the class. In the kernel, a *class* is a struct that contains function pointers. This creates a contract between *implementers* and *users* since it forces them to use the same function signature without having to call the function directly. To be a class, the function pointers must specify that a pointer to the class, known as a *class handle*, be one of the parameters. Thus the member functions (also known as *methods*) have access to member variables (also known as *fields*) allowing the same implementation to have multiple *instances*.”h]”(hŒYThe central concept in kernel object oriented programming is the class. In the kernel, a ”…”�”}”(hjêh²hh³Nh´Nubj )�”}”(hŒ*class*”h]”hŒclass”…”�”}”(hjòh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjêubhŒN is a struct that contains function pointers. This creates a contract between ”…”�”}”(hjêh²hh³Nh´Nubj )�”}”(hŒ*implementers*”h]”hŒ implementers”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjêubhŒ and ”…”�”}”(hjêh²hh³Nh´Nubj )�”}”(hŒ*users*”h]”hŒusers”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjêubhŒÁ since it forces them to use the same function signature without having to call the function directly. To be a class, the function pointers must specify that a pointer to the class, known as a ”…”�”}”(hjêh²hh³Nh´Nubj )�”}”(hŒ*class handle*”h]”hŒ class handle”…”�”}”(hj(h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjêubhŒE, be one of the parameters. Thus the member functions (also known as ”…”�”}”(hjêh²hh³Nh´Nubj )�”}”(hŒ *methods*”h]”hŒmethods”…”�”}”(hj:h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjêubhŒ1) have access to member variables (also known as ”…”�”}”(hjêh²hh³Nh´Nubj )�”}”(hŒ*fields*”h]”hŒfields”…”�”}”(hjLh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjêubhŒ4) allowing the same implementation to have multiple ”…”�”}”(hjêh²hh³Nh´Nubj )�”}”(hŒ *instances*”h]”hŒ instances”…”�”}”(hj^h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjêubhŒ.”…”�”}”(hjêh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M7hjËh²hubhï)�”}”(hXØA class can be *overridden* by *child classes* by embedding the *parent class* in the child class. Then when the child class *method* is called, the child implementation knows that the pointer passed to it is of a parent contained within the child. Thus, the child can compute the pointer to itself because the pointer to the parent is always a fixed offset from the pointer to the child. This offset is the offset of the parent contained in the child struct. For example:”h]”(hŒA class can be ”…”�”}”(hjvh²hh³Nh´Nubj )�”}”(hŒ *overridden*”h]”hŒ overridden”…”�”}”(hj~h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjvubhŒ by ”…”�”}”(hjvh²hh³Nh´Nubj )�”}”(hŒ*child classes*”h]”hŒ child classes”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjvubhŒ by embedding the ”…”�”}”(hjvh²hh³Nh´Nubj )�”}”(hŒ*parent class*”h]”hŒ parent class”…”�”}”(hj¢h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjvubhŒ/ in the child class. Then when the child class ”…”�”}”(hjvh²hh³Nh´Nubj )�”}”(hŒ*method*”h]”hŒmethod”…”�”}”(hj´h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjvubhXS is called, the child implementation knows that the pointer passed to it is of a parent contained within the child. Thus, the child can compute the pointer to itself because the pointer to the parent is always a fixed offset from the pointer to the child. This offset is the offset of the parent contained in the child struct. For example:”…”�”}”(hjvh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M@hjËh²hubj')�”}”(hXìstruct shape { int (*area)(struct shape *this); }; struct rectangle { struct shape parent; int length; int width; }; int rectangle_area(struct shape *this) { struct rectangle *self = container_of(this, struct rectangle, parent); return self->length * self->width; }; void rectangle_new(struct rectangle *self, int length, int width) { self->parent.area = rectangle_area; self->length = length; self->width = width; }”h]”hXìstruct shape { int (*area)(struct shape *this); }; struct rectangle { struct shape parent; int length; int width; }; int rectangle_area(struct shape *this) { struct rectangle *self = container_of(this, struct rectangle, parent); return self->length * self->width; }; void rectangle_new(struct rectangle *self, int length, int width) { self->parent.area = rectangle_area; self->length = length; self->width = width; }”…”�”}”hjÌsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´MHhjËh²hubhï)�”}”(hŒoIn this example, computing the pointer to the child from the pointer to the parent is done by ``container_of``.”h]”(hŒ^In this example, computing the pointer to the child from the pointer to the parent is done by ”…”�”}”(hjÛh²hh³Nh´Nubhù)�”}”(hŒ``container_of``”h]”hŒ container_of”…”�”}”(hjãh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÛubhŒ.”…”�”}”(hjÛh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MbhjËh²hubeh}”(h]”Œclasses”ah ]”h"]”Œclasses”ah$]”h&]”uh1hÈhj¬h²hh³hÇh´M/ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒFaking Classes”h]”hŒFaking Classes”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj h²hh³hÇh´Mfubhï)�”}”(hŒÃIn order to unit test a piece of code that calls a method in a class, the behavior of the method must be controllable, otherwise the test ceases to be a unit test and becomes an integration test.”h]”hŒÃIn order to unit test a piece of code that calls a method in a class, the behavior of the method must be controllable, otherwise the test ceases to be a unit test and becomes an integration test.”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mhhj h²hubhï)�”}”(hXtA fake class implements a piece of code that is different than what runs in a production instance, but behaves identical from the standpoint of the callers. This is done to replace a dependency that is hard to deal with, or is slow. For example, implementing a fake EEPROM that stores the "contents" in an internal buffer. Assume we have a class that represents an EEPROM:”h]”hXxA fake class implements a piece of code that is different than what runs in a production instance, but behaves identical from the standpoint of the callers. This is done to replace a dependency that is hard to deal with, or is slow. For example, implementing a fake EEPROM that stores the “contentsâ€� in an internal buffer. Assume we have a class that represents an EEPROM:”…”�”}”(hj" h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mlhj h²hubj')�”}”(hŒËstruct eeprom { ssize_t (*read)(struct eeprom *this, size_t offset, char *buffer, size_t count); ssize_t (*write)(struct eeprom *this, size_t offset, const char *buffer, size_t count); };”h]”hŒËstruct eeprom { ssize_t (*read)(struct eeprom *this, size_t offset, char *buffer, size_t count); ssize_t (*write)(struct eeprom *this, size_t offset, const char *buffer, size_t count); };”…”�”}”hj0 sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´Mrhj h²hubhï)�”}”(hŒ;And we want to test code that buffers writes to the EEPROM:”h]”hŒ;And we want to test code that buffers writes to the EEPROM:”…”�”}”(hj? h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Myhj h²hubj')�”}”(hX_struct eeprom_buffer { ssize_t (*write)(struct eeprom_buffer *this, const char *buffer, size_t count); int flush(struct eeprom_buffer *this); size_t flush_count; /* Flushes when buffer exceeds flush_count. */ }; struct eeprom_buffer *new_eeprom_buffer(struct eeprom *eeprom); void destroy_eeprom_buffer(struct eeprom *eeprom);”h]”hX_struct eeprom_buffer { ssize_t (*write)(struct eeprom_buffer *this, const char *buffer, size_t count); int flush(struct eeprom_buffer *this); size_t flush_count; /* Flushes when buffer exceeds flush_count. */ }; struct eeprom_buffer *new_eeprom_buffer(struct eeprom *eeprom); void destroy_eeprom_buffer(struct eeprom *eeprom);”…”�”}”hjM sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´M{hj h²hubhï)�”}”(hŒcontents + offset, count); return count; } ssize_t fake_eeprom_write(struct eeprom *parent, size_t offset, const char *buffer, size_t count) { struct fake_eeprom *this = container_of(parent, struct fake_eeprom, parent); count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset); memcpy(this->contents + offset, buffer, count); return count; } void fake_eeprom_init(struct fake_eeprom *this) { this->parent.read = fake_eeprom_read; this->parent.write = fake_eeprom_write; memset(this->contents, 0, FAKE_EEPROM_CONTENTS_SIZE); }”h]”hXÉstruct fake_eeprom { struct eeprom parent; char contents[FAKE_EEPROM_CONTENTS_SIZE]; }; ssize_t fake_eeprom_read(struct eeprom *parent, size_t offset, char *buffer, size_t count) { struct fake_eeprom *this = container_of(parent, struct fake_eeprom, parent); count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset); memcpy(buffer, this->contents + offset, count); return count; } ssize_t fake_eeprom_write(struct eeprom *parent, size_t offset, const char *buffer, size_t count) { struct fake_eeprom *this = container_of(parent, struct fake_eeprom, parent); count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset); memcpy(this->contents + offset, buffer, count); return count; } void fake_eeprom_init(struct fake_eeprom *this) { this->parent.read = fake_eeprom_read; this->parent.write = fake_eeprom_write; memset(this->contents, 0, FAKE_EEPROM_CONTENTS_SIZE); }”…”�”}”hj| sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´Mˆhj h²hubhï)�”}”(hŒ3We can now use it to test ``struct eeprom_buffer``:”h]”(hŒWe can now use it to test ”…”�”}”(hj‹ h²hh³Nh´Nubhù)�”}”(hŒ``struct eeprom_buffer``”h]”hŒstruct eeprom_buffer”…”�”}”(hj“ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj‹ ubhŒ:”…”�”}”(hj‹ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mªhj h²hubj')�”}”(hXÓ struct eeprom_buffer_test { struct fake_eeprom *fake_eeprom; struct eeprom_buffer *eeprom_buffer; }; static void eeprom_buffer_test_does_not_write_until_flush(struct kunit *test) { struct eeprom_buffer_test *ctx = test->priv; struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; char buffer[] = {0xff}; eeprom_buffer->flush_count = SIZE_MAX; eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0); eeprom_buffer->flush(eeprom_buffer); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); } static void eeprom_buffer_test_flushes_after_flush_count_met(struct kunit *test) { struct eeprom_buffer_test *ctx = test->priv; struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; char buffer[] = {0xff}; eeprom_buffer->flush_count = 2; eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); } static void eeprom_buffer_test_flushes_increments_of_flush_count(struct kunit *test) { struct eeprom_buffer_test *ctx = test->priv; struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; char buffer[] = {0xff, 0xff}; eeprom_buffer->flush_count = 2; eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); eeprom_buffer->write(eeprom_buffer, buffer, 2); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); /* Should have only flushed the first two bytes. */ KUNIT_EXPECT_EQ(test, fake_eeprom->contents[2], 0); } static int eeprom_buffer_test_init(struct kunit *test) { struct eeprom_buffer_test *ctx; ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); ctx->fake_eeprom = kunit_kzalloc(test, sizeof(*ctx->fake_eeprom), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->fake_eeprom); fake_eeprom_init(ctx->fake_eeprom); ctx->eeprom_buffer = new_eeprom_buffer(&ctx->fake_eeprom->parent); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->eeprom_buffer); test->priv = ctx; return 0; } static void eeprom_buffer_test_exit(struct kunit *test) { struct eeprom_buffer_test *ctx = test->priv; destroy_eeprom_buffer(ctx->eeprom_buffer); }”h]”hXÓ struct eeprom_buffer_test { struct fake_eeprom *fake_eeprom; struct eeprom_buffer *eeprom_buffer; }; static void eeprom_buffer_test_does_not_write_until_flush(struct kunit *test) { struct eeprom_buffer_test *ctx = test->priv; struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; char buffer[] = {0xff}; eeprom_buffer->flush_count = SIZE_MAX; eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0); eeprom_buffer->flush(eeprom_buffer); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); } static void eeprom_buffer_test_flushes_after_flush_count_met(struct kunit *test) { struct eeprom_buffer_test *ctx = test->priv; struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; char buffer[] = {0xff}; eeprom_buffer->flush_count = 2; eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); } static void eeprom_buffer_test_flushes_increments_of_flush_count(struct kunit *test) { struct eeprom_buffer_test *ctx = test->priv; struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; char buffer[] = {0xff, 0xff}; eeprom_buffer->flush_count = 2; eeprom_buffer->write(eeprom_buffer, buffer, 1); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); eeprom_buffer->write(eeprom_buffer, buffer, 2); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); /* Should have only flushed the first two bytes. */ KUNIT_EXPECT_EQ(test, fake_eeprom->contents[2], 0); } static int eeprom_buffer_test_init(struct kunit *test) { struct eeprom_buffer_test *ctx; ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); ctx->fake_eeprom = kunit_kzalloc(test, sizeof(*ctx->fake_eeprom), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->fake_eeprom); fake_eeprom_init(ctx->fake_eeprom); ctx->eeprom_buffer = new_eeprom_buffer(&ctx->fake_eeprom->parent); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->eeprom_buffer); test->priv = ctx; return 0; } static void eeprom_buffer_test_exit(struct kunit *test) { struct eeprom_buffer_test *ctx = test->priv; destroy_eeprom_buffer(ctx->eeprom_buffer); }”…”�”}”hj« sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´M¬hj h²hubeh}”(h]”Œfaking-classes”ah ]”h"]”Œfaking classes”ah$]”h&]”uh1hÈhj¬h²hh³hÇh´Mfubeh}”(h]”Œisolating-behavior”ah ]”h"]”Œisolating behavior”ah$]”h&]”uh1hÈhj›h²hh³hÇh´M$ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒTesting Against Multiple Inputs”h]”hŒTesting Against Multiple Inputs”…”�”}”(hjÍ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjÊ h²hh³hÇh´Mubhï)�”}”(hŒvTesting just a few inputs is not enough to ensure that the code works correctly, for example: testing a hash function.”h]”hŒvTesting just a few inputs is not enough to ensure that the code works correctly, for example: testing a hash function.”…”�”}”(hjÛ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MhjÊ h²hubhï)�”}”(hŒ‚We can write a helper macro or function. The function is called for each input. For example, to test ``sha1sum(1)``, we can write:”h]”(hŒeWe can write a helper macro or function. The function is called for each input. For example, to test ”…”�”}”(hjé h²hh³Nh´Nubhù)�”}”(hŒ``sha1sum(1)``”h]”hŒ sha1sum(1)”…”�”}”(hjñ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjé ubhŒ, we can write:”…”�”}”(hjé h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M hjÊ h²hubj')�”}”(hX#define TEST_SHA1(in, want) \ sha1sum(in, out); \ KUNIT_EXPECT_STREQ_MSG(test, out, want, "sha1sum(%s)", in); char out[40]; TEST_SHA1("hello world", "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"); TEST_SHA1("hello world!", "430ce34d020724ed75a196dfc2ad67c77772d169");”h]”hX#define TEST_SHA1(in, want) \ sha1sum(in, out); \ KUNIT_EXPECT_STREQ_MSG(test, out, want, "sha1sum(%s)", in); char out[40]; TEST_SHA1("hello world", "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"); TEST_SHA1("hello world!", "430ce34d020724ed75a196dfc2ad67c77772d169");”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´MhjÊ h²hubhï)�”}”(hŒ—Note the use of the ``_MSG`` version of ``KUNIT_EXPECT_STREQ`` to print a more detailed error and make the assertions clearer within the helper macros.”h]”(hŒNote the use of the ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ``_MSG``”h]”hŒ_MSG”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒ version of ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_EXPECT_STREQ``”h]”hŒKUNIT_EXPECT_STREQ”…”�”}”(hj2 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒY to print a more detailed error and make the assertions clearer within the helper macros.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MhjÊ h²hubhï)�”}”(hŒÂThe ``_MSG`` variants are useful when the same expectation is called multiple times (in a loop or helper function) and thus the line number is not enough to identify what failed, as shown below.”h]”(hŒThe ”…”�”}”(hjJ h²hh³Nh´Nubhù)�”}”(hŒ``_MSG``”h]”hŒ_MSG”…”�”}”(hjR h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjJ ubhŒ¶ variants are useful when the same expectation is called multiple times (in a loop or helper function) and thus the line number is not enough to identify what failed, as shown below.”…”�”}”(hjJ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MhjÊ h²hubhï)�”}”(hŒsIn complicated cases, we recommend using a *table-driven test* compared to the helper macro variation, for example:”h]”(hŒ+In complicated cases, we recommend using a ”…”�”}”(hjj h²hh³Nh´Nubj )�”}”(hŒ*table-driven test*”h]”hŒtable-driven test”…”�”}”(hjr h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hjj ubhŒ5 compared to the helper macro variation, for example:”…”�”}”(hjj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MhjÊ h²hubj')�”}”(hXMint i; char out[40]; struct sha1_test_case { const char *str; const char *sha1; }; struct sha1_test_case cases[] = { { .str = "hello world", .sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", }, { .str = "hello world!", .sha1 = "430ce34d020724ed75a196dfc2ad67c77772d169", }, }; for (i = 0; i < ARRAY_SIZE(cases); ++i) { sha1sum(cases[i].str, out); KUNIT_EXPECT_STREQ_MSG(test, out, cases[i].sha1, "sha1sum(%s)", cases[i].str); }”h]”hXMint i; char out[40]; struct sha1_test_case { const char *str; const char *sha1; }; struct sha1_test_case cases[] = { { .str = "hello world", .sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", }, { .str = "hello world!", .sha1 = "430ce34d020724ed75a196dfc2ad67c77772d169", }, }; for (i = 0; i < ARRAY_SIZE(cases); ++i) { sha1sum(cases[i].str, out); KUNIT_EXPECT_STREQ_MSG(test, out, cases[i].sha1, "sha1sum(%s)", cases[i].str); }”…”�”}”hjŠ sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´M"hjÊ h²hubhï)�”}”(hŒ4There is more boilerplate code involved, but it can:”h]”hŒ4There is more boilerplate code involved, but it can:”…”�”}”(hj™ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M=hjÊ h²hubj�)�”}”(hhh]”(j”)�”}”(hŒ|be more readable when there are multiple inputs/outputs (due to field names). * For example, see ``fs/ext4/inode-test.c``. ”h]”(hï)�”}”(hŒMbe more readable when there are multiple inputs/outputs (due to field names).”h]”hŒMbe more readable when there are multiple inputs/outputs (due to field names).”…”�”}”(hj® h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M?hjª ubj�)�”}”(hhh]”j”)�”}”(hŒ+For example, see ``fs/ext4/inode-test.c``. ”h]”hï)�”}”(hŒ*For example, see ``fs/ext4/inode-test.c``.”h]”(hŒFor example, see ”…”�”}”(hjà h²hh³Nh´Nubhù)�”}”(hŒ``fs/ext4/inode-test.c``”h]”hŒfs/ext4/inode-test.c”…”�”}”(hjË h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjà ubhŒ.”…”�”}”(hjà h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MAhj¿ ubah}”(h]”h ]”h"]”h$]”h&]”uh1j“hj¼ ubah}”(h]”h ]”h"]”h$]”h&]”jÅŒ*”uh1jŽh³hÇh´MAhjª ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j“hj§ h²hh³Nh´Nubj”)�”}”(hŒ¬reduce duplication if test cases are shared across multiple tests. * For example: if we want to test ``sha256sum``, we could add a ``sha256`` field and reuse ``cases``. ”h]”(hï)�”}”(hŒBreduce duplication if test cases are shared across multiple tests.”h]”hŒBreduce duplication if test cases are shared across multiple tests.”…”�”}”(hjú h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MChjö ubj�)�”}”(hhh]”j”)�”}”(hŒdFor example: if we want to test ``sha256sum``, we could add a ``sha256`` field and reuse ``cases``. ”h]”hï)�”}”(hŒcFor example: if we want to test ``sha256sum``, we could add a ``sha256`` field and reuse ``cases``.”h]”(hŒ For example: if we want to test ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ ``sha256sum``”h]”hŒ sha256sum”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒ, we could add a ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ ``sha256``”h]”hŒsha256”…”�”}”(hj) h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒ field and reuse ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ ``cases``”h]”hŒcases”…”�”}”(hj; h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒ.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MEhj ubah}”(h]”h ]”h"]”h$]”h&]”uh1j“hj ubah}”(h]”h ]”h"]”h$]”h&]”jÅjï uh1jŽh³hÇh´MEhjö ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j“hj§ h²hh³Nh´Nubj”)�”}”(hŒ(be converted to a "parameterized test". ”h]”hï)�”}”(hŒ'be converted to a "parameterized test".”h]”hŒ+be converted to a “parameterized testâ€�.”…”�”}”(hji h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MHhje ubah}”(h]”h ]”h"]”h$]”h&]”uh1j“hj§ h²hh³hÇh´Nubeh}”(h]”h ]”h"]”h$]”h&]”jÅjï uh1jŽh³hÇh´M?hjÊ h²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒParameterized Testing”h]”hŒParameterized Testing”…”�”}”(hj† h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjƒ h²hh³hÇh´MKubhï)�”}”(hŒ¹To run a test case against multiple inputs, KUnit provides a parameterized testing framework. This feature formalizes and extends the concept of table-driven tests discussed previously.”h]”hŒ¹To run a test case against multiple inputs, KUnit provides a parameterized testing framework. This feature formalizes and extends the concept of table-driven tests discussed previously.”…”�”}”(hj” h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MMhjƒ h²hubhï)�”}”(hXaA KUnit test is determined to be parameterized if a parameter generator function is provided when registering the test case. A test user can either write their own generator function or use one that is provided by KUnit. The generator function is stored in ``kunit_case->generate_params`` and can be set using the macros described in the section below.”h]”(hXA KUnit test is determined to be parameterized if a parameter generator function is provided when registering the test case. A test user can either write their own generator function or use one that is provided by KUnit. The generator function is stored in ”…”�”}”(hj¢ h²hh³Nh´Nubhù)�”}”(hŒ``kunit_case->generate_params``”h]”hŒkunit_case->generate_params”…”�”}”(hjª h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj¢ ubhŒ@ and can be set using the macros described in the section below.”…”�”}”(hj¢ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MQhjƒ h²hubhï)�”}”(hX7To establish the terminology, a "parameterized test" is a test which is run multiple times (once per "parameter" or "parameter run"). Each parameter run has both its own independent ``struct kunit`` (the "parameter run context") and access to a shared parent ``struct kunit`` (the "parameterized test context").”h]”(hŒÂTo establish the terminology, a “parameterized testâ€� is a test which is run multiple times (once per “parameterâ€� or “parameter runâ€�). Each parameter run has both its own independent ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ``struct kunit``”h]”hŒ struct kunit”…”�”}”(hjÊ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒA (the “parameter run contextâ€�) and access to a shared parent ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ``struct kunit``”h]”hŒ struct kunit”…”�”}”(hjÜ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒ( (the “parameterized test contextâ€�).”…”�”•G}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MWhjƒ h²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒPassing Parameters to a Test”h]”hŒPassing Parameters to a Test”…”�”}”(hj÷ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjô h²hh³hÇh´M]ubhï)�”}”(hŒ9There are three ways to provide the parameters to a test:”h]”hŒ9There are three ways to provide the parameters to a test:”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M^hjô h²hubhï)�”}”(hŒArray Parameter Macros:”h]”hŒArray Parameter Macros:”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M`hjô h²hubhŒ block_quote”“”)�”}”(hŒøKUnit provides special support for the common table-driven testing pattern. By applying either ``KUNIT_ARRAY_PARAM`` or ``KUNIT_ARRAY_PARAM_DESC`` to the ``cases`` array from the previous section, we can create a parameterized test as shown below: ”h]”hï)�”}”(hŒ÷KUnit provides special support for the common table-driven testing pattern. By applying either ``KUNIT_ARRAY_PARAM`` or ``KUNIT_ARRAY_PARAM_DESC`` to the ``cases`` array from the previous section, we can create a parameterized test as shown below:”h]”(hŒ_KUnit provides special support for the common table-driven testing pattern. By applying either ”…”�”}”(hj' h²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_ARRAY_PARAM``”h]”hŒKUNIT_ARRAY_PARAM”…”�”}”(hj/ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj' ubhŒ or ”…”�”}”(hj' h²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_ARRAY_PARAM_DESC``”h]”hŒKUNIT_ARRAY_PARAM_DESC”…”�”}”(hjA h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj' ubhŒ to the ”…”�”}”(hj' h²hh³Nh´Nubhù)�”}”(hŒ ``cases``”h]”hŒcases”…”�”}”(hjS h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj' ubhŒT array from the previous section, we can create a parameterized test as shown below:”…”�”}”(hj' h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mbhj# ubah}”(h]”h ]”h"]”h$]”h&]”uh1j! h³hÇh´Mbhjô h²hubj')�”}”(hX7// This is copy-pasted from above. struct sha1_test_case { const char *str; const char *sha1; }; static const struct sha1_test_case cases[] = { { .str = "hello world", .sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", }, { .str = "hello world!", .sha1 = "430ce34d020724ed75a196dfc2ad67c77772d169", }, }; // Creates `sha1_gen_params()` to iterate over `cases` while using // the struct member `str` for the case description. KUNIT_ARRAY_PARAM_DESC(sha1, cases, str); // Looks no different from a normal test. static void sha1_test(struct kunit *test) { // This function can just contain the body of the for-loop. // The former `cases[i]` is accessible under test->param_value. char out[40]; struct sha1_test_case *test_param = (struct sha1_test_case *)(test->param_value); sha1sum(test_param->str, out); KUNIT_EXPECT_STREQ_MSG(test, out, test_param->sha1, "sha1sum(%s)", test_param->str); } // Instead of KUNIT_CASE, we use KUNIT_CASE_PARAM and pass in the // function declared by KUNIT_ARRAY_PARAM or KUNIT_ARRAY_PARAM_DESC. static struct kunit_case sha1_test_cases[] = { KUNIT_CASE_PARAM(sha1_test, sha1_gen_params), {} };”h]”hX7// This is copy-pasted from above. struct sha1_test_case { const char *str; const char *sha1; }; static const struct sha1_test_case cases[] = { { .str = "hello world", .sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", }, { .str = "hello world!", .sha1 = "430ce34d020724ed75a196dfc2ad67c77772d169", }, }; // Creates `sha1_gen_params()` to iterate over `cases` while using // the struct member `str` for the case description. KUNIT_ARRAY_PARAM_DESC(sha1, cases, str); // Looks no different from a normal test. static void sha1_test(struct kunit *test) { // This function can just contain the body of the for-loop. // The former `cases[i]` is accessible under test->param_value. char out[40]; struct sha1_test_case *test_param = (struct sha1_test_case *)(test->param_value); sha1sum(test_param->str, out); KUNIT_EXPECT_STREQ_MSG(test, out, test_param->sha1, "sha1sum(%s)", test_param->str); } // Instead of KUNIT_CASE, we use KUNIT_CASE_PARAM and pass in the // function declared by KUNIT_ARRAY_PARAM or KUNIT_ARRAY_PARAM_DESC. static struct kunit_case sha1_test_cases[] = { KUNIT_CASE_PARAM(sha1_test, sha1_gen_params), {} };”…”�”}”hjq sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´Mghjô h²hubhï)�”}”(hŒ$Custom Parameter Generator Function:”h]”hŒ$Custom Parameter Generator Function:”…”�”}”(hj€ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M‘hjô h²hubj" )�”}”(hX�The generator function is responsible for generating parameters one-by-one and has the following signature: ``const void* (*)(struct kunit *test, const void *prev, char *desc)``. You can pass the generator function to the ``KUNIT_CASE_PARAM`` or ``KUNIT_CASE_PARAM_WITH_INIT`` macros. The function receives the previously generated parameter as the ``prev`` argument (which is ``NULL`` on the first call) and can also access the parameterized test context passed as the ``test`` argument. KUnit calls this function repeatedly until it returns ``NULL``, which signifies that a parameterized test ended. Below is an example of how it works: ”h]”(hï)�”}”(hXThe generator function is responsible for generating parameters one-by-one and has the following signature: ``const void* (*)(struct kunit *test, const void *prev, char *desc)``. You can pass the generator function to the ``KUNIT_CASE_PARAM`` or ``KUNIT_CASE_PARAM_WITH_INIT`` macros.”h]”(hŒlThe generator function is responsible for generating parameters one-by-one and has the following signature: ”…”�”}”(hj’ h²hh³Nh´Nubhù)�”}”(hŒE``const void* (*)(struct kunit *test, const void *prev, char *desc)``”h]”hŒAconst void* (*)(struct kunit *test, const void *prev, char *desc)”…”�”}”(hjš h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj’ ubhŒ-. You can pass the generator function to the ”…”�”}”(hj’ h²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_CASE_PARAM``”h]”hŒKUNIT_CASE_PARAM”…”�”}”(hj¬ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj’ ubhŒ or ”…”�”}”(hj’ h²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_CASE_PARAM_WITH_INIT``”h]”hŒKUNIT_CASE_PARAM_WITH_INIT”…”�”}”(hj¾ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj’ ubhŒ macros.”…”�”}”(hj’ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M“hjŽ ubhï)�”}”(hX<The function receives the previously generated parameter as the ``prev`` argument (which is ``NULL`` on the first call) and can also access the parameterized test context passed as the ``test`` argument. KUnit calls this function repeatedly until it returns ``NULL``, which signifies that a parameterized test ended.”h]”(hŒ@The function receives the previously generated parameter as the ”…”�”}”(hjÖ h²hh³Nh´Nubhù)�”}”(hŒ``prev``”h]”hŒprev”…”�”}”(hjÞ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÖ ubhŒ argument (which is ”…”�”}”(hjÖ h²hh³Nh´Nubhù)�”}”(hŒ``NULL``”h]”hŒNULL”…”�”}”(hjð h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÖ ubhŒU on the first call) and can also access the parameterized test context passed as the ”…”�”}”(hjÖ h²hh³Nh´Nubhù)�”}”(hŒ``test``”h]”hŒtest”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÖ ubhŒA argument. KUnit calls this function repeatedly until it returns ”…”�”}”(hjÖ h²hh³Nh´Nubhù)�”}”(hŒ``NULL``”h]”hŒNULL”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÖ ubhŒ2, which signifies that a parameterized test ended.”…”�”}”(hjÖ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M™hjŽ ubhï)�”}”(hŒ$Below is an example of how it works:”h]”hŒ$Below is an example of how it works:”…”�”}”(hj, h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MŸhjŽ ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j! h³hÇh´M“hjô h²hubj')�”}”(hX9#define MAX_TEST_BUFFER_SIZE 8 // Example generator function. It produces a sequence of buffer sizes that // are powers of two, starting at 1 (e.g., 1, 2, 4, 8). static const void *buffer_size_gen_params(struct kunit *test, const void *prev, char *desc) { long prev_buffer_size = (long)prev; long next_buffer_size = 1; // Start with an initial size of 1. // Stop generating parameters if the limit is reached or exceeded. if (prev_buffer_size >= MAX_TEST_BUFFER_SIZE) return NULL; // For subsequent calls, calculate the next size by doubling the previous one. if (prev) next_buffer_size = prev_buffer_size << 1; return (void *)next_buffer_size; } // Simple test to validate that kunit_kzalloc provides zeroed memory. static void buffer_zero_test(struct kunit *test) { long buffer_size = (long)test->param_value; // Use kunit_kzalloc to allocate a zero-initialized buffer. This makes the // memory "parameter run managed," meaning it's automatically cleaned up at // the end of each parameter run. int *buf = kunit_kzalloc(test, buffer_size * sizeof(int), GFP_KERNEL); // Ensure the allocation was successful. KUNIT_ASSERT_NOT_NULL(test, buf); // Loop through the buffer and confirm every element is zero. for (int i = 0; i < buffer_size; i++) KUNIT_EXPECT_EQ(test, buf[i], 0); } static struct kunit_case buffer_test_cases[] = { KUNIT_CASE_PARAM(buffer_zero_test, buffer_size_gen_params), {} };”h]”hX9#define MAX_TEST_BUFFER_SIZE 8 // Example generator function. It produces a sequence of buffer sizes that // are powers of two, starting at 1 (e.g., 1, 2, 4, 8). static const void *buffer_size_gen_params(struct kunit *test, const void *prev, char *desc) { long prev_buffer_size = (long)prev; long next_buffer_size = 1; // Start with an initial size of 1. // Stop generating parameters if the limit is reached or exceeded. if (prev_buffer_size >= MAX_TEST_BUFFER_SIZE) return NULL; // For subsequent calls, calculate the next size by doubling the previous one. if (prev) next_buffer_size = prev_buffer_size << 1; return (void *)next_buffer_size; } // Simple test to validate that kunit_kzalloc provides zeroed memory. static void buffer_zero_test(struct kunit *test) { long buffer_size = (long)test->param_value; // Use kunit_kzalloc to allocate a zero-initialized buffer. This makes the // memory "parameter run managed," meaning it's automatically cleaned up at // the end of each parameter run. int *buf = kunit_kzalloc(test, buffer_size * sizeof(int), GFP_KERNEL); // Ensure the allocation was successful. KUNIT_ASSERT_NOT_NULL(test, buf); // Loop through the buffer and confirm every element is zero. for (int i = 0; i < buffer_size; i++) KUNIT_EXPECT_EQ(test, buf[i], 0); } static struct kunit_case buffer_test_cases[] = { KUNIT_CASE_PARAM(buffer_zero_test, buffer_size_gen_params), {} };”…”�”}”hj@ sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´M¡hjô h²hubhï)�”}”(hŒ:Runtime Parameter Array Registration in the Init Function:”h]”hŒ:Runtime Parameter Array Registration in the Init Function:”…”�”}”(hjO h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÍhjô h²hubj" )�”}”(hXÆFor scenarios where you might need to initialize a parameterized test, you can directly register a parameter array to the parameterized test context. To do this, you must pass the parameterized test context, the array itself, the array size, and a ``get_description()`` function to the ``kunit_register_params_array()`` macro. This macro populates ``struct kunit_params`` within the parameterized test context, effectively storing a parameter array object. The ``get_description()`` function will be used for populating parameter descriptions and has the following signature: ``void (*)(struct kunit *test, const void *param, char *desc)``. Note that it also has access to the parameterized test context. .. important:: When using this way to register a parameter array, you will need to manually pass ``kunit_array_gen_params()`` as the generator function to ``KUNIT_CASE_PARAM_WITH_INIT``. ``kunit_array_gen_params()`` is a KUnit helper that will use the registered array to generate the parameters. If needed, instead of passing the KUnit helper, you can also pass your own custom generator function that utilizes the parameter array. To access the parameter array from within the parameter generator function use ``test->params_array.params``. The ``kunit_register_params_array()`` macro should be called within a ``param_init()`` function that initializes the parameterized test and has the following signature ``int (*)(struct kunit *test)``. For a detailed explanation of this mechanism please refer to the "Adding Shared Resources" section that is after this one. This method supports registering both dynamically built and static parameter arrays. The code snippet below shows the ``example_param_init_dynamic_arr`` test that utilizes ``make_fibonacci_params()`` to create a dynamic array, which is then registered using ``kunit_register_params_array()``. To see the full code please refer to lib/kunit/kunit-example-test.c. ”h]”(hï)�”}”(hŒ•For scenarios where you might need to initialize a parameterized test, you can directly register a parameter array to the parameterized test context.”h]”hŒ•For scenarios where you might need to initialize a parameterized test, you can directly register a parameter array to the parameterized test context.”…”�”}”(hja h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÏhj] ubhï)�”}”(hX*To do this, you must pass the parameterized test context, the array itself, the array size, and a ``get_description()`` function to the ``kunit_register_params_array()`` macro. This macro populates ``struct kunit_params`` within the parameterized test context, effectively storing a parameter array object. The ``get_description()`` function will be used for populating parameter descriptions and has the following signature: ``void (*)(struct kunit *test, const void *param, char *desc)``. Note that it also has access to the parameterized test context.”h]”(hŒbTo do this, you must pass the parameterized test context, the array itself, the array size, and a ”…”�”}”(hjo h²hh³Nh´Nubhù)�”}”(hŒ``get_description()``”h]”hŒget_description()”…”�”}”(hjw h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjo ubhŒ function to the ”…”�”}”(hjo h²hh³Nh´Nubhù)�”}”(hŒ!``kunit_register_params_array()``”h]”hŒkunit_register_params_array()”…”�”}”(hj‰ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjo ubhŒ macro. This macro populates ”…”�”}”(hjo h²hh³Nh´Nubhù)�”}”(hŒ``struct kunit_params``”h]”hŒstruct kunit_params”…”�”}”(hj› h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjo ubhŒZ within the parameterized test context, effectively storing a parameter array object. The ”…”�”}”(hjo h²hh³Nh´Nubhù)�”}”(hŒ``get_description()``”h]”hŒget_description()”…”�”}”(hj­ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjo ubhŒ^ function will be used for populating parameter descriptions and has the following signature: ”…”�”}”(hjo h²hh³Nh´Nubhù)�”}”(hŒ?``void (*)(struct kunit *test, const void *param, char *desc)``”h]”hŒ;void (*)(struct kunit *test, const void *param, char *desc)”…”�”}”(hj¿ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjo ubhŒA. Note that it also has access to the parameterized test context.”…”�”}”(hjo h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÒhj] ubj" )�”}”(hX8.. important:: When using this way to register a parameter array, you will need to manually pass ``kunit_array_gen_params()`` as the generator function to ``KUNIT_CASE_PARAM_WITH_INIT``. ``kunit_array_gen_params()`` is a KUnit helper that will use the registered array to generate the parameters. If needed, instead of passing the KUnit helper, you can also pass your own custom generator function that utilizes the parameter array. To access the parameter array from within the parameter generator function use ``test->params_array.params``. ”h]”j_)�”}”(hXWhen using this way to register a parameter array, you will need to manually pass ``kunit_array_gen_params()`` as the generator function to ``KUNIT_CASE_PARAM_WITH_INIT``. ``kunit_array_gen_params()`` is a KUnit helper that will use the registered array to generate the parameters. If needed, instead of passing the KUnit helper, you can also pass your own custom generator function that utilizes the parameter array. To access the parameter array from within the parameter generator function use ``test->params_array.params``.”h]”(hï)�”}”(hXWhen using this way to register a parameter array, you will need to manually pass ``kunit_array_gen_params()`` as the generator function to ``KUNIT_CASE_PARAM_WITH_INIT``. ``kunit_array_gen_params()`` is a KUnit helper that will use the registered array to generate the parameters.”h]”(hŒRWhen using this way to register a parameter array, you will need to manually pass ”…”�”}”(hjß h²hh³Nh´Nubhù)�”}”(hŒ``kunit_array_gen_params()``”h]”hŒkunit_array_gen_params()”…”�”}”(hjç h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjß ubhŒ as the generator function to ”…”�”}”(hjß h²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_CASE_PARAM_WITH_INIT``”h]”hŒKUNIT_CASE_PARAM_WITH_INIT”…”�”}”(hjù h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjß ubhŒ. ”…”�”}”(hjß h²hh³Nh´Nubhù)�”}”(hŒ``kunit_array_gen_params()``”h]”hŒkunit_array_gen_params()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjß ubhŒQ is a KUnit helper that will use the registered array to generate the parameters.”…”�”}”(hjß h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÜhjÛ ubhï)�”}”(hŒõIf needed, instead of passing the KUnit helper, you can also pass your own custom generator function that utilizes the parameter array. To access the parameter array from within the parameter generator function use ``test->params_array.params``.”h]”(hŒ×If needed, instead of passing the KUnit helper, you can also pass your own custom generator function that utilizes the parameter array. To access the parameter array from within the parameter generator function use ”…”�”}”(hj#h²hh³Nh´Nubhù)�”}”(hŒ``test->params_array.params``”h]”hŒtest->params_array.params”…”�”}”(hj+h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj#ubhŒ.”…”�”}”(hj#h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MáhjÛ ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j^hj× ubah}”(h]”h ]”h"]”h$]”h&]”uh1j! h³hÇh´MÛhj] ubhï)�”}”(hX˜The ``kunit_register_params_array()`` macro should be called within a ``param_init()`` function that initializes the parameterized test and has the following signature ``int (*)(struct kunit *test)``. For a detailed explanation of this mechanism please refer to the "Adding Shared Resources" section that is after this one. This method supports registering both dynamically built and static parameter arrays.”h]”(hŒThe ”…”�”}”(hjOh²hh³Nh´Nubhù)�”}”(hŒ!``kunit_register_params_array()``”h]”hŒkunit_register_params_array()”…”�”}”(hjWh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjOubhŒ! macro should be called within a ”…”�”}”(hjOh²hh³Nh´Nubhù)�”}”(hŒ``param_init()``”h]”hŒ param_init()”…”�”}”(hjih²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjOubhŒR function that initializes the parameterized test and has the following signature ”…”�”}”(hjOh²hh³Nh´Nubhù)�”}”(hŒ``int (*)(struct kunit *test)``”h]”hŒint (*)(struct kunit *test)”…”�”}”(hj{h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjOubhŒÕ. For a detailed explanation of this mechanism please refer to the “Adding Shared Resourcesâ€� section that is after this one. This method supports registering both dynamically built and static parameter arrays.”…”�”}”(hjOh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mæhj] ubhï)�”}”(hXThe code snippet below shows the ``example_param_init_dynamic_arr`` test that utilizes ``make_fibonacci_params()`` to create a dynamic array, which is then registered using ``kunit_register_params_array()``. To see the full code please refer to lib/kunit/kunit-example-test.c.”h]”(hŒ!The code snippet below shows the ”…”�”}”(hj“h²hh³Nh´Nubhù)�”}”(hŒ"``example_param_init_dynamic_arr``”h]”hŒexample_param_init_dynamic_arr”…”�”}”(hj›h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj“ubhŒ test that utilizes ”…”�”}”(hj“h²hh³Nh´Nubhù)�”}”(hŒ``make_fibonacci_params()``”h]”hŒmake_fibonacci_params()”…”�”}”(hj­h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj“ubhŒ; to create a dynamic array, which is then registered using ”…”�”}”(hj“h²hh³Nh´Nubhù)�”}”(hŒ!``kunit_register_params_array()``”h]”hŒkunit_register_params_array()”…”�”}”(hj¿h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj“ubhŒF. To see the full code please refer to lib/kunit/kunit-example-test.c.”…”�”}”(hj“h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Míhj] ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j! h³hÇh´MÏhjô h²hubj')�”}”(hXˆ/* * Example of a parameterized test param_init() function that registers a dynamic * array of parameters. */ static int example_param_init_dynamic_arr(struct kunit *test) { size_t seq_size; int *fibonacci_params; kunit_info(test, "initializing parameterized test\n"); seq_size = 6; fibonacci_params = make_fibonacci_params(test, seq_size); if (!fibonacci_params) return -ENOMEM; /* * Passes the dynamic parameter array information to the parameterized test * context struct kunit. The array and its metadata will be stored in * test->parent->params_array. The array itself will be located in * params_data.params. */ kunit_register_params_array(test, fibonacci_params, seq_size, example_param_dynamic_arr_get_desc); return 0; } static struct kunit_case example_test_cases[] = { /* * Note how we pass kunit_array_gen_params() to use the array we * registered in example_param_init_dynamic_arr() to generate * parameters. */ KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_dynamic_arr, kunit_array_gen_params, example_param_init_dynamic_arr, example_param_exit_dynamic_arr), {} };”h]”hXˆ/* * Example of a parameterized test param_init() function that registers a dynamic * array of parameters. */ static int example_param_init_dynamic_arr(struct kunit *test) { size_t seq_size; int *fibonacci_params; kunit_info(test, "initializing parameterized test\n"); seq_size = 6; fibonacci_params = make_fibonacci_params(test, seq_size); if (!fibonacci_params) return -ENOMEM; /* * Passes the dynamic parameter array information to the parameterized test * context struct kunit. The array and its metadata will be stored in * test->parent->params_array. The array itself will be located in * params_data.params. */ kunit_register_params_array(test, fibonacci_params, seq_size, example_param_dynamic_arr_get_desc); return 0; } static struct kunit_case example_test_cases[] = { /* * Note how we pass kunit_array_gen_params() to use the array we * registered in example_param_init_dynamic_arr() to generate * parameters. */ KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_dynamic_arr, kunit_array_gen_params, example_param_init_dynamic_arr, example_param_exit_dynamic_arr), {} };”…”�”}”hjÝsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´Mòhjô h²hubeh}”(h]”Œpassing-parameters-to-a-test”ah ]”h"]”Œpassing parameters to a test”ah$]”h&]”uh1hÈhjƒ h²hh³hÇh´M]ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒAdding Shared Resources”h]”hŒAdding Shared Resources”…”�”}”(hj÷h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjôh²hh³hÇh´Mubhï)�”}”(hXAll parameter runs in this framework hold a reference to the parameterized test context, which can be accessed using the parent ``struct kunit`` pointer. The parameterized test context is not used to execute any test logic itself; instead, it serves as a container for shared resources.”h]”(hŒ€All parameter runs in this framework hold a reference to the parameterized test context, which can be accessed using the parent ”…”�”}”(hjh²hh³Nh´Nubhù)�”}”(hŒ``struct kunit``”h]”hŒ struct kunit”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒŽ pointer. The parameterized test context is not used to execute any test logic itself; instead, it serves as a container for shared resources.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mhjôh²hubhï)�”}”(hX"It's possible to add resources to share between parameter runs within a parameterized test by using ``KUNIT_CASE_PARAM_WITH_INIT``, to which you pass custom ``param_init()`` and ``param_exit()`` functions. These functions run once before and once after the parameterized test, respectively.”h]”(hŒfIt’s possible to add resources to share between parameter runs within a parameterized test by using ”…”�”}”(hj%h²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_CASE_PARAM_WITH_INIT``”h]”hŒKUNIT_CASE_PARAM_WITH_INIT”…”�”}”(hj-h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj%ubhŒ, to which you pass custom ”…”�”}”(hj%h²hh³Nh´Nubhù)�”}”(hŒ``param_init()``”h]”hŒ param_init()”…”�”}”(hj?h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj%ubhŒ and ”…”�”}”(hj%h²hh³Nh´Nubhù)�”}”(hŒ``param_exit()``”h]”hŒ param_exit()”…”�”}”(hjQh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj%ubhŒ` functions. These functions run once before and once after the parameterized test, respectively.”…”�”}”(hj%h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M"hjôh²hubhï)�”}”(hXThe ``param_init()`` function, with the signature ``int (*)(struct kunit *test)``, can be used for adding resources to the ``resources`` or ``priv`` fields of the parameterized test context, registering the parameter array, and any other initialization logic.”h]”(hŒThe ”…”�”}”(hjih²hh³Nh´Nubhù)�”}”(hŒ``param_init()``”h]”hŒ param_init()”…”�”}”(hjqh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjiubhŒ function, with the signature ”…”�”}”(hjih²hh³Nh´Nubhù)�”}”(hŒ``int (*)(struct kunit *test)``”h]”hŒint (*)(struct kunit *test)”…”�”}”(hjƒh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjiubhŒ*, can be used for adding resources to the ”…”�”}”(hjih²hh³Nh´Nubhù)�”}”(hŒ ``resources``”h]”hŒ resources”…”�”}”(hj•h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjiubhŒ or ”…”�”}”(hjih²hh³Nh´Nubhù)�”}”(hŒ``priv``”h]”hŒpriv”…”�”}”(hj§h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjiubhŒo fields of the parameterized test context, registering the parameter array, and any other initialization logic.”…”�”}”(hjih²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M'hjôh²hubhï)�”}”(hXThe ``param_exit()`` function, with the signature ``void (*)(struct kunit *test)``, can be used to release any resources that were not parameterized test managed (i.e. not automatically cleaned up after the parameterized test ends) and for any other exit logic.”h]”(hŒThe ”…”�”}”(hj¿h²hh³Nh´Nubhù)�”}”(hŒ``param_exit()``”h]”hŒ param_exit()”…”�”}”(hjÇh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj¿ubhŒ function, with the signature ”…”�”}”(hj¿h²hh³Nh´Nubhù)�”}”(hŒ ``void (*)(struct kunit *test)``”h]”hŒvoid (*)(struct kunit *test)”…”�”}”(hjÙh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj¿ubhŒ³, can be used to release any resources that were not parameterized test managed (i.e. not automatically cleaned up after the parameterized test ends) and for any other exit logic.”…”�”}”(hj¿h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M,hjôh²hubhï)�”}”(hXBoth ``param_init()`` and ``param_exit()`` are passed the parameterized test context behind the scenes. However, the test case function receives the parameter run context. Therefore, to manage and access shared resources from within a test case function, you must use ``test->parent``.”h]”(hŒBoth ”…”�”}”(hjñh²hh³Nh´Nubhù)�”}”(hŒ``param_init()``”h]”hŒ param_init()”…”�”}”(hjùh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjñubhŒ and ”…”�”}”(hjñh²hh³Nh´Nubhù)�”}”(hŒ``param_exit()``”h]”hŒ param_exit()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjñubhŒâ are passed the parameterized test context behind the scenes. However, the test case function receives the parameter run context. Therefore, to manage and access shared resources from within a test case function, you must use ”…”�”}”(hjñh²hh³Nh´Nubhù)�”}”(hŒ``test->parent``”h]”hŒ test->parent”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjñubhŒ.”…”�”}”(hjñh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M1hjôh²hubhï)�”}”(hXŠFor instance, finding a shared resource allocated by the Resource API requires passing ``test->parent`` to ``kunit_find_resource()``. This principle extends to all other APIs that might be used in the test case function, including ``kunit_kzalloc()``, ``kunit_kmalloc_array()``, and others (see Documentation/dev-tools/kunit/api/test.rst and the Documentation/dev-tools/kunit/api/resource.rst).”h]”(hŒWFor instance, finding a shared resource allocated by the Resource API requires passing ”…”�”}”(hj5h²hh³Nh´Nubhù)�”}”(hŒ``test->parent``”h]”hŒ test->parent”…”�”}”(hj=h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj5ubhŒ to ”…”�”}”(hj5h²hh³Nh´Nubhù)�”}”(hŒ``kunit_find_resource()``”h]”hŒkunit_find_resource()”…”�”}”(hjOh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj5ubhŒc. This principle extends to all other APIs that might be used in the test case function, including ”…”�”}”(hj5h²hh³Nh´Nubhù)�”}”(hŒ``kunit_kzalloc()``”h]”hŒkunit_kzalloc()”…”�”}”(hjah²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj5ubhŒ, ”…”�”}”(hj5h²hh³Nh´Nubhù)�”}”(hŒ``kunit_kmalloc_array()``”h]”hŒkunit_kmalloc_array()”…”�”}”(hjsh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj5ubhŒu, and others (see Documentation/dev-tools/kunit/api/test.rst and the Documentation/dev-tools/kunit/api/resource.rst).”…”�”}”(hj5h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M6hjôh²hubjh)�”}”(hŒËThe ``suite->init()`` function, which executes before each parameter run, receives the parameter run context. Therefore, any resources set up in ``suite->init()`` are cleaned up after each parameter run.”h]”hï)�”}”(hŒËThe ``suite->init()`` function, which executes before each parameter run, receives the parameter run context. Therefore, any resources set up in ``suite->init()`` are cleaned up after each parameter run.”h]”(hŒThe ”…”�”}”(hj�h²hh³Nh´Nubhù)�”}”(hŒ``suite->init()``”h]”hŒ suite->init()”…”�”}”(hj—h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj�ubhŒ| function, which executes before each parameter run, receives the parameter run context. Therefore, any resources set up in ”…”�”}”(hj�h²hh³Nh´Nubhù)�”}”(hŒ``suite->init()``”h]”hŒ suite->init()”…”�”}”(hj©h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj�ubhŒ) are cleaned up after each parameter run.”…”�”}”(hj�h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M>hj‹ubah}”(h]”h ]”h"]”h$]”h&]”uh1jghjôh²hh³hÇh´Nubhï)�”}”(hXThe code below shows how you can add the shared resources. Note that this code utilizes the Resource API, which you can read more about here: Documentation/dev-tools/kunit/api/resource.rst. To see the full version of this code please refer to lib/kunit/kunit-example-test.c.”h]”hXThe code below shows how you can add the shared resources. Note that this code utilizes the Resource API, which you can read more about here: Documentation/dev-tools/kunit/api/resource.rst. To see the full version of this code please refer to lib/kunit/kunit-example-test.c.”…”�”}”(hjÇh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MBhjôh²hubj')�”}”(hXþstatic int example_resource_init(struct kunit_resource *res, void *context) { ... /* Code that allocates memory and stores context in res->data. */ } /* This function deallocates memory for the kunit_resource->data field. */ static void example_resource_free(struct kunit_resource *res) { kfree(res->data); } /* This match function locates a test resource based on defined criteria. */ static bool example_resource_alloc_match(struct kunit *test, struct kunit_resource *res, void *match_data) { return res->data && res->free == example_resource_free; } /* Function to initialize the parameterized test. */ static int example_param_init(struct kunit *test) { int ctx = 3; /* Data to be stored. */ void *data = kunit_alloc_resource(test, example_resource_init, example_resource_free, GFP_KERNEL, &ctx); if (!data) return -ENOMEM; kunit_register_params_array(test, example_params_array, ARRAY_SIZE(example_params_array)); return 0; } /* Example test that uses shared resources in test->resources. */ static void example_params_test_with_init(struct kunit *test) { int threshold; const struct example_param *param = test->param_value; /* Here we pass test->parent to access the parameterized test context. */ struct kunit_resource *res = kunit_find_resource(test->parent, example_resource_alloc_match, NULL); threshold = *((int *)res->data); KUNIT_ASSERT_LE(test, param->value, threshold); kunit_put_resource(res); } static struct kunit_case example_test_cases[] = { KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen_params, example_param_init, NULL), {} };”h]”hXþstatic int example_resource_init(struct kunit_resource *res, void *context) { ... /* Code that allocates memory and stores context in res->data. */ } /* This function deallocates memory for the kunit_resource->data field. */ static void example_resource_free(struct kunit_resource *res) { kfree(res->data); } /* This match function locates a test resource based on defined criteria. */ static bool example_resource_alloc_match(struct kunit *test, struct kunit_resource *res, void *match_data) { return res->data && res->free == example_resource_free; } /* Function to initialize the parameterized test. */ static int example_param_init(struct kunit *test) { int ctx = 3; /* Data to be stored. */ void *data = kunit_alloc_resource(test, example_resource_init, example_resource_free, GFP_KERNEL, &ctx); if (!data) return -ENOMEM; kunit_register_params_array(test, example_params_array, ARRAY_SIZE(example_params_array)); return 0; } /* Example test that uses shared resources in test->resources. */ static void example_params_test_with_init(struct kunit *test) { int threshold; const struct example_param *param = test->param_value; /* Here we pass test->parent to access the parameterized test context. */ struct kunit_resource *res = kunit_find_resource(test->parent, example_resource_alloc_match, NULL); threshold = *((int *)res->data); KUNIT_ASSERT_LE(test, param->value, threshold); kunit_put_resource(res); } static struct kunit_case example_test_cases[] = { KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen_params, example_param_init, NULL), {} };”…”�”}”hjÕsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´MGhjôh²hubhï)�”}”(hŒûAs an alternative to using the KUnit Resource API for sharing resources, you can place them in ``test->parent->priv``. This serves as a more lightweight method for resource storage, best for scenarios where complex resource management is not required.”h]”(hŒ_As an alternative to using the KUnit Resource API for sharing resources, you can place them in ”…”�”}”(hjäh²hh³Nh´Nubhù)�”}”(hŒ``test->parent->priv``”h]”hŒtest->parent->priv”…”�”}”(hjìh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjäubhŒ†. This serves as a more lightweight method for resource storage, best for scenarios where complex resource management is not required.”…”�”}”(hjäh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M~hjôh²hubhï)�”}”(hX•As stated previously ``param_init()`` and ``param_exit()`` get the parameterized test context. So, you can directly use ``test->priv`` within ``param_init/exit`` to manage shared resources. However, from within the test case function, you must navigate up to the parent ``struct kunit`` i.e. the parameterized test context. Therefore, you need to use ``test->parent->priv`` to access those same resources.”h]”(hŒAs stated previously ”…”�”}”(hjh²hh³Nh´Nubhù)�”}”(hŒ``param_init()``”h]”hŒ param_init()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒ and ”…”�”}”(hjh²hh³Nh´Nubhù)�”}”(hŒ``param_exit()``”h]”hŒ param_exit()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒ> get the parameterized test context. So, you can directly use ”…”�”}”(hjh²hh³Nh´Nubhù)�”}”(hŒ``test->priv``”h]”hŒ test->priv”…”�”}”(hj0h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒ within ”…”�”}”(hjh²hh³Nh´Nubhù)�”}”(hŒ``param_init/exit``”h]”hŒparam_init/exit”…”�”}”(hjBh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒm to manage shared resources. However, from within the test case function, you must navigate up to the parent ”…”�”}”(hjh²hh³Nh´Nubhù)�”}”(hŒ``struct kunit``”h]”hŒ struct kunit”…”�”}”(hjTh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒA i.e. the parameterized test context. Therefore, you need to use ”…”�”}”(hjh²hh³Nh´Nubhù)�”}”(hŒ``test->parent->priv``”h]”hŒtest->parent->priv”…”�”}”(hjfh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒ to access those same resources.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mƒhjôh²hubhï)�”}”(hX´The resources placed in ``test->parent->priv`` will need to be allocated in memory to persist across the parameter runs. If memory is allocated using the KUnit memory allocation APIs (described more in the "Allocating Memory" section below), you won't need to worry about deallocation. The APIs will make the memory parameterized test 'managed', ensuring that it will automatically get cleaned up after the parameterized test concludes.”h]”(hŒThe resources placed in ”…”�”}”(hj~h²hh³Nh´Nubhù)�”}”(hŒ``test->parent->priv``”h]”hŒtest->parent->priv”…”�”}”(hj†h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj~ubhX� will need to be allocated in memory to persist across the parameter runs. If memory is allocated using the KUnit memory allocation APIs (described more in the “Allocating Memoryâ€� section below), you won’t need to worry about deallocation. The APIs will make the memory parameterized test ‘managed’, ensuring that it will automatically get cleaned up after the parameterized test concludes.”…”�”}”(hj~h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MŠhjôh²hubhï)�”}”(hŒUThe code below demonstrates example usage of the ``priv`` field for shared resources:”h]”(hŒ1The code below demonstrates example usage of the ”…”�”}”(hjžh²hh³Nh´Nubhù)�”}”(hŒ``priv``”h]”hŒpriv”…”�”}”(hj¦h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjžubhŒ field for shared resources:”…”�”}”(hjžh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M‘hjôh²hubj')�”}”(hXvstatic const struct example_param { int value; } example_params_array[] = { { .value = 3, }, { .value = 2, }, { .value = 1, }, { .value = 0, }, }; /* Initialize the parameterized test context. */ static int example_param_init_priv(struct kunit *test) { int ctx = 3; /* Data to be stored. */ int arr_size = ARRAY_SIZE(example_params_array); /* * Allocate memory using kunit_kzalloc(). Since the `param_init` * function receives the parameterized test context, this memory * allocation will be scoped to the lifetime of the parameterized test. */ test->priv = kunit_kzalloc(test, sizeof(int), GFP_KERNEL); /* Assign the context value to test->priv.*/ *((int *)test->priv) = ctx; /* Register the parameter array. */ kunit_register_params_array(test, example_params_array, arr_size, NULL); return 0; } static void example_params_test_with_init_priv(struct kunit *test) { int threshold; const struct example_param *param = test->param_value; /* By design, test->parent will not be NULL. */ KUNIT_ASSERT_NOT_NULL(test, test->parent); /* Here we use test->parent->priv to access the shared resource. */ threshold = *(int *)test->parent->priv; KUNIT_ASSERT_LE(test, param->value, threshold); } static struct kunit_case example_tests[] = { KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_priv, kunit_array_gen_params, example_param_init_priv, NULL), {} };”h]”hXvstatic const struct example_param { int value; } example_params_array[] = { { .value = 3, }, { .value = 2, }, { .value = 1, }, { .value = 0, }, }; /* Initialize the parameterized test context. */ static int example_param_init_priv(struct kunit *test) { int ctx = 3; /* Data to be stored. */ int arr_size = ARRAY_SIZE(example_params_array); /* * Allocate memory using kunit_kzalloc(). Since the `param_init` * function receives the parameterized test context, this memory * allocation will be scoped to the lifetime of the parameterized test. */ test->priv = kunit_kzalloc(test, sizeof(int), GFP_KERNEL); /* Assign the context value to test->priv.*/ *((int *)test->priv) = ctx; /* Register the parameter array. */ kunit_register_params_array(test, example_params_array, arr_size, NULL); return 0; } static void example_params_test_with_init_priv(struct kunit *test) { int threshold; const struct example_param *param = test->param_value; /* By design, test->parent will not be NULL. */ KUNIT_ASSERT_NOT_NULL(test, test->parent); /* Here we use test->parent->priv to access the shared resource. */ threshold = *(int *)test->parent->priv; KUNIT_ASSERT_LE(test, param->value, threshold); } static struct kunit_case example_tests[] = { KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_priv, kunit_array_gen_params, example_param_init_priv, NULL), {} };”…”�”}”hj¾sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´M”hjôh²hubeh}”(h]”Œadding-shared-resources”ah ]”h"]”Œadding shared resources”ah$]”h&]”uh1hÈhjƒ h²hh³hÇh´Mubeh}”(h]”Œparameterized-testing”ah ]”h"]”Œparameterized testing”ah$]”h&]”uh1hÈhjÊ h²hh³hÇh´MKubeh}”(h]”Œtesting-against-multiple-inputs”ah ]”h"]”Œtesting against multiple inputs”ah$]”h&]”uh1hÈhj›h²hh³hÇh´MubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒAllocating Memory”h]”hŒAllocating Memory”…”�”}”(hjèh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjåh²hh³hÇh´MÊubhï)�”}”(hŒ’Where you might use ``kzalloc``, you can instead use ``kunit_kzalloc`` as KUnit will then ensure that the memory is freed once the test completes.”h]”(hŒWhere you might use ”…”�”}”(hjöh²hh³Nh´Nubhù)�”}”(hŒ ``kzalloc``”h]”hŒkzalloc”…”�”}”(hjþh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjöubhŒ, you can instead use ”…”�”}”(hjöh²hh³Nh´Nubhù)�”}”(hŒ``kunit_kzalloc``”h]”hŒ kunit_kzalloc”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjöubhŒL as KUnit will then ensure that the memory is freed once the test completes.”…”�”}”(hjöh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÌhjåh²hubhï)�”}”(hŒ¨This is useful because it lets us use the ``KUNIT_ASSERT_EQ`` macros to exit early from a test without having to worry about remembering to call ``kfree``. For example:”h]”(hŒ*This is useful because it lets us use the ”…”�”}”(hj(h²hh³Nh´Nubhù)�”}”(hŒ``KUNIT_ASSERT_EQ``”h]”hŒKUNIT_ASSERT_EQ”…”�”}”(hj0h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj(ubhŒT macros to exit early from a test without having to worry about remembering to call ”…”�”}”(hj(h²hh³Nh´Nubhù)�”}”(hŒ ``kfree``”h]”hŒkfree”…”�”}”(hjBh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj(ubhŒ. For example:”…”�”}”(hj(h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÏhjåh²hubj')�”}”(hŒþvoid example_test_allocation(struct kunit *test) { char *buffer = kunit_kzalloc(test, 16, GFP_KERNEL); /* Ensure allocation succeeded. */ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer); KUNIT_ASSERT_STREQ(test, buffer, ""); }”h]”hŒþvoid example_test_allocation(struct kunit *test) { char *buffer = kunit_kzalloc(test, 16, GFP_KERNEL); /* Ensure allocation succeeded. */ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer); KUNIT_ASSERT_STREQ(test, buffer, ""); }”…”�”}”hjZsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´MÓhjåh²hubeh}”(h]”Œallocating-memory”ah ]”h"]”Œallocating memory”ah$]”h&]”uh1hÈhj›h²hh³hÇh´MÊubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒRegistering Cleanup Actions”h]”hŒRegistering Cleanup Actions”…”�”}”(hjth²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjqh²hh³hÇh´Mßubhï)�”}”(hŒÚIf you need to perform some cleanup beyond simple use of ``kunit_kzalloc``, you can register a custom "deferred action", which is a cleanup function run when the test exits (whether cleanly, or via a failed assertion).”h]”(hŒ9If you need to perform some cleanup beyond simple use of ”…”�”}”(hj‚h²hh³Nh´Nubhù)�”}”(hŒ``kunit_kzalloc``”h]”hŒ kunit_kzalloc”…”�”}”(hjŠh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj‚ubhŒ”, you can register a custom “deferred actionâ€�, which is a cleanup function run when the test exits (whether cleanly, or via a failed assertion).”…”�”}”(hj‚h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Máhjqh²hubhï)�”}”(hX Actions are simple functions with no return value, and a single ``void*`` context argument, and fulfill the same role as "cleanup" functions in Python and Go tests, "defer" statements in languages which support them, and (in some cases) destructors in RAII languages.”h]”(hŒ@Actions are simple functions with no return value, and a single ”…”�”}”(hj¢h²hh³Nh´Nubhù)�”}”(hŒ ``void*``”h]”hŒvoid*”…”�”}”(hjªh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj¢ubhŒÊ context argument, and fulfill the same role as “cleanupâ€� functions in Python and Go tests, “deferâ€� statements in languages which support them, and (in some cases) destructors in RAII languages.”…”�”}”(hj¢h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Måhjqh²hubhï)�”}”(hŒyThese are very useful for unregistering things from global lists, closing files or other resources, or freeing resources.”h]”hŒyThese are very useful for unregistering things from global lists, closing files or other resources, or freeing resources.”…”�”}”(hjÂh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mêhjqh²hubhï)�”}”(hŒ For example:”h]”hŒ For example:”…”�”}”(hjÐh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Míhjqh²hubj')�”}”(hX%static void cleanup_device(void *ctx) { struct device *dev = (struct device *)ctx; device_unregister(dev); } void example_device_test(struct kunit *test) { struct my_device dev; device_register(&dev); kunit_add_action(test, &cleanup_device, &dev); }”h]”hX%static void cleanup_device(void *ctx) { struct device *dev = (struct device *)ctx; device_unregister(dev); } void example_device_test(struct kunit *test) { struct my_device dev; device_register(&dev); kunit_add_action(test, &cleanup_device, &dev); }”…”�”}”hjÞsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7ŒC”j9}”uh1j&h³hÇh´Mïhjqh²hubhï)�”}”(hŒÓNote that, for functions like device_unregister which only accept a single pointer-sized argument, it's possible to automatically generate a wrapper with the ``KUNIT_DEFINE_ACTION_WRAPPER()`` macro, for example:”h]”(hŒ Note that, for functions like device_unregister which only accept a single pointer-sized argument, it’s possible to automatically generate a wrapper with the ”…”�”}”(hjîh²hh³Nh´Nubhù)�”}”(hŒ!``KUNIT_DEFINE_ACTION_WRAPPER()``”h]”hŒKUNIT_DEFINE_ACTION_WRAPPER()”…”�”}”(hjöh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjîubhŒ macro, for example:”…”�”}”(hjîh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mhjqh²hubj')�”}”(hŒ•KUNIT_DEFINE_ACTION_WRAPPER(device_unregister, device_unregister_wrapper, struct device *); kunit_add_action(test, &device_unregister_wrapper, &dev);”h]”hŒ•KUNIT_DEFINE_ACTION_WRAPPER(device_unregister, device_unregister_wrapper, struct device *); kunit_add_action(test, &device_unregister_wrapper, &dev);”…”�”}”hjsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7jìj9}”uh1j&h³hÇh´Mhjqh²hubhï)�”}”(hŒšYou should do this in preference to manually casting to the ``kunit_action_t`` type, as casting function pointers will break Control Flow Integrity (CFI).”h]”(hŒ #include ... VISIBLE_IF_KUNIT int do_interesting_thing() { ... } EXPORT_SYMBOL_IF_KUNIT(do_interesting_thing); /* In the header file "my_file.h" */ #if IS_ENABLED(CONFIG_KUNIT) int do_interesting_thing(void); #endif /* In the KUnit test file "my_file_test.c" */ #include #include ... MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); ... // Use do_interesting_thing() in tests”h]”hX /* In the file containing functions to test "my_file.c" */ #include #include ... VISIBLE_IF_KUNIT int do_interesting_thing() { ... } EXPORT_SYMBOL_IF_KUNIT(do_interesting_thing); /* In the header file "my_file.h" */ #if IS_ENABLED(CONFIG_KUNIT) int do_interesting_thing(void); #endif /* In the KUnit test file "my_file_test.c" */ #include #include ... MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); ... // Use do_interesting_thing() in tests”…”�”}”hjÄsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´Mhj¥h²hubhï)�”}”(hŒÓFor a full example, see this `patch `_ where a test is modified to conditionally expose static functions for testing using the macros above.”h]”(hŒFor a full example, see this ”…”�”}”(hjÓh²hh³Nh´NubhŒ reference”“”)�”}”(hŒP`patch `_”h]”hŒpatch”…”�”}”(hjÝh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œpatch”Œrefuri”ŒEhttps://lore.kernel.org/all/20221207014024.340230-3-rmoar@google.com/”uh1jÛhjÓubjô)�”}”(hŒH ”h]”h}”(h]”Œpatch”ah ]”h"]”Œpatch”ah$]”h&]”Œrefuri”jîuh1jóŒ referenced”KhjÓubhŒf where a test is modified to conditionally expose static functions for testing using the macros above.”…”�”}”(hjÓh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M:hj¥h²hubhï)�”}”(hŒ³As an **alternative** to the method above, you could conditionally ``#include`` the test file at the end of your .c file. This is not recommended but works if needed. For example:”h]”(hŒAs an ”…”�”}”(hjh²hh³Nh´NubjQ)�”}”(hŒ**alternative**”h]”hŒ alternative”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jPhjubhŒ. to the method above, you could conditionally ”…”�”}”(hjh²hh³Nh´Nubhù)�”}”(hŒ ``#include``”h]”hŒ#include”…”�”}”(hj!h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒd the test file at the end of your .c file. This is not recommended but works if needed. For example:”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M>hj¥h²hubj')�”}”(hŒw/* In "my_file.c" */ static int do_interesting_thing(); #ifdef CONFIG_MY_KUNIT_TEST #include "my_kunit_test.c" #endif”h]”hŒw/* In "my_file.c" */ static int do_interesting_thing(); #ifdef CONFIG_MY_KUNIT_TEST #include "my_kunit_test.c" #endif”…”�”}”hj9sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´MBhj¥h²hubeh}”(h]”Œtesting-static-functions”ah ]”h"]”Œtesting static functions”ah$]”h&]”uh1hÈhj›h²hh³hÇh´MubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒInjecting Test-Only Code”h]”hŒInjecting Test-Only Code”…”�”}”(hjSh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjPh²hh³hÇh´MMubhï)�”}”(hŒGSimilar to as shown above, we can add test-specific logic. For example:”h]”hŒGSimilar to as shown above, we can add test-specific logic. For example:”…”�”}”(hjah²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MOhjPh²hubj')�”}”(hŒ–/* In my_file.h */ #ifdef CONFIG_MY_KUNIT_TEST /* Defined in my_kunit_test.c */ void test_only_hook(void); #else void test_only_hook(void) { } #endif”h]”hŒ–/* In my_file.h */ #ifdef CONFIG_MY_KUNIT_TEST /* Defined in my_kunit_test.c */ void test_only_hook(void); #else void test_only_hook(void) { } #endif”…”�”}”hjosbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´MQhjPh²hubhï)�”}”(hŒ‹This test-only code can be made more useful by accessing the current ``kunit_test`` as shown in next section: *Accessing The Current Test*.”h]”(hŒEThis test-only code can be made more useful by accessing the current ”…”�”}”(hj~h²hh³Nh´Nubhù)�”}”(hŒ``kunit_test``”h]”hŒ kunit_test”…”�”}”(hj†h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj~ubhŒ as shown in next section: ”…”�”}”(hj~h²hh³Nh´Nubj )�”}”(hŒ*Accessing The Current Test*”h]”hŒAccessing The Current Test”…”�”}”(hj˜h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j hj~ubhŒ.”…”�”}”(hj~h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M\hjPh²hubeh}”(h]”Œinjecting-test-only-code”ah ]”h"]”Œinjecting test-only code”ah$]”h&]”uh1hÈhj›h²hh³hÇh´MMubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒAccessing The Current Test”h]”hŒAccessing The Current Test”…”�”}”(hj»h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj¸h²hh³hÇh´M`ubhï)�”}”(hXsIn some cases, we need to call test-only code from outside the test file. This is helpful, for example, when providing a fake implementation of a function, or to fail any current test from within an error handler. We can do this via the ``kunit_test`` field in ``task_struct``, which we can access using the ``kunit_get_current_test()`` function in ``kunit/test-bug.h``.”h]”(hŒîIn some cases, we need to call test-only code from outside the test file. This is helpful, for example, when providing a fake implementation of a function, or to fail any current test from within an error handler. We can do this via the ”…”�”}”(hjÉh²hh³Nh´Nubhù)�”}”(hŒ``kunit_test``”h]”hŒ kunit_test”…”�”}”(hjÑh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÉubhŒ field in ”…”�”}”(hjÉh²hh³Nh´Nubhù)�”}”(hŒ``task_struct``”h]”hŒ task_struct”…”�”}”(hjãh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÉubhŒ , which we can access using the ”…”�”}”(hjÉh²hh³Nh´Nubhù)�”}”(hŒ``kunit_get_current_test()``”h]”hŒkunit_get_current_test()”…”�”}”(hjõh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÉubhŒ function in ”…”�”}”(hjÉh²hh³Nh´Nubhù)�”}”(hŒ``kunit/test-bug.h``”h]”hŒkunit/test-bug.h”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÉubhŒ.”…”�”}”(hjÉh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mbhj¸h²hubhï)�”}”(hX,``kunit_get_current_test()`` is safe to call even if KUnit is not enabled. If KUnit is not enabled, or if no test is running in the current task, it will return ``NULL``. This compiles down to either a no-op or a static key check, so will have a negligible performance impact when no test is running.”h]”(hù)�”}”(hŒ``kunit_get_current_test()``”h]”hŒkunit_get_current_test()”…”�”}”(hj#h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒ… is safe to call even if KUnit is not enabled. If KUnit is not enabled, or if no test is running in the current task, it will return ”…”�”}”(hjh²hh³Nh´Nubhù)�”}”(hŒ``NULL``”h]”hŒNULL”…”�”}”(hj5h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjubhŒƒ. This compiles down to either a no-op or a static key check, so will have a negligible performance impact when no test is running.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mhhj¸h²hubhï)�”}”(hŒXThe example below uses this to implement a "mock" implementation of a function, ``foo``:”h]”(hŒTThe example below uses this to implement a “mockâ€� implementation of a function, ”…”�”}”(hjMh²hh³Nh´Nubhù)�”}”(hŒ``foo``”h]”hŒfoo”…”�”}”(hjUh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjMubhŒ:”…”�”}”(hjMh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mmhj¸h²hubj')�”}”(hX�#include /* for kunit_get_current_test */ struct test_data { int foo_result; int want_foo_called_with; }; static int fake_foo(int arg) { struct kunit *test = kunit_get_current_test(); struct test_data *test_data = test->priv; KUNIT_EXPECT_EQ(test, test_data->want_foo_called_with, arg); return test_data->foo_result; } static void example_simple_test(struct kunit *test) { /* Assume priv (private, a member used to pass test data from * the init function) is allocated in the suite's .init */ struct test_data *test_data = test->priv; test_data->foo_result = 42; test_data->want_foo_called_with = 1; /* In a real test, we'd probably pass a pointer to fake_foo somewhere * like an ops struct, etc. instead of calling it directly. */ KUNIT_EXPECT_EQ(test, fake_foo(1), 42); }”h]”hX�#include /* for kunit_get_current_test */ struct test_data { int foo_result; int want_foo_called_with; }; static int fake_foo(int arg) { struct kunit *test = kunit_get_current_test(); struct test_data *test_data = test->priv; KUNIT_EXPECT_EQ(test, test_data->want_foo_called_with, arg); return test_data->foo_result; } static void example_simple_test(struct kunit *test) { /* Assume priv (private, a member used to pass test data from * the init function) is allocated in the suite's .init */ struct test_data *test_data = test->priv; test_data->foo_result = 42; test_data->want_foo_called_with = 1; /* In a real test, we'd probably pass a pointer to fake_foo somewhere * like an ops struct, etc. instead of calling it directly. */ KUNIT_EXPECT_EQ(test, fake_foo(1), 42); }”…”�”}”hjmsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´Mohj¸h²hubhï)�”}”(hXIn this example, we are using the ``priv`` member of ``struct kunit`` as a way of passing data to the test from the init function. In general ``priv`` is pointer that can be used for any user data. This is preferred over static variables, as it avoids concurrency issues.”h]”(hŒ"In this example, we are using the ”…”�”}”(hj|h²hh³Nh´Nubhù)�”}”(hŒ``priv``”h]”hŒpriv”…”�”}”(hj„h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj|ubhŒ member of ”…”�”}”(hj|h²hh³Nh´Nubhù)�”}”(hŒ``struct kunit``”h]”hŒ struct kunit”…”�”}”(hj–h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj|ubhŒI as a way of passing data to the test from the init function. In general ”…”�”}”(hj|h²hh³Nh´Nubhù)�”}”(hŒ``priv``”h]”hŒpriv”…”�”}”(hj¨h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj|ubhŒy is pointer that can be used for any user data. This is preferred over static variables, as it avoids concurrency issues.”…”�”}”(hj|h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M�hj¸h²hubhï)�”}”(hXéHad we wanted something more flexible, we could have used a named ``kunit_resource``. Each test can have multiple resources which have string names providing the same flexibility as a ``priv`` member, but also, for example, allowing helper functions to create resources without conflicting with each other. It is also possible to define a clean up function for each resource, making it easy to avoid resource leaks. For more information, see Documentation/dev-tools/kunit/api/resource.rst.”h]”(hŒBHad we wanted something more flexible, we could have used a named ”…”�”}”(hjÀh²hh³Nh´Nubhù)�”}”(hŒ``kunit_resource``”h]”hŒkunit_resource”…”�”}”(hjÈh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÀubhŒd. Each test can have multiple resources which have string names providing the same flexibility as a ”…”�”•5}”(hjÀh²hh³Nh´Nubhù)�”}”(hŒ``priv``”h]”hŒpriv”…”�”}”(hjÚh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÀubhX) member, but also, for example, allowing helper functions to create resources without conflicting with each other. It is also possible to define a clean up function for each resource, making it easy to avoid resource leaks. For more information, see Documentation/dev-tools/kunit/api/resource.rst.”…”�”}”(hjÀh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M”hj¸h²hubeh}”(h]”Œaccessing-the-current-test”ah ]”h"]”Œaccessing the current test”ah$]”h&]”uh1hÈhj›h²hh³hÇh´M`ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒFailing The Current Test”h]”hŒFailing The Current Test”…”�”}”(hjýh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjúh²hh³hÇh´Mœubhï)�”}”(hX!If we want to fail the current test, we can use ``kunit_fail_current_test(fmt, args...)`` which is defined in ```` and does not require pulling in ````. For example, we have an option to enable some extra debug checks on some data structures as shown below:”h]”(hŒ0If we want to fail the current test, we can use ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ)``kunit_fail_current_test(fmt, args...)``”h]”hŒ%kunit_fail_current_test(fmt, args...)”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒ which is defined in ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ````”h]”hŒ”…”�”}”(hj%h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒ! and does not require pulling in ”…”�”}”(hj h²hh³Nh´Nubhù)�”}”(hŒ````”h]”hŒ”…”�”}”(hj7h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj ubhŒj. For example, we have an option to enable some extra debug checks on some data structures as shown below:”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mžhjúh²hubj')�”}”(hXU#include #ifdef CONFIG_EXTRA_DEBUG_CHECKS static void validate_my_data(struct data *data) { if (is_valid(data)) return; kunit_fail_current_test("data %p is invalid", data); /* Normal, non-KUnit, error reporting code here. */ } #else static void my_debug_function(void) { } #endif”h]”hXU#include #ifdef CONFIG_EXTRA_DEBUG_CHECKS static void validate_my_data(struct data *data) { if (is_valid(data)) return; kunit_fail_current_test("data %p is invalid", data); /* Normal, non-KUnit, error reporting code here. */ } #else static void my_debug_function(void) { } #endif”…”�”}”hjOsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´M£hjúh²hubhï)�”}”(hX(``kunit_fail_current_test()`` is safe to call even if KUnit is not enabled. If KUnit is not enabled, or if no test is running in the current task, it will do nothing. This compiles down to either a no-op or a static key check, so will have a negligible performance impact when no test is running.”h]”(hù)�”}”(hŒ``kunit_fail_current_test()``”h]”hŒkunit_fail_current_test()”…”�”}”(hjbh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj^ubhX  is safe to call even if KUnit is not enabled. If KUnit is not enabled, or if no test is running in the current task, it will do nothing. This compiles down to either a no-op or a static key check, so will have a negligible performance impact when no test is running.”…”�”}”(hj^h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´Mµhjúh²hubeh}”(h]”Œfailing-the-current-test”ah ]”h"]”Œfailing the current test”ah$]”h&]”uh1hÈhj›h²hh³hÇh´MœubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ!Managing Fake Devices and Drivers”h]”hŒ!Managing Fake Devices and Drivers”…”�”}”(hj…h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj‚h²hh³hÇh´M»ubhï)�”}”(hXWhen testing drivers or code which interacts with drivers, many functions will require a ``struct device`` or ``struct device_driver``. In many cases, setting up a real device is not required to test any given function, so a fake device can be used instead.”h]”(hŒYWhen testing drivers or code which interacts with drivers, many functions will require a ”…”�”}”(hj“h²hh³Nh´Nubhù)�”}”(hŒ``struct device``”h]”hŒ struct device”…”�”}”(hj›h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj“ubhŒ or ”…”�”}”(hj“h²hh³Nh´Nubhù)�”}”(hŒ``struct device_driver``”h]”hŒstruct device_driver”…”�”}”(hj­h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj“ubhŒ{. In many cases, setting up a real device is not required to test any given function, so a fake device can be used instead.”…”�”}”(hj“h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M½hj‚h²hubhï)�”}”(hX%KUnit provides helper functions to create and manage these fake devices, which are internally of type ``struct kunit_device``, and are attached to a special ``kunit_bus``. These devices support managed device resources (devres), as described in Documentation/driver-api/driver-model/devres.rst”h]”(hŒfKUnit provides helper functions to create and manage these fake devices, which are internally of type ”…”�”}”(hjÅh²hh³Nh´Nubhù)�”}”(hŒ``struct kunit_device``”h]”hŒstruct kunit_device”…”�”}”(hjÍh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÅubhŒ , and are attached to a special ”…”�”}”(hjÅh²hh³Nh´Nubhù)�”}”(hŒ ``kunit_bus``”h]”hŒ kunit_bus”…”�”}”(hjßh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjÅubhŒ{. These devices support managed device resources (devres), as described in Documentation/driver-api/driver-model/devres.rst”…”�”}”(hjÅh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÂhj‚h²hubhï)�”}”(hX+To create a KUnit-managed ``struct device_driver``, use ``kunit_driver_create()``, which will create a driver with the given name, on the ``kunit_bus``. This driver will automatically be destroyed when the corresponding test finishes, but can also be manually destroyed with ``driver_unregister()``.”h]”(hŒTo create a KUnit-managed ”…”�”}”(hj÷h²hh³Nh´Nubhù)�”}”(hŒ``struct device_driver``”h]”hŒstruct device_driver”…”�”}”(hjÿh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj÷ubhŒ, use ”…”�”}”(hj÷h²hh³Nh´Nubhù)�”}”(hŒ``kunit_driver_create()``”h]”hŒkunit_driver_create()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj÷ubhŒ9, which will create a driver with the given name, on the ”…”�”}”(hj÷h²hh³Nh´Nubhù)�”}”(hŒ ``kunit_bus``”h]”hŒ kunit_bus”…”�”}”(hj#h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj÷ubhŒ|. This driver will automatically be destroyed when the corresponding test finishes, but can also be manually destroyed with ”…”�”}”(hj÷h²hh³Nh´Nubhù)�”}”(hŒ``driver_unregister()``”h]”hŒdriver_unregister()”…”�”}”(hj5h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj÷ubhŒ.”…”�”}”(hj÷h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÇhj‚h²hubhï)�”}”(hXÉTo create a fake device, use the ``kunit_device_register()``, which will create and register a device, using a new KUnit-managed driver created with ``kunit_driver_create()``. To provide a specific, non-KUnit-managed driver, use ``kunit_device_register_with_driver()`` instead. Like with managed drivers, KUnit-managed fake devices are automatically cleaned up when the test finishes, but can be manually cleaned up early with ``kunit_device_unregister()``.”h]”(hŒ!To create a fake device, use the ”…”�”}”(hjMh²hh³Nh´Nubhù)�”}”(hŒ``kunit_device_register()``”h]”hŒkunit_device_register()”…”�”}”(hjUh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjMubhŒY, which will create and register a device, using a new KUnit-managed driver created with ”…”�”}”(hjMh²hh³Nh´Nubhù)�”}”(hŒ``kunit_driver_create()``”h]”hŒkunit_driver_create()”…”�”}”(hjgh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjMubhŒ7. To provide a specific, non-KUnit-managed driver, use ”…”�”}”(hjMh²hh³Nh´Nubhù)�”}”(hŒ'``kunit_device_register_with_driver()``”h]”hŒ#kunit_device_register_with_driver()”…”�”}”(hjyh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjMubhŒŸ instead. Like with managed drivers, KUnit-managed fake devices are automatically cleaned up when the test finishes, but can be manually cleaned up early with ”…”�”}”(hjMh²hh³Nh´Nubhù)�”}”(hŒ``kunit_device_unregister()``”h]”hŒkunit_device_unregister()”…”�”}”(hj‹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhjMubhŒ.”…”�”}”(hjMh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÌhj‚h²hubhï)�”}”(hŒ¹The KUnit devices should be used in preference to ``root_device_register()``, and instead of ``platform_device_register()`` in cases where the device is not otherwise a platform device.”h]”(hŒ2The KUnit devices should be used in preference to ”…”�”}”(hj£h²hh³Nh´Nubhù)�”}”(hŒ``root_device_register()``”h]”hŒroot_device_register()”…”�”}”(hj«h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj£ubhŒ, and instead of ”…”�”}”(hj£h²hh³Nh´Nubhù)�”}”(hŒ``platform_device_register()``”h]”hŒplatform_device_register()”…”�”}”(hj½h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1høhj£ubhŒ> in cases where the device is not otherwise a platform device.”…”�”}”(hj£h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´MÓhj‚h²hubhï)�”}”(hŒ For example:”h]”hŒ For example:”…”�”}”(hjÕh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´M×hj‚h²hubj')�”}”(hXù#include static void test_my_device(struct kunit *test) { struct device *fake_device; const char *dev_managed_string; // Create a fake device. fake_device = kunit_device_register(test, "my_device"); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_device) // Pass it to functions which need a device. dev_managed_string = devm_kstrdup(fake_device, "Hello, World!"); // Everything is cleaned up automatically when the test ends. }”h]”hXù#include static void test_my_device(struct kunit *test) { struct device *fake_device; const char *dev_managed_string; // Create a fake device. fake_device = kunit_device_register(test, "my_device"); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_device) // Pass it to functions which need a device. dev_managed_string = devm_kstrdup(fake_device, "Hello, World!"); // Everything is cleaned up automatically when the test ends. }”…”�”}”hjãsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj6‰j7j8j9}”uh1j&h³hÇh´MÙhj‚h²hubeh}”(h]”Œ!managing-fake-devices-and-drivers”ah ]”h"]”Œ!managing fake devices and drivers”ah$]”h&]”uh1hÈhj›h²hh³hÇh´M»ubeh}”(h]”Œcommon-patterns”ah ]”h"]”Œcommon patterns”ah$]”h&]”uh1hÈhhh²hh³hÇh´M!ubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÇuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)�”}”(hÍNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”j%Œerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÇŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”j]”jõasŒnameids”}”(j˜j•j‹jˆjÇjÄjƒj€j&j#jj jjjŒjj‹jˆjÿjüjÇ jÄ j jýj¿ j¼ jâjßjÚj×jñjîjÒjÏjnjkj¢jŸjMjJjøjõjµj²j÷jôjj|j÷jôuŒ nametypes”}”(j˜‰j‹‰jljjƒ‰j&‰j‰j‰jŒˆj‹‰jÿ‰jÇ ‰j ‰j¿ ‰jâ‰jÚ‰jñ‰jÒ‰jn‰j¢‰jM‰jøˆjµ‰j÷‰j‰j÷‰uh}”(j•hÊjˆhÝjÄjj€jÊj#jŽj j)jjjjjˆjjüj›jÄ j¬jýjËj¼ j jßjÊ j×jƒ jîjô jÏjôjkjåjŸjqjJj¥jõjïj²jPjôj¸j|jújôj‚uŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”…”R”Œparse_messages”]”Œtransform_messages”]”hŒsystem_message”“”)�”}”(hhh]”hï)�”}”(hhh]”hŒ6Hyperlink target "kunit-on-non-uml" is not referenced.”…”�”}”hj�sbah}”(h]”h ]”h"]”h$]”h&]”uh1hîhjŒubah}”(h]”h ]”h"]”h$]”h&]”Œlevel”KŒtype”ŒINFO”Œsource”hÇŒline”Muh1jŠubaŒ transformer”NŒ include_log”]”Œ decoration”Nh²hub.