mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/5] kunit: decrease layers of assertion macros
@ 2022-01-18 22:35 Daniel Latypov
  2022-01-18 22:35 ` [PATCH 1/5] kunit: make KUNIT_EXPECT_EQ() use KUNIT_EXPECT_EQ_MSG(), etc Daniel Latypov
                   ` (4 more replies)
  0 siblings, 5 replies; 17+ messages in thread
From: Daniel Latypov @ 2022-01-18 22:35 UTC (permalink / raw)
  To: brendanhiggins, davidgow
  Cc: linux-kernel, kunit-dev, linux-kselftest, skhan, Daniel Latypov

Note: this series applies on top of the series reducing stack usage,
https://lore.kernel.org/linux-kselftest/20220113165931.451305-1-dlatypov@google.com/
There's no real smenatic dependency between these, just potential for
merge conflicts.

The current layout of the assertion macros is confusing.

Here's the call chain for KUNIT_EXPECT_EQ() and KUNIT_EXPECT_EQ_MSG()
  KUNIT_EXPECT_EQ =>
  KUNIT_BINARY_EQ_ASSERTION => # note: not shared with the _MSG variant
  KUNIT_BINARY_EQ_MSG_ASSERTION =>
  KUNIT_BASE_EQ_MSG_ASSERTION =>
  KUNIT_BASE_BINARY_ASSERTION

  KUNIT_EXPECT_EQ_MSG =>
  KUNIT_BINARY_EQ_MSG_ASSERTION =>
  KUNIT_BASE_EQ_MSG_ASSERTION =>
  KUNIT_BASE_BINARY_ASSERTION

After this series
  KUNIT_EXPECT_EQ =>
  KUNIT_EXPECT_EQ_MSG =>
  KUNIT_BINARY_INT_ASSERTION =>
  KUNIT_BASE_BINARY_ASSERTION

The current macro layout tries hard to reduce duplication, but comes at
the cost of a lot of intermediates that can simply vanish.

The same call-chain again, but annotated with the info we add:
  KUNIT_EXPECT_EQ => specify we're an EXPECT, not an ASSERT
  KUNIT_BINARY_EQ_ASSERTION => specify we have a NULL msg
  KUNIT_BINARY_EQ_MSG_ASSERTION => specify we work with ints, not ptrs
  KUNIT_BASE_EQ_MSG_ASSERTION => specify that the op is '=='
  KUNIT_BASE_BINARY_ASSERTION

We can see that each level of the chain only specifes one parameter at
a time. We've taken the concept of DRY too far.

The following is a full snippet of all the macros needed for
KUNIT_EXPECT_EQ, showing that a bit of repetition is just fine:
  #define KUNIT_BINARY_INT_ASSERTION(test,				       \
				     assert_type,			       \
				     left,				       \
				     op,				       \
				     right,				       \
				     fmt,				       \
				      ...)				       \
	  KUNIT_BASE_BINARY_ASSERTION(test,				       \
				      kunit_binary_assert,		       \
				      KUNIT_INIT_BINARY_ASSERT_STRUCT,	       \
				      assert_type,			       \
				      left, op, right,			       \
				      fmt,				       \
				      ##__VA_ARGS__)

  #define KUNIT_EXPECT_EQ(test, left, right) \
	  KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)

  #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)		       \
	  KUNIT_BINARY_INT_ASSERTION(test,				       \
				     KUNIT_EXPECTATION,			       \
				     left, ==, right,			       \
				     fmt,				       \
				      ##__VA_ARGS__)

as opposed to our current DRYer version

  #define KUNIT_BASE_EQ_MSG_ASSERTION(test,                                      \
				      assert_class,                              \
				      ASSERT_CLASS_INIT,                         \
				      assert_type,                               \
				      left,                                      \
				      right,                                     \
				      fmt,                                       \
				      ...)                                       \
	  KUNIT_BASE_BINARY_ASSERTION(test,                                      \
				      assert_class,                              \
				      ASSERT_CLASS_INIT,                         \
				      assert_type,                               \
				      left, ==, right,                           \
				      fmt,                                       \
				      ##__VA_ARGS__)

  #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
	  KUNIT_BASE_EQ_MSG_ASSERTION(test,                                      \
				      kunit_binary_assert,                       \
				      KUNIT_INIT_BINARY_ASSERT_STRUCT,           \
				      assert_type,                               \
				      left,                                      \
				      right,                                     \
				      fmt,                                       \
				      ##__VA_ARGS__)

  #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right)              \
	  KUNIT_BINARY_EQ_MSG_ASSERTION(test,                                    \
					assert_type,                             \
					left,                                    \
					right,                                   \
					NULL)
  #define KUNIT_EXPECT_EQ(test, left, right) \
	  KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)

  #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)                       \
	  KUNIT_BINARY_EQ_MSG_ASSERTION(test,                                    \
					KUNIT_EXPECTATION,                       \
					left,                                    \
					right,                                   \
					fmt,                                     \
					##__VA_ARGS__)



Daniel Latypov (5):
  kunit: make KUNIT_EXPECT_EQ() use KUNIT_EXPECT_EQ_MSG(), etc.
  kunit: drop unused intermediate macros for ptr inequality checks
  kunit: reduce layering in string assertion macros
  kunit: decrease macro layering for integer asserts
  kunit: decrease macro layering for EQ/NE asserts

 include/kunit/test.h | 660 ++++++++++---------------------------------
 1 file changed, 142 insertions(+), 518 deletions(-)

-- 
2.34.1.703.g22d0c6ccf7-goog


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2022-01-21 22:26 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-18 22:35 [PATCH 0/5] kunit: decrease layers of assertion macros Daniel Latypov
2022-01-18 22:35 ` [PATCH 1/5] kunit: make KUNIT_EXPECT_EQ() use KUNIT_EXPECT_EQ_MSG(), etc Daniel Latypov
2022-01-21  7:21   ` David Gow
2022-01-21 22:10   ` Brendan Higgins
2022-01-18 22:35 ` [PATCH 2/5] kunit: drop unused intermediate macros for ptr inequality checks Daniel Latypov
2022-01-21  7:21   ` David Gow
2022-01-21 22:12   ` Brendan Higgins
2022-01-18 22:35 ` [PATCH 3/5] kunit: reduce layering in string assertion macros Daniel Latypov
2022-01-21  7:21   ` David Gow
2022-01-21 22:13   ` Brendan Higgins
2022-01-18 22:35 ` [PATCH 4/5] kunit: decrease macro layering for integer asserts Daniel Latypov
2022-01-21  7:21   ` David Gow
2022-01-21 22:17   ` Brendan Higgins
2022-01-18 22:35 ` [PATCH 5/5] kunit: decrease macro layering for EQ/NE asserts Daniel Latypov
2022-01-21  7:21   ` David Gow
2022-01-21 22:25     ` Brendan Higgins
2022-01-21 22:25   ` Brendan Higgins

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®