Skip to contentSkip to conversation

kunit: introduce class mocking support.

RFC Superseded Daniel Latypov 3 participants 24 messages Sep 18, 2020 – Oct 1, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover kunit: introduce class mocking support.

# Background
KUnit currently lacks any first-class support for mocking. For an overview and discussion on the pros and cons, see
https://martinfowler.com/articles/mocksArentStubs.html

This patch set introduces the basic machinery needed for mocking:
setting and validating expectations, setting default actions, etc.

Using that basic infrastructure, we add macros for "class mocking", as it's probably the easiest type of mocking to start with.

## Class mocking

By "class mocking", we're referring mocking out function pointers stored in structs like:
struct sender {
int (*send)(struct sender *sender, int data);
};

After the necessary DEFINE_* macros, we can then write code like
struct MOCK(sender) mock_sender = CONSTRUCT_MOCK(sender, test);

  /* Fake an error for a specific input. */
  handle = KUNIT_EXPECT_CALL(send(<omitted>, kunit_int_eq(42)));
  handle->action = kunit_int_return(test, -EINVAL);

/* Pass the mocked object to some code under test. */ KUNIT_EXPECT_EQ(test, -EINVAL, send_message(...));

I.e. the goal is to make it easier to test
1) with less dependencies (we don't need to setup a real sender)
2) unusual/error conditions more easily.

In the future, we hope to build upon this to support mocking in more contexts, e.g. standalone funcs, etc.

# TODOs

## Naming
This introduces a number of new macros for dealing with mocks, e.g:
DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),

                           RETURNS(int),
                           PARAMS(struct example *, int));

