[dm-devel] [PATCH 2/3] multipath-tools tests: fix bitfield tests for big endian

mwilck at suse.com mwilck at suse.com
Fri Aug 21 22:42:23 UTC 2020


From: Martin Wilck <mwilck at suse.com>

On big endian systems, the 32bit words need to be swapped in
the test code to get the byte ordering right.

Fixes: "libmultipath: create bitfield abstraction"
Signed-off-by: Martin Wilck <mwilck at suse.com>
---
 tests/util.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/tests/util.c b/tests/util.c
index 63a5f59..ec38c55 100644
--- a/tests/util.c
+++ b/tests/util.c
@@ -162,6 +162,22 @@ static int test_basenamecpy(void)
 	return cmocka_run_group_tests(tests, NULL, NULL);
 }
 
+/*
+ * On big endian systems, if bitfield_t is 32bit, we need
+ * to swap the two 32 bit parts of a 64bit value to make
+ * the tests below work.
+ */
+static uint64_t maybe_swap(uint64_t v)
+{
+	uint32_t *s = (uint32_t *)&v;
+
+	if (sizeof(bitfield_t) == 4)
+		/* this is identity for little endian */
+		return ((uint64_t)s[1] << 32) | s[0];
+	else
+		return v;
+}
+
 static void test_bitmask_1(void **state)
 {
 	struct bitfield *bf;
@@ -184,7 +200,7 @@ static void test_bitmask_1(void **state)
 				       b, j, k, arr[k]);
 #endif
 				if (k == j)
-					assert_int_equal(arr[j], 1ULL << i);
+					assert_int_equal(maybe_swap(arr[j]), 1ULL << i);
 				else
 					assert_int_equal(arr[k], 0ULL);
 			}
@@ -235,7 +251,7 @@ static void test_bitmask_2(void **state)
 					assert_int_equal(arr[k], 0ULL);
 				else
 					assert_int_equal(
-						arr[k],
+						maybe_swap(arr[k]),
 						(1ULL << (i + 1)) - 1);
 			}
 		}
@@ -260,7 +276,7 @@ static void test_bitmask_2(void **state)
 					assert_int_equal(arr[k], ~0ULL);
 				else
 					assert_int_equal(
-						arr[k],
+						maybe_swap(arr[k]),
 						~((1ULL << (i + 1)) - 1));
 			}
 		}
-- 
2.28.0





More information about the dm-devel mailing list