From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dwdohert@ecsmtp.ir.intel.com>
Received: from mga09.intel.com (mga09.intel.com [134.134.136.24])
 by dpdk.org (Postfix) with ESMTP id AB3D768CD
 for <dev@dpdk.org>; Mon, 21 Jul 2014 16:51:27 +0200 (CEST)
Received: from orsmga002.jf.intel.com ([10.7.209.21])
 by orsmga102.jf.intel.com with ESMTP; 21 Jul 2014 07:47:02 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.01,701,1400050800"; d="scan'208";a="576476764"
Received: from irvmail001.ir.intel.com ([163.33.26.43])
 by orsmga002.jf.intel.com with ESMTP; 21 Jul 2014 07:52:39 -0700
Received: from sivswdev02.ir.intel.com (sivswdev02.ir.intel.com
 [10.237.217.46])
 by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id
 s6LEqd22019363; Mon, 21 Jul 2014 15:52:39 +0100
Received: from sivswdev02.ir.intel.com (localhost [127.0.0.1])
 by sivswdev02.ir.intel.com with ESMTP id s6LEqdtb030231;
 Mon, 21 Jul 2014 15:52:39 +0100
Received: (from dwdohert@localhost)
 by sivswdev02.ir.intel.com with  id s6LEqcCn030227;
 Mon, 21 Jul 2014 15:52:38 +0100
From: Declan Doherty <declan.doherty@intel.com>
To: dev@dpdk.org
Date: Mon, 21 Jul 2014 15:52:16 +0100
Message-Id: <1405954337-28488-2-git-send-email-declan.doherty@intel.com>
X-Mailer: git-send-email 1.7.0.7
In-Reply-To: <1405954337-28488-1-git-send-email-declan.doherty@intel.com>
References: <1405954337-28488-1-git-send-email-declan.doherty@intel.com>
Subject: [dpdk-dev] [PATCH 1/2] test app:
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: patches and discussions about DPDK <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Mon, 21 Jul 2014 14:51:28 -0000

- Adding common test assertion macros for unit testing
- Structs for encapsulating unit tests / test suites data.
- test suite runner method

Signed-off-by: Declan Doherty <declan.doherty@intel.com>
---
 app/test/test.c |   53 +++++++++++++++++++++++++++++++++++
 app/test/test.h |   82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 135 insertions(+), 0 deletions(-)

diff --git a/app/test/test.c b/app/test/test.c
index a41e43d..589a168 100644
--- a/app/test/test.c
+++ b/app/test/test.c
@@ -153,3 +153,56 @@ main(int argc, char **argv)
 
 	return 0;
 }
+
+
+int
+unit_test_suite_runner(struct unit_test_suite *suite)
+{
+	int retval, i = 0;
+
+	if (suite->suite_name)
+		printf("Test Suite : %s\n", suite->suite_name);
+
+	if (suite->setup)
+		if (suite->setup() != 0)
+			return -1;
+
+	while (suite->unit_test_cases[i].testcase) {
+		/* Run test case setup */
+		if (suite->unit_test_cases[i].setup) {
+			retval = suite->unit_test_cases[i].setup();
+			if (retval != 0)
+				return retval;
+		}
+
+		/* Run test case */
+		if (suite->unit_test_cases[i].testcase() == 0) {
+			printf("TestCase %2d: %s\n", i,
+					suite->unit_test_cases[i].success_msg ?
+					suite->unit_test_cases[i].success_msg :
+					"passed");
+		}
+		else {
+			printf("TestCase %2d: %s\n", i, suite->unit_test_cases[i].fail_msg ?
+					suite->unit_test_cases[i].fail_msg :
+					"failed");
+			return -1;
+		}
+
+		/* Run test case teardown */
+		if (suite->unit_test_cases[i].teardown) {
+			retval = suite->unit_test_cases[i].teardown();
+			if (retval != 0)
+				return retval;
+		}
+
+		i++;
+	}
+
+	/* Run test suite teardown */
+	if (suite->teardown)
+		if (suite->teardown() != 0)
+			return -1;
+
+	return 0;
+}
diff --git a/app/test/test.h b/app/test/test.h
index c00e1d6..181c38e 100644
--- a/app/test/test.h
+++ b/app/test/test.h
@@ -34,6 +34,88 @@
 #ifndef _TEST_H_
 #define _TEST_H_
 
+#define TEST_ASSERT(cond, msg, ...) do {						\
+		if (!(cond)) {											\
+			printf("TestCase %s() line %d failed: "			\
+				msg "\n", __func__, __LINE__, ##__VA_ARGS__);	\
+			return -1;											\
+		}														\
+} while (0)
+
+#define TEST_ASSERT_EQUAL(a, b, msg, ...)  {					\
+		if (!(a == b)) {										\
+			printf("TestCase %s() line %d failed: "				\
+				msg "\n", __func__, __LINE__, ##__VA_ARGS__);	\
+			return -1;											\
+		}														\
+} while (0)
+
+#define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {				\
+		if (!(a != b)) {										\
+			printf("TestCase %s() line %d failed: "			\
+				msg "\n", __func__, __LINE__, ##__VA_ARGS__);	\
+			return -1;											\
+		}														\
+} while (0)
+
+#define TEST_ASSERT_SUCCESS(val, msg, ...) do {					\
+		if (!(val == 0)) {										\
+			printf("TestCase %s() line %d failed: "			\
+				msg "\n", __func__, __LINE__, ##__VA_ARGS__);	\
+			return -1;											\
+		}														\
+} while (0)
+
+#define TEST_ASSERT_FAIL(val, msg, ...) do {					\
+		if (!(val != -1)) {										\
+			printf("TestCase %s() line %d failed: "			\
+				msg "\n", __func__, __LINE__, ##__VA_ARGS__);	\
+			return -1;											\
+		}														\
+} while (0)
+
+
+#define TEST_ASSERT_NULL(val, msg, ...) do {					\
+		if (!(val == NULL)) {									\
+			printf("TestCase %s() line %d failed: "			\
+				msg "\n", __func__, __LINE__, ##__VA_ARGS__);	\
+			return -1;											\
+		}														\
+} while (0)
+
+#define TEST_ASSERT_NOT_NULL(val, msg, ...) do {				\
+		if (!(val != NULL)) {									\
+			printf("TestCase %s() line %d failed: "			\
+				msg "\n", __func__, __LINE__, ##__VA_ARGS__);	\
+			return -1;											\
+		}														\
+} while (0)
+
+struct unit_test_case {
+	int (*setup)(void);
+	int (*teardown)(void);
+	int (*testcase)(void);
+	const char *success_msg;
+	const char *fail_msg;
+};
+
+#define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed"}
+
+#define TEST_CASE_ST(setup, teardown, testcase) 		\
+		{ setup, teardown, testcase, #testcase " succeeded",	\
+		#testcase " failed "}
+
+#define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL }
+
+struct unit_test_suite {
+	const char *suite_name;
+	int (*setup)(void);
+	int (*teardown)(void);
+	struct unit_test_case unit_test_cases[];
+};
+
+int unit_test_suite_runner(struct unit_test_suite *suite);
+
 /* icc on baremetal gives us troubles with function named 'main' */
 #ifdef RTE_EXEC_ENV_BAREMETAL
 #define main _main
-- 
1.7.0.7