2019-09-23 17:02:34 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
/*
|
|
|
|
* Assertion and expectation serialization API.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019, Google LLC.
|
|
|
|
* Author: Brendan Higgins <brendanhiggins@google.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _KUNIT_ASSERT_H
|
|
|
|
#define _KUNIT_ASSERT_H
|
|
|
|
|
|
|
|
#include <linux/err.h>
|
2022-01-20 10:09:19 +08:00
|
|
|
#include <linux/printk.h>
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
struct kunit;
|
2020-01-07 06:28:18 +08:00
|
|
|
struct string_stream;
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* enum kunit_assert_type - Type of expectation/assertion.
|
|
|
|
* @KUNIT_ASSERTION: Used to denote that a kunit_assert represents an assertion.
|
|
|
|
* @KUNIT_EXPECTATION: Denotes that a kunit_assert represents an expectation.
|
|
|
|
*
|
|
|
|
* Used in conjunction with a &struct kunit_assert to denote whether it
|
|
|
|
* represents an expectation or an assertion.
|
|
|
|
*/
|
|
|
|
enum kunit_assert_type {
|
|
|
|
KUNIT_ASSERTION,
|
|
|
|
KUNIT_EXPECTATION,
|
|
|
|
};
|
|
|
|
|
kunit: split out part of kunit_assert into a static const
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, &loc, KUNIT_EXPECTATION, &__assertion.assert, ...);
};
[1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2022-01-14 00:59:30 +08:00
|
|
|
/**
|
|
|
|
* struct kunit_loc - Identifies the source location of a line of code.
|
|
|
|
* @line: the line number in the file.
|
|
|
|
* @file: the file name.
|
|
|
|
*/
|
|
|
|
struct kunit_loc {
|
|
|
|
int line;
|
|
|
|
const char *file;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ }
|
|
|
|
|
2019-09-23 17:02:34 +08:00
|
|
|
/**
|
|
|
|
* struct kunit_assert - Data for printing a failed assertion or expectation.
|
|
|
|
* @format: a function which formats the data in this kunit_assert to a string.
|
|
|
|
*
|
|
|
|
* Represents a failed expectation/assertion. Contains all the data necessary to
|
|
|
|
* format a string to a user reporting the failure.
|
|
|
|
*/
|
|
|
|
struct kunit_assert {
|
|
|
|
void (*format)(const struct kunit_assert *assert,
|
2022-01-26 05:00:09 +08:00
|
|
|
const struct va_format *message,
|
2019-09-23 17:02:34 +08:00
|
|
|
struct string_stream *stream);
|
|
|
|
};
|
|
|
|
|
kunit: split out part of kunit_assert into a static const
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, &loc, KUNIT_EXPECTATION, &__assertion.assert, ...);
};
[1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2022-01-14 00:59:30 +08:00
|
|
|
void kunit_assert_prologue(const struct kunit_loc *loc,
|
|
|
|
enum kunit_assert_type type,
|
|
|
|
struct string_stream *stream);
|
2019-09-23 17:02:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* struct kunit_fail_assert - Represents a plain fail expectation/assertion.
|
|
|
|
* @assert: The parent of this type.
|
|
|
|
*
|
|
|
|
* Represents a simple KUNIT_FAIL/KUNIT_ASSERT_FAILURE that always fails.
|
|
|
|
*/
|
|
|
|
struct kunit_fail_assert {
|
|
|
|
struct kunit_assert assert;
|
|
|
|
};
|
|
|
|
|
|
|
|
void kunit_fail_assert_format(const struct kunit_assert *assert,
|
2022-01-26 05:00:09 +08:00
|
|
|
const struct va_format *message,
|
2019-09-23 17:02:34 +08:00
|
|
|
struct string_stream *stream);
|
|
|
|
|
|
|
|
/**
|
2022-01-14 00:59:31 +08:00
|
|
|
* KUNIT_INIT_FAIL_ASSERT_STRUCT - Initializer for &struct kunit_fail_assert.
|
2019-09-23 17:02:34 +08:00
|
|
|
*
|
|
|
|
* Initializes a &struct kunit_fail_assert. Intended to be used in
|
|
|
|
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
|
|
|
|
*/
|
2022-01-14 00:59:31 +08:00
|
|
|
#define KUNIT_INIT_FAIL_ASSERT_STRUCT { \
|
2022-01-26 05:00:09 +08:00
|
|
|
.assert = { .format = kunit_fail_assert_format }, \
|
2019-09-23 17:02:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
|
|
|
|
* @assert: The parent of this type.
|
|
|
|
* @condition: A string representation of a conditional expression.
|
|
|
|
* @expected_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
|
|
|
|
*
|
|
|
|
* Represents a simple expectation or assertion that simply asserts something is
|
|
|
|
* true or false. In other words, represents the expectations:
|
|
|
|
* KUNIT_{EXPECT|ASSERT}_{TRUE|FALSE}
|
|
|
|
*/
|
|
|
|
struct kunit_unary_assert {
|
|
|
|
struct kunit_assert assert;
|
|
|
|
const char *condition;
|
|
|
|
bool expected_true;
|
|
|
|
};
|
|
|
|
|
|
|
|
void kunit_unary_assert_format(const struct kunit_assert *assert,
|
2022-01-26 05:00:09 +08:00
|
|
|
const struct va_format *message,
|
2019-09-23 17:02:34 +08:00
|
|
|
struct string_stream *stream);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* KUNIT_INIT_UNARY_ASSERT_STRUCT() - Initializes &struct kunit_unary_assert.
|
|
|
|
* @cond: A string representation of the expression asserted true or false.
|
|
|
|
* @expect_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise.
|
|
|
|
*
|
|
|
|
* Initializes a &struct kunit_unary_assert. Intended to be used in
|
|
|
|
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
|
|
|
|
*/
|
2022-01-14 00:59:31 +08:00
|
|
|
#define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \
|
2022-01-26 05:00:09 +08:00
|
|
|
.assert = { .format = kunit_unary_assert_format }, \
|
2019-09-23 17:02:34 +08:00
|
|
|
.condition = cond, \
|
|
|
|
.expected_true = expect_true \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is
|
|
|
|
* not NULL and not a -errno.
|
|
|
|
* @assert: The parent of this type.
|
|
|
|
* @text: A string representation of the expression passed to the expectation.
|
|
|
|
* @value: The actual evaluated pointer value of the expression.
|
|
|
|
*
|
|
|
|
* Represents an expectation/assertion that a pointer is not null and is does
|
|
|
|
* not contain a -errno. (See IS_ERR_OR_NULL().)
|
|
|
|
*/
|
|
|
|
struct kunit_ptr_not_err_assert {
|
|
|
|
struct kunit_assert assert;
|
|
|
|
const char *text;
|
|
|
|
const void *value;
|
|
|
|
};
|
|
|
|
|
|
|
|
void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
|
2022-01-26 05:00:09 +08:00
|
|
|
const struct va_format *message,
|
2019-09-23 17:02:34 +08:00
|
|
|
struct string_stream *stream);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* KUNIT_INIT_PTR_NOT_ERR_ASSERT_STRUCT() - Initializes a
|
|
|
|
* &struct kunit_ptr_not_err_assert.
|
|
|
|
* @txt: A string representation of the expression passed to the expectation.
|
|
|
|
* @val: The actual evaluated pointer value of the expression.
|
|
|
|
*
|
|
|
|
* Initializes a &struct kunit_ptr_not_err_assert. Intended to be used in
|
|
|
|
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
|
|
|
|
*/
|
2022-01-14 00:59:31 +08:00
|
|
|
#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \
|
2022-01-26 05:00:09 +08:00
|
|
|
.assert = { .format = kunit_ptr_not_err_assert_format }, \
|
2019-09-23 17:02:34 +08:00
|
|
|
.text = txt, \
|
|
|
|
.value = val \
|
|
|
|
}
|
|
|
|
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
/**
|
|
|
|
* struct kunit_binary_assert_text - holds strings for &struct
|
|
|
|
* kunit_binary_assert and friends to try and make the structs smaller.
|
|
|
|
* @operation: A string representation of the comparison operator (e.g. "==").
|
|
|
|
* @left_text: A string representation of the left expression (e.g. "2+2").
|
|
|
|
* @right_text: A string representation of the right expression (e.g. "2+2").
|
|
|
|
*/
|
|
|
|
struct kunit_binary_assert_text {
|
|
|
|
const char *operation;
|
|
|
|
const char *left_text;
|
|
|
|
const char *right_text;
|
|
|
|
};
|
|
|
|
|
2019-09-23 17:02:34 +08:00
|
|
|
/**
|
|
|
|
* struct kunit_binary_assert - An expectation/assertion that compares two
|
|
|
|
* non-pointer values (for example, KUNIT_EXPECT_EQ(test, 1 + 1, 2)).
|
|
|
|
* @assert: The parent of this type.
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
* @text: Holds the textual representations of the operands and op (e.g. "==").
|
2019-09-23 17:02:34 +08:00
|
|
|
* @left_value: The actual evaluated value of the expression in the left slot.
|
|
|
|
* @right_value: The actual evaluated value of the expression in the right slot.
|
|
|
|
*
|
|
|
|
* Represents an expectation/assertion that compares two non-pointer values. For
|
|
|
|
* example, to expect that 1 + 1 == 2, you can use the expectation
|
|
|
|
* KUNIT_EXPECT_EQ(test, 1 + 1, 2);
|
|
|
|
*/
|
|
|
|
struct kunit_binary_assert {
|
|
|
|
struct kunit_assert assert;
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
const struct kunit_binary_assert_text *text;
|
2019-09-23 17:02:34 +08:00
|
|
|
long long left_value;
|
|
|
|
long long right_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
void kunit_binary_assert_format(const struct kunit_assert *assert,
|
2022-01-26 05:00:09 +08:00
|
|
|
const struct va_format *message,
|
2019-09-23 17:02:34 +08:00
|
|
|
struct string_stream *stream);
|
|
|
|
|
|
|
|
/**
|
2022-01-26 05:00:10 +08:00
|
|
|
* KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like
|
|
|
|
* kunit_binary_assert, kunit_binary_ptr_assert, etc.
|
|
|
|
*
|
|
|
|
* @format_func: a function which formats the assert to a string.
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
* @text_: Pointer to a kunit_binary_assert_text.
|
2019-09-23 17:02:34 +08:00
|
|
|
* @left_val: The actual evaluated value of the expression in the left slot.
|
|
|
|
* @right_val: The actual evaluated value of the expression in the right slot.
|
|
|
|
*
|
2022-01-26 05:00:10 +08:00
|
|
|
* Initializes a binary assert like kunit_binary_assert,
|
|
|
|
* kunit_binary_ptr_assert, etc. This relies on these structs having the same
|
|
|
|
* fields but with different types for left_val/right_val.
|
|
|
|
* This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ, etc.
|
2019-09-23 17:02:34 +08:00
|
|
|
*/
|
2022-01-26 05:00:10 +08:00
|
|
|
#define KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
text_, \
|
2019-09-23 17:02:34 +08:00
|
|
|
left_val, \
|
|
|
|
right_val) { \
|
2022-01-26 05:00:10 +08:00
|
|
|
.assert = { .format = format_func }, \
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
.text = text_, \
|
2019-09-23 17:02:34 +08:00
|
|
|
.left_value = left_val, \
|
|
|
|
.right_value = right_val \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct kunit_binary_ptr_assert - An expectation/assertion that compares two
|
|
|
|
* pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)).
|
|
|
|
* @assert: The parent of this type.
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
* @text: Holds the textual representations of the operands and op (e.g. "==").
|
2019-09-23 17:02:34 +08:00
|
|
|
* @left_value: The actual evaluated value of the expression in the left slot.
|
|
|
|
* @right_value: The actual evaluated value of the expression in the right slot.
|
|
|
|
*
|
|
|
|
* Represents an expectation/assertion that compares two pointer values. For
|
|
|
|
* example, to expect that foo and bar point to the same thing, you can use the
|
|
|
|
* expectation KUNIT_EXPECT_PTR_EQ(test, foo, bar);
|
|
|
|
*/
|
|
|
|
struct kunit_binary_ptr_assert {
|
|
|
|
struct kunit_assert assert;
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
const struct kunit_binary_assert_text *text;
|
2019-09-23 17:02:34 +08:00
|
|
|
const void *left_value;
|
|
|
|
const void *right_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
|
2022-01-26 05:00:09 +08:00
|
|
|
const struct va_format *message,
|
2019-09-23 17:02:34 +08:00
|
|
|
struct string_stream *stream);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct kunit_binary_str_assert - An expectation/assertion that compares two
|
|
|
|
* string values (for example, KUNIT_EXPECT_STREQ(test, foo, "bar")).
|
|
|
|
* @assert: The parent of this type.
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
* @text: Holds the textual representations of the operands and comparator.
|
2019-09-23 17:02:34 +08:00
|
|
|
* @left_value: The actual evaluated value of the expression in the left slot.
|
|
|
|
* @right_value: The actual evaluated value of the expression in the right slot.
|
|
|
|
*
|
|
|
|
* Represents an expectation/assertion that compares two string values. For
|
|
|
|
* example, to expect that the string in foo is equal to "bar", you can use the
|
|
|
|
* expectation KUNIT_EXPECT_STREQ(test, foo, "bar");
|
|
|
|
*/
|
|
|
|
struct kunit_binary_str_assert {
|
|
|
|
struct kunit_assert assert;
|
kunit: factor out str constants from binary assertion structs
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>
2022-01-26 05:00:11 +08:00
|
|
|
const struct kunit_binary_assert_text *text;
|
2019-09-23 17:02:34 +08:00
|
|
|
const char *left_value;
|
|
|
|
const char *right_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
void kunit_binary_str_assert_format(const struct kunit_assert *assert,
|
2022-01-26 05:00:09 +08:00
|
|
|
const struct va_format *message,
|
2019-09-23 17:02:34 +08:00
|
|
|
struct string_stream *stream);
|
|
|
|
|
|
|
|
#endif /* _KUNIT_ASSERT_H */
|