Linux-kselftest Archive on lore.kernel.org
 help / color / Atom feed
From: Brendan Higgins <brendanhiggins@google.com>
To: frowand.list@gmail.com, gregkh@linuxfoundation.org,
	jpoimboe@redhat.com, keescook@google.com,
	kieran.bingham@ideasonboard.com, mcgrof@kernel.org,
	peterz@infradead.org, robh@kernel.org, sboyd@kernel.org,
	shuah@kernel.org, tytso@mit.edu, yamada.masahiro@socionext.com
Cc: devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org,
	kunit-dev@googlegroups.com, linux-doc@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-kbuild@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-nvdimm@lists.01.org, linux-um@lists.infradead.org,
	Alexander.Levin@microsoft.com, Tim.Bird@sony.com,
	amir73il@gmail.com, dan.carpenter@oracle.com, daniel@ffwll.ch,
	jdike@addtoit.com, joel@jms.id.au, julia.lawall@lip6.fr,
	khilman@baylibre.com, knut.omang@oracle.com, logang@deltatee.com,
	mpe@ellerman.id.au, pmladek@suse.com, rdunlap@infradead.org,
	richard@nod.at, rientjes@google.com, rostedt@goodmis.org,
	wfg@linux.intel.com, Brendan Higgins <brendanhiggins@google.com>
Subject: [PATCH v12 05/18] kunit: test: add the concept of expectations
Date: Mon, 12 Aug 2019 11:24:08 -0700
Message-ID: <20190812182421.141150-6-brendanhiggins@google.com> (raw)
In-Reply-To: <20190812182421.141150-1-brendanhiggins@google.com>

Add support for expectations, which allow properties to be specified and
then verified in tests.

Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Logan Gunthorpe <logang@deltatee.com>
---
 include/kunit/test.h | 843 +++++++++++++++++++++++++++++++++++++++++++
 kunit/test.c         |  58 +++
 2 files changed, 901 insertions(+)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index d0bf112910caf..2625bcfeb19ac 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -9,8 +9,10 @@
 #ifndef _KUNIT_TEST_H
 #define _KUNIT_TEST_H
 
+#include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/slab.h>
+#include <kunit/assert.h>
 
 struct kunit_resource;
 