...
KUNIT_EXPECT_CALL(foo(mock_get_ctrl(mock_example), ...);
For consistency, we could prefix everything with KUNIT, e.g. KUNIT_DEFINE_STRUCT_CLASS_MOCK and kunit_mock_get_ctrl, but it feels like the names might be long enough that they would hinder readability.

## Usage
For now the only use of class mocking is in kunit-example-test.c As part of changing this from an RFC to a real patch set, we're hoping to include at least one example.

Pointers to bits of code where this would be useful that aren't too hairy would be appreciated.
E.g. could easily add a test for tools/perf/ui/progress.h, e.g. that ui_progress__init() calls ui_progress_ops.init(), but that likely isn't useful to anyone.

Brendan Higgins (9):
kunit: test: add kunit_stream a std::stream like logger
kunit: test: add concept of post conditions
checkpatch: add support for struct MOCK(foo) syntax
kunit: mock: add parameter list manipulation macros
kunit: mock: add internal mock infrastructure
kunit: mock: add basic matchers and actions
kunit: mock: add class mocking support
kunit: mock: add struct param matcher
kunit: mock: implement nice, strict and naggy mock distinctions

Daniel Latypov (2):
Revert "kunit: move string-stream.h to lib/kunit"
kunit: expose kunit_set_failure() for use by mocking

Marcelo Schmitt (1):
kunit: mock: add macro machinery to pick correct format args

 include/kunit/assert.h                 |   3 +-
 include/kunit/kunit-stream.h           |  94 +++
 include/kunit/mock.h                   | 902 +++++++++++++++++++++++++
 include/kunit/params.h                 | 305 +++++++++
 {lib => include}/kunit/string-stream.h |   2 +
 include/kunit/test.h                   |   9 +
 lib/kunit/Makefile                     |   9 +-
 lib/kunit/assert.c                     |   2 -
 lib/kunit/common-mocks.c               | 409 +++++++++++
 lib/kunit/kunit-example-test.c         |  90 +++
 lib/kunit/kunit-stream.c               | 110 +++
 lib/kunit/mock-macro-test.c            | 241 +++++++
 lib/kunit/mock-test.c                  | 531 +++++++++++++++
 lib/kunit/mock.c                       | 370 ++++++++++
 lib/kunit/string-stream-test.c         |   3 +-
 lib/kunit/string-stream.c              |   5 +-
 lib/kunit/test.c                       |  15 +-
 scripts/checkpatch.pl                  |   4 +
 18 files changed, 3091 insertions(+), 13 deletions(-)
 create mode 100644 include/kunit/kunit-stream.h
 create mode 100644 include/kunit/mock.h
 create mode 100644 include/kunit/params.h
 rename {lib => include}/kunit/string-stream.h (95%)
 create mode 100644 lib/kunit/common-mocks.c
 create mode 100644 lib/kunit/kunit-stream.c
 create mode 100644 lib/kunit/mock-macro-test.c
 create mode 100644 lib/kunit/mock-test.c
 create mode 100644 lib/kunit/mock.c

base-commit: 10b82d5176488acee2820e5a2cf0f2ec5c3488b6

Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#1 lore raw

RFC 1/12 Revert "kunit: move string-stream.h to lib/kunit"

This reverts commit 109fb06fdc6f6788df7dfbc235f7636a38e28fd4.

string-stream will be used by kunit mocking code to print messages about mock expectations.
It makes the code signifcantly simpler if string-stream objects can be part of structs declared in mocking headers.

---
 include/kunit/assert.h                 | 3 +--
 {lib => include}/kunit/string-stream.h | 0
 lib/kunit/assert.c                     | 2 --
 lib/kunit/string-stream-test.c         | 3 +--
 lib/kunit/string-stream.c              | 3 +--
 lib/kunit/test.c                       | 2 +-
 6 files changed, 4 insertions(+), 9 deletions(-)
 rename {lib => include}/kunit/string-stream.h (100%)
include/kunit/assert.h +1 -2
index ad889b539ab3..db6a0fca09b4 100644
@@ -9,11 +9,10 @@
 #ifndef _KUNIT_ASSERT_H
 #define _KUNIT_ASSERT_H
 
+#include <kunit/string-stream.h>
 #include <linux/err.h>
-#include <linux/kernel.h>
 
 struct kunit;
-struct string_stream;
 
 /**
  * enum kunit_assert_type - Type of expectation/assertion.
include/kunit/string-stream.h renamed from lib/kunit/string-stream.h +0 -0
similarity index 100%
rename from lib/kunit/string-stream.h
rename to include/kunit/string-stream.h
lib/kunit/assert.c +0 -2
index 33acdaa28a7d..9c12e30792ba 100644
@@ -8,8 +8,6 @@
 #include <kunit/assert.h>
 #include <kunit/test.h>
 
-#include "string-stream.h"
-
 void kunit_base_assert_format(const struct kunit_assert *assert,
 			      struct string_stream *stream)
 {
lib/kunit/string-stream-test.c +1 -2
index 110f3a993250..121f9ab11501 100644
@@ -6,11 +6,10 @@
  * Author: Brendan Higgins <[email protected]>
  */
 
+#include <kunit/string-stream.h>
 #include <kunit/test.h>
 #include <linux/slab.h>
 
-#include "string-stream.h"
-
 static void string_stream_test_empty_on_creation(struct kunit *test)
 {
 	struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
lib/kunit/string-stream.c +1 -2
index 141789ca8949..151a0e7ac349 100644
@@ -6,12 +6,11 @@
  * Author: Brendan Higgins <[email protected]>
  */
 
+#include <kunit/string-stream.h>
 #include <kunit/test.h>
 #include <linux/list.h>
 #include <linux/slab.h>
 
-#include "string-stream.h"
-
 struct string_stream_fragment_alloc_context {
 	struct kunit *test;
 	int len;
lib/kunit/test.c +1 -1
index c36037200310..670d1cc9c105 100644
@@ -7,12 +7,12 @@
  */
 
 #include <kunit/test.h>
+#include <kunit/string-stream.h>
 #include <linux/kernel.h>
 #include <linux/kref.h>
 #include <linux/sched/debug.h>
 
 #include "debugfs.h"
-#include "string-stream.h"
 #include "try-catch-impl.h"
 
 static void kunit_set_failure(struct kunit *test)
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#2 lore raw

Re: [RFC v1 01/12] Revert "kunit: move string-stream.h to lib/kunit"

reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # install arm cross compiling tool for…

Hi Daniel,

[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on 10b82d5176488acee2820e5a2cf0f2ec5c3488b6]

url:    https://github.com/0day-ci/linux/commits/Daniel-Latypov/kunit-introduce-class-mocking-support/20200919-023253
base:    10b82d5176488acee2820e5a2cf0f2ec5c3488b6
config: arm-randconfig-r013-20200917 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 0ff28fa6a75617d61b1aeea77463d6a1684c3c89)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

>> lib/kunit/debugfs.c:12:10: fatal error: 'string-stream.h' file not found
   #include "string-stream.h"
            ^~~~~~~~~~~~~~~~~

1 error generated.

# https://github.com/0day-ci/linux/commit/6aca0cd5a2ad0c09602bc75f28ae8d8fac5a5166
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Daniel-Latypov/kunit-introduce-class-mocking-support/20200919-023253 git checkout 6aca0cd5a2ad0c09602bc75f28ae8d8fac5a5166
vim +12 lib/kunit/debugfs.c

e2219db280e3fe5 Alan Maguire 2020-03-26 11
e2219db280e3fe5 Alan Maguire 2020-03-26 @12 #include "string-stream.h" e2219db280e3fe5 Alan Maguire 2020-03-26 13

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw

RFC 2/12 kunit: test: add kunit_stream a std::stream like logger

In reply to Daniel Latypov

From: Brendan Higgins <[email protected]>

A lot of the expectation and assertion infrastructure prints out fairly complicated test failure messages, so add a C++ style log library for for logging test results called struct kunit_stream.

kunit_stream allows us to construct a message before we know whether we want to print it out; this can be extremely handy if there is information you might need for a failure message that is easiest to collect in the steps leading up to the actual check.

Note that kunit_stream is distinct from string_stream: whereas string_stream is really just a string builder, kunit_stream is specifically used for constructing expectation/assertion (introduced later in this series) messages to a user of KUnit.

Patch was previously reviewed at
https://lore.kernel.org/linux-fsdevel/[email protected]/
Primary differences are
* kunit/ => lib/kunit/
* re-expose string_stream_clear(), which was made static since then

---
 include/kunit/kunit-stream.h  |  94 +++++++++++++++++++++++++++++
 include/kunit/string-stream.h |   2 +
 include/kunit/test.h          |   1 +
 lib/kunit/Makefile            |   1 +
 lib/kunit/kunit-stream.c      | 110 ++++++++++++++++++++++++++++++++++
 lib/kunit/string-stream.c     |   2 +-
 6 files changed, 209 insertions(+), 1 deletion(-)
 create mode 100644 include/kunit/kunit-stream.h
 create mode 100644 lib/kunit/kunit-stream.c
include/kunit/kunit-stream.h new file +94 -0
new file mode 100644
index 000000000000..9a768732a04b
@@ -0,0 +1,94 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * C++ stream style string formatter and printer used in KUnit for outputting
+ * KUnit messages.
+ *
+ * Copyright (C) 2020, Google LLC.
+ * Author: Brendan Higgins <[email protected]>
+ */
+
+#ifndef _KUNIT_KUNIT_STREAM_H
+#define _KUNIT_KUNIT_STREAM_H
+
+#include <linux/types.h>
+#include <kunit/string-stream.h>
+
+struct kunit;
+
+/**
+ * struct kunit_stream - a std::stream style string builder.
+ *
+ * A std::stream style string builder. Allows messages to be built up and
+ * printed all at once. Note that the intention is to only use
+ * &struct kunit_stream to communicate with a user of KUnit, most often to
+ * communicate something about an expectation or an assertion to the user. If
+ * you want a similar interface, but aren't sure if this is the right class for
+ * you to use, you probably want to use the related string_stream class, which
+ * is allowed for generic string construction in a similar manner. This class is
+ * really only for the KUnit library to communicate certain kinds of information
+ * to KUnit users and should not be used by anyone else.
+ *
+ * A note on &struct kunit_stream's usage: a kunit_stream will generally
+ * accompany *one* expectation or assertion. Multiple expectations/assertions
+ * may be validated concurrently at any given time, even within a single test
+ * case, so sharing a kunit_stream between expectations/assertions may result in
+ * unintended consequences.
+ */
+struct kunit_stream {
+	/* private: internal use only. */
+	struct kunit *test;
+	struct string_stream *internal_stream;
+};
+
+/**
+ * alloc_kunit_stream() - constructs a new &struct kunit_stream.
+ * @test: The test context object.
+ * @gfp: The GFP flags to use for internal allocations.
+ *
+ * Constructs a new test managed &struct kunit_stream.
+ */
+struct kunit_stream *alloc_kunit_stream(struct kunit *test,
+					gfp_t gfp);
+
+/**
+ * kunit_stream_add(): adds the formatted input to the internal buffer.
+ * @kstream: the stream being operated on.
+ * @fmt: printf style format string to append to stream.
+ *
+ * Appends the formatted string, @fmt, to the internal buffer.
+ */
+void __printf(2, 3) kunit_stream_add(struct kunit_stream *kstream,
+				     const char *fmt, ...);
+
+/**
+ * kunit_stream_append(): appends the contents of @other to @kstream.
+ * @kstream: the stream to which @other is appended.
+ * @other: the stream whose contents are appended to @kstream.
+ *
+ * Appends the contents of @other to @kstream.
+ */
+void kunit_stream_append(struct kunit_stream *kstream,
+			 struct kunit_stream *other);
+
+/**
+ * kunit_stream_commit(): prints out the internal buffer to the user.
+ * @kstream: the stream being operated on.
+ *
+ * Outputs the contents of the internal buffer as a kunit_printk formatted
+ * output. KUNIT_STREAM ONLY OUTPUTS ITS BUFFER TO THE USER IF COMMIT IS
+ * CALLED!!! The reason for this is that it allows us to construct a message
+ * before we know whether we want to print it out; this can be extremely handy
+ * if there is information you might need for a failure message that is easiest
+ * to collect in the steps leading up to the actual check.
+ */
+void kunit_stream_commit(struct kunit_stream *kstream);
+
+/**
+ * kunit_stream_clear(): clears the internal buffer.
+ * @kstream: the stream being operated on.
+ *
+ * Clears the contents of the internal buffer.
+ */
+void kunit_stream_clear(struct kunit_stream *kstream);
+
+#endif /* _KUNIT_KUNIT_STREAM_H */
include/kunit/string-stream.h +2 -0
index fe98a00b75a9..44677c386890 100644
@@ -46,6 +46,8 @@ int string_stream_append(struct string_stream *stream,
 
 bool string_stream_is_empty(struct string_stream *stream);
 
+void string_stream_clear(struct string_stream *stream);
+
 int string_stream_destroy(struct string_stream *stream);
 
 #endif /* _KUNIT_STRING_STREAM_H */
include/kunit/test.h +1 -0
index 59f3144f009a..687782fa44d9 100644
@@ -10,6 +10,7 @@
 #define _KUNIT_TEST_H
 
 #include <kunit/assert.h>
+#include <kunit/kunit-stream.h>
 #include <kunit/try-catch.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
lib/kunit/Makefile +1 -0
index 724b94311ca3..523a7b0f9783 100644
@@ -3,6 +3,7 @@ obj-$(CONFIG_KUNIT) +=			kunit.o
 kunit-objs +=				test.o \
 					string-stream.o \
 					assert.o \
+					kunit-stream.o \
 					try-catch.o
 
 ifeq ($(CONFIG_KUNIT_DEBUGFS),y)
lib/kunit/kunit-stream.c new file +110 -0

110 lines not on this page · raw message

lib/kunit/string-stream.c +1 -1
index 151a0e7ac349..a94004134928 100644
@@ -112,7 +112,7 @@ int string_stream_add(struct string_stream *stream, const char *fmt, ...)
 	return result;
 }
 
-static void string_stream_clear(struct string_stream *stream)
+void string_stream_clear(struct string_stream *stream)
 {
 	struct string_stream_fragment *frag_container, *frag_container_safe;
 
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#4 lore raw

RFC 3/12 kunit: test: add concept of post conditions

In reply to Daniel Latypov

From: Brendan Higgins <[email protected]>

Add a way to specify that certain conditions must be met at the end of a test case.

---
 include/kunit/test.h |  6 ++++++
 lib/kunit/test.c     | 11 +++++++++++
 2 files changed, 17 insertions(+)
include/kunit/test.h +6 -0
index 687782fa44d9..0eb3abb00da4 100644
@@ -190,6 +190,11 @@ struct kunit_suite {
 	char *log;
 };
 
+struct kunit_post_condition {
+	struct list_head node;
+	void (*validate)(struct kunit_post_condition *condition);
+};
+
 /**
  * struct kunit - represents a running instance of a test.
  *
@@ -223,6 +228,7 @@ struct kunit {
 	 * protect it with some type of lock.
 	 */
 	struct list_head resources; /* Protected by lock. */
+	struct list_head post_conditions;
 };
 
 void kunit_init_test(struct kunit *test, const char *name, char *log);
lib/kunit/test.c +11 -0
index 670d1cc9c105..4e8c74c89073 100644
@@ -228,6 +228,7 @@ void kunit_init_test(struct kunit *test, const char *name, char *log)
 {
 	spin_lock_init(&test->lock);
 	INIT_LIST_HEAD(&test->resources);
+	INIT_LIST_HEAD(&test->post_conditions);
 	test->name = name;
 	test->log = log;
 	if (test->log)
@@ -269,6 +270,16 @@ static void kunit_case_internal_cleanup(struct kunit *test)
 static void kunit_run_case_cleanup(struct kunit *test,
 				   struct kunit_suite *suite)
 {
+	struct kunit_post_condition *condition, *condition_safe;
+
+	list_for_each_entry_safe(condition,
+				 condition_safe,
+				 &test->post_conditions,
+				 node) {
+		condition->validate(condition);
+		list_del(&condition->node);
+	}
+
 	if (suite->exit)
 		suite->exit(test);
 
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#5 lore raw

RFC 4/12 checkpatch: add support for struct MOCK(foo) syntax

In reply to Daniel Latypov

From: Brendan Higgins <[email protected]>

KUnit will soon add macros for generating mocks from types, the generated mock types are named like struct MOCK(foo) (where the base type is struct foo).

Add struct MOCK(foo) as a NonptrType so that it is recognized correctly in declarations.

---
 scripts/checkpatch.pl | 4 ++++
 1 file changed, 4 insertions(+)
scripts/checkpatch.pl +4 -0
index 504d2e431c60..b40a68e7bc25 100755
@@ -796,6 +796,10 @@ sub build_types {
 			(?:
 				(?:typeof|__typeof__)\s*\([^\)]*\)|
 				(?:$typeTypedefs\b)|
+				# Matching a \b breaks struct MOCK(foo) syntax,
+				# so we need to have it not lumped in with the
+				# types in @typeList.
+				(?:struct\s+MOCK\($Ident\))|
 				(?:${all}\b)
 			)
 			(?:\s+$Modifier|\s+const)*
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#6 lore raw

RFC 5/12 kunit: mock: add parameter list manipulation macros

In reply to Daniel Latypov

From: Brendan Higgins <[email protected]>

Add macros for parsing and manipulating parameter lists needed for generating mocks.

---
 include/kunit/params.h      | 305 ++++++++++++++++++++++++++++++++++++
 lib/kunit/Makefile          |   3 +-
 lib/kunit/mock-macro-test.c | 150 ++++++++++++++++++
 3 files changed, 457 insertions(+), 1 deletion(-)
 create mode 100644 include/kunit/params.h
 create mode 100644 lib/kunit/mock-macro-test.c
include/kunit/params.h new file +305 -0
new file mode 100644
index 000000000000..06efcb58c2c0
@@ -0,0 +1,305 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Macros for parsing and manipulating parameter lists needed for generating
+ * mocks.
+ *
+ * Copyright (C) 2020, Google LLC.
+ * Author: Brendan Higgins <[email protected]>
+ */
+
+#ifndef _KUNIT_PARAMS_H
+#define _KUNIT_PARAMS_H
+
+#define NUM_VA_ARGS_IMPL(__dummy,					       \
+			 __1,						       \
+			 __2,						       \
+			 __3,						       \
+			 __4,						       \
+			 __5,						       \
+			 __6,						       \
+			 __7,						       \
+			 __8,						       \
+			 __9,						       \
+			 __10,						       \
+			 __11,						       \
+			 __12,						       \
+			 __13,						       \
+			 __14,						       \
+			 __15,						       \
+			 __16,						       \
+			 __nargs, args...) __nargs
+
+#define NUM_VA_ARGS(args...) NUM_VA_ARGS_IMPL(__dummy, ##args,		       \
+					  16,				       \
+					  15,				       \
+					  14,				       \
+					  13,				       \
+					  12,				       \
+					  11,				       \
+					  10,				       \
+					  9,				       \
+					  8,				       \
+					  7,				       \
+					  6,				       \
+					  5,				       \
+					  4,				       \
+					  3,				       \
+					  2,				       \
+					  1,				       \
+					  0)
+
+#define CONCAT_INTERNAL(left, right) left##right
+#define CONCAT(left, right) CONCAT_INTERNAL(left, right)
+
+#define EMPTY()
+
+/*
+ * Takes the name of a function style macro such as FOO() and prevents it from
+ * being evaluated on the current pass.
+ *
+ * This is useful when you need to write a "recursive" macro since a macro name
+ * becomes painted after it is pasted. If a different macro is pasted, this
+ * different macro won't be pasted; if we then defer the evaluation of the this
+ * "indirection macro", we can prevent the original definition from getting
+ * painted.
+ *
+ * Example:
+ *   #define EXAMPLE EXPAND(FOO()) // FOO() is evaluated on 1st pass.
+ *   #define EXAMPLE EXPAND(DEFER(FOO)()) // FOO() is evaluated on the second
+ *					  // pass.
+ */
+#define DEFER(macro_id) macro_id EMPTY()
+
+/*
+ * Takes the name of a function style macro such as FOO() and prevents it from
+ * being evaluated on the current or following pass.
+ *
+ * This is useful when you need to DEFER inside an operation which causes an
+ * extra pass, like IF.
+ *
+ * Example:
+ *   #define EXAMPLE EXPAND(FOO()) // FOO() is evaluated on 1st pass.
+ *   #define EXAMPLE EXPAND(DEFER(FOO)()) // FOO() is evaluated on the second
+ *					  // pass.
+ *   #define EXAMPLE EXPAND(OBSTRUCT(FOO)()) // FOO() is evaluated on the third
+ *					     // pass.
+ */
+#define OBSTRUCT(macro_id) macro_id DEFER(EMPTY)()
+
+#define EXPAND_1(args...) args
+#define EXPAND_2(args...) EXPAND_1(EXPAND_1(args))
+#define EXPAND_4(args...) EXPAND_2(EXPAND_2(args))
+#define EXPAND_8(args...) EXPAND_4(EXPAND_4(args))
+#define EXPAND_16(args...) EXPAND_8(EXPAND_8(args))
+
+/*
+ * Causes multiple evaluation passes of a macro.
+ *
+ * CPP is implemented as a push down automaton. It consumes a stream of tokens
+ * and as it comes across macros, it either evaluates them and pastes the
+ * result, or if the macro is a function macro, it pushes the macro to a stack,
+ * it evaluates the input to the function macro, pops the state from the stack
+ * and continues.
+ *
+ * This macro serves the function of making the cursor return to the beginging
+ * of a macro that requires mulitple passes to evaluate. It is most useful when
+ * used with DEFER(...) and OBSTRUCT(...).
+ */
+#define EXPAND(args...) EXPAND_16(args)
+
+#define INC(id) INC_##id
+#define INC_0  1
+#define INC_1  2
+#define INC_2  3
+#define INC_3  4
+#define INC_4  5
+#define INC_5  6
+#define INC_6  7
+#define INC_7  8
+#define INC_8  9
+#define INC_9  10
+#define INC_10 11
+#define INC_11 12
+#define INC_12 13
+#define INC_13 14
+#define INC_14 15
+#define INC_15 16
+#define INC_16 17
+
+#define DEC(id) DEC_##id
+#define DEC_1  0
+#define DEC_2  1
+#define DEC_3  2
+#define DEC_4  3
+#define DEC_5  4
+#define DEC_6  5
+#define DEC_7  6
+#define DEC_8  7
+#define DEC_9  8
+#define DEC_10 9
+#define DEC_11 10
+#define DEC_12 11
+#define DEC_13 12
+#define DEC_14 13
+#define DEC_15 14
+#define DEC_16 15
+
+#define DROP_FIRST_ARG_INTERNAL(dropped, x, args...) x
+#define DROP_FIRST_ARG(args...) DROP_FIRST_ARG_INTERNAL(args)
+
+#define EQUAL(left, right) EQUAL_##left##_##right
+#define EQUAL_0_0 dropped, 1
+#define EQUAL_1_1 dropped, 1
+#define EQUAL_2_2 dropped, 1
+#define EQUAL_3_3 dropped, 1
+#define EQUAL_4_4 dropped, 1
+#define EQUAL_5_5 dropped, 1
+#define EQUAL_6_6 dropped, 1
+#define EQUAL_7_7 dropped, 1
+#define EQUAL_8_8 dropped, 1
+#define EQUAL_9_9 dropped, 1
+#define EQUAL_10_10 dropped, 1
+#define EQUAL_11_11 dropped, 1
+#define EQUAL_12_12 dropped, 1
+#define EQUAL_13_13 dropped, 1
+#define EQUAL_14_14 dropped, 1
+#define EQUAL_15_15 dropped, 1
+#define EQUAL_16_16 dropped, 1
+
+#define IS_EQUAL(left, right) DROP_FIRST_ARG(EQUAL(left, right), 0)
+
+#define NOT_INTERNAL(condition) NOT_##condition
+#define NOT(condition) NOT_INTERNAL(condition)
+#define NOT_0 1
+#define NOT_1 0
+
+#define IS_NOT_EQUAL(left, right) NOT(IS_EQUAL(left, right))
+
+#define EMPTY_IMPL(tokens) CONCAT(EMPTY_, tokens)
+#define IS_EMPTY(tokens)
+
+#define OR_INTERNAL(left, right) OR_##left##_##right
+#define OR(left, right) OR_INTERNAL(left, right)
+#define OR_0_0 0
+#define OR_0_1 1
+#define OR_1_0 1
+#define OR_1_1 1
+
+#define IF(condition) CONCAT(IF_, condition)
+#define IF_0(body)
+#define IF_1(body) body
+
+#define COMMA() ,
+
+#define APPLY_TOKENS_INTERNAL(tokens, yield_token, seen_token) \
+		IF(yield_token)(IF(seen_token)(COMMA()) tokens)
+#define APPLY_TOKENS(tokens, yield_token, seen_token) \
+		APPLY_TOKENS_INTERNAL(tokens, yield_token, seen_token)
+
+/*
+ * Provides the indirection to keep the PARAM_LIST_RECURSE_INTERNAL from getting
+ * pasted, only useful if used with DEFER(...) or OBSTRUCT(...).
+ */
+#define PARAM_LIST_RECURSE_INDIRECT() PARAM_LIST_RECURSE_INTERNAL
+
+/*
+ * Given a starting index, a number of args, a MACRO to apply, and a list of
+ * types (with at least one element) this will call MACRO with the first type in
+ * the list and index; it will then call itself again on all remaining types, if
+ * any, while incrementing index, and decrementing nargs.
+ *
+ * Assumes nargs is the number of types in the list.
+ */
+#define PARAM_LIST_RECURSE_INTERNAL(index,				       \
+				    nargs,				       \
+				    MACRO,				       \
+				    FILTER,				       \
+				    context,				       \
+				    seen_token,				       \
+				    type,				       \
+				    args...)				       \
+		APPLY_TOKENS(MACRO(context, type, index),		       \
+			     FILTER(context, type, index),		       \
+			     seen_token)				       \
+		IF(IS_NOT_EQUAL(nargs, 1))				       \
+			(OBSTRUCT(PARAM_LIST_RECURSE_INDIRECT)()	       \
+			 (INC(index), DEC(nargs),			       \
+			  MACRO, FILTER, context,			       \
+			  OR(seen_token, FILTER(context, type, index)),	       \
+			  args))
+
+#define PARAM_LIST_RECURSE(index, nargs, MACRO, FILTER, context, args...)      \
+		IF(IS_NOT_EQUAL(nargs, 0))				       \
+			(OBSTRUCT(PARAM_LIST_RECURSE_INTERNAL)(index,	       \
+							       nargs,	       \
+							       MACRO,	       \
+							       FILTER,	       \
+							       context,	       \
+							       0,	       \
+							       args))
+
+#define FILTER_NONE(context, type, index) 1
+
+#define FILTER_INDEX_INTERNAL(index_to_drop, type, index) \
+		IS_NOT_EQUAL(index, index_to_drop)
+#define FILTER_INDEX(index_to_drop, type, index) \
+		FILTER_INDEX_INTERNAL(index_to_drop, type, index)
+
+/*
+ * Applies a MACRO which takes a type and the index of the type and outputs a
+ * sequence of tokens to a list of types.
+ */
+#define FOR_EACH_PARAM(MACRO, FILTER, context, args...) \
+		EXPAND(PARAM_LIST_RECURSE(0,\
+					  NUM_VA_ARGS(args),\
+					  MACRO,\
+					  FILTER,\
+					  context,\
+					  args))
+
+#define PRODUCE_TYPE_AND_ARG(context, type, index) type arg##index
+#define PARAM_LIST_FROM_TYPES(args...)					       \
+		FOR_EACH_PARAM(PRODUCE_TYPE_AND_ARG,			       \
+			       FILTER_NONE,				       \
+			       not_used,				       \
+			       args)
+
+#define PRODUCE_TYPE_NAME(context, type, index) #type
+#define TYPE_NAMES_FROM_TYPES(handle_index, args...)			       \
+		FOR_EACH_PARAM(PRODUCE_TYPE_NAME,			       \
+			       FILTER_INDEX,				       \
+			       handle_index,				       \
+			       args)
+
+#define PRODUCE_PTR_TO_ARG(context, type, index) &arg##index
+#define PTR_TO_ARG_FROM_TYPES(handle_index, args...)			       \
+		FOR_EACH_PARAM(PRODUCE_PTR_TO_ARG,			       \
+			       FILTER_INDEX,				       \
+			       handle_index,				       \
+			       args)
+
+#define PRODUCE_MATCHER_AND_ARG(ctrl_index, type, index)		       \
+		IF(IS_EQUAL(index, ctrl_index))(struct mock *arg##ctrl_index)  \
+		IF(IS_NOT_EQUAL(index, ctrl_index))(			       \
+				struct mock_param_matcher *arg##index)
+#define MATCHER_PARAM_LIST_FROM_TYPES(ctrl_index, args...)		       \
+		FOR_EACH_PARAM(PRODUCE_MATCHER_AND_ARG,			       \
+			       FILTER_NONE,				       \
+			       ctrl_index,				       \
+			       args)
+
+#define PRODUCE_ARG(context, type, index) arg##index
+#define ARG_NAMES_FROM_TYPES(ctrl_index, args...)			       \
+		FOR_EACH_PARAM(PRODUCE_ARG,				       \
+			       FILTER_INDEX,				       \
+			       ctrl_index,				       \
+			       args)
+
+#define PRODUCE_ARRAY_ACCESSOR(context, type, index) *((type *) params[index])
+#define ARRAY_ACCESSORS_FROM_TYPES(args...)				       \
+		FOR_EACH_PARAM(PRODUCE_ARRAY_ACCESSOR,			       \
+			       FILTER_NONE,				       \
+			       not_used,				       \
+			       args)
+
+#endif /* _KUNIT_PARAMS_H */
lib/kunit/Makefile +2 -1
index 523a7b0f9783..1707660c8b1c 100644
@@ -10,7 +10,8 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y)
 kunit-objs +=				debugfs.o
 endif
 
-obj-$(CONFIG_KUNIT_TEST) +=		kunit-test.o
+obj-$(CONFIG_KUNIT_TEST) +=		kunit-test.o \
+					mock-macro-test.o
 
 # string-stream-test compiles built-in only.
 ifeq ($(CONFIG_KUNIT_TEST),y)
lib/kunit/mock-macro-test.c new file +150 -0

150 lines not on this page · raw message

Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#7 lore raw

RFC 6/12 kunit: expose kunit_set_failure() for use by mocking

In reply to Daniel Latypov

Being able to fail the test outside of expectations and assertions is a requirement for new features, e.g. mocking, dynamic analysis, etc.

---
 include/kunit/test.h | 2 ++
 lib/kunit/test.c     | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/kunit/test.h +2 -0
index 0eb3abb00da4..05330593243d 100644
@@ -233,6 +233,8 @@ struct kunit {
 
 void kunit_init_test(struct kunit *test, const char *name, char *log);
 
+void kunit_set_failure(struct kunit *test);
+
 int kunit_run_tests(struct kunit_suite *suite);
 
 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
lib/kunit/test.c +1 -1
index 4e8c74c89073..1ccf6dbecd73 100644
@@ -15,7 +15,7 @@
 #include "debugfs.h"
 #include "try-catch-impl.h"
 
-static void kunit_set_failure(struct kunit *test)
+void kunit_set_failure(struct kunit *test)
 {
 	WRITE_ONCE(test->success, false);
 }
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#8 lore raw

RFC 7/12 kunit: mock: add internal mock infrastructure

In reply to Daniel Latypov

From: Brendan Higgins <[email protected]>

Add the core internal mechanisms that mocks are implemented with; in particular, this adds the mechanisms by which expectation on mocks are validated and by which actions may be supplied and then executed when mocks are called.

---
 include/kunit/mock.h | 126 +++++++++++++++
 lib/kunit/Makefile   |   1 +
 lib/kunit/mock.c     | 364 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 491 insertions(+)
 create mode 100644 include/kunit/mock.h
 create mode 100644 lib/kunit/mock.c
include/kunit/mock.h new file +126 -0
new file mode 100644
index 000000000000..299b423fdd51
@@ -0,0 +1,126 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Mocking API for KUnit.
+ *
+ * Copyright (C) 2020, Google LLC.
+ * Author: Brendan Higgins <[email protected]>
+ */
+
+#ifndef _KUNIT_MOCK_H
+#define _KUNIT_MOCK_H
+
+#include <linux/types.h>
+#include <linux/tracepoint.h> /* For PARAMS(...) */
+#include <kunit/test.h>
+#include <kunit/kunit-stream.h>
+#include <kunit/params.h>
+
+/**
+ * struct mock_param_matcher - represents a matcher used in a *call expectation*
+ * @match: the function that performs the matching
+ *
+ * The matching function takes a couple of parameters:
+ *
+ * - ``this``: refers to the parent struct
+ * - ``stream``: a &kunit_stream to which a detailed message should be added as
+ *   to why the parameter matches or not
+ * - ``param``: a pointer to the parameter to check for a match
+ *
+ * The matching function should return whether or not the passed parameter
+ * matches.
+ */
+struct mock_param_matcher {
+	bool (*match)(struct mock_param_matcher *this,
+		      struct kunit_stream *stream,
+		      const void *param);
+};
+
+#define MOCK_MAX_PARAMS 255
+
+struct mock_matcher {
+	struct mock_param_matcher *matchers[MOCK_MAX_PARAMS];
+	int num;
+};
+
+/**
+ * struct mock_action - Represents an action that a mock performs when
+ *                      expectation is matched
+ * @do_action: the action to perform
+ *
+ * The action function is given some parameters:
+ *
+ * - ``this``: refers to the parent struct
+ * - ``params``: an array of pointers to the params passed into the mocked
+ *   method or function. **The class argument is excluded for a mocked class
+ *   method.**
+ * - ``len``: size of ``params``
+ *
+ * The action function returns a pointer to the value that the mocked method
+ * or function should be returning.
+ */
+struct mock_action {
+	void *(*do_action)(struct mock_action *this,
+			   const void **params,
+			   int len);
+};
+
+/**
+ * struct mock_expectation - represents a *call expectation* on a function.
+ * @action: A &struct mock_action to perform when the function is called.
+ * @max_calls_expected: maximum number of times an expectation may be called.
+ * @min_calls_expected: minimum number of times an expectation may be called.
+ * @retire_on_saturation: no longer match once ``max_calls_expected`` is
+ *			  reached.
+ *
+ * Represents a *call expectation* on a function created with
+ * KUNIT_EXPECT_CALL().
+ */
+struct mock_expectation {
+	struct mock_action *action;
+	int max_calls_expected;
+	int min_calls_expected;
+	bool retire_on_saturation;
+	/* private: internal use only. */
+	const char *expectation_name;
+	struct list_head node;
+	struct mock_matcher *matcher;
+	int times_called;
+};
+
+struct mock_method {
+	struct list_head node;
+	const char *method_name;
+	const void *method_ptr;
+	struct mock_action *default_action;
+	struct list_head expectations;
+};
+
+struct mock {
+	struct kunit_post_condition parent;
+	struct kunit *test;
+	struct list_head methods;
+	/* TODO([email protected]): add locking to do_expect. */
+	const void *(*do_expect)(struct mock *mock,
+				 const char *method_name,
+				 const void *method_ptr,
+				 const char * const *param_types,
+				 const void **params,
+				 int len);
+};
+
+void mock_init_ctrl(struct kunit *test, struct mock *mock);
+
+void mock_validate_expectations(struct mock *mock);
+
+int mock_set_default_action(struct mock *mock,
+			    const char *method_name,
+			    const void *method_ptr,
+			    struct mock_action *action);
+
+struct mock_expectation *mock_add_matcher(struct mock *mock,
+					  const char *method_name,
+					  const void *method_ptr,
+					  struct mock_param_matcher *matchers[],
+					  int len);
+
+#endif /* _KUNIT_MOCK_H */
lib/kunit/Makefile +1 -0
index 1707660c8b1c..a51157970502 100644
@@ -1,6 +1,7 @@
 obj-$(CONFIG_KUNIT) +=			kunit.o
 
 kunit-objs +=				test.o \
+					mock.o \
 					string-stream.o \
 					assert.o \
 					kunit-stream.o \
lib/kunit/mock.c new file +364 -0

364 lines not on this page · raw message

Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#9 lore raw

RFC 8/12 kunit: mock: add basic matchers and actions

In reply to Daniel Latypov

From: Brendan Higgins <[email protected]>

Add basic matchers and actions needed for any kind of mocking to be useful; these matchers and actions are how expectations for mocks are
described: what calls the mocks are expected to receive, and what the mock should do under those circumstances.

---
 include/kunit/mock.h     | 222 ++++++++++++++++++++++++++++++++
 lib/kunit/Makefile       |   1 +
 lib/kunit/common-mocks.c | 271 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 494 insertions(+)
 create mode 100644 lib/kunit/common-mocks.c
include/kunit/mock.h +222 -0
index 299b423fdd51..13fdeb8730b5 100644
@@ -123,4 +123,226 @@ struct mock_expectation *mock_add_matcher(struct mock *mock,
 					  struct mock_param_matcher *matchers[],
 					  int len);
 
+#define CONVERT_TO_ACTUAL_TYPE(type, ptr) (*((type *) ptr))
+
+/**
+ * DOC: Built In Matchers
+ *
+ * These are the matchers that can be used when matching arguments in
+ * :c:func:`KUNIT_EXPECT_CALL` (more can be defined manually).
+ *
+ * For example, there's a matcher that matches any arguments:
+ *
+ * .. code-block:: c
+ *
+ *    struct mock_param_matcher *any(struct kunit *test);
+ *
+ * There are matchers for integers based on the binary condition:
+ *
+ * * eq: equals to
+ * * ne: not equal to
+ * * lt: less than
+ * * le: less than or equal to
+ * * gt: greater than
+ * * ge: greater than or equal to
+ *
+ * .. code-block:: c
+ *
+ *    struct mock_param_matcher *kunit_int_eq(struct kunit *test, int expected);
+ *    struct mock_param_matcher *kunit_int_ne(struct kunit *test, int expected);
+ *    struct mock_param_matcher *kunit_int_lt(struct kunit *test, int expected);
+ *    struct mock_param_matcher *kunit_int_le(struct kunit *test, int expected);
+ *    struct mock_param_matcher *kunit_int_gt(struct kunit *test, int expected);
+ *    struct mock_param_matcher *kunit_int_ge(struct kunit *test, int expected);
+ *
+ * For a detailed list, please see
+ * ``include/linux/mock.h``.
+ */
+
+/* Matches any argument */
+struct mock_param_matcher *kunit_any(struct kunit *test);
+
+/*
+ * Matches different types of integers, the argument is compared to the
+ * `expected` field, based on the comparison defined.
+ */
+struct mock_param_matcher *kunit_u8_eq(struct kunit *test, u8 expected);
+struct mock_param_matcher *kunit_u8_ne(struct kunit *test, u8 expected);
+struct mock_param_matcher *kunit_u8_le(struct kunit *test, u8 expected);
+struct mock_param_matcher *kunit_u8_lt(struct kunit *test, u8 expected);
+struct mock_param_matcher *kunit_u8_ge(struct kunit *test, u8 expected);
+struct mock_param_matcher *kunit_u8_gt(struct kunit *test, u8 expected);
+
+struct mock_param_matcher *kunit_u16_eq(struct kunit *test, u16 expected);
+struct mock_param_matcher *kunit_u16_ne(struct kunit *test, u16 expected);
+struct mock_param_matcher *kunit_u16_le(struct kunit *test, u16 expected);
+struct mock_param_matcher *kunit_u16_lt(struct kunit *test, u16 expected);
+struct mock_param_matcher *kunit_u16_ge(struct kunit *test, u16 expected);
+struct mock_param_matcher *kunit_u16_gt(struct kunit *test, u16 expected);
+
+struct mock_param_matcher *kunit_u32_eq(struct kunit *test, u32 expected);
+struct mock_param_matcher *kunit_u32_ne(struct kunit *test, u32 expected);
+struct mock_param_matcher *kunit_u32_le(struct kunit *test, u32 expected);
+struct mock_param_matcher *kunit_u32_lt(struct kunit *test, u32 expected);
+struct mock_param_matcher *kunit_u32_ge(struct kunit *test, u32 expected);
+struct mock_param_matcher *kunit_u32_gt(struct kunit *test, u32 expected);
+
+struct mock_param_matcher *kunit_u64_eq(struct kunit *test, u64 expected);
+struct mock_param_matcher *kunit_u64_ne(struct kunit *test, u64 expected);
+struct mock_param_matcher *kunit_u64_le(struct kunit *test, u64 expected);
+struct mock_param_matcher *kunit_u64_lt(struct kunit *test, u64 expected);
+struct mock_param_matcher *kunit_u64_ge(struct kunit *test, u64 expected);
+struct mock_param_matcher *kunit_u64_gt(struct kunit *test, u64 expected);
+
+struct mock_param_matcher *kunit_char_eq(struct kunit *test, char expected);
+struct mock_param_matcher *kunit_char_ne(struct kunit *test, char expected);
+struct mock_param_matcher *kunit_char_le(struct kunit *test, char expected);
+struct mock_param_matcher *kunit_char_lt(struct kunit *test, char expected);
+struct mock_param_matcher *kunit_char_ge(struct kunit *test, char expected);
+struct mock_param_matcher *kunit_char_gt(struct kunit *test, char expected);
+
+struct mock_param_matcher *kunit_uchar_eq(struct kunit *test,
+					 unsigned char expected);
+struct mock_param_matcher *kunit_uchar_ne(struct kunit *test,
+					 unsigned char expected);
+struct mock_param_matcher *kunit_uchar_le(struct kunit *test,
+					 unsigned char expected);
+struct mock_param_matcher *kunit_uchar_lt(struct kunit *test,
+					 unsigned char expected);
+struct mock_param_matcher *kunit_uchar_ge(struct kunit *test,
+					 unsigned char expected);
+struct mock_param_matcher *kunit_uchar_gt(struct kunit *test,
+					 unsigned char expected);
+
+struct mock_param_matcher *kunit_schar_eq(struct kunit *test,
+					 signed char expected);
+struct mock_param_matcher *kunit_schar_ne(struct kunit *test,
+					 signed char expected);
+struct mock_param_matcher *kunit_schar_le(struct kunit *test,
+					 signed char expected);
+struct mock_param_matcher *kunit_schar_lt(struct kunit *test,
+					 signed char expected);
+struct mock_param_matcher *kunit_schar_ge(struct kunit *test,
+					 signed char expected);
+struct mock_param_matcher *kunit_schar_gt(struct kunit *test,
+					 signed char expected);
+
+struct mock_param_matcher *kunit_short_eq(struct kunit *test, short expected);
+struct mock_param_matcher *kunit_short_ne(struct kunit *test, short expected);
+struct mock_param_matcher *kunit_short_le(struct kunit *test, short expected);
+struct mock_param_matcher *kunit_short_lt(struct kunit *test, short expected);
+struct mock_param_matcher *kunit_short_ge(struct kunit *test, short expected);
+struct mock_param_matcher *kunit_short_gt(struct kunit *test, short expected);
+
+struct mock_param_matcher *kunit_ushort_eq(struct kunit *test,
+					  unsigned short expected);
+struct mock_param_matcher *kunit_ushort_ne(struct kunit *test,
+					  unsigned short expected);
+struct mock_param_matcher *kunit_ushort_le(struct kunit *test,
+					  unsigned short expected);
+struct mock_param_matcher *kunit_ushort_lt(struct kunit *test,
+					  unsigned short expected);
+struct mock_param_matcher *kunit_ushort_ge(struct kunit *test,
+					  unsigned short expected);
+struct mock_param_matcher *kunit_ushort_gt(struct kunit *test,
+					  unsigned short expected);
+
+struct mock_param_matcher *kunit_int_eq(struct kunit *test, int expected);
+struct mock_param_matcher *kunit_int_ne(struct kunit *test, int expected);
+struct mock_param_matcher *kunit_int_lt(struct kunit *test, int expected);
+struct mock_param_matcher *kunit_int_le(struct kunit *test, int expected);
+struct mock_param_matcher *kunit_int_gt(struct kunit *test, int expected);
+struct mock_param_matcher *kunit_int_ge(struct kunit *test, int expected);
+
+struct mock_param_matcher *kunit_uint_eq(struct kunit *test,
+					unsigned int expected);
+struct mock_param_matcher *kunit_uint_ne(struct kunit *test,
+					unsigned int expected);
+struct mock_param_matcher *kunit_uint_lt(struct kunit *test,
+					unsigned int expected);
+struct mock_param_matcher *kunit_uint_le(struct kunit *test,
+					unsigned int expected);
+struct mock_param_matcher *kunit_uint_gt(struct kunit *test,
+					unsigned int expected);
+struct mock_param_matcher *kunit_uint_ge(struct kunit *test,
+					unsigned int expected);
+
+struct mock_param_matcher *kunit_long_eq(struct kunit *test, long expected);
+struct mock_param_matcher *kunit_long_ne(struct kunit *test, long expected);
+struct mock_param_matcher *kunit_long_le(struct kunit *test, long expected);
+struct mock_param_matcher *kunit_long_lt(struct kunit *test, long expected);
+struct mock_param_matcher *kunit_long_ge(struct kunit *test, long expected);
+struct mock_param_matcher *kunit_long_gt(struct kunit *test, long expected);
+
+struct mock_param_matcher *kunit_ulong_eq(struct kunit *test,
+					 unsigned long expected);
+struct mock_param_matcher *kunit_ulong_ne(struct kunit *test,
+					 unsigned long expected);
+struct mock_param_matcher *kunit_ulong_le(struct kunit *test,
+					 unsigned long expected);
+struct mock_param_matcher *kunit_ulong_lt(struct kunit *test,
+					 unsigned long expected);
+struct mock_param_matcher *kunit_ulong_ge(struct kunit *test,
+					 unsigned long expected);
+struct mock_param_matcher *kunit_ulong_gt(struct kunit *test,
+					 unsigned long expected);
+
+struct mock_param_matcher *kunit_longlong_eq(struct kunit *test,
+					    long long expected);
+struct mock_param_matcher *kunit_longlong_ne(struct kunit *test,
+					    long long expected);
+struct mock_param_matcher *kunit_longlong_le(struct kunit *test,
+					    long long expected);
+struct mock_param_matcher *kunit_longlong_lt(struct kunit *test,
+					    long long expected);
+struct mock_param_matcher *kunit_longlong_ge(struct kunit *test,
+					    long long expected);
+struct mock_param_matcher *kunit_longlong_gt(struct kunit *test,
+					    long long expected);
+
+struct mock_param_matcher *kunit_ulonglong_eq(struct kunit *test,
+					     unsigned long long expected);
+struct mock_param_matcher *kunit_ulonglong_ne(struct kunit *test,
+					     unsigned long long expected);
+struct mock_param_matcher *kunit_ulonglong_le(struct kunit *test,
+					     unsigned long long expected);
+struct mock_param_matcher *kunit_ulonglong_lt(struct kunit *test,
+					     unsigned long long expected);
+struct mock_param_matcher *kunit_ulonglong_ge(struct kunit *test,
+					     unsigned long long expected);
+struct mock_param_matcher *kunit_ulonglong_gt(struct kunit *test,
+					     unsigned long long expected);
+
+/* Matches pointers. */
+struct mock_param_matcher *kunit_ptr_eq(struct kunit *test, void *expected);
+struct mock_param_matcher *kunit_ptr_ne(struct kunit *test, void *expected);
+struct mock_param_matcher *kunit_ptr_lt(struct kunit *test, void *expected);
+struct mock_param_matcher *kunit_ptr_le(struct kunit *test, void *expected);
+struct mock_param_matcher *kunit_ptr_gt(struct kunit *test, void *expected);
+struct mock_param_matcher *kunit_ptr_ge(struct kunit *test, void *expected);
+
+/* Matches memory sections and strings. */
+struct mock_param_matcher *kunit_memeq(struct kunit *test,
+				      const void *buf,
+				      size_t size);
+struct mock_param_matcher *kunit_streq(struct kunit *test, const char *str);
+
+struct mock_action *kunit_u8_return(struct kunit *test, u8 ret);
+struct mock_action *kunit_u16_return(struct kunit *test, u16 ret);
+struct mock_action *kunit_u32_return(struct kunit *test, u32 ret);
+struct mock_action *kunit_u64_return(struct kunit *test, u64 ret);
+struct mock_action *kunit_char_return(struct kunit *test, char ret);
+struct mock_action *kunit_uchar_return(struct kunit *test, unsigned char ret);
+struct mock_action *kunit_schar_return(struct kunit *test, signed char ret);
+struct mock_action *kunit_short_return(struct kunit *test, short ret);
+struct mock_action *kunit_ushort_return(struct kunit *test, unsigned short ret);
+struct mock_action *kunit_int_return(struct kunit *test, int ret);
+struct mock_action *kunit_uint_return(struct kunit *test, unsigned int ret);
+struct mock_action *kunit_long_return(struct kunit *test, long ret);
+struct mock_action *kunit_ulong_return(struct kunit *test, unsigned long ret);
+struct mock_action *kunit_longlong_return(struct kunit *test, long long ret);
+struct mock_action *kunit_ulonglong_return(struct kunit *test,
+					  unsigned long long ret);
+struct mock_action *kunit_ptr_return(struct kunit *test, void *ret);
+
 #endif /* _KUNIT_MOCK_H */
lib/kunit/Makefile +1 -0
index a51157970502..a7a3c5e0a8bf 100644
@@ -2,6 +2,7 @@ obj-$(CONFIG_KUNIT) +=			kunit.o
 
 kunit-objs +=				test.o \
 					mock.o \
+					common-mocks.o \
 					string-stream.o \
 					assert.o \
 					kunit-stream.o \
lib/kunit/common-mocks.c new file +271 -0

271 lines not on this page · raw message

Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#10 lore raw

Re: [RFC v1 08/12] kunit: mock: add basic matchers and actions

reproduce:

Hi Daniel,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on 10b82d5176488acee2820e5a2cf0f2ec5c3488b6]

url:    https://github.com/0day-ci/linux/commits/Daniel-Latypov/kunit-introduce-class-mocking-support/20200919-023253
base:    10b82d5176488acee2820e5a2cf0f2ec5c3488b6
config: i386-randconfig-s002-20200917 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.2-201-g24bdaac6-dirty
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

>> lib/kunit/common-mocks.c:105:1: sparse: sparse: symbol 'to_mock_u8_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:106:1: sparse: sparse: symbol 'to_mock_u16_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:107:1: sparse: sparse: symbol 'to_mock_u32_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:108:1: sparse: sparse: symbol 'to_mock_u64_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:109:1: sparse: sparse: symbol 'to_mock_char_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:110:1: sparse: sparse: symbol 'to_mock_uchar_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:111:1: sparse: sparse: symbol 'to_mock_schar_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:112:1: sparse: sparse: symbol 'to_mock_short_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:113:1: sparse: sparse: symbol 'to_mock_ushort_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:114:1: sparse: sparse: symbol 'to_mock_int_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:115:1: sparse: sparse: symbol 'to_mock_uint_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:116:1: sparse: sparse: symbol 'to_mock_long_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:117:1: sparse: sparse: symbol 'to_mock_ulong_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:118:1: sparse: sparse: symbol 'to_mock_longlong_matcher' was not declared. Should it be static?
>> lib/kunit/common-mocks.c:119:1: sparse: sparse: symbol 'to_mock_ulonglong_matcher' was not declared. Should it be static?

lib/kunit/common-mocks.c:119:1: sparse: sparse: too many warnings

Please review and possibly fold the followup patch.

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#11 lore raw

[RFC PATCH] kunit: mock: to_mock_u8_matcher can be static

In reply to Daniel Latypov
Signed-off-by: kernel test robot <[email protected]>
---
 common-mocks.c |   30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
lib/kunit/common-mocks.c +15 -15
index b3a410fda63a8a..463a301518d368 100644
@@ -102,21 +102,21 @@ struct mock_param_matcher *kunit_any(struct kunit *test)
 
 #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
 
-DEFINE_MATCHER(u8);
-DEFINE_MATCHER(u16);
-DEFINE_MATCHER(u32);
-DEFINE_MATCHER(u64);
-DEFINE_MATCHER(char);
-DEFINE_MATCHER_WITH_TYPENAME(uchar, unsigned char);
-DEFINE_MATCHER_WITH_TYPENAME(schar, signed char);
-DEFINE_MATCHER(short);
-DEFINE_MATCHER_WITH_TYPENAME(ushort, unsigned short);
-DEFINE_MATCHER(int);
-DEFINE_MATCHER_WITH_TYPENAME(uint, unsigned int);
-DEFINE_MATCHER(long);
-DEFINE_MATCHER_WITH_TYPENAME(ulong, unsigned long);
-DEFINE_MATCHER_WITH_TYPENAME(longlong, long long);
-DEFINE_MATCHER_WITH_TYPENAME(ulonglong, unsigned long long);
+static DEFINE_MATCHER(u8);
+static DEFINE_MATCHER(u16);
+static DEFINE_MATCHER(u32);
+static DEFINE_MATCHER(u64);
+static DEFINE_MATCHER(char);
+static DEFINE_MATCHER_WITH_TYPENAME(uchar, unsigned char);
+static DEFINE_MATCHER_WITH_TYPENAME(schar, signed char);
+static DEFINE_MATCHER(short);
+static DEFINE_MATCHER_WITH_TYPENAME(ushort, unsigned short);
+static DEFINE_MATCHER(int);
+static DEFINE_MATCHER_WITH_TYPENAME(uint, unsigned int);
+static DEFINE_MATCHER(long);
+static DEFINE_MATCHER_WITH_TYPENAME(ulong, unsigned long);
+static DEFINE_MATCHER_WITH_TYPENAME(longlong, long long);
+static DEFINE_MATCHER_WITH_TYPENAME(ulonglong, unsigned long long);
 
 DEFINE_MATCHER_WITH_TYPENAME(ptr, void *);
 
#12 lore raw

Re: [RFC v1 08/12] kunit: mock: add basic matchers and actions

In reply to Daniel Latypov
reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # install arm cross compiling tool for…

Hi Daniel,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on 10b82d5176488acee2820e5a2cf0f2ec5c3488b6]

url:    https://github.com/0day-ci/linux/commits/Daniel-Latypov/kunit-introduce-class-mocking-support/20200919-023253
base:    10b82d5176488acee2820e5a2cf0f2ec5c3488b6
config: arm-randconfig-r013-20200917 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 0ff28fa6a75617d61b1aeea77463d6a1684c3c89)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> lib/kunit/common-mocks.c:105:1: warning: no previous prototype for function 'to_mock_u8_matcher' [-Wmissing-prototypes]
   DEFINE_MATCHER(u8);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:89:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_TO_MATCHER_STRUCT(type_name)                            \
                   ^
   lib/kunit/common-mocks.c:37:3: note: expanded from macro 'DEFINE_TO_MATCHER_STRUCT'
                   to_mock_##type_name##_matcher(                                 \
                   ^
   <scratch space>:32:1: note: expanded from here
   to_mock_u8_matcher
   ^
   lib/kunit/common-mocks.c:105:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:89:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_TO_MATCHER_STRUCT(type_name)                            \
                   ^
   lib/kunit/common-mocks.c:36:3: note: expanded from macro 'DEFINE_TO_MATCHER_STRUCT'
                   struct mock_##type_name##_matcher *                            \
                   ^
