[v10,2/6] test/bitops: add bit operation test case
diff mbox series

Message ID 20200427075856.12098-3-joyce.kong@arm.com
State Accepted
Delegated to: Thomas Monjalon
Headers show
Series
  • implement common bit operation APIs
Related show

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Joyce Kong April 27, 2020, 7:58 a.m. UTC
Add test cases for setting bit, clearing bit, testing
and setting bit, testing and clearing bit operation.

Signed-off-by: Joyce Kong <joyce.kong@arm.com>
Reviewed-by: Gavin Hu <gavin.hu@arm.com>
Reviewed-by: Phil Yang <phil.yang@arm.com>
---
 MAINTAINERS               |   1 +
 app/test/Makefile         |   1 +
 app/test/autotest_data.py |   6 ++
 app/test/meson.build      |   2 +
 app/test/test_bitops.c    | 137 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 147 insertions(+)
 create mode 100644 app/test/test_bitops.c

Patch
diff mbox series

diff --git a/MAINTAINERS b/MAINTAINERS
index c8d4b99a7..cff9f590d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -253,6 +253,7 @@  F: app/test/test_bitmap.c
 Bitops
 M: Joyce Kong <joyce.kong@arm.com>
 F: lib/librte_eal/include/rte_bitops.h
+F: app/test/test_bitops.c
 
 MCSlock - EXPERIMENTAL
 M: Phil Yang <phil.yang@arm.com>
diff --git a/app/test/Makefile b/app/test/Makefile
index 4582eca6c..eaad3b71d 100644
--- a/app/test/Makefile
+++ b/app/test/Makefile
@@ -70,6 +70,7 @@  SRCS-y += test_ticketlock.c
 SRCS-y += test_memory.c
 SRCS-y += test_memzone.c
 SRCS-y += test_bitmap.c
+SRCS-y += test_bitops.c
 SRCS-y += test_reciprocal_division.c
 SRCS-y += test_reciprocal_division_perf.c
 SRCS-y += test_fbarray.c
diff --git a/app/test/autotest_data.py b/app/test/autotest_data.py
index 7b1d01389..fc3fcc159 100644
--- a/app/test/autotest_data.py
+++ b/app/test/autotest_data.py
@@ -404,6 +404,12 @@ 
         "Func":    default_autotest,
         "Report":  None,
     },
