mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-26 05:34:13 +08:00
2b6861e237
If the compiler doesn't optimize them away, each kunit assertion (use of KUNIT_EXPECT_EQ, etc.) can use 88 bytes of stack space in the worst and most common case. This has led to compiler warnings and a suggestion from Linus to move data from the structs into static const's where possible [1]. This builds upon [2] which did so for the base struct kunit_assert type. That only reduced sizeof(struct kunit_binary_assert) from 88 to 64. Given these are by far the most commonly used asserts, this patch factors out the textual representations of the operands and comparator into another static const, saving 16 more bytes. In detail, KUNIT_EXPECT_EQ(test, 2 + 2, 5) yields the following struct (struct kunit_binary_assert) { .assert = <struct kunit_assert>, .operation = "==", .left_text = "2 + 2", .left_value = 4, .right_text = "5", .right_value = 5, } After this change static const struct kunit_binary_assert_text __text = { .operation = "==", .left_text = "2 + 2", .right_text = "5", }; (struct kunit_binary_assert) { .assert = <struct kunit_assert>, .text = &__text, .left_value = 4, .right_value = 5, } This also DRYs the code a bit more since these str fields were repeated for the string and pointer versions of kunit_binary_assert. Note: we could name the kunit_binary_assert_text fields left/right instead of left_text/right_text. But that would require changing the macros a bit since they have args called "left" and "right" which would be substituted in `.left = #left` as `.2 + 2 = \"2 + 2\"`. [1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ [2] https://lore.kernel.org/linux-kselftest/20220113165931.451305-6-dlatypov@google.com/ Signed-off-by: Daniel Latypov <dlatypov@google.com> Reviewed-by: David Gow <davidgow@google.com> Reviewed-by: Brendan Higgins <brendanhiggins@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
207 lines
6.2 KiB
C
207 lines
6.2 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Assertion and expectation serialization API.
|
|
*
|
|
* Copyright (C) 2019, Google LLC.
|
|
* Author: Brendan Higgins <brendanhiggins@google.com>
|
|
*/
|
|
#include <kunit/assert.h>
|
|
#include <kunit/test.h>
|
|
|
|
#include "string-stream.h"
|
|
|
|
void kunit_assert_prologue(const struct kunit_loc *loc,
|
|
enum kunit_assert_type type,
|
|
struct string_stream *stream)
|
|
{
|
|
const char *expect_or_assert = NULL;
|
|
|
|
switch (type) {
|
|
case KUNIT_EXPECTATION:
|
|
expect_or_assert = "EXPECTATION";
|
|
break;
|
|
case KUNIT_ASSERTION:
|
|
expect_or_assert = "ASSERTION";
|
|
break;
|
|
}
|
|
|
|
string_stream_add(stream, "%s FAILED at %s:%d\n",
|
|
expect_or_assert, loc->file, loc->line);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_assert_prologue);
|
|
|
|
static void kunit_assert_print_msg(const struct va_format *message,
|
|
struct string_stream *stream)
|
|
{
|
|
if (message->fmt)
|
|
string_stream_add(stream, "\n%pV", message);
|
|
}
|
|
|
|
void kunit_fail_assert_format(const struct kunit_assert *assert,
|
|
const struct va_format *message,
|
|
struct string_stream *stream)
|
|
{
|
|
string_stream_add(stream, "%pV", message);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
|
|
|
|
void kunit_unary_assert_format(const struct kunit_assert *assert,
|
|
const struct va_format *message,
|
|
struct string_stream *stream)
|
|
{
|
|
struct kunit_unary_assert *unary_assert;
|
|
|
|
unary_assert = container_of(assert, struct kunit_unary_assert, assert);
|
|
|
|
if (unary_assert->expected_true)
|
|
string_stream_add(stream,
|
|
KUNIT_SUBTEST_INDENT "Expected %s to be true, but is false\n",
|
|
unary_assert->condition);
|
|
else
|
|
string_stream_add(stream,
|
|
KUNIT_SUBTEST_INDENT "Expected %s to be false, but is true\n",
|
|
unary_assert->condition);
|
|
kunit_assert_print_msg(message, stream);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
|
|
|
|
void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
|
|
const struct va_format *message,
|
|
struct string_stream *stream)
|
|
{
|
|
struct kunit_ptr_not_err_assert *ptr_assert;
|
|
|
|
ptr_assert = container_of(assert, struct kunit_ptr_not_err_assert,
|
|
assert);
|
|
|
|
if (!ptr_assert->value) {
|
|
string_stream_add(stream,
|
|
KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n",
|
|
ptr_assert->text);
|
|
} else if (IS_ERR(ptr_assert->value)) {
|
|
string_stream_add(stream,
|
|
KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n",
|
|
ptr_assert->text,
|
|
PTR_ERR(ptr_assert->value));
|
|
}
|
|
kunit_assert_print_msg(message, stream);
|
|
}
|
|
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,
|
|
const struct va_format *message,
|
|
struct string_stream *stream)
|
|
{
|
|
struct kunit_binary_assert *binary_assert;
|
|
|
|
binary_assert = container_of(assert, struct kunit_binary_assert,
|
|
assert);
|
|
|
|
string_stream_add(stream,
|
|
KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
|
|
binary_assert->text->left_text,
|
|
binary_assert->text->operation,
|
|
binary_assert->text->right_text);
|
|
if (!is_literal(stream->test, binary_assert->text->left_text,
|
|
binary_assert->left_value, stream->gfp))
|
|
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
|
|
binary_assert->text->left_text,
|
|
binary_assert->left_value);
|
|
if (!is_literal(stream->test, binary_assert->text->right_text,
|
|
binary_assert->right_value, stream->gfp))
|
|
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
|
|
binary_assert->text->right_text,
|
|
binary_assert->right_value);
|
|
kunit_assert_print_msg(message, stream);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
|
|
|
|
void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
|
|
const struct va_format *message,
|
|
struct string_stream *stream)
|
|
{
|
|
struct kunit_binary_ptr_assert *binary_assert;
|
|
|
|
binary_assert = container_of(assert, struct kunit_binary_ptr_assert,
|
|
assert);
|
|
|
|
string_stream_add(stream,
|
|
KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
|
|
binary_assert->text->left_text,
|
|
binary_assert->text->operation,
|
|
binary_assert->text->right_text);
|
|
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px\n",
|
|
binary_assert->text->left_text,
|
|
binary_assert->left_value);
|
|
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px",
|
|
binary_assert->text->right_text,
|
|
binary_assert->right_value);
|
|
kunit_assert_print_msg(message, stream);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
|
|
|
|
/* Checks if KUNIT_EXPECT_STREQ() args were string literals.
|
|
* Note: `text` will have ""s where as `value` will not.
|
|
*/
|
|
static bool is_str_literal(const char *text, const char *value)
|
|
{
|
|
int len;
|
|
|
|
len = strlen(text);
|
|
if (len < 2)
|
|
return false;
|
|
if (text[0] != '\"' || text[len - 1] != '\"')
|
|
return false;
|
|
|
|
return strncmp(text + 1, value, len - 2) == 0;
|
|
}
|
|
|
|
void kunit_binary_str_assert_format(const struct kunit_assert *assert,
|
|
const struct va_format *message,
|
|
struct string_stream *stream)
|
|
{
|
|
struct kunit_binary_str_assert *binary_assert;
|
|
|
|
binary_assert = container_of(assert, struct kunit_binary_str_assert,
|
|
assert);
|
|
|
|
string_stream_add(stream,
|
|
KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
|
|
binary_assert->text->left_text,
|
|
binary_assert->text->operation,
|
|
binary_assert->text->right_text);
|
|
if (!is_str_literal(binary_assert->text->left_text, binary_assert->left_value))
|
|
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"\n",
|
|
binary_assert->text->left_text,
|
|
binary_assert->left_value);
|
|
if (!is_str_literal(binary_assert->text->right_text, binary_assert->right_value))
|
|
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"",
|
|
binary_assert->text->right_text,
|
|
binary_assert->right_value);
|
|
kunit_assert_print_msg(message, stream);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
|