>> lib/kunit/common-mocks.c:105:1: warning: no previous prototype for function 'match_u8_eq' [-Wmissing-prototypes]
   DEFINE_MATCHER(u8);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:90:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, eq, ==)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:42:1: note: expanded from here
   match_u8_eq
   ^
   lib/kunit/common-mocks.c:105:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:90:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, eq, ==)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:105:1: warning: no previous prototype for function 'match_u8_ne' [-Wmissing-prototypes]
   DEFINE_MATCHER(u8);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:92:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ne, !=)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:59:1: note: expanded from here
   match_u8_ne
   ^
   lib/kunit/common-mocks.c:105:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:92:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ne, !=)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:105:1: warning: no previous prototype for function 'match_u8_le' [-Wmissing-prototypes]
   DEFINE_MATCHER(u8);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:94:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, le, <=)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:76:1: note: expanded from here
   match_u8_le
   ^
   lib/kunit/common-mocks.c:105:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:94:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, le, <=)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:105:1: warning: no previous prototype for function 'match_u8_lt' [-Wmissing-prototypes]
   DEFINE_MATCHER(u8);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:96:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, lt, <)                      \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:93:1: note: expanded from here
   match_u8_lt
   ^
   lib/kunit/common-mocks.c:105:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:96:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, lt, <)                      \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:105:1: warning: no previous prototype for function 'match_u8_ge' [-Wmissing-prototypes]
   DEFINE_MATCHER(u8);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:98:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ge, >=)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:110:1: note: expanded from here
   match_u8_ge
   ^
   lib/kunit/common-mocks.c:105:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:98:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ge, >=)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:105:1: warning: no previous prototype for function 'match_u8_gt' [-Wmissing-prototypes]
   DEFINE_MATCHER(u8);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:100:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, gt, >)                      \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:127:1: note: expanded from here
   match_u8_gt
   ^
   lib/kunit/common-mocks.c:105:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:100:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, gt, >)                      \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:106:1: warning: no previous prototype for function 'to_mock_u16_matcher' [-Wmissing-prototypes]
   DEFINE_MATCHER(u16);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:89:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_TO_MATCHER_STRUCT(type_name)                            \
                   ^
   lib/kunit/common-mocks.c:37:3: note: expanded from macro 'DEFINE_TO_MATCHER_STRUCT'
                   to_mock_##type_name##_matcher(                                 \
                   ^
   <scratch space>:145:1: note: expanded from here
   to_mock_u16_matcher
   ^
   lib/kunit/common-mocks.c:106:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:89:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_TO_MATCHER_STRUCT(type_name)                            \
                   ^
   lib/kunit/common-mocks.c:36:3: note: expanded from macro 'DEFINE_TO_MATCHER_STRUCT'
                   struct mock_##type_name##_matcher *                            \
                   ^
