<feed xmlns='http://www.w3.org/2005/Atom'>
<title>linux/lib/kunit/test.c, branch v5.19</title>
<subtitle>Mirror of https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/
</subtitle>
<id>https://git.shady.money/linux/atom?h=v5.19</id>
<link rel='self' href='https://git.shady.money/linux/atom?h=v5.19'/>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/'/>
<updated>2022-05-16T19:23:00Z</updated>
<entry>
<title>kunit: take `kunit_assert` as `const`</title>
<updated>2022-05-16T19:23:00Z</updated>
<author>
<name>Miguel Ojeda</name>
<email>ojeda@kernel.org</email>
</author>
<published>2022-05-02T09:36:25Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=7466886b400b1904ce30fa311904849e314a2cf4'/>
<id>urn:sha1:7466886b400b1904ce30fa311904849e314a2cf4</id>
<content type='text'>
The `kunit_do_failed_assertion` function passes its
`struct kunit_assert` argument to `kunit_fail`. This one,
in turn, calls its `format` field passing the assert again
as a `const` pointer.

Therefore, the whole chain may be made `const`.

Signed-off-by: Miguel Ojeda &lt;ojeda@kernel.org&gt;
Reviewed-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Reviewed-by: Kees Cook &lt;keescook@chromium.org&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>kunit: add ability to specify suite-level init and exit functions</title>
<updated>2022-05-02T18:35:51Z</updated>
<author>
<name>Daniel Latypov</name>
<email>dlatypov@google.com</email>
</author>
<published>2022-04-29T18:12:57Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=1cdba21db2ca31514c60b9732fc3963ae24c59e0'/>
<id>urn:sha1:1cdba21db2ca31514c60b9732fc3963ae24c59e0</id>
<content type='text'>
KUnit has support for setup/cleanup logic for each test case in a suite.
But it lacks the ability to specify setup/cleanup for the entire suite
itself.

This can be used to do setup that is too expensive or cumbersome to do
for each test.
Or it can be used to do simpler things like log debug information after
the suite completes.
It's a fairly common feature, so the lack of it is noticeable.

Some examples in other frameworks and languages:
* https://docs.python.org/3/library/unittest.html#setupclass-and-teardownclass
* https://google.github.io/googletest/reference/testing.html#Test::SetUpTestSuite

Meta:
This is very similar to this patch here: https://lore.kernel.org/linux-kselftest/20210805043503.20252-3-bvanassche@acm.org/
The changes from that patch:
* pass in `struct kunit *` so users can do stuff like
  `kunit_info(suite, "debug message")`
* makes sure the init failure is bubbled up as a failure
* updates kunit-example-test.c to use a suite init
* Updates kunit/usage.rst to mention the new support
* some minor cosmetic things
  * use `suite_{init,exit}` instead of `{init/exit}_suite`
  * make suite init error message more consistent w/ test init
  * etc.

Signed-off-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Reviewed-by: David Gow &lt;davidgow@google.com&gt;
Reviewed-by: Brendan Higgins &lt;brendanhiggins@google.com&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>kunit: rename print_subtest_{start,end} for clarity (s/subtest/suite)</title>
<updated>2022-05-02T18:35:39Z</updated>
<author>
<name>Daniel Latypov</name>
<email>dlatypov@google.com</email>
</author>
<published>2022-04-29T18:12:56Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=cae56e1740f559703c94b7f4d772d873b8a01395'/>
<id>urn:sha1:cae56e1740f559703c94b7f4d772d873b8a01395</id>
<content type='text'>
These names sound more general than they are.

The _end() function increments a `static int kunit_suite_counter`, so it
can only safely be called on suites, aka top-level subtests.
It would need to have a separate counter for each level of subtest to be
generic enough.

So rename it to make it clear it's only appropriate for suites.

Signed-off-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Reviewed-by: David Gow &lt;davidgow@google.com&gt;
Reviewed-by: Brendan Higgins &lt;brendanhiggins@google.com&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>kunit: split resource API impl from test.c into new resource.c</title>
<updated>2022-04-04T22:23:14Z</updated>
<author>
<name>Daniel Latypov</name>
<email>dlatypov@google.com</email>
</author>
<published>2022-03-28T17:41:43Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=cdebea6968faafa955b3cc9196003e7f17f78955'/>
<id>urn:sha1:cdebea6968faafa955b3cc9196003e7f17f78955</id>
<content type='text'>
We've split out the declarations from include/kunit/test.h into
resource.h.
This patch splits out the definitions as well for consistency.

A side effect of this is git blame won't properly track history by
default, users need to run
$ git blame -L ,1 -C13 lib/kunit/resource.c

