Skip to content

Commit 65af9b9

Browse files
dlatypovshuahkh
authored andcommitted
kunit: don't show 1 == 1 in failed assertion messages
Currently, given something (fairly dystopian) like > KUNIT_EXPECT_EQ(test, 2 + 2, 5) KUnit will prints a failure message like this. > Expected 2 + 2 == 5, but > 2 + 2 == 4 > 5 == 5 With this patch, the output just becomes > Expected 2 + 2 == 5, but > 2 + 2 == 4 This patch is slightly hacky, but it's quite common* to compare an expression to a literal integer value, so this can make KUnit less chatty in many cases. (This patch also fixes variants like KUNIT_EXPECT_GT, LE, et al.). It also allocates an additional string briefly, but given this only happens on test failures, it doesn't seem too bad a tradeoff. Also, in most cases it'll realize the lengths are unequal and bail out before the allocation. We could save the result of the formatted string to avoid wasting this extra work, but it felt cleaner to leave it as-is. Edge case: for something silly and unrealistic like > KUNIT_EXPECT_EQ(test, 4, 5); It'll generate this message with a trailing "but" > Expected 4 == 5, but > <next line of normal output> It didn't feel worth adding a check up-front to see if both sides are literals to handle this better. *A quick grep suggests 100+ comparisons to an integer literal as the right hand side. Signed-off-by: Daniel Latypov <[email protected]> Tested-by: David Gow <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 243180f commit 65af9b9

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

lib/kunit/assert.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,29 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
8585
}
8686
EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
8787

88+
/* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */
89+
static bool is_literal(struct kunit *test, const char *text, long long value,
90+
gfp_t gfp)
91+
{
92+
char *buffer;
93+
int len;
94+
bool ret;
95+
96+
len = snprintf(NULL, 0, "%lld", value);
97+
if (strlen(text) != len)
98+
return false;
99+
100+
buffer = kunit_kmalloc(test, len+1, gfp);
101+
if (!buffer)
102+
return false;
103+
104+
snprintf(buffer, len+1, "%lld", value);
105+
ret = strncmp(buffer, text, len) == 0;
106+
107+
kunit_kfree(test, buffer);
108+
return ret;
109+
}
110+
88111
void kunit_binary_assert_format(const struct kunit_assert *assert,
89112
struct string_stream *stream)
90113
{
@@ -97,12 +120,16 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
97120
binary_assert->left_text,
98121
binary_assert->operation,
99122
binary_assert->right_text);
100-
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
101-
binary_assert->left_text,
102-
binary_assert->left_value);
103-
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
104-
binary_assert->right_text,
105-
binary_assert->right_value);
123+
if (!is_literal(stream->test, binary_assert->left_text,
124+
binary_assert->left_value, stream->gfp))
125+
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
126+
binary_assert->left_text,
127+
binary_assert->left_value);
128+
if (!is_literal(stream->test, binary_assert->right_text,
129+
binary_assert->right_value, stream->gfp))
130+
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
131+
binary_assert->right_text,
132+
binary_assert->right_value);
106133
kunit_assert_print_msg(assert, stream);
107134
}
108135
EXPORT_SYMBOL_GPL(kunit_binary_assert_format);

0 commit comments

Comments
 (0)