>> lib/kunit/common-mocks.c:106:1: warning: no previous prototype for function 'match_u16_eq' [-Wmissing-prototypes]
   DEFINE_MATCHER(u16);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:90:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, eq, ==)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:155:1: note: expanded from here
   match_u16_eq
   ^
   lib/kunit/common-mocks.c:106:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:90:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, eq, ==)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:106:1: warning: no previous prototype for function 'match_u16_ne' [-Wmissing-prototypes]
   DEFINE_MATCHER(u16);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:92:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ne, !=)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:172:1: note: expanded from here
   match_u16_ne
   ^
   lib/kunit/common-mocks.c:106:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:92:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ne, !=)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:106:1: warning: no previous prototype for function 'match_u16_le' [-Wmissing-prototypes]
   DEFINE_MATCHER(u16);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:94:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, le, <=)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:189:1: note: expanded from here
   match_u16_le
   ^
   lib/kunit/common-mocks.c:106:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:94:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, le, <=)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:106:1: warning: no previous prototype for function 'match_u16_lt' [-Wmissing-prototypes]
   DEFINE_MATCHER(u16);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:96:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, lt, <)                      \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:206:1: note: expanded from here
   match_u16_lt
   ^
   lib/kunit/common-mocks.c:106:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:96:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, lt, <)                      \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:106:1: warning: no previous prototype for function 'match_u16_ge' [-Wmissing-prototypes]
   DEFINE_MATCHER(u16);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:98:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ge, >=)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:223:1: note: expanded from here
   match_u16_ge
   ^
   lib/kunit/common-mocks.c:106:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:98:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ge, >=)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:106:1: warning: no previous prototype for function 'match_u16_gt' [-Wmissing-prototypes]
   DEFINE_MATCHER(u16);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:100:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, gt, >)                      \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:240:1: note: expanded from here
   match_u16_gt
   ^
   lib/kunit/common-mocks.c:106:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:100:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, gt, >)                      \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:107:1: warning: no previous prototype for function 'to_mock_u32_matcher' [-Wmissing-prototypes]
   DEFINE_MATCHER(u32);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:89:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_TO_MATCHER_STRUCT(type_name)                            \
                   ^
   lib/kunit/common-mocks.c:37:3: note: expanded from macro 'DEFINE_TO_MATCHER_STRUCT'
                   to_mock_##type_name##_matcher(                                 \
                   ^
   <scratch space>:258:1: note: expanded from here
   to_mock_u32_matcher
   ^
   lib/kunit/common-mocks.c:107:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:89:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_TO_MATCHER_STRUCT(type_name)                            \
                   ^
   lib/kunit/common-mocks.c:36:3: note: expanded from macro 'DEFINE_TO_MATCHER_STRUCT'
                   struct mock_##type_name##_matcher *                            \
                   ^
>> lib/kunit/common-mocks.c:107:1: warning: no previous prototype for function 'match_u32_eq' [-Wmissing-prototypes]
   DEFINE_MATCHER(u32);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:90:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, eq, ==)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:268:1: note: expanded from here
   match_u32_eq
   ^
   lib/kunit/common-mocks.c:107:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:90:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, eq, ==)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:107:1: warning: no previous prototype for function 'match_u32_ne' [-Wmissing-prototypes]
   DEFINE_MATCHER(u32);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:92:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ne, !=)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:285:1: note: expanded from here
   match_u32_ne
   ^
   lib/kunit/common-mocks.c:107:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:92:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ne, !=)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:107:1: warning: no previous prototype for function 'match_u32_le' [-Wmissing-prototypes]
   DEFINE_MATCHER(u32);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:94:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, le, <=)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:302:1: note: expanded from here
   match_u32_le
   ^
   lib/kunit/common-mocks.c:107:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:94:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, le, <=)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:107:1: warning: no previous prototype for function 'match_u32_lt' [-Wmissing-prototypes]
   DEFINE_MATCHER(u32);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:96:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, lt, <)                      \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:15:1: note: expanded from here
   match_u32_lt
   ^
   lib/kunit/common-mocks.c:107:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:96:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, lt, <)                      \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
>> lib/kunit/common-mocks.c:107:1: warning: no previous prototype for function 'match_u32_ge' [-Wmissing-prototypes]
   DEFINE_MATCHER(u32);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:98:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ge, >=)                     \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:32:1: note: expanded from here
   match_u32_ge
   ^
   lib/kunit/common-mocks.c:107:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:98:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, ge, >=)                     \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
   lib/kunit/common-mocks.c:107:1: warning: no previous prototype for function 'match_u32_gt' [-Wmissing-prototypes]
   DEFINE_MATCHER(u32);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:100:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, gt, >)                      \
                   ^
   lib/kunit/common-mocks.c:46:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                        ^
   <scratch space>:49:1: note: expanded from here
   match_u32_gt
   ^
   lib/kunit/common-mocks.c:107:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:100:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, gt, >)                      \
                   ^
   lib/kunit/common-mocks.c:46:3: note: expanded from macro 'DEFINE_MATCH_FUNC'
                   bool match_##type_name##_##op_name(                            \
                   ^
   lib/kunit/common-mocks.c:108:1: warning: no previous prototype for function 'to_mock_u64_matcher' [-Wmissing-prototypes]
   DEFINE_MATCHER(u64);
   ^
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:89:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_TO_MATCHER_STRUCT(type_name)                            \
                   ^
   lib/kunit/common-mocks.c:37:3: note: expanded from macro 'DEFINE_TO_MATCHER_STRUCT'
                   to_mock_##type_name##_matcher(                                 \
                   ^
   <scratch space>:67:1: note: expanded from here
   to_mock_u64_matcher
   ^
   lib/kunit/common-mocks.c:108:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^
   lib/kunit/common-mocks.c:89:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_TO_MATCHER_STRUCT(type_name)                            \
                   ^
   lib/kunit/common-mocks.c:36:3: note: expanded from macro 'DEFINE_TO_MATCHER_STRUCT'
                   struct mock_##type_name##_matcher *                            \
                   ^
   lib/kunit/common-mocks.c:108:1: warning: format specifies type 'int' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
   DEFINE_MATCHER(u64);
   ^~~~~~~~~~~~~~~~~~~
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/kunit/common-mocks.c:90:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, eq, ==)                     \
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/kunit/common-mocks.c:59:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                                                    actual,                       \
                                                    ^~~~~~
   lib/kunit/common-mocks.c:108:1: warning: format specifies type 'int' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
   DEFINE_MATCHER(u64);
   ^~~~~~~~~~~~~~~~~~~
   lib/kunit/common-mocks.c:103:30: note: expanded from macro 'DEFINE_MATCHER'
   #define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type)
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/kunit/common-mocks.c:90:3: note: expanded from macro 'DEFINE_MATCHER_WITH_TYPENAME'
                   DEFINE_MATCH_FUNC(type_name, type, eq, ==)                     \
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/kunit/common-mocks.c:60:8: note: expanded from macro 'DEFINE_MATCH_FUNC'
                                                    matcher->expected);           \
                                                    ^~~~~~~~~~~~~~~~~

lib/kunit/common-mocks.c:108:1: warning: format specifies type 'int' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat] DEFINE_MATCHER(u64);

# https://github.com/0day-ci/linux/commit/32db0336f68431c2ad957f5dcd80ce827488660f
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Daniel-Latypov/kunit-introduce-class-mocking-support/20200919-023253 git checkout 32db0336f68431c2ad957f5dcd80ce827488660f
vim +/to_mock_u8_matcher +105 lib/kunit/common-mocks.c

