Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kunit: log numbers in decimal and hex

When KUNIT_EXPECT_EQ() or KUNIT_ASSERT_EQ() log a failure, they log the
two values being compared, with numerical values logged in decimal.

In some cases, decimal output is painful to consume, and hexadecimal
output would be more helpful. For example, this is the case for tests
I'm currently developing for the arm64 insn encoding/decoding code,
where comparing two 32-bit instruction opcodes results in output such
as:

| # test_insn_add_shifted_reg: EXPECTATION FAILED at arch/arm64/lib/test_insn.c:2791
| Expected obj_insn == gen_insn, but
| obj_insn == 2332164128
| gen_insn == 1258422304

To make this easier to consume, this patch logs the values in both
decimal and hexadecimal:

| # test_insn_add_shifted_reg: EXPECTATION FAILED at arch/arm64/lib/test_insn.c:2791
| Expected obj_insn == gen_insn, but
| obj_insn == 2332164128 (0x8b020020)
| gen_insn == 1258422304 (0x4b020020)

As can be seen from the example, having hexadecimal makes it
significantly easier for a human to spot which specific bits are
incorrect.

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Brendan Higgins <brendan.higgins@linux.dev>
Cc: David Gow <davidgow@google.com>
Cc: linux-kselftest@vger.kernel.org
Cc: kunit-dev@googlegroups.com
Acked-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Mark Rutland and committed by
Shuah Khan
7b1dd2cf 247f34f7

+4 -2
+4 -2
lib/kunit/assert.c
··· 127 127 binary_assert->text->right_text); 128 128 if (!is_literal(stream->test, binary_assert->text->left_text, 129 129 binary_assert->left_value, stream->gfp)) 130 - string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n", 130 + string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)\n", 131 131 binary_assert->text->left_text, 132 + binary_assert->left_value, 132 133 binary_assert->left_value); 133 134 if (!is_literal(stream->test, binary_assert->text->right_text, 134 135 binary_assert->right_value, stream->gfp)) 135 - string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld", 136 + string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)", 136 137 binary_assert->text->right_text, 138 + binary_assert->right_value, 137 139 binary_assert->right_value); 138 140 kunit_assert_print_msg(message, stream); 139 141 }