kunit: don't show `1 == 1` in failed assertion messages
authorDaniel Latypov <dlatypov@google.com>
Fri, 5 Feb 2021 22:14:09 +0000 (14:14 -0800)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 8 Feb 2021 22:43:34 +0000 (15:43 -0700)
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 <dlatypov@google.com>
Tested-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
lib/kunit/assert.c

index 33acdaa28a7d9c6412ff4ec6a470615217da6dd2..e0ec7d6fed6fef485e3768bbae6f8ac947862033 100644 (file)
@@ -85,6 +85,29 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
 }
 EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
 
+/* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */
+static bool is_literal(struct kunit *test, const char *text, long long value,
+                      gfp_t gfp)
+{
+       char *buffer;
+       int len;
+       bool ret;
+
+       len = snprintf(NULL, 0, "%lld", value);
+       if (strlen(text) != len)
+               return false;
+
+       buffer = kunit_kmalloc(test, len+1, gfp);
+       if (!buffer)
+               return false;
+
+       snprintf(buffer, len+1, "%lld", value);
+       ret = strncmp(buffer, text, len) == 0;
+
+       kunit_kfree(test, buffer);
+       return ret;
+}
+
 void kunit_binary_assert_format(const struct kunit_assert *assert,
                                struct string_stream *stream)
 {
@@ -97,12 +120,16 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
                          binary_assert->left_text,
                          binary_assert->operation,
                          binary_assert->right_text);
-       string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
-                         binary_assert->left_text,
-                         binary_assert->left_value);
-       string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
-                         binary_assert->right_text,
-                         binary_assert->right_value);
+       if (!is_literal(stream->test, binary_assert->left_text,
+                       binary_assert->left_value, stream->gfp))
+               string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
+                                 binary_assert->left_text,
+                                 binary_assert->left_value);
+       if (!is_literal(stream->test, binary_assert->right_text,
+                       binary_assert->right_value, stream->gfp))
+               string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
+                                 binary_assert->right_text,
+                                 binary_assert->right_value);
        kunit_assert_print_msg(assert, stream);
 }
 EXPORT_SYMBOL_GPL(kunit_binary_assert_format);