104

 > 105	DEFINE_MATCHER(u8);
 > 106	DEFINE_MATCHER(u16);
 > 107	DEFINE_MATCHER(u32);
 > 108	DEFINE_MATCHER(u64);
 > 109	DEFINE_MATCHER(char);
 > 110	DEFINE_MATCHER_WITH_TYPENAME(uchar, unsigned char);
 > 111	DEFINE_MATCHER_WITH_TYPENAME(schar, signed char);
 > 112	DEFINE_MATCHER(short);
 > 113	DEFINE_MATCHER_WITH_TYPENAME(ushort, unsigned short);
 > 114	DEFINE_MATCHER(int);
 > 115	DEFINE_MATCHER_WITH_TYPENAME(uint, unsigned int);
 > 116	DEFINE_MATCHER(long);
 > 117	DEFINE_MATCHER_WITH_TYPENAME(ulong, unsigned long);
 > 118	DEFINE_MATCHER_WITH_TYPENAME(longlong, long long);
 > 119	DEFINE_MATCHER_WITH_TYPENAME(ulonglong, unsigned long long);
   120	
 > 121	DEFINE_MATCHER_WITH_TYPENAME(ptr, void *);
   122	
   123	struct mock_memeq_matcher {
   124		struct mock_param_matcher matcher;
   125		const void *expected;
   126		size_t size;
   127	};
   128	
   129	static bool match_memeq(struct mock_param_matcher *pmatcher,
   130				struct kunit_stream *stream,
   131				const void *pactual)
   132	{
   133		struct mock_memeq_matcher *matcher =
   134				container_of(pmatcher,
   135					     struct mock_memeq_matcher,
   136					     matcher);
   137		const void *actual = CONVERT_TO_ACTUAL_TYPE(const void *, pactual);
   138		bool matches = !memcmp(actual, matcher->expected, matcher->size);
   139		int i;
   140	
   141		for (i = 0; i < matcher->size; i++)
   142			kunit_stream_add(stream, "%02x, ", ((const char *) actual)[i]);
   143		if (matches)
   144			kunit_stream_add(stream, "== ");
   145		else
   146			kunit_stream_add(stream, "!= ");
   147		for (i = 0; i < matcher->size; i++)
   148			kunit_stream_add(stream,
   149				    "%02x, ",
   150				    ((const char *) matcher->expected)[i]);
   151	
   152		return matches;
   153	}
   154	
   155	struct mock_param_matcher *kunit_memeq(struct kunit *test,
   156					      const void *buf,
   157					      size_t size)
   158	{
   159		struct mock_memeq_matcher *matcher;
   160	
   161		matcher = kunit_kzalloc(test, sizeof(*matcher), GFP_KERNEL);
   162		if (!matcher)
   163			return NULL;
   164	
   165		matcher->matcher.match = match_memeq;
   166		matcher->expected = buf;
   167		matcher->size = size;
   168	
   169		return &matcher->matcher;
   170	}
   171	
   172	struct mock_streq_matcher {
   173		struct mock_param_matcher matcher;
   174		const char *expected;
   175	};
   176	
   177	static bool match_streq(struct mock_param_matcher *pmatcher,
   178				struct kunit_stream *stream,
   179				const void *pactual)
   180	{
   181		struct mock_streq_matcher *matcher =
   182				container_of(pmatcher,
   183					     struct mock_streq_matcher,
   184					     matcher);
   185		const char *actual = CONVERT_TO_ACTUAL_TYPE(const char *, pactual);
   186		bool matches = !strcmp(actual, matcher->expected);
   187	
   188		if (matches)
   189			kunit_stream_add(stream, "%s == %s", actual, matcher->expected);
   190		else
   191			kunit_stream_add(stream, "%s != %s", actual, matcher->expected);
   192	
   193		return matches;
   194	}
   195	
   196	struct mock_param_matcher *kunit_streq(struct kunit *test, const char *str)
   197	{
   198		struct mock_streq_matcher *matcher;
   199	
   200		matcher = kunit_kzalloc(test, sizeof(*matcher), GFP_KERNEL);
   201		if (!matcher)
   202			return NULL;
   203	
   204		matcher->matcher.match = match_streq;
   205		matcher->expected = str;
   206	
   207		return &matcher->matcher;
   208	}
   209	
   210	#define DEFINE_RETURN_ACTION_STRUCT(type_name, type)			       \
   211			struct mock_##type_name##_action {			       \
   212				struct mock_action action;			       \
   213				type ret;					       \
   214			}
   215	
   216	#define DEFINE_RETURN_ACTION_FUNC(type_name, type)			       \
   217			void *do_##type_name##_return(struct mock_action *paction,     \
   218						      const void **params,	       \
   219						      int len)			       \
   220			{							       \
   221				struct mock_##type_name##_action *action =	       \
   222						container_of(paction,		       \
   223							     struct mock_##type_name##_action,\
   224							     action);		       \
   225										       \
   226				return (void *) &action->ret;			       \
   227			}
   228	
   229	#define DEFINE_RETURN_ACTION_FACTORY(type_name, type)			       \
   230			struct mock_action *kunit_##type_name##_return(		       \
   231					struct kunit *test,			       \
   232					type ret)				       \
   233			{							       \
   234				struct mock_##type_name##_action *action;	       \
   235										       \
   236				action = kunit_kmalloc(test,			       \
   237						      sizeof(*action),		       \
   238						      GFP_KERNEL);		       \
   239				if (!action)					       \
   240					return NULL;				       \
   241										       \
   242				action->action.do_action = do_##type_name##_return;    \
   243				action->ret = ret;				       \
   244										       \
   245				return &action->action;				       \
   246			}
   247	
   248	#define DEFINE_RETURN_ACTION_WITH_TYPENAME(type_name, type)		       \
   249			DEFINE_RETURN_ACTION_STRUCT(type_name, type);		       \
   250			DEFINE_RETURN_ACTION_FUNC(type_name, type);		       \
   251			DEFINE_RETURN_ACTION_FACTORY(type_name, type)
   252	
   253	#define DEFINE_RETURN_ACTION(type) \
   254			DEFINE_RETURN_ACTION_WITH_TYPENAME(type, type)
   255	
 > 256	DEFINE_RETURN_ACTION(u8);
 > 257	DEFINE_RETURN_ACTION(u16);
 > 258	DEFINE_RETURN_ACTION(u32);
 > 259	DEFINE_RETURN_ACTION(u64);
 > 260	DEFINE_RETURN_ACTION(char);
 > 261	DEFINE_RETURN_ACTION_WITH_TYPENAME(uchar, unsigned char);
 > 262	DEFINE_RETURN_ACTION_WITH_TYPENAME(schar, signed char);
 > 263	DEFINE_RETURN_ACTION(short);
 > 264	DEFINE_RETURN_ACTION_WITH_TYPENAME(ushort, unsigned short);
 > 265	DEFINE_RETURN_ACTION(int);
 > 266	DEFINE_RETURN_ACTION_WITH_TYPENAME(uint, unsigned int);
 > 267	DEFINE_RETURN_ACTION(long);
 > 268	DEFINE_RETURN_ACTION_WITH_TYPENAME(ulong, unsigned long);
 > 269	DEFINE_RETURN_ACTION_WITH_TYPENAME(longlong, long long);
 > 270	DEFINE_RETURN_ACTION_WITH_TYPENAME(ulonglong, unsigned long long);
 > 271	DEFINE_RETURN_ACTION_WITH_TYPENAME(ptr, void *);

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#13 lore raw

RFC 9/12 kunit: mock: add macro machinery to pick correct format args

In reply to Daniel Latypov

From: Marcelo Schmitt <[email protected]>

Note: It was unclear if there was existing code that could be reused.

This is used by DEFINE_MATCHER to generate matching funcs for primitive types that don't trigger compiler warnings.

After preprocessing, we now generate matcher func code like
kunit_stream_add(stream, "%p not > "%p", actual, matcher->expected)
as opposed to the hoping %d will work for all types.

---
 lib/kunit/common-mocks.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
lib/kunit/common-mocks.c +23 -2
index b3a410fda63a..1f755f26cf5f 100644
@@ -42,6 +42,27 @@ struct mock_param_matcher *kunit_any(struct kunit *test)
 					    matcher);			       \
 		}
 
+#define TYPE_FRMT(type_name) FORMAT_##type_name
+#define FORMAT_u8 "%hu"
+#define FORMAT_u16 "%hu"
+#define FORMAT_u32 "%u"
+#define FORMAT_u64 "%llu"
+#define FORMAT_char "%c"
+#define FORMAT_uchar "%c"
+#define FORMAT_schar "%c"
+#define FORMAT_short "%hd"
+#define FORMAT_ushort "%hu"
+#define FORMAT_int "%d"
+#define FORMAT_uint "%u"
+#define FORMAT_long "%ld"
+#define FORMAT_ulong "%lu"
+#define FORMAT_longlong "%lld"
+#define FORMAT_ulonglong "%llu"
+#define FORMAT_ptr "%p"
+
+#define CMP_FORMAT(type_name, msg, op)                                        \
+	       TYPE_FRMT(type_name) msg " " #op " " TYPE_FRMT(type_name)
+
 #define DEFINE_MATCH_FUNC(type_name, type, op_name, op)			       \
 		bool match_##type_name##_##op_name(			       \
 				struct mock_param_matcher *pmatcher,	       \
@@ -55,12 +76,12 @@ struct mock_param_matcher *kunit_any(struct kunit *test)
 									       \
 			if (matches)					       \
 				kunit_stream_add(stream,		       \
-						 "%d "#op" %d",		       \
+						 CMP_FORMAT(type_name, "", op),\
 						 actual,		       \
 						 matcher->expected);	       \
 			else						       \
 				kunit_stream_add(stream,		       \
-						 "%d not "#op" %d",	       \
+						 CMP_FORMAT(type_name, " not", op), \
 						 actual,		       \
 						 matcher->expected);	       \
 									       \
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#14 lore raw

RFC 10/12 kunit: mock: add class mocking support

In reply to Daniel Latypov

From: Brendan Higgins <[email protected]>

Introduce basic class mocking, the ability to automatically generate a Linux C-style class implementation whose behavior is controlled by test cases, which can also set expectations on when and how mocks are called.

---
 include/kunit/mock.h           | 433 +++++++++++++++++++++++++++++++++
 lib/kunit/Makefile             |   5 +-
 lib/kunit/kunit-example-test.c |  90 +++++++
 lib/kunit/mock-macro-test.c    |  91 +++++++
 lib/kunit/mock-test.c          | 320 ++++++++++++++++++++++++
 5 files changed, 937 insertions(+), 2 deletions(-)
 create mode 100644 lib/kunit/mock-test.c
include/kunit/mock.h +433 -0
index 13fdeb8730b5..43f797c7e8bb 100644
@@ -123,6 +123,439 @@ struct mock_expectation *mock_add_matcher(struct mock *mock,
 					  struct mock_param_matcher *matchers[],
 					  int len);
 