@@ -319,4 +321,845 @@ void __printf(3, 4) kunit_printk(const char *level,
 #define kunit_err(test, fmt, ...) \
 		kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
 
+/*
+ * Generates a compile-time warning in case of comparing incompatible types.
+ */
+#define __kunit_typecheck(lhs, rhs) \
+	((void) __typecheck(lhs, rhs))
+
+/**
+ * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
+ * @test: The test context object.
+ *
+ * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
+ * words, it does nothing and only exists for code clarity. See
+ * KUNIT_EXPECT_TRUE() for more information.
+ */
+#define KUNIT_SUCCEED(test) do {} while (0)
+
+void kunit_do_assertion(struct kunit *test,
+			struct kunit_assert *assert,
+			bool pass,
+			const char *fmt, ...);
+
+#define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do {  \
+	struct assert_class __assertion = INITIALIZER;			       \
+	kunit_do_assertion(test,					       \
+			   &__assertion.assert,				       \
+			   pass,					       \
+			   fmt,						       \
+			   ##__VA_ARGS__);				       \
+} while (0)
+
+
+#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...)		       \
+		KUNIT_ASSERTION(test,					       \
+				false,					       \
+				kunit_fail_assert,			       \
+				KUNIT_INIT_FAIL_ASSERT_STRUCT(test,	       \
+							      assert_type),    \
+				fmt,					       \
+				##__VA_ARGS__)
+
+/**
+ * KUNIT_FAIL() - Always causes a test to fail when evaluated.
+ * @test: The test context object.
+ * @fmt: an informational message to be printed when the assertion is made.
+ * @...: string format arguments.
+ *
+ * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
+ * other words, it always results in a failed expectation, and consequently
+ * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
+ * for more information.
+ */
+#define KUNIT_FAIL(test, fmt, ...)					       \
+		KUNIT_FAIL_ASSERTION(test,				       \
+				     KUNIT_EXPECTATION,			       \
+				     fmt,				       \
+				     ##__VA_ARGS__)
+
+#define KUNIT_UNARY_ASSERTION(test,					       \
+			      assert_type,				       \
+			      condition,				       \
+			      expected_true,				       \
+			      fmt,					       \
+			      ...)					       \
+		KUNIT_ASSERTION(test,					       \
+				!!(condition) == !!expected_true,	       \
+				kunit_unary_assert,			       \
+				KUNIT_INIT_UNARY_ASSERT_STRUCT(test,	       \
+							       assert_type,    \
+							       #condition,     \
+							       expected_true), \
+				fmt,					       \
+				##__VA_ARGS__)
+
+#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
+		KUNIT_UNARY_ASSERTION(test,				       \
+				      assert_type,			       \
+				      condition,			       \
+				      true,				       \
+				      fmt,				       \
+				      ##__VA_ARGS__)
+
+#define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
+		KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
+
+#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)      \
+		KUNIT_UNARY_ASSERTION(test,				       \
+				      assert_type,			       \
+				      condition,			       \
+				      false,				       \
+				      fmt,				       \
+				      ##__VA_ARGS__)
+
+#define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
+		KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
+
+#define KUNIT_BINARY_CLASS \
+		kunit_binary_assert, KUNIT_INIT_BINARY_ASSERT_STRUCT
+#define KUNIT_BINARY_PTR_CLASS \
+		kunit_binary_ptr_assert, KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT
+
+/*
+ * A factory macro for defining the assertions andexpectations for the basic
+ * comparisons defined for the built in types.
+ *
+ * Unfortunately, there is no common type that all types can be promoted to for
+ * which all the binary operators behave the same way as for the actual types
+ * (for example, there is no type that long long and unsigned long long can
+ * both be cast to where the comparison result is preserved for all values). So
+ * the best we can do is do the comparison in the original types and then coerce
+ * everything to long long for printing; this way, the comparison behaves
+ * correctly and the printed out value usually makes sense without
+ * interpretation, but can always be interpretted to figure out the actual
+ * value.
+ */
+#define KUNIT_BASE_BINARY_ASSERTION(test,				       \
+				    assert_class,			       \
+				    ASSERT_CLASS_INIT,			       \
+				    assert_type,			       \
+				    left,				       \
+				    op,					       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+do {									       \
+	typeof(left) __left = (left);					       \
+	typeof(right) __right = (right);				       \
+	__kunit_typecheck(__left, __right);				       \
+									       \
+	KUNIT_ASSERTION(test,						       \
+			__left op __right,				       \
+			assert_class,					       \
+			ASSERT_CLASS_INIT(test,				       \
+					  assert_type,			       \
+					  #op,				       \
+					  #left,			       \
+					  __left,			       \
+					  #right,			       \
+					  __right),			       \
+			fmt,						       \
+			##__VA_ARGS__);					       \
+} while (0)
+
+#define KUNIT_BINARY_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_BASE_EQ_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, ==, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_NE_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, !=, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_LT_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, <, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_LE_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, <=, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_GT_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    assert_type,		       \
+					    left, >, right,		       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BASE_GE_MSG_ASSERTION(test,				       \
+				    ASSERT_CLASS,			       \
+				    assert_type,			       \
+				    left,				       \
+				    right,				       \
+				    fmt,				       \
+				    ...)				       \
+		KUNIT_BASE_BINARY_ASSERTION(test,			       \
+					    ASSERT_CLASS,		       \
+					    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_CLASS,		       \
+					    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_BINARY_PTR_EQ_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_EQ_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_NE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_NE_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_NE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_LT_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_LT_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_LT_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_LE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_LE_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_LE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_GT_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_GT_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_GT_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
+		KUNIT_BASE_GE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_CLASS,		       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_GE_MSG_ASSERTION(test,			       \
+					      assert_type,		       \
+					      left,			       \
+					      right,			       \
+					      NULL)
+
+#define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BASE_GE_MSG_ASSERTION(test,			       \
+					    KUNIT_BINARY_PTR_CLASS,	       \
+					    assert_type,		       \
+					    left,			       \
+					    right,			       \
+					    fmt,			       \
+					    ##__VA_ARGS__)
+
+#define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_STR_ASSERTION(test,				       \
+				   assert_type,				       \
+				   left,				       \
+				   op,					       \
+				   right,				       \
+				   fmt,					       \
+				   ...)					       \
+do {									       \
+	typeof(left) __left = (left);					       \
+	typeof(right) __right = (right);				       \
+									       \
+	KUNIT_ASSERTION(test,						       \
+			strcmp(__left, __right) op 0,			       \
+			kunit_binary_str_assert,			       \
+			KUNIT_INIT_BINARY_ASSERT_STRUCT(test,		       \
+							assert_type,	       \
+							#op,		       \
+							#left,		       \
+							__left,		       \
+							#right,		       \
+							__right),	       \
+			fmt,						       \
+			##__VA_ARGS__);					       \
+} while (0)
+
+#define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BINARY_STR_ASSERTION(test,			       \
+					   assert_type,			       \
+					   left, ==, right,		       \
+					   fmt,				       \
+					   ##__VA_ARGS__)
+
+#define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,				       \
+					  assert_type,			       \
+					  left,				       \
+					  right,			       \
+					  fmt,				       \
+					  ...)				       \
+		KUNIT_BINARY_STR_ASSERTION(test,			       \
+					   assert_type,			       \
+					   left, !=, right,		       \
+					   fmt,				       \
+					   ##__VA_ARGS__)
+
+#define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right)	       \
+		KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,			       \
+						  assert_type,		       \
+						  left,			       \
+						  right,		       \
+						  NULL)
+
+#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
+						assert_type,		       \
+						ptr,			       \
+						fmt,			       \
+						...)			       \
+do {									       \
+	typeof(ptr) __ptr = (ptr);					       \
+									       \
+	KUNIT_ASSERTION(test,						       \
+			!IS_ERR_OR_NULL(__ptr),				       \
+			kunit_ptr_not_err_assert,			       \
+			KUNIT_INIT_PTR_NOT_ERR_STRUCT(test,		       \
+						      assert_type,	       \
+						      #ptr,		       \
+						      __ptr),		       \
+			fmt,						       \
+			##__VA_ARGS__);					       \
+} while (0)
+
+#define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr)	       \
+		KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,		       \
+							assert_type,	       \
+							ptr,		       \
+							NULL)
+
+/**
+ * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
+ * @test: The test context object.
+ * @condition: an arbitrary boolean expression. The test fails when this does
+ * not evaluate to true.
+ *
+ * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
+ * to fail when the specified condition is not met; however, it will not prevent
+ * the test case from continuing to run; this is otherwise known as an
+ * *expectation failure*.
+ */
+#define KUNIT_EXPECT_TRUE(test, condition) \
+		KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
+
+#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)		       \
+		KUNIT_TRUE_MSG_ASSERTION(test,				       \
+					 KUNIT_EXPECTATION,		       \
+					 condition,			       \
+					 fmt,				       \
+					 ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
+ * @test: The test context object.
+ * @condition: an arbitrary boolean expression. The test fails when this does
+ * not evaluate to false.
+ *
+ * Sets an expectation that @condition evaluates to false. See
+ * KUNIT_EXPECT_TRUE() for more information.
+ */
+#define KUNIT_EXPECT_FALSE(test, condition) \
+		KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
+
+#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...)		       \
+		KUNIT_FALSE_MSG_ASSERTION(test,				       \
+					  KUNIT_EXPECTATION,		       \
+					  condition,			       \
+					  fmt,				       \
+					  ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are
+ * equal. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
+ * more information.
+ */
+#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__)
+
+/**
+ * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a pointer.
+ * @right: an arbitrary expression that evaluates to a pointer.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are
+ * equal. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
+ * more information.
+ */
+#define KUNIT_EXPECT_PTR_EQ(test, left, right)				       \
+		KUNIT_BINARY_PTR_EQ_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right)
+
+#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,			       \
+						  KUNIT_EXPECTATION,	       \
+						  left,			       \
+						  right,		       \
+						  fmt,			       \
+						  ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are not
+ * equal. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
+ * more information.
+ */
+#define KUNIT_EXPECT_NE(test, left, right) \
+		KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_NE_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right,			       \
+					      fmt,			       \
+					      ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a pointer.
+ * @right: an arbitrary expression that evaluates to a pointer.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are not
+ * equal. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
+ * more information.
+ */
+#define KUNIT_EXPECT_PTR_NE(test, left, right)				       \
+		KUNIT_BINARY_PTR_NE_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right)
+
+#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,			       \
+						  KUNIT_EXPECTATION,	       \
+						  left,			       \
+						  right,		       \
+						  fmt,			       \
+						  ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the value that @left evaluates to is less than the
+ * value that @right evaluates to. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
+ * more information.
+ */
+#define KUNIT_EXPECT_LT(test, left, right) \
+		KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_LT_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right,			       \
+					      fmt,			       \
+					      ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the value that @left evaluates to is less than or
+ * equal to the value that @right evaluates to. Semantically this is equivalent
+ * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
+ * more information.
+ */
+#define KUNIT_EXPECT_LE(test, left, right) \
+		KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_LE_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right,			       \
+					      fmt,			       \
+					      ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the value that @left evaluates to is greater than
+ * the value that @right evaluates to. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
+ * more information.
+ */
+#define KUNIT_EXPECT_GT(test, left, right) \
+		KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_GT_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right,			       \
+					      fmt,			       \
+					      ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a primitive C type.
+ * @right: an arbitrary expression that evaluates to a primitive C type.
+ *
+ * Sets an expectation that the value that @left evaluates to is greater than
+ * the value that @right evaluates to. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
+ * more information.
+ */
+#define KUNIT_EXPECT_GE(test, left, right) \
+		KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
+
+#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_GE_MSG_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right,			       \
+					      fmt,			       \
+					      ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a null terminated string.
+ * @right: an arbitrary expression that evaluates to a null terminated string.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are
+ * equal. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
+ * for more information.
+ */
+#define KUNIT_EXPECT_STREQ(test, left, right)				       \
+		KUNIT_BINARY_STR_EQ_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right)
+
+#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,			       \
+						  KUNIT_EXPECTATION,	       \
+						  left,			       \
+						  right,		       \
+						  fmt,			       \
+						  ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
+ * @test: The test context object.
+ * @left: an arbitrary expression that evaluates to a null terminated string.
+ * @right: an arbitrary expression that evaluates to a null terminated string.
+ *
+ * Sets an expectation that the values that @left and @right evaluate to are
+ * not equal. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
+ * for more information.
+ */
+#define KUNIT_EXPECT_STRNEQ(test, left, right)				       \
+		KUNIT_BINARY_STR_NE_ASSERTION(test,			       \
+					      KUNIT_EXPECTATION,	       \
+					      left,			       \
+					      right)
+
+#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
+		KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,			       \
+						  KUNIT_EXPECTATION,	       \
+						  left,			       \
+						  right,		       \
+						  fmt,			       \
+						  ##__VA_ARGS__)
+
+/**
+ * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
+ * @test: The test context object.
+ * @ptr: an arbitrary pointer.
+ *
+ * Sets an expectation that the value that @ptr evaluates to is not null and not
+ * an errno stored in a pointer. This is semantically equivalent to
+ * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
+ * more information.
+ */
+#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr)				       \
+		KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test,		       \
+						    KUNIT_EXPECTATION,	       \
+						    ptr)
+
+#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
+		KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,		       \
+							KUNIT_EXPECTATION,     \
+							ptr,		       \
+							fmt,		       \
+							##__VA_ARGS__)
+
 #endif /* _KUNIT_TEST_H */
diff --git a/kunit/test.c b/kunit/test.c
index 4c178a817f2fe..e5080a2c6b29c 100644
--- a/kunit/test.c
+++ b/kunit/test.c
@@ -120,6 +120,64 @@ static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case,
 			      test_case->name);
 }
 
+static void kunit_print_string_stream(struct kunit *test,
+				      struct string_stream *stream)
+{
+	struct string_stream_fragment *fragment;
+	char *buf;
+
+	buf = string_stream_get_string(stream);
+	if (!buf) {
+		kunit_err(test,
+			  "Could not allocate buffer, dumping stream:\n");
+		list_for_each_entry(fragment, &stream->fragments, node) {
+			kunit_err(test, fragment->fragment);
+		}
+		kunit_err(test, "\n");
+	} else {
+		kunit_err(test, buf);
+	}
+}
+
+static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
+{
+	struct string_stream *stream;
+
+	kunit_set_failure(test);
+
+	stream = alloc_string_stream(test, GFP_KERNEL);
+	if (!stream) {
+		warn_slowpath_fmt(assert->file,
+				  assert->line,
+				  "Could not allocate stream to print failed assertion.\n");
+		return;
+	}
+
+	assert->format(assert, stream);
+
+	kunit_print_string_stream(test, stream);
+}
+
+void kunit_do_assertion(struct kunit *test,
+			struct kunit_assert *assert,
+			bool pass,
+			const char *fmt, ...)
+{
+	va_list args;
+
+	if (pass)
+		return;
+
+	va_start(args, fmt);
+
+	assert->message.fmt = fmt;
+	assert->message.va = &args;
+
+	kunit_fail(test, assert);
+
+	va_end(args);
+}
+
 void kunit_init_test(struct kunit *test, const char *name)
 {
 	spin_lock_init(&test->lock);
-- 
2.23.0.rc1.153.gdeed80330f-goog


  parent reply index

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-12 18:24 [PATCH v12 00/18] kunit: introduce KUnit, the Linux kernel unit testing framework Brendan Higgins
2019-08-12 18:24 ` [PATCH v12 01/18] kunit: test: add KUnit test runner core Brendan Higgins
2019-08-12 18:24 ` [PATCH v12 02/18] kunit: test: add test resource management API Brendan Higgins
2019-08-12 22:10   ` Stephen Boyd
2019-08-12 18:24 ` [PATCH v12 03/18] kunit: test: add string_stream a std::stream like string builder Brendan Higgins
2019-08-12 22:55   ` Stephen Boyd
2019-08-12 23:33     ` Brendan Higgins
2019-08-12 23:59       ` Stephen Boyd
2019-08-13  0:41         ` Brendan Higgins
2019-08-13  4:56           ` Stephen Boyd
2019-08-13  5:02             ` Brendan Higgins
2019-08-13  5:30               ` Stephen Boyd
2019-08-13  9:04                 ` Brendan Higgins
2019-08-13  9:12                   ` Brendan Higgins
2019-08-13 16:48                     ` Stephen Boyd
2019-08-12 18:24 ` [PATCH v12 04/18] kunit: test: add assertion printing library Brendan Higgins
2019-08-12 23:46   ` Stephen Boyd
2019-08-12 23:56     ` Brendan Higgins
2019-08-13  4:27       ` Brendan Higgins
2019-08-13  4:57         ` Stephen Boyd
2019-08-13  5:03           ` Brendan Higgins
2019-08-12 18:24 ` Brendan Higgins [this message]
2019-08-12 23:57   ` [PATCH v12 05/18] kunit: test: add the concept of expectations Stephen Boyd
2019-08-13  0:33     ` Brendan Higgins
2019-08-13  5:02       ` Stephen Boyd
2019-08-13  5:04         ` Brendan Higgins
2019-08-12 18:24 ` [PATCH v12 06/18] kbuild: enable building KUnit Brendan Higgins
2019-08-12 18:24 ` [PATCH v12 07/18] kunit: test: add initial tests Brendan Higgins
2019-08-12 23:59   ` Stephen Boyd
2019-08-12 18:24 ` [PATCH v12 08/18] objtool: add kunit_try_catch_throw to the noreturn list Brendan Higgins
2019-08-12 18:24 ` [PATCH v12 09/18] kunit: test: add support for test abort Brendan Higgins
2019-08-13  4:21   ` Stephen Boyd
2019-08-13  4:57     ` Brendan Higgins
2019-08-13  5:56       ` Stephen Boyd
2019-08-13  7:52         ` Brendan Higgins
2019-08-13 17:06           ` Stephen Boyd
2019-08-12 18:24 ` [PATCH v12 10/18] kunit: test: add tests for kunit " Brendan Higgins
2019-08-13  4:24   ` Stephen Boyd
2019-08-13  5:06     ` Brendan Higgins
2019-08-13  5:57       ` Stephen Boyd
2019-08-13  7:53         ` Brendan Higgins
2019-08-12 18:24 ` [PATCH v12 11/18] kunit: test: add the concept of assertions Brendan Higgins
2019-08-13  4:55   ` Stephen Boyd
2019-08-13  5:09     ` Brendan Higgins
2019-08-12 18:24 ` [PATCH v12 12/18] kunit: test: add tests for KUnit managed resources Brendan Higgins
2019-08-13  4:31   ` Stephen Boyd
2019-08-13  7:57     ` Brendan Higgins
2019-08-13 17:07       ` Stephen Boyd
2019-08-12 18:24 ` [PATCH v12 13/18] kunit: tool: add Python wrappers for running KUnit tests Brendan Higgins
2019-08-13  6:02   ` Stephen Boyd
2019-08-12 18:24 ` [PATCH v12 14/18] kunit: defconfig: add defconfigs for building " Brendan Higgins
2019-08-13  4:38   ` Stephen Boyd
2019-08-13  7:59     ` Brendan Higgins
2019-08-12 18:24 ` [PATCH v12 15/18] Documentation: kunit: add documentation for KUnit Brendan Higgins
2019-08-13  4:46   ` Stephen Boyd
2019-08-12 18:24 ` [PATCH v12 16/18] MAINTAINERS: add entry for KUnit the unit testing framework Brendan Higgins
2019-08-13  5:26   ` Stephen Boyd
2019-08-12 18:24 ` [PATCH v12 17/18] kernel/sysctl-test: Add null pointer test for sysctl.c:proc_dointvec() Brendan Higgins
2019-08-13  4:48   ` Stephen Boyd
2019-08-12 18:24 ` [PATCH v12 18/18] MAINTAINERS: add proc sysctl KUnit test to PROC SYSCTL section Brendan Higgins

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190812182421.141150-6-brendanhiggins@google.com \
    --to=brendanhiggins@google.com \
    --cc=Alexander.Levin@microsoft.com \
    --cc=Tim.Bird@sony.com \
    --cc=amir73il@gmail.com \
    --cc=dan.carpenter@oracle.com \
    --cc=daniel@ffwll.ch \
    --cc=devicetree@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=frowand.list@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jdike@addtoit.com \
    --cc=joel@jms.id.au \
    --cc=jpoimboe@redhat.com \
    --cc=julia.lawall@lip6.fr \
    --cc=keescook@google.com \
    --cc=khilman@baylibre.com \
    --cc=kieran.bingham@ideasonboard.com \
    --cc=knut.omang@oracle.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-nvdimm@lists.01.org \
    --cc=linux-um@lists.infradead.org \
    --cc=logang@deltatee.com \
    --cc=mcgrof@kernel.org \
    --cc=mpe@ellerman.id.au \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=rdunlap@infradead.org \
    --cc=richard@nod.at \
    --cc=rientjes@google.com \
    --cc=robh@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=sboyd@kernel.org \
    --cc=shuah@kernel.org \
    --cc=tytso@mit.edu \
    --cc=wfg@linux.intel.com \
    --cc=yamada.masahiro@socionext.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-kselftest Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-kselftest/0 linux-kselftest/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-kselftest linux-kselftest/ https://lore.kernel.org/linux-kselftest \
		linux-kselftest@vger.kernel.org
	public-inbox-index linux-kselftest

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kselftest


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git