+    {
+        "Name":    "Bitops test",
+        "Command": "bitops_test",
+        "Func":    default_autotest,
+        "Report":  None,
+    },
     {
         "Name":    "Hash multiwriter autotest",
         "Command": "hash_multiwriter_autotest",
diff --git a/app/test/meson.build b/app/test/meson.build
index fc60acbe7..fed6dd51b 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -12,6 +12,7 @@  test_sources = files('commands.c',
 	'test_alarm.c',
 	'test_atomic.c',
 	'test_barrier.c',
+	'test_bitops.c',
 	'test_bpf.c',
 	'test_byteorder.c',
 	'test_cmdline.c',
@@ -171,6 +172,7 @@  fast_tests = [
         ['acl_autotest', true],
         ['alarm_autotest', false],
         ['atomic_autotest', false],
+        ['bitops_autotest', true],
         ['byteorder_autotest', true],
         ['cmdline_autotest', true],
         ['common_autotest', true],
diff --git a/app/test/test_bitops.c b/app/test/test_bitops.c
new file mode 100644
index 000000000..dac92c61e
--- /dev/null
+++ b/app/test/test_bitops.c
@@ -0,0 +1,137 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2019 Arm Limited
+ */
+
+#include <rte_launch.h>
+#include <rte_bitops.h>
+#include "test.h"
+
+uint32_t val32;
+uint64_t val64;
+
+#define MAX_BITS_32 32
+#define MAX_BITS_64 64
+/*
+ * Bitops functions
+ * ================
+ *
+ * - The main test function performs several subtests.
+ * - Check bit operations on one core.
+ *   - Initialize valXX to specified values, then set each bit of valXX
+ *     to 1 one by one in "test_bit_relaxed_set".
+ *
+ *   - Clear each bit of valXX to 0 one by one in "test_bit_relaxed_clear".
+ *
+ *   - Function "test_bit_relaxed_test_set_clear" checks whether each bit
+ *     of valXX can do "test and set" and "test and clear" correctly.
+ */
+
+static int
+test_bit_relaxed_set(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < MAX_BITS_32; i++)
+		rte_bit_relaxed_set32(i, &val32);
+
+	for (i = 0; i < MAX_BITS_32; i++)
+		if (!rte_bit_relaxed_get32(i, &val32)) {
+			printf("Failed to set bit in relaxed version.\n");
+			return TEST_FAILED;
+		}
+
+	for (i = 0; i < MAX_BITS_64; i++)
+		rte_bit_relaxed_set64(i, &val64);
+
+	for (i = 0; i < MAX_BITS_64; i++)
+		if (!rte_bit_relaxed_get64(i, &val64)) {
+			printf("Failed to set bit in relaxed version.\n");
+			return TEST_FAILED;
+		}
+
+	return TEST_SUCCESS;
+}
+
+static int
+test_bit_relaxed_clear(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < MAX_BITS_32; i++)
+		rte_bit_relaxed_clear32(i, &val32);
+
+	for (i = 0; i < MAX_BITS_32; i++)
+		if (rte_bit_relaxed_get32(i, &val32)) {
+			printf("Failed to clear bit in relaxed version.\n");
+			return TEST_FAILED;
+		}
+
+	for (i = 0; i < MAX_BITS_64; i++)
+		rte_bit_relaxed_clear64(i, &val64);
+
+	for (i = 0; i < MAX_BITS_64; i++)
+		if (rte_bit_relaxed_get64(i, &val64)) {
+			printf("Failed to clear bit in relaxed version.\n");
+			return TEST_FAILED;
+		}
+
+	return TEST_SUCCESS;
+}
+
+static int
+test_bit_relaxed_test_set_clear(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < MAX_BITS_32; i++)
+		rte_bit_relaxed_test_and_set32(i, &val32);
+
+	for (i = 0; i < MAX_BITS_32; i++)
+		if (!rte_bit_relaxed_test_and_clear32(i, &val32)) {
+			printf("Failed to set and test bit in relaxed version.\n");
+			return TEST_FAILED;
+	}
+
+	for (i = 0; i < MAX_BITS_32; i++)
+		if (rte_bit_relaxed_get32(i, &val32)) {
+			printf("Failed to test and clear bit in relaxed version.\n");
+			return TEST_FAILED;
+		}
+
+	for (i = 0; i < MAX_BITS_64; i++)
+		rte_bit_relaxed_test_and_set64(i, &val64);
+
+	for (i = 0; i < MAX_BITS_64; i++)
+		if (!rte_bit_relaxed_test_and_clear64(i, &val64)) {
+			printf("Failed to set and test bit in relaxed version.\n");
+			return TEST_FAILED;
+		}
+
+	for (i = 0; i < MAX_BITS_64; i++)
+		if (rte_bit_relaxed_get64(i, &val64)) {
+			printf("Failed to test and clear bit in relaxed version.\n");
+			return TEST_FAILED;
+		}
+
+	return TEST_SUCCESS;
+}
+
+static int
+test_bitops(void)
+{
+	val32 = 0;
+	val64 = 0;
+
+	if (test_bit_relaxed_set() < 0)
+		return TEST_FAILED;
+
+	if (test_bit_relaxed_clear() < 0)
+		return TEST_FAILED;
+
+	if (test_bit_relaxed_test_set_clear() < 0)
+		return TEST_FAILED;
+
+	return TEST_SUCCESS;
+}
+
+REGISTER_TEST_COMMAND(bitops_autotest, test_bitops);