+#define MOCK(name) name##_mock
+
+/**
+ * KUNIT_EXPECT_CALL() - Declares a *call expectation* on a mock function.
+ * @expectation_call: a mocked method or function with parameters replaced with
+ *                    matchers.
+ *
+ * Example:
+ *
+ * .. code-block:: c
+ *
+ *	// Class to mock.
+ *	struct example {
+ *		int (*foo)(struct example *, int);
+ *	};
+ *
+ *	// Define the mock.
+ *	DECLARE_STRUCT_CLASS_MOCK_PREREQS(example);
+ *
+ *	DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
+ *				 RETURNS(int),
+ *				 PARAMS(struct example *, int));
+ *
+ *	static int example_init(struct MOCK(example) *mock_example)
+ *	{
+ *		struct example *example = mock_get_trgt(mock_example);
+ *
+ *		example->foo = foo;
+ *		return 0;
+ *	}
+ *
+ *	DEFINE_STRUCT_CLASS_MOCK_INIT(example, example_init);
+ *
+ *	static void foo_example_test_success(struct kunit *test)
+ *	{
+ *		struct MOCK(example) *mock_example;
+ *		struct example *example = mock_get_trgt(mock_example);
+ *		struct mock_expectation *handle;
+ *
+ *		mock_example = CONSTRUCT_MOCK(example, test);
+ *
+ *		handle = KUNIT_EXPECT_CALL(foo(mock_get_ctrl(mock_example),
+ *					       kunit_int_eq(test, 5)));
+ *		handle->action = int_return(test, 2);
+ *
+ *		KUNIT_EXPECT_EQ(test, 2, example_bar(example, 5));
+ *	}
+ *
+ * Return:
+ * A &struct mock_expectation representing the call expectation.
+ * allowing additional conditions and actions to be specified.
+ */
+#define KUNIT_EXPECT_CALL(expectation_call) mock_master_##expectation_call
+
+#define mock_get_ctrl_internal(mock_object) (&(mock_object)->ctrl)
+#define mock_get_ctrl(mock_object) mock_get_ctrl_internal(mock_object)
+
+#define mock_get_trgt_internal(mock_object) (&(mock_object)->trgt)
+#define mock_get_trgt(mock_object) mock_get_trgt_internal(mock_object)
+
+#define mock_get_test(mock_object) (mock_get_ctrl(mock_object)->test)
+
+#define CLASS(struct_name) struct_name
+#define HANDLE_INDEX(index) index
+#define METHOD(method_name) method_name
+#define RETURNS(return_type) return_type
+/* #define PARAMS(...) __VA_ARGS__ included by linux/tracepoint.h */
+
+#define MOCK_INIT_ID(struct_name) struct_name##mock_init
+#define REAL_ID(func_name) __real__##func_name
+#define INVOKE_ID(func_name) __invoke__##func_name
+
+#define DECLARE_MOCK_CLIENT(name, return_type, param_types...) \
+		return_type name(PARAM_LIST_FROM_TYPES(param_types))
+
+#define DECLARE_MOCK_MASTER(name, ctrl_index, param_types...)		       \
+		struct mock_expectation *mock_master_##name(		       \
+				MATCHER_PARAM_LIST_FROM_TYPES(ctrl_index,      \
+							      param_types))
+
+#define DECLARE_MOCK_COMMON(name, handle_index, return_type, param_types...)   \
+		DECLARE_MOCK_CLIENT(name, return_type, param_types);	       \
+		DECLARE_MOCK_MASTER(name, handle_index, param_types)
+
+#define DECLARE_STRUCT_CLASS_MOCK_STRUCT(struct_name)			       \
+		struct MOCK(struct_name) {				       \
+			struct mock		ctrl;			       \
+			struct struct_name	trgt;			       \
+		}
+
+#define DECLARE_STRUCT_CLASS_MOCK_CONVERTER(struct_name)		       \
+		static inline struct mock *from_##struct_name##_to_mock(       \
+				const struct struct_name *trgt)		       \
+		{							       \
+			return mock_get_ctrl(				       \
+					container_of(trgt,		       \
+						     struct MOCK(struct_name), \
+						     trgt));		       \
+		}
+
+/**
+ * DECLARE_STRUCT_CLASS_MOCK_PREREQS() - Create a mock child class
+ * @struct_name: name of the class/struct to be mocked
+ *
+ * Creates a mock child class of ``struct_name`` named
+ * ``struct MOCK(struct_name)`` along with supporting internally used methods.
+ *
+ * See KUNIT_EXPECT_CALL() for example usages.
+ */
+#define DECLARE_STRUCT_CLASS_MOCK_PREREQS(struct_name)			       \
+		DECLARE_STRUCT_CLASS_MOCK_STRUCT(struct_name);		       \
+		DECLARE_STRUCT_CLASS_MOCK_CONVERTER(struct_name)
+
+#define DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name,		       \
+							struct_name,	       \
+							handle_index,	       \
+							return_type,	       \
+							param_types...)	       \
+		DECLARE_MOCK_COMMON(name,				       \
+				    handle_index,			       \
+				    return_type,			       \
+				    param_types)
+
+#define DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name,			       \
+					       struct_name,		       \
+					       handle_index,		       \
+					       return_type,		       \
+					       param_types...)		       \
+		DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name,	       \
+								struct_name,   \
+								handle_index,  \
+								return_type,   \
+								param_types)
+
+/**
+ * DECLARE_STRUCT_CLASS_MOCK()
+ * @name: method name
+ * @struct_name: name of the class/struct
+ * @return_type: return type of the method
+ * @param_types: parameters of the method
+ *
+ * Same as DEFINE_STRUCT_CLASS_MOCK(), but only makes header compatible
+ * declarations.
+ */
+#define DECLARE_STRUCT_CLASS_MOCK(name,					       \
+				  struct_name,				       \
+				  return_type,				       \
+				  param_types...)			       \
+		DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name,		       \
+						       struct_name,	       \
+						       0,		       \
+						       return_type,	       \
+						       param_types)
+
+/**
+ * DECLARE_STRUCT_CLASS_MOCK_VOID_RETURN()
+ * @name: method name
+ * @struct_name: name of the class/struct
+ * @param_types: parameters of the method
+ *
+ * Same as DEFINE_STRUCT_CLASS_MOCK_VOID_RETURN(), but only makes header
+ * compatible declarations.
+ */
+#define DECLARE_STRUCT_CLASS_MOCK_VOID_RETURN(name,			       \
+					      struct_name,		       \
+					      param_types...)		       \
+		DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name,		       \
+						       struct_name,	       \
+						       0,		       \
+						       void,		       \
+						       param_types)
+
+/**
+ * DECLARE_STRUCT_CLASS_MOCK_INIT()
+ * @struct_name: name of the class/struct
+ *
+ * Same as DEFINE_STRUCT_CLASS_MOCK_INIT(), but only makes header compatible
+ * declarations.
+ */
+#define DECLARE_STRUCT_CLASS_MOCK_INIT(struct_name)			       \
+		struct MOCK(struct_name) *MOCK_INIT_ID(struct_name)(	       \
+				struct kunit *test)
+
+/**
+ * CONSTRUCT_MOCK()
+ * @struct_name: name of the class
+ * @test: associated test
+ *
+ * Constructs and allocates a test managed ``struct MOCK(struct_name)`` given
+ * the name of the class for which the mock is defined and a test object.
+ *
+ * See KUNIT_EXPECT_CALL() for example usage.
+ */
+#define CONSTRUCT_MOCK(struct_name, test) MOCK_INIT_ID(struct_name)(test)
+
+#define DEFINE_MOCK_CLIENT_COMMON(name,					       \
+				  handle_index,				       \
+				  MOCK_SOURCE,				       \
+				  mock_source_ctx,			       \
+				  return_type,				       \
+				  RETURN,				       \
+				  param_types...)			       \
+		return_type name(PARAM_LIST_FROM_TYPES(param_types))	       \
+		{							       \
+			struct mock *mock = MOCK_SOURCE(mock_source_ctx,       \
+							handle_index);	       \
+			static const char * const param_type_names[] = {       \
+				TYPE_NAMES_FROM_TYPES(handle_index,	       \
+						      param_types)	       \
+			};						       \
+			const void *params[] = {			       \
+				PTR_TO_ARG_FROM_TYPES(handle_index,	       \
+						      param_types)	       \
+			};						       \
+			const void *retval;				       \
+									       \
+			retval = mock->do_expect(mock,			       \
+						 #name,			       \
+						 name,			       \
+						 param_type_names,	       \
+						 params,		       \
+						 ARRAY_SIZE(params));	       \
+			KUNIT_ASSERT_NOT_ERR_OR_NULL(mock->test, retval);      \
+			if (!retval) {					       \
+				kunit_info(mock->test,			       \
+					   "no action installed for "#name"\n");\
+				BUG();					       \
+			}						       \
+			RETURN(return_type, retval);			       \
+		}
+
+#define CLASS_MOCK_CLIENT_SOURCE(ctx, handle_index) ctx(arg##handle_index)
+#define DEFINE_MOCK_METHOD_CLIENT_COMMON(name,				       \
+					 handle_index,			       \
+					 mock_converter,		       \
+					 return_type,			       \
+					 RETURN,			       \
+					 param_types...)		       \
+		DEFINE_MOCK_CLIENT_COMMON(name,				       \
+					  handle_index,			       \
+					  CLASS_MOCK_CLIENT_SOURCE,	       \
+					  mock_converter,		       \
+					  return_type,			       \
+					  RETURN,			       \
+					  param_types)
+
+#define CAST_AND_RETURN(return_type, retval) return *((return_type *) retval)
+#define NO_RETURN(return_type, retval)
+
+#define DEFINE_MOCK_METHOD_CLIENT(name,					       \
+				  handle_index,				       \
+				  mock_converter,			       \
+				  return_type,				       \
+				  param_types...)			       \
+		DEFINE_MOCK_METHOD_CLIENT_COMMON(name,			       \
+						 handle_index,		       \
+						 mock_converter,	       \
+						 return_type,		       \
+						 CAST_AND_RETURN,	       \
+						 param_types)
+
+#define DEFINE_MOCK_METHOD_CLIENT_VOID_RETURN(name,			       \
+					      handle_index,		       \
+					      mock_converter,		       \
+					      param_types...)		       \
+		DEFINE_MOCK_METHOD_CLIENT_COMMON(name,			       \
+						 handle_index,		       \
+						 mock_converter,	       \
+						 void,			       \
+						 NO_RETURN,		       \
+						 param_types)
+
+#define DEFINE_MOCK_MASTER_COMMON_INTERNAL(name,			       \
+					   ctrl_index,			       \
+					   MOCK_SOURCE,			       \
+					   param_types...)		       \
+		struct mock_expectation *mock_master_##name(		       \
+				MATCHER_PARAM_LIST_FROM_TYPES(ctrl_index,      \
+							      param_types))    \
+		{ \
+			struct mock_param_matcher *matchers[] = {	       \
+				ARG_NAMES_FROM_TYPES(ctrl_index, param_types)  \
+			};						       \
+									       \
+			return mock_add_matcher(MOCK_SOURCE(ctrl_index),       \
+						#name,			       \
+						(const void *) name,	       \
+						matchers,		       \
+						ARRAY_SIZE(matchers));	       \
+		}
+#define DEFINE_MOCK_MASTER_COMMON(name,					       \
+				  ctrl_index,				       \
+				  MOCK_SOURCE,				       \
+				  param_types...)			       \
+		DEFINE_MOCK_MASTER_COMMON_INTERNAL(name,		       \
+						   ctrl_index,		       \
+						   MOCK_SOURCE,		       \
+						   param_types)
+
+#define CLASS_MOCK_MASTER_SOURCE(ctrl_index) arg##ctrl_index
+#define DEFINE_MOCK_METHOD_MASTER(name, ctrl_index, param_types...)	       \
+		DEFINE_MOCK_MASTER_COMMON(name,				       \
+					  ctrl_index,			       \
+					  CLASS_MOCK_MASTER_SOURCE,	       \
+					  param_types)
+
+#define DEFINE_MOCK_COMMON(name,					       \
+			   handle_index,				       \
+			   mock_converter,				       \
+			   return_type,					       \
+			   param_types...)				       \
+		DEFINE_MOCK_METHOD_CLIENT(name,				       \
+					  handle_index,			       \
+					  mock_converter,		       \
+					  return_type,			       \
+					  param_types);			       \
+		DEFINE_MOCK_METHOD_MASTER(name, handle_index, param_types)
+
+#define DEFINE_MOCK_COMMON_VOID_RETURN(name,				       \
+				       handle_index,			       \
+				       mock_converter,			       \
+				       param_types...)			       \
+		DEFINE_MOCK_METHOD_CLIENT_VOID_RETURN(name,		       \
+						      handle_index,	       \
+						      mock_converter,	       \
+						      param_types);	       \
+		DEFINE_MOCK_METHOD_MASTER(name, handle_index, param_types)
+
+#define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name,		       \
+						       struct_name,	       \
+						       handle_index,	       \
+						       return_type,	       \
+						       param_types...)	       \
+		DEFINE_MOCK_COMMON(name,				       \
+				   handle_index,			       \
+				   from_##struct_name##_to_mock,	       \
+				   return_type, param_types)
+#define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name,			       \
+					      struct_name,		       \
+					      handle_index,		       \
+					      return_type,		       \
+					      param_types...)		       \
+		DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name,	       \
+							       struct_name,    \
+							       handle_index,   \
+							       return_type,    \
+							       param_types)
+
+#define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN_INTERNAL(	       \
+		name,							       \
+		struct_name,						       \
+		handle_index,						       \
+		param_types...)						       \
+		DEFINE_MOCK_COMMON_VOID_RETURN(name,			       \
+					       handle_index,		       \
+					       from_##struct_name##_to_mock,   \
+					       param_types)
+#define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN(name,		       \
+							  struct_name,	       \
+							  handle_index,	       \
+							  param_types...)      \
+		DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN_INTERNAL(    \
+				name,					       \
+				struct_name,				       \
+				handle_index,				       \
+				param_types)
+
+/**
+ * DEFINE_STRUCT_CLASS_MOCK()
+ * @name: name of the method
+ * @struct_name: name of the class of which the method belongs
+ * @return_type: return type of the method to be created. **Must not be void.**
+ * @param_types: parameters to method to be created.
+ *
+ * See KUNIT_EXPECT_CALL() for example usage.
+ */
+#define DEFINE_STRUCT_CLASS_MOCK(name,					       \
+				 struct_name,				       \
+				 return_type,				       \
+				 param_types...)			       \
+		DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name,		       \
+						      struct_name,	       \
+						      0,		       \
+						      return_type,	       \
+						      param_types)
+
+/**
+ * DEFINE_STRUCT_CLASS_MOCK_VOID_RETURN()
+ * @name: name of the method
+ * @struct_name: name of the class of which the method belongs
+ * @param_types: parameters to method to be created.
+ *
+ * Same as DEFINE_STRUCT_CLASS_MOCK() except the method has a ``void`` return
+ * type.
+ */
+#define DEFINE_STRUCT_CLASS_MOCK_VOID_RETURN(name, struct_name, param_types...)\
+		DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN(name,	       \
+								  struct_name, \
+								  0,	       \
+								  param_types)
+
+/**
+ * DEFINE_STRUCT_CLASS_MOCK_INIT()
+ * @struct_name: name of the class
+ * @init_func: a function of type ``int (*)(struct MOCK(struct_name) *)``. This
+ *             function is passed a pointer to an allocated, *but not
+ *             initialized*, ``struct MOCK(struct_name)``. The job of this user
+ *             provided function is to perform remaining initialization. Usually
+ *             this entails assigning mock methods to the function pointers in
+ *             the parent struct.
+ *
+ * See KUNIT_EXPECT_CALL() for example usage.
+ */
+#define DEFINE_STRUCT_CLASS_MOCK_INIT(struct_name, init_func)		       \
+		struct MOCK(struct_name) *MOCK_INIT_ID(struct_name)(	       \
+				struct kunit *test)			       \
+		{							       \
+			struct MOCK(struct_name) *mock_obj;		       \
+									       \
+			mock_obj = kunit_kzalloc(test,			       \
+						sizeof(*mock_obj),	       \
+						GFP_KERNEL);		       \
+			if (!mock_obj)					       \
+				return NULL;				       \
+									       \
+			mock_init_ctrl(test, mock_get_ctrl(mock_obj));	       \
+									       \
+			if (init_func(mock_obj))			       \
+				return NULL;				       \
+									       \
+			return mock_obj;				       \
+		}
+
 #define CONVERT_TO_ACTUAL_TYPE(type, ptr) (*((type *) ptr))
 
 /**
lib/kunit/Makefile +3 -2
index a7a3c5e0a8bf..649e1c1f0d00 100644
@@ -13,11 +13,12 @@ kunit-objs +=				debugfs.o
 endif
 
 obj-$(CONFIG_KUNIT_TEST) +=		kunit-test.o \
-					mock-macro-test.o
+					mock-macro-test.o \
+					mock-test.o
 
 # string-stream-test compiles built-in only.
 ifeq ($(CONFIG_KUNIT_TEST),y)
 obj-$(CONFIG_KUNIT_TEST) +=		string-stream-test.o
 endif
 
-obj-$(CONFIG_KUNIT_EXAMPLE_TEST) +=	kunit-example-test.o
+obj-$(CONFIG_KUNIT_EXAMPLE_TEST)	+= kunit-example-test.o
lib/kunit/kunit-example-test.c +90 -0

114 lines not on this page · raw message

lib/kunit/mock-macro-test.c +91 -0

114 lines not on this page · raw message

lib/kunit/mock-test.c new file +320 -0

320 lines not on this page · raw message

Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#15 lore raw

Re: [RFC v1 10/12] kunit: mock: add class mocking support

reproduce (this is a W=1 build):

Hi Daniel,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on 10b82d5176488acee2820e5a2cf0f2ec5c3488b6]

url:    https://github.com/0day-ci/linux/commits/Daniel-Latypov/kunit-introduce-class-mocking-support/20200919-023253
base:    10b82d5176488acee2820e5a2cf0f2ec5c3488b6
config: x86_64-allyesconfig (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # save the attached .config to linux build tree
        make W=1 ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

In file included from lib/kunit/kunit-example-test.c:10:

>> lib/kunit/kunit-example-test.c:60:33: warning: no previous prototype for 'foo' [-Wmissing-prototypes]
      60 | DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
         |                                 ^~~
   include/kunit/mock.h:328:15: note: in definition of macro 'DEFINE_MOCK_CLIENT_COMMON'
     328 |   return_type name(PARAM_LIST_FROM_TYPES(param_types))        \
         |               ^~~~
   include/kunit/mock.h:380:3: note: in expansion of macro 'DEFINE_MOCK_METHOD_CLIENT_COMMON'
     380 |   DEFINE_MOCK_METHOD_CLIENT_COMMON(name,          \
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/kunit/mock.h:437:3: note: in expansion of macro 'DEFINE_MOCK_METHOD_CLIENT'
     437 |   DEFINE_MOCK_METHOD_CLIENT(name,           \
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~
   include/kunit/mock.h:459:3: note: in expansion of macro 'DEFINE_MOCK_COMMON'
     459 |   DEFINE_MOCK_COMMON(name,           \
         |   ^~~~~~~~~~~~~~~~~~
   include/kunit/mock.h:468:3: note: in expansion of macro 'DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL'
     468 |   DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name,        \
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/kunit/mock.h:506:3: note: in expansion of macro 'DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX'
     506 |   DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name,         \
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/kunit/kunit-example-test.c:60:1: note: in expansion of macro 'DEFINE_STRUCT_CLASS_MOCK'
      60 | DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
         | ^~~~~~~~~~~~~~~~~~~~~~~~
   lib/kunit/kunit-example-test.c:60:26: note: in expansion of macro 'METHOD'
      60 | DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
         |                          ^~~~~~
>> include/kunit/mock.h:402:28: warning: no previous prototype for 'mock_master_foo' [-Wmissing-prototypes]
     402 |   struct mock_expectation *mock_master_##name(         \
         |                            ^~~~~~~~~~~~
   include/kunit/mock.h:420:3: note: in expansion of macro 'DEFINE_MOCK_MASTER_COMMON_INTERNAL'
     420 |   DEFINE_MOCK_MASTER_COMMON_INTERNAL(name,         \
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/kunit/mock.h:427:3: note: in expansion of macro 'DEFINE_MOCK_MASTER_COMMON'
     427 |   DEFINE_MOCK_MASTER_COMMON(name,           \
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~
   include/kunit/mock.h:442:3: note: in expansion of macro 'DEFINE_MOCK_METHOD_MASTER'
     442 |   DEFINE_MOCK_METHOD_MASTER(name, handle_index, param_types)
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~
   include/kunit/mock.h:459:3: note: in expansion of macro 'DEFINE_MOCK_COMMON'
     459 |   DEFINE_MOCK_COMMON(name,           \
         |   ^~~~~~~~~~~~~~~~~~
   include/kunit/mock.h:468:3: note: in expansion of macro 'DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL'
     468 |   DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name,        \
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/kunit/mock.h:506:3: note: in expansion of macro 'DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX'
     506 |   DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name,         \
         |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/kunit/kunit-example-test.c:60:1: note: in expansion of macro 'DEFINE_STRUCT_CLASS_MOCK'
      60 | DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
         | ^~~~~~~~~~~~~~~~~~~~~~~~
>> lib/kunit/kunit-example-test.c:85:31: warning: no previous prototype for 'examplemock_init' [-Wmissing-prototypes]
      85 | DEFINE_STRUCT_CLASS_MOCK_INIT(example, example_init);
         |                               ^~~~~~~
   include/kunit/mock.h:194:35: note: in definition of macro 'MOCK_INIT_ID'
     194 | #define MOCK_INIT_ID(struct_name) struct_name##mock_init
         |                                   ^~~~~~~~~~~
   lib/kunit/kunit-example-test.c:85:1: note: in expansion of macro 'DEFINE_STRUCT_CLASS_MOCK_INIT'
      85 | DEFINE_STRUCT_CLASS_MOCK_INIT(example, example_init);
         | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# https://github.com/0day-ci/linux/commit/80a5526fd886c8b3d5ad6ba475b3d1087943f70a
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Daniel-Latypov/kunit-introduce-class-mocking-support/20200919-023253 git checkout 80a5526fd886c8b3d5ad6ba475b3d1087943f70a
vim +/foo +60 lib/kunit/kunit-example-test.c

    55	
    56	/*
    57	 * This macro creates a mock implementation of the specified method of the
    58	 * specified class.
    59	 */
  > 60	DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
    61				 RETURNS(int),
    62				 PARAMS(struct example *, int));
    63	
    64	/*
    65	 * This tells KUnit how to initialize the parts of the mock that come from the
    66	 * parent. In this example, all we have to do is populate the member functions
    67	 * of the parent class with the mock versions we defined.
    68	 */
    69	static int example_init(struct MOCK(example) *mock_example)
    70	{
    71		/* This is how you get a pointer to the parent class of a mock. */
    72		struct example *example = mock_get_trgt(mock_example);
    73	
    74		/*
    75		 * Here we populate the member function (method) with our mock method.
    76		 */
    77		example->foo = foo;
    78		return 0;
    79	}
    80	
    81	/*
    82	 * This registers our parent init function above, allowing KUnit to create a
    83	 * constructor for the mock.
    84	 */
  > 85	DEFINE_STRUCT_CLASS_MOCK_INIT(example, example_init);
    86	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#16 lore raw

Re: [RFC v1 10/12] kunit: mock: add class mocking support