Signed-off-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Reviewed-by: David Gow &lt;davidgow@google.com&gt;
Reviewed-by: Brendan Higgins &lt;brendanhiggins@google.com&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>kunit: remove va_format from kunit_assert</title>
<updated>2022-01-31T18:55:27Z</updated>
<author>
<name>Daniel Latypov</name>
<email>dlatypov@google.com</email>
</author>
<published>2022-01-25T21:00:09Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=6419abb80e82c603bbec6d7f5af6c2f79fa5c4ae'/>
<id>urn:sha1:6419abb80e82c603bbec6d7f5af6c2f79fa5c4ae</id>
<content type='text'>
The concern is that having a lot of redundant fields in kunit_assert can
blow up stack usage if the compiler doesn't optimize them away [1].

The comment on this field implies that it was meant to be initialized
when the expect/assert was declared, but this only happens when we run
kunit_do_failed_assertion().

We don't need to access it outside of that function, so move it out of
the struct and make it a local variable there.

This change also takes the chance to reduce the number of macros by
inlining the now simplified KUNIT_INIT_ASSERT_STRUCT() macro.

[1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ

Signed-off-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Reviewed-by: David Gow &lt;davidgow@google.com&gt;
Reviewed-by: Brendan Higgins &lt;brendanhiggins@google.com&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>kunit: split out part of kunit_assert into a static const</title>
<updated>2022-01-25T19:49:59Z</updated>
<author>
<name>Daniel Latypov</name>
<email>dlatypov@google.com</email>
</author>
<published>2022-01-13T16:59:30Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=21957f90b28f6bc118c055e3e564d45f6e4df45d'/>
<id>urn:sha1:21957f90b28f6bc118c055e3e564d45f6e4df45d</id>
<content type='text'>
This is per Linus's suggestion in [1].

The issue there is that every KUNIT_EXPECT/KUNIT_ASSERT puts a
kunit_assert object onto the stack. Normally we rely on compilers to
elide this, but when that doesn't work out, this blows up the stack
usage of kunit test functions.

We can move some data off the stack by making it static.
This change introduces a new `struct kunit_loc` to hold the file and
line number and then just passing assert_type (EXPECT or ASSERT) as an
argument.

In [1], it was suggested to also move out the format string as well, but
users could theoretically craft a format string at runtime, so we can't.

This change leaves a copy of `assert_type` in kunit_assert for now
because cleaning up all the macros to not pass it around is a bit more
involved.

Here's an example of the expanded code for KUNIT_FAIL():
if (__builtin_expect(!!(!(false)), 0)) {
  static const struct kunit_loc loc = { .file = ... };
  struct kunit_fail_assert __assertion = { .assert = { .type ...  };
  kunit_do_failed_assertion(test, &amp;loc, KUNIT_EXPECTATION, &amp;__assertion.assert, ...);
};

[1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ

Signed-off-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Suggested-by: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
Reviewed-by: David Gow &lt;davidgow@google.com&gt;
Reviewed-by: Brendan Higgins &lt;brendanhiggins@google.com&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>kunit: factor out kunit_base_assert_format() call into kunit_fail()</title>
<updated>2022-01-25T19:49:53Z</updated>
<author>
<name>Daniel Latypov</name>
<email>dlatypov@google.com</email>
</author>
<published>2022-01-13T16:59:29Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=dd640d70874bd27fb081d444252677766321c32f'/>
<id>urn:sha1:dd640d70874bd27fb081d444252677766321c32f</id>
<content type='text'>
We call this function first thing for all the assertion `format()`
functions.
This is the part that prints the file and line number and assertion type
(EXPECTATION, ASSERTION).

Having it as part of the format functions lets us have the flexibility
to not print that information (or print it differently) for new
assertion types, but I think this we don't need that.

And in the future, we'd like to consider factoring that data (file,
line#, type) out of the kunit_assert struct and into a `static`
variable, as Linus suggested [1], so we'd need to extract it anyways.

[1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ

Signed-off-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Reviewed-by: David Gow &lt;davidgow@google.com&gt;
Reviewed-by: Brendan Higgins &lt;brendanhiggins@google.com&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>kunit: move check if assertion passed into the macros</title>
<updated>2022-01-25T19:49:40Z</updated>
<author>
<name>Daniel Latypov</name>
<email>dlatypov@google.com</email>
</author>
<published>2022-01-13T16:59:27Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=4fdacef8ac5a5382eeb1bc6fc2632d71a09d52cd'/>
<id>urn:sha1:4fdacef8ac5a5382eeb1bc6fc2632d71a09d52cd</id>
<content type='text'>
Currently the code always calls kunit_do_assertion() even though it does
nothing when `pass` is true.

This change moves the `if(!(pass))` check into the macro instead
and renames the function to kunit_do_failed_assertion().
I feel this a bit easier to read and understand.

This has the potential upside of avoiding a function call that does
nothing most of the time (assuming your tests are passing) but comes
with the downside of generating a bit more code and branches. We try to
mitigate the branches by tagging them with `unlikely()`.

This also means we don't have to initialize structs that we don't need,
which will become a tiny bit more expensive if we switch over to using
static variables to try and reduce stack usage. (There's runtime code
to check if the variable has been initialized yet or not).

Signed-off-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Reviewed-by: Brendan Higgins &lt;brendanhiggins@google.com&gt;
Reviewed-by: David Gow &lt;davidgow@google.com&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>kunit: Report test parameter results as (K)TAP subtests</title>
<updated>2021-12-13T20:36:29Z</updated>
<author>
<name>David Gow</name>
<email>davidgow@google.com</email>
</author>
<published>2021-11-02T07:30:14Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=44b7da5fcd4c99de1ec5cc783cdd605398246280'/>
<id>urn:sha1:44b7da5fcd4c99de1ec5cc783cdd605398246280</id>
<content type='text'>
Currently, the results for individial parameters in a parameterised test
are simply output as (K)TAP diagnostic lines.

As kunit_tool now supports nested subtests, report each parameter as its
own subtest.

For example, here's what the output now looks like:
	# Subtest: inode_test_xtimestamp_decoding
	ok 1 - 1901-12-13 Lower bound of 32bit &lt; 0 timestamp, no extra bits
	ok 2 - 1969-12-31 Upper bound of 32bit &lt; 0 timestamp, no extra bits
	ok 3 - 1970-01-01 Lower bound of 32bit &gt;=0 timestamp, no extra bits
	ok 4 - 2038-01-19 Upper bound of 32bit &gt;=0 timestamp, no extra bits
	ok 5 - 2038-01-19 Lower bound of 32bit &lt;0 timestamp, lo extra sec bit on
	ok 6 - 2106-02-07 Upper bound of 32bit &lt;0 timestamp, lo extra sec bit on
	ok 7 - 2106-02-07 Lower bound of 32bit &gt;=0 timestamp, lo extra sec bit on
	ok 8 - 2174-02-25 Upper bound of 32bit &gt;=0 timestamp, lo extra sec bit on
	ok 9 - 2174-02-25 Lower bound of 32bit &lt;0 timestamp, hi extra sec bit on
	ok 10 - 2242-03-16 Upper bound of 32bit &lt;0 timestamp, hi extra sec bit on
	ok 11 - 2242-03-16 Lower bound of 32bit &gt;=0 timestamp, hi extra sec bit on
	ok 12 - 2310-04-04 Upper bound of 32bit &gt;=0 timestamp, hi extra sec bit on
	ok 13 - 2310-04-04 Upper bound of 32bit&gt;=0 timestamp, hi extra sec bit 1. 1 ns
	ok 14 - 2378-04-22 Lower bound of 32bit&gt;= timestamp. Extra sec bits 1. Max ns
	ok 15 - 2378-04-22 Lower bound of 32bit &gt;=0 timestamp. All extra sec bits on
	ok 16 - 2446-05-10 Upper bound of 32bit &gt;=0 timestamp. All extra sec bits on
	# inode_test_xtimestamp_decoding: pass:16 fail:0 skip:0 total:16
	ok 1 - inode_test_xtimestamp_decoding

Signed-off-by: David Gow &lt;davidgow@google.com&gt;
Reviewed-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Reviewed-by: Brendan Higgins &lt;brendanhiggins@google.com&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
<entry>
<title>kunit: Don't crash if no parameters are generated</title>
<updated>2021-12-13T20:36:21Z</updated>
<author>
<name>David Gow</name>
<email>davidgow@google.com</email>
</author>
<published>2021-11-02T07:30:13Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/linux/commit/?id=37dbb4c7c7442dbfc9b651e4ddd4afe30b26afc9'/>
<id>urn:sha1:37dbb4c7c7442dbfc9b651e4ddd4afe30b26afc9</id>
<content type='text'>
It's possible that a parameterised test could end up with zero
parameters. At the moment, the test function will nevertheless be called
with NULL as the parameter. Instead, don't try to run the test code, and
just mark the test as SKIPped.

Reported-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Signed-off-by: David Gow &lt;davidgow@google.com&gt;
Reviewed-by: Daniel Latypov &lt;dlatypov@google.com&gt;
Reviewed-by: Brendan Higgins &lt;brendanhiggins@google.com&gt;
Signed-off-by: Shuah Khan &lt;skhan@linuxfoundation.org&gt;
</content>
</entry>
</feed>