In reply to Daniel Latypov
reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux…

Hi Daniel,

[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on 10b82d5176488acee2820e5a2cf0f2ec5c3488b6]

url:    https://github.com/0day-ci/linux/commits/Daniel-Latypov/kunit-introduce-class-mocking-support/20200919-023253
base:    10b82d5176488acee2820e5a2cf0f2ec5c3488b6
config: arc-randconfig-r025-20200917 (attached as .config)
compiler: arceb-elf-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arc

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "mock_init_ctrl" [lib/kunit/kunit-example-test.ko] undefined!
>> ERROR: modpost: "kunit_int_return" [lib/kunit/kunit-example-test.ko] undefined!
>> ERROR: modpost: "kunit_int_eq" [lib/kunit/kunit-example-test.ko] undefined!
>> ERROR: modpost: "mock_add_matcher" [lib/kunit/kunit-example-test.ko] undefined!

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#17 lore raw

Re: [RFC v1 10/12] kunit: mock: add class mocking support

In reply to Daniel Latypov
reproduce:

Hi Daniel,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on 10b82d5176488acee2820e5a2cf0f2ec5c3488b6]

url:    https://github.com/0day-ci/linux/commits/Daniel-Latypov/kunit-introduce-class-mocking-support/20200919-023253
base:    10b82d5176488acee2820e5a2cf0f2ec5c3488b6
config: i386-randconfig-s002-20200917 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.2-201-g24bdaac6-dirty
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

>> lib/kunit/mock-macro-test.c:54:1: sparse: sparse: symbol 'mock_macro_dummy_structmock_init' was not declared. Should it be static?

--

Please review and possibly fold the followup patch.

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#18 lore raw

[RFC PATCH] kunit: mock: one_param can be static

In reply to Daniel Latypov
Signed-off-by: kernel test robot <[email protected]>
---
 mock-macro-test.c |   10 +++++-----
 mock-test.c       |    2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)
lib/kunit/mock-macro-test.c +5 -5
index d196dbee2407e7..6ed10383db1c6d 100644
@@ -21,20 +21,20 @@ struct mock_macro_dummy_struct {
 
 DECLARE_STRUCT_CLASS_MOCK_PREREQS(mock_macro_dummy_struct);
 
-DEFINE_STRUCT_CLASS_MOCK(METHOD(one_param), CLASS(mock_macro_dummy_struct),
+static DEFINE_STRUCT_CLASS_MOCK(METHOD(one_param), CLASS(mock_macro_dummy_struct),
 			 RETURNS(int),
 			 PARAMS(struct mock_macro_dummy_struct *));
 
-DEFINE_STRUCT_CLASS_MOCK(METHOD(two_param), CLASS(mock_macro_dummy_struct),
+static DEFINE_STRUCT_CLASS_MOCK(METHOD(two_param), CLASS(mock_macro_dummy_struct),
 			 RETURNS(int),
 			 PARAMS(struct mock_macro_dummy_struct *, int));
 
-DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(METHOD(non_first_slot_param),
+static DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(METHOD(non_first_slot_param),
 			 CLASS(mock_macro_dummy_struct), HANDLE_INDEX(1),
 			 RETURNS(int),
 			 PARAMS(int, struct mock_macro_dummy_struct *));
 
-DEFINE_STRUCT_CLASS_MOCK(METHOD(void_ptr_return),
+static DEFINE_STRUCT_CLASS_MOCK(METHOD(void_ptr_return),
 			 CLASS(mock_macro_dummy_struct),
 			 RETURNS(void *),
 			 PARAMS(struct mock_macro_dummy_struct *));
@@ -51,7 +51,7 @@ static int mock_macro_dummy_struct_init(
 	return 0;
 }
 
-DEFINE_STRUCT_CLASS_MOCK_INIT(mock_macro_dummy_struct,
+static DEFINE_STRUCT_CLASS_MOCK_INIT(mock_macro_dummy_struct,
 			      mock_macro_dummy_struct_init);
 
 struct mock_macro_context {
lib/kunit/mock-test.c +1 -1
index 8a0fa33d087cbd..c5970a6a91bc73 100644
@@ -25,7 +25,7 @@ static void adder_real_init(struct adder *adder)
 }
 
 DECLARE_STRUCT_CLASS_MOCK_PREREQS(adder);
-DEFINE_STRUCT_CLASS_MOCK(METHOD(mock_add), CLASS(adder), RETURNS(int),
+static DEFINE_STRUCT_CLASS_MOCK(METHOD(mock_add), CLASS(adder), RETURNS(int),
 				     PARAMS(struct adder*, int, int));
 DECLARE_STRUCT_CLASS_MOCK_INIT(adder);
 
#19 lore raw

RFC 11/12 kunit: mock: add struct param matcher

In reply to Daniel Latypov

From: Brendan Higgins <[email protected]>

Add parameter matcher builder for matching struct values.

---
 include/kunit/mock.h     |  58 +++++++++++++++++++
 lib/kunit/common-mocks.c | 117 +++++++++++++++++++++++++++++++++++++++
 lib/kunit/mock-test.c    |  40 +++++++++++++
 3 files changed, 215 insertions(+)
include/kunit/mock.h +58 -0
index 43f797c7e8bb..4a03f0627afa 100644
@@ -758,8 +758,18 @@ struct mock_param_matcher *kunit_ptr_ge(struct kunit *test, void *expected);
 struct mock_param_matcher *kunit_memeq(struct kunit *test,
 				      const void *buf,
 				      size_t size);
+
 struct mock_param_matcher *kunit_streq(struct kunit *test, const char *str);
 
+struct mock_param_matcher *kunit_str_contains(struct kunit *test,
+					      const char *needle);
+
+/* Matches var-arg arguments. */
+struct mock_param_matcher *kunit_va_format_cmp(
+		struct kunit *test,
+		struct mock_param_matcher *fmt_matcher,
+		struct mock_param_matcher *va_matcher);
+
 struct mock_action *kunit_u8_return(struct kunit *test, u8 ret);
 struct mock_action *kunit_u16_return(struct kunit *test, u16 ret);
 struct mock_action *kunit_u32_return(struct kunit *test, u32 ret);
@@ -778,4 +788,52 @@ struct mock_action *kunit_ulonglong_return(struct kunit *test,
 					  unsigned long long ret);
 struct mock_action *kunit_ptr_return(struct kunit *test, void *ret);
 
+/**
+ * struct mock_struct_matcher_entry - composed with other &struct
+ *                                    mock_struct_matcher_entry to make a
+ *                                    &struct struct_matcher
+ * @member_offset: offset of this member
+ * @matcher: matcher for this particular member
+ *
+ * This is used for struct_cmp() matchers.
+ */
+struct mock_struct_matcher_entry {
+	size_t member_offset;
+	struct mock_param_matcher *matcher;
+};
+
+static inline void init_mock_struct_matcher_entry_internal(
+		struct mock_struct_matcher_entry *entry,
+		size_t offset,
+		struct mock_param_matcher *matcher)
+{
+	entry->member_offset = offset;
+	entry->matcher = matcher;
+}
+
+/**
+ * INIT_MOCK_STRUCT_MATCHER_ENTRY()
+ * @entry: the &struct mock_struct_matcher_entry to initialize
+ * @type: the struct being matched
+ * @member: the member of the struct being matched, used to calculate the offset
+ * @matcher: matcher to match that member
+ *
+ * Initializes ``entry`` to match ``type->member`` with ``matcher``.
+ */
+#define INIT_MOCK_STRUCT_MATCHER_ENTRY(entry, type, member, matcher)	       \
+		init_mock_struct_matcher_entry_internal(entry,		       \
+							offsetof(type, member),\
+							matcher)
+
+static inline void INIT_MOCK_STRUCT_MATCHER_ENTRY_LAST(
+		struct mock_struct_matcher_entry *entry)
+{
+	entry->matcher = NULL;
+}
+
+struct mock_param_matcher *kunit_struct_cmp(
+		struct kunit *test,
+		const char *struct_name,
+		struct mock_struct_matcher_entry *entries);
+
 #endif /* _KUNIT_MOCK_H */
lib/kunit/common-mocks.c +117 -0

123 lines not on this page · raw message

lib/kunit/mock-test.c +40 -0
index 8a0fa33d087c..df0969b43ade 100644
@@ -243,6 +243,45 @@ static void mock_test_do_expect_default_return(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, 0, expectation->times_called);
 }
 
+/*
+ * Method called on naggy mock with no expectations will not fail, but will show
+ * a warning message
+ */
+static void mock_test_naggy_no_expectations_no_fail(struct kunit *test)
+{
+	struct mock_test_context *ctx = test->priv;
+	struct kunit *failing_test = ctx->failing_test;
+	struct mock *mock = ctx->mock;
+
+	int param0 = 5, param1 = -5;
+	static const char * const two_param_types[] = {"int", "int"};
+	const void *two_params[] = {&param0, &param1};
+
+	mock_set_default_action(mock,
+				"add",
+				real_add,
+				kunit_int_return(failing_test, -4));
+
+
+	KUNIT_EXPECT_CALL(mock_add(
+			mock,
+			kunit_any(failing_test),
+			kunit_va_format_cmp(failing_test,
+					    kunit_str_contains(failing_test,
+							       "Method was called with no expectations declared"),
+					    kunit_any(failing_test))));
+
+	mock->do_expect(mock,
+			"add",
+			real_add,
+			two_param_types,
+			two_params,
+			ARRAY_SIZE(two_params));
+	mock_validate_expectations(mock);
+
+	KUNIT_EXPECT_FALSE(test, failing_test->success);
+}
+
 static void mock_test_mock_validate_expectations(struct kunit *test)
 {
 	struct mock_test_context *ctx = test->priv;
@@ -307,6 +346,7 @@ static struct kunit_case mock_test_cases[] = {
 	KUNIT_CASE(mock_test_failed_expect_call_fails_test),
 	KUNIT_CASE(mock_test_do_expect_default_return),
 	KUNIT_CASE(mock_test_mock_validate_expectations),
+	KUNIT_CASE(mock_test_naggy_no_expectations_no_fail),
 	{}
 };
 
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#20 lore raw

RFC 12/12 kunit: mock: implement nice, strict and naggy mock distinctions

In reply to Daniel Latypov

From: Brendan Higgins <[email protected]>

Nice mocks only fail when there is an expectation on a method, but none match a given call. Strict mocks only pass when there is a matching expectation for every call. Naggy mocks have the same pass/fail behavior as nice, but report a warning in any case a strict mock would fail.

---
 include/kunit/mock.h  |  63 ++++++++++++++++
 lib/kunit/mock-test.c | 171 ++++++++++++++++++++++++++++++++++++++++++
 lib/kunit/mock.c      |  10 ++-
 3 files changed, 242 insertions(+), 2 deletions(-)
include/kunit/mock.h +63 -0
index 4a03f0627afa..955c4267d726 100644
@@ -95,10 +95,17 @@ struct mock_method {
 	struct list_head expectations;
 };
 
+enum mock_type {
+	MOCK_TYPE_NICE,
+	MOCK_TYPE_NAGGY,
+	MOCK_TYPE_STRICT
+};
+
 struct mock {
 	struct kunit_post_condition parent;
 	struct kunit *test;
 	struct list_head methods;
+	enum mock_type type;
 	/* TODO([email protected]): add locking to do_expect. */
 	const void *(*do_expect)(struct mock *mock,
 				 const char *method_name,
@@ -108,6 +115,8 @@ struct mock {
 				 int len);
 };
 
+#define DEFAULT_MOCK_TYPE MOCK_TYPE_NAGGY
+
 void mock_init_ctrl(struct kunit *test, struct mock *mock);
 
 void mock_validate_expectations(struct mock *mock);
@@ -125,6 +134,60 @@ struct mock_expectation *mock_add_matcher(struct mock *mock,
 
 #define MOCK(name) name##_mock
 
+/**
+ * STRICT_MOCK() - sets the mock to be strict and returns the mock
+ * @mock: the mock
+ *
+ * For an example, see ``The Nice, the Strict, and the Naggy`` under
+ * ``Using KUnit``.
+ */
+#define STRICT_MOCK(mock) \
+({ \
+	mock_get_ctrl(mock)->type = MOCK_TYPE_STRICT; \
+	mock; \
+})
+
+static inline bool is_strict_mock(struct mock *mock)
+{
+	return mock->type == MOCK_TYPE_STRICT;
+}
+
+/**
+ * NICE_MOCK() - sets the mock to be nice and returns the mock
+ * @mock: the mock
+ *
+ * For an example, see ``The Nice, the Strict, and the Naggy`` under
+ * ``Using KUnit``.
+ */
+#define NICE_MOCK(mock) \
+({ \
+	mock_get_ctrl(mock)->type = MOCK_TYPE_NICE; \
+	mock; \
+})
+
+static inline bool is_nice_mock(struct mock *mock)
+{
+	return mock->type == MOCK_TYPE_NICE;
+}
+
+/**
+ * NAGGY_MOCK() - sets the mock to be naggy and returns the mock
+ * @mock: the mock
+ *
+ * For an example, see ``The Nice, the Strict, and the Naggy`` under
+ * ``Using KUnit``.
+ */
+#define NAGGY_MOCK(mock) \
+({ \
+	mock_get_ctrl(mock)->type = MOCK_TYPE_NAGGY; \
+	mock; \
+})
+
+static inline bool is_naggy_mock(struct mock *mock)
+{
+	return mock->type == MOCK_TYPE_NAGGY;
+}
+
 /**
  * KUNIT_EXPECT_CALL() - Declares a *call expectation* on a mock function.
  * @expectation_call: a mocked method or function with parameters replaced with
lib/kunit/mock-test.c +171 -0

202 lines not on this page · raw message

lib/kunit/mock.c +8 -2
index 12fb88899451..f1fa7a5b9dd4 100644
@@ -85,6 +85,7 @@ void mock_init_ctrl(struct kunit *test, struct mock *mock)
 	mock->test = test;
 	INIT_LIST_HEAD(&mock->methods);
 	mock->do_expect = mock_do_expect;
+	mock->type = DEFAULT_MOCK_TYPE;
 	mock->parent.validate = mock_validate_wrapper;
 	list_add_tail(&mock->parent.node, &test->post_conditions);
 }
@@ -283,7 +284,12 @@ static struct mock_expectation *mock_apply_expectations(
 		mock_add_method_expectation_error(test, stream,
 			"Method was called with no expectations declared: ",
 			mock, method, type_names, params, len);
-		kunit_stream_commit(stream);
+		if (is_strict_mock(mock))
+			fail_and_flush(test, stream);
+		else if (is_naggy_mock(mock))
+			kunit_stream_commit(stream);
+		else
+			kunit_stream_clear(stream);
 		return NULL;
 	}
 
@@ -313,7 +319,7 @@ static struct mock_expectation *mock_apply_expectations(
 		}
 	}
 
-	if (expectations_all_saturated) {
+	if (expectations_all_saturated && !is_nice_mock(mock)) {
 		mock_add_method_expectation_error(test, stream,
 			"Method was called with fully saturated expectations: ",
 			mock, method, type_names, params, len);
Signature · 1 line
2.28.0.681.g6f77f65b4e-goog
#21 lore raw
In reply to Daniel Latypov
Quoted 12 lines · Daniel Latypov

# Background
KUnit currently lacks any first-class support for mocking. For an overview and discussion on the pros and cons, see
https://martinfowler.com/articles/mocksArentStubs.html

This patch set introduces the basic machinery needed for mocking:
setting and validating expectations, setting default actions, etc.

Using that basic infrastructure, we add macros for "class mocking", as it's probably the easiest type of mocking to start with.

Quoted 7 lines

## Class mocking

By "class mocking", we're referring mocking out function pointers stored in structs like:

  struct sender {
        int (*send)(struct sender *sender, int data);

};

Discussed this offline a bit with Brendan and David. The requirement that the first argument is a sender * means this doesn't work for a few common cases.

E.g. ops structs don't usually have funcs that take XXX_ops * pci_ops all take a struct pci_bus *, which at least contains a struct pci_ops*.

Why does this matter?
We need to stash a struct mock somewhere to store expectations. For this version of class mocking (in patch 10), we've assumed we could have

struct MOCK(sender) {
struct mock ctrl;
struct sender trgt; //&trgt assumed to be the first param
}

Then we can always use container_of(sender) to get the MOCK(sender) which holds ctrl.
But if the first parameter isn't a struct sender*, this trick obviously doesn't work.

So to support something like pci_ops, we'd need another approach to stash ctrl.

After thinking a bit more, we could perhaps decouple the first param from the mocked struct.
Using pci_ops as the example:

struct MOCK(pci_ops) {
struct mock ctrl;
struct pci_bus *self; // this is the first param. Using
python terminology here.
struct pci_ops trgt; // continue to store this, as this holds
the function pointers
}

// Name of this func is currently based on the class we're mocking static inline struct mock *from_pci_ops_to_mock(
const struct pci_bus *self)

{
          return mock_get_ctrl(container_of(self, struct MOCK(pci_ops), self));
}

// then in tests, we'd need something like
struct pci_bus bus;
bus.ops = mock_get_trgt(mock_ops);
mock_ops.self = &bus;

Do others have thoughts/opinions?

After grepping around for examples, I'm struck by how the number of outliers there are.
E.g. most take a struct thing * as the first param, but cfspi_ifc in caif_spi.h opts to take that as the last parameter.
And others take a different mix of structs for each function.

But it feels like a decoupled self / struct-holding-func-pointers approach should be generic enough, as far as I can tell. The more exotic types will probably have to remain unsupported.

Quoted 96 lines

After the necessary DEFINE_* macros, we can then write code like
struct MOCK(sender) mock_sender = CONSTRUCT_MOCK(sender, test);

  /* Fake an error for a specific input. */
  handle = KUNIT_EXPECT_CALL(send(<omitted>, kunit_int_eq(42)));
  handle->action = kunit_int_return(test, -EINVAL);

/* Pass the mocked object to some code under test. */ KUNIT_EXPECT_EQ(test, -EINVAL, send_message(...));

I.e. the goal is to make it easier to test
1) with less dependencies (we don't need to setup a real sender)
2) unusual/error conditions more easily.

In the future, we hope to build upon this to support mocking in more contexts, e.g. standalone funcs, etc.

# TODOs

## Naming
This introduces a number of new macros for dealing with mocks, e.g:
DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),

                           RETURNS(int),
                           PARAMS(struct example *, int));

...
KUNIT_EXPECT_CALL(foo(mock_get_ctrl(mock_example), ...);
For consistency, we could prefix everything with KUNIT, e.g. KUNIT_DEFINE_STRUCT_CLASS_MOCK and kunit_mock_get_ctrl, but it feels like the names might be long enough that they would hinder readability.

## Usage
For now the only use of class mocking is in kunit-example-test.c As part of changing this from an RFC to a real patch set, we're hoping to include at least one example.

Pointers to bits of code where this would be useful that aren't too hairy would be appreciated.
E.g. could easily add a test for tools/perf/ui/progress.h, e.g. that ui_progress__init() calls ui_progress_ops.init(), but that likely isn't useful to anyone.

Brendan Higgins (9):
kunit: test: add kunit_stream a std::stream like logger
kunit: test: add concept of post conditions
checkpatch: add support for struct MOCK(foo) syntax
kunit: mock: add parameter list manipulation macros
kunit: mock: add internal mock infrastructure
kunit: mock: add basic matchers and actions
kunit: mock: add class mocking support
kunit: mock: add struct param matcher
kunit: mock: implement nice, strict and naggy mock distinctions

Daniel Latypov (2):
Revert "kunit: move string-stream.h to lib/kunit"
kunit: expose kunit_set_failure() for use by mocking

Marcelo Schmitt (1):
kunit: mock: add macro machinery to pick correct format args

 include/kunit/assert.h                 |   3 +-
 include/kunit/kunit-stream.h           |  94 +++
 include/kunit/mock.h                   | 902 +++++++++++++++++++++++++
 include/kunit/params.h                 | 305 +++++++++
 {lib => include}/kunit/string-stream.h |   2 +
 include/kunit/test.h                   |   9 +
 lib/kunit/Makefile                     |   9 +-
 lib/kunit/assert.c                     |   2 -
 lib/kunit/common-mocks.c               | 409 +++++++++++
 lib/kunit/kunit-example-test.c         |  90 +++
 lib/kunit/kunit-stream.c               | 110 +++
 lib/kunit/mock-macro-test.c            | 241 +++++++
 lib/kunit/mock-test.c                  | 531 +++++++++++++++
 lib/kunit/mock.c                       | 370 ++++++++++
 lib/kunit/string-stream-test.c         |   3 +-
 lib/kunit/string-stream.c              |   5 +-
 lib/kunit/test.c                       |  15 +-
 scripts/checkpatch.pl                  |   4 +
 18 files changed, 3091 insertions(+), 13 deletions(-)
 create mode 100644 include/kunit/kunit-stream.h
 create mode 100644 include/kunit/mock.h
 create mode 100644 include/kunit/params.h
 rename {lib => include}/kunit/string-stream.h (95%)
 create mode 100644 lib/kunit/common-mocks.c
 create mode 100644 lib/kunit/kunit-stream.c
 create mode 100644 lib/kunit/mock-macro-test.c
 create mode 100644 lib/kunit/mock-test.c
 create mode 100644 lib/kunit/mock.c

base-commit: 10b82d5176488acee2820e5a2cf0f2ec5c3488b6 --
2.28.0.681.g6f77f65b4e-goog

#22 lore raw
Quoted 66 lines · Daniel Latypov
Quoted 12 lines · Daniel Latypov

# Background
KUnit currently lacks any first-class support for mocking. For an overview and discussion on the pros and cons, see
https://martinfowler.com/articles/mocksArentStubs.html

This patch set introduces the basic machinery needed for mocking:
setting and validating expectations, setting default actions, etc.

Using that basic infrastructure, we add macros for "class mocking", as it's probably the easiest type of mocking to start with.

Quoted 7 lines

## Class mocking

By "class mocking", we're referring mocking out function pointers stored in structs like:

  struct sender {
        int (*send)(struct sender *sender, int data);

};

Discussed this offline a bit with Brendan and David. The requirement that the first argument is a sender * means this doesn't work for a few common cases.

E.g. ops structs don't usually have funcs that take XXX_ops * pci_ops all take a struct pci_bus *, which at least contains a struct pci_ops*.

Why does this matter?
We need to stash a struct mock somewhere to store expectations. For this version of class mocking (in patch 10), we've assumed we could have

struct MOCK(sender) {
struct mock ctrl;
struct sender trgt; //&trgt assumed to be the first param
}

Then we can always use container_of(sender) to get the MOCK(sender) which holds ctrl.
But if the first parameter isn't a struct sender*, this trick obviously doesn't work.

So to support something like pci_ops, we'd need another approach to stash ctrl.

After thinking a bit more, we could perhaps decouple the first param from the mocked struct.
Using pci_ops as the example:

struct MOCK(pci_ops) {
struct mock ctrl;
struct pci_bus *self; // this is the first param. Using
python terminology here.
struct pci_ops trgt; // continue to store this, as this holds
the function pointers
}

// Name of this func is currently based on the class we're mocking static inline struct mock *from_pci_ops_to_mock(
const struct pci_bus *self)

{
          return mock_get_ctrl(container_of(self, struct MOCK(pci_ops), self));
}
Quoted 6 lines

// then in tests, we'd need something like
struct pci_bus bus;
bus.ops = mock_get_trgt(mock_ops);
mock_ops.self = &bus;

Do others have thoughts/opinions?

In the above example, wouldn't we actually want to mock struct pci_bus, not struct pci_ops?

I think pci_bus is what actually gets implemented. Consider the Rockchip PCIe host controller:

Rockchip provides it's own pci_ops struct:

https://elixir.bootlin.com/linux/latest/source/drivers/pci/controller/pcie-rockchip-host.c#L256

If you look at one of the implemented methods, say rockchip_pcie_rd_conf(), you will see:

...
struct rockchip_pcie *rockchip = bus->sysdata;
...
(This function signature is:

int (*read)(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *val);

).

Thus, conceptually struct pci_bus is what is being manipulated; it best fits the candidate for the *self pointer.

In fact - if I am not mistaken - I think if you were to mock pci_bus and just pretend that the methods were on pci_bus, not pci_ops, I think it might just work.

The bigger problem is that it seems pci_bus does not want the user to allocate it: in the case of rockchip_pcie, it is allocated by devm_pci_alloc_host_bridge(). Thus, embedding pci_bus inside of a MOCK(pci_bus) would probably not work, so you would need to have different tooling around that and would then need to provide a custom definition of from_pci_bus_to_mock() (generated by DECLARE_STRUCT_CLASS_MOCK_CONVERTER()).

Quoted 4 lines

After grepping around for examples, I'm struck by how the number of outliers there are.
E.g. most take a struct thing * as the first param, but cfspi_ifc in caif_spi.h opts to take that as the last parameter.

That's not a problem, just use the DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX() variant to create your mock (as opposed to DECLARE_STRUCT_CLASS_MOCK()).

For example let's say you have the following struct that you want to mock:

struct example {
    ...
    int (*foo)(int bar, char baz, struct example *self);
};

You could mock the function with:

DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(

    METHOD(foo), CLASS(example),
    2, /* The "handle" is in position 2. */
    RETURNS(int),
    PARAMS(int, char, struct example *));
Quoted 5 lines

And others take a different mix of structs for each function.

But it feels like a decoupled self / struct-holding-func-pointers approach should be generic enough, as far as I can tell. The more exotic types will probably have to remain unsupported.

I think the code is pretty much here to handle any situation in which one of the parameters input to the function can be used to look up a mock object; we just need to expose the from_<class>_to_mock() function to the user to override. The problem I see with what we have now is the assumption that the user gets to create the object that they want to mock. Your example has illustrated that that is not a safe assumption.

But yes, sufficiently exoctic cases will have to be unsupported.

BTW, sorry for not sharing the above in our offline discussion; since we were talking without concrete examples in front of us, the problem sounded worse than it looks here.

[...]

#23 lore raw
Quoted 96 lines · Brendan Higgins
Quoted 66 lines · Daniel Latypov
Quoted 12 lines · Daniel Latypov

# Background
KUnit currently lacks any first-class support for mocking. For an overview and discussion on the pros and cons, see
https://martinfowler.com/articles/mocksArentStubs.html

This patch set introduces the basic machinery needed for mocking:
setting and validating expectations, setting default actions, etc.

Using that basic infrastructure, we add macros for "class mocking", as it's probably the easiest type of mocking to start with.

Quoted 7 lines

## Class mocking

By "class mocking", we're referring mocking out function pointers stored in structs like:

  struct sender {
        int (*send)(struct sender *sender, int data);

};

Discussed this offline a bit with Brendan and David. The requirement that the first argument is a sender * means this doesn't work for a few common cases.

E.g. ops structs don't usually have funcs that take XXX_ops * pci_ops all take a struct pci_bus *, which at least contains a struct pci_ops*.

Why does this matter?
We need to stash a struct mock somewhere to store expectations. For this version of class mocking (in patch 10), we've assumed we could have

struct MOCK(sender) {
struct mock ctrl;
struct sender trgt; //&trgt assumed to be the first param
}

Then we can always use container_of(sender) to get the MOCK(sender) which holds ctrl.
But if the first parameter isn't a struct sender*, this trick obviously doesn't work.

So to support something like pci_ops, we'd need another approach to stash ctrl.

After thinking a bit more, we could perhaps decouple the first param from the mocked struct.
Using pci_ops as the example:

struct MOCK(pci_ops) {
struct mock ctrl;
struct pci_bus *self; // this is the first param. Using
python terminology here.
struct pci_ops trgt; // continue to store this, as this holds
the function pointers
}

// Name of this func is currently based on the class we're mocking static inline struct mock *from_pci_ops_to_mock(
const struct pci_bus *self)

{
          return mock_get_ctrl(container_of(self, struct MOCK(pci_ops), self));
}
Quoted 6 lines

// then in tests, we'd need something like
struct pci_bus bus;
bus.ops = mock_get_trgt(mock_ops);
mock_ops.self = &bus;

Do others have thoughts/opinions?

In the above example, wouldn't we actually want to mock struct pci_bus, not struct pci_ops?

I think pci_bus is what actually gets implemented. Consider the Rockchip PCIe host controller:

Rockchip provides it's own pci_ops struct:

https://elixir.bootlin.com/linux/latest/source/drivers/pci/controller/pcie-rockchip-host.c#L256

If you look at one of the implemented methods, say rockchip_pcie_rd_conf(), you will see:

...
struct rockchip_pcie *rockchip = bus->sysdata;
...
(This function signature is:

int (*read)(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *val);

Quoted 8 lines

).

Thus, conceptually struct pci_bus is what is being manipulated; it best fits the candidate for the *self pointer.

In fact - if I am not mistaken - I think if you were to mock pci_bus and just pretend that the methods were on pci_bus, not pci_ops, I think it might just work.

It works with this code as-is, yes.

But messing around with it, it seems like it might be helpful for the mock init funcs access to struct kunit *test in case they want to allocate.

E.g. in cases where the ops struct points to a shared instance. We'd want to allocate a new ops struct so we don't accidentally affect more objects than expected by altering global state.

It's a simple enough change, i.e.

include/kunit/mock.h +1 -1
index 955c4267d726..9006f0e492dc 100644
@@ -613,7 +613,7 @@ static inline bool is_naggy_mock(struct mock *mock)
                                                                               \
                        mock_init_ctrl(test, mock_get_ctrl(mock_obj));         \
                                                                               \
-                       if (init_func(mock_obj))                               \
+                       if (init_func(test, mock_obj))                         \
                                return NULL;                                   \
                                                                               \
                        return mock_obj;                                       \
lib/kunit/kunit-example-test.c +1 -1
index cd538cdb2a96..38c87f163d2f 100644
@@ -66,7 +66,7 @@ DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
  * parent. In this example, all we have to do is populate the member functions
  * of the parent class with the mock versions we defined.
  */
-static int example_init(struct MOCK(example) *mock_example)
+static int example_init(struct kunit *test, struct MOCK(example) *mock_example)
 {
        /* This is how you get a pointer to the parent class of a mock. */
        struct example *example = mock_get_trgt(mock_example);
Quoted 8 lines

The bigger problem is that it seems pci_bus does not want the user to allocate it: in the case of rockchip_pcie, it is allocated by devm_pci_alloc_host_bridge(). Thus, embedding pci_bus inside of a MOCK(pci_bus) would probably not work, so you would need to have different tooling around that and would then need to provide a custom definition of from_pci_bus_to_mock() (generated by DECLARE_STRUCT_CLASS_MOCK_CONVERTER()).

Yeah, I'm still not sure about this.

One approach would be to only support objects we can allocate and thus embed inside a wrapper MOCK(class) struct.
Users would have to write a custom wrapper struct and from_<class>_to_mock() funcs for everything else.

E.g. they'd write

struct MOCK(pci_bus) {
struct mock ctrl;
struct pci_bus *bus; // normally, the macro would generate without *
}

static inline struct mock *from_pci_ops_to_mock(

    const struct pci_bus *self) {

// let user provide magic logic to do this lookup
struct MOCK(pci_bus) *mock = somehow_get_wrapper(pci_bus); return mock_get_ctrl(mock);
}

With the proposed change above to pass a struct kunit * to the init, we open the possibility of using a kunit_resource to store this mapping.
Perhaps the conversion func could also be changed to take a struct kunit * as well?

static int mock_pci_bus_init(struct kunit *test, struct MOCK(pci_bus) *mocked) {
// somehow establish mapping
kunit_add_named_resource(test, ....);
}

Given resources are looked up via a linear scan, perhaps we do something like create a single instance for each mocked type. E.g. we have a map_pci_bus_to_mock hashtable that either gets allocated or updated with each call?

Quoted 9 lines
Quoted 5 lines

After grepping around for examples, I'm struck by how the number of outliers there are.
E.g. most take a struct thing * as the first param, but cfspi_ifc in caif_spi.h opts to take that as the last parameter.

That's not a problem, just use the DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX() variant to create your mock (as opposed to DECLARE_STRUCT_CLASS_MOCK()).

Here's the function pointers
void (*ss_cb) (bool assert, struct cfspi_ifc *ifc); void (*xfer_done_cb) (struct cfspi_ifc *ifc);

So sadly that won't work (unless you only wanted to mock one of the funcs). But as we agree that we don't want to try and support everything, this is fine.

Quoted 22 lines

For example let's say you have the following struct that you want to mock:

struct example {
    ...
    int (*foo)(int bar, char baz, struct example *self);
};

You could mock the function with:

DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(

    METHOD(foo), CLASS(example),
    2, /* The "handle" is in position 2. */
    RETURNS(int),
    PARAMS(int, char, struct example *));
Quoted 5 lines

And others take a different mix of structs for each function.

But it feels like a decoupled self / struct-holding-func-pointers approach should be generic enough, as far as I can tell. The more exotic types will probably have to remain unsupported.

Quoted 7 lines

I think the code is pretty much here to handle any situation in which one of the parameters input to the function can be used to look up a mock object; we just need to expose the from_<class>_to_mock() function to the user to override. The problem I see with what we have now is the assumption that the user gets to create the object that they want to mock. Your example has illustrated that that is not a safe assumption.

Agree.
The code itself is more or less able to handle most of these use cases. Concern was more about how frequent + painful minor deviations from the current pattern would be.
As noted above, I think allowing the mock init funcs to safely allocate a new ops struct if they want might be enough.

For more exotic cases, users can write custom from_<class>_to_mock() funcs to handle a lot of cases.
As long as the first argument to the mocked functions matches the first argument of _to_mock(), then it should all work out.

Quoted 8 lines

But yes, sufficiently exoctic cases will have to be unsupported.

BTW, sorry for not sharing the above in our offline discussion; since we were talking without concrete examples in front of us, the problem sounded worse than it looks here.

[...]