migrate compare tests - relates to #342

Sun, 31 Dec 2023 15:02:11 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 31 Dec 2023 15:02:11 +0100
changeset 787
d0f02310aa47
parent 786
b0ebb3d88407
child 788
b34ff44e6433

migrate compare tests - relates to #342

tests/Makefile file | annotate | diff | comparison | revisions
tests/test_compare.c file | annotate | diff | comparison | revisions
tests/test_compare.cpp file | annotate | diff | comparison | revisions
tests/ucxtest.c file | annotate | diff | comparison | revisions
--- a/tests/Makefile	Sun Dec 31 14:29:46 2023 +0100
+++ b/tests/Makefile	Sun Dec 31 15:02:11 2023 +0100
@@ -28,6 +28,7 @@
 TEST_DIR=$(build_dir)/tests
 
 SRC = util_allocator.c test_utils.c test_hash_key.c test_allocator.c \
+	test_compare.c \
 	test_string.c test_printf.c test_mempool.c test_hash_map.c ucxtest.c
 
 OBJ_EXT=.o
@@ -51,6 +52,11 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
+$(TEST_DIR)/test_compare$(OBJ_EXT): test_compare.c ../src/cx/test.h \
+ ../src/cx/compare.h ../src/cx/common.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS) -c $<
+
 $(TEST_DIR)/test_hash_key$(OBJ_EXT): test_hash_key.c ../src/cx/test.h \
  ../src/cx/hash_key.h ../src/cx/common.h ../src/cx/string.h \
  ../src/cx/allocator.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_compare.c	Sun Dec 31 15:02:11 2023 +0100
@@ -0,0 +1,183 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cx/test.h"
+#include <limits.h>
+#include <float.h>
+
+#include "cx/compare.h"
+
+#define test_compare_gen_subroutine(T, max_number, signed_type) \
+static CX_TEST_SUBROUTINE( \
+        test_sub_cmp_##T, \
+        cx_compare_func fnc \
+) { \
+    T m = max_number / 512; \
+    T x, y; \
+    x = (signed_type ? -3 : 3) * m; \
+    y = 5 * m; \
+    CX_TEST_ASSERT(fnc(&x, &y) < 0); \
+    CX_TEST_ASSERT(fnc(&y, &x) > 0); \
+    x = 120 * m; \
+    y = 348 * m; \
+    CX_TEST_ASSERT(fnc(&x, &y) < 0); \
+    CX_TEST_ASSERT(fnc(&y, &x) > 0); \
+    if (signed_type) { \
+        x = -120 * m; \
+        y = -348 * m; \
+        CX_TEST_ASSERT(fnc(&x, &y) > 0); \
+        CX_TEST_ASSERT(fnc(&y, &x) < 0); \
+    } \
+    x = y; \
+    CX_TEST_ASSERT(fnc(&x, &y) == 0); \
+    CX_TEST_ASSERT(fnc(&y, &x) == 0); \
+}
+
+
+// type aliases for types containing space characters
+typedef long long cx_longlong;
+typedef unsigned long cx_ulong;
+typedef unsigned long long cx_ulonglong;
+
+// generate sub routines depending on the type
+test_compare_gen_subroutine(int, INT_MAX, true)
+test_compare_gen_subroutine(long, LONG_MAX, true)
+test_compare_gen_subroutine(cx_longlong, LLONG_MAX, true)
+test_compare_gen_subroutine(int16_t, INT16_MAX, true)
+test_compare_gen_subroutine(int32_t, INT32_MAX, true)
+test_compare_gen_subroutine(int64_t, INT64_MAX, true)
+test_compare_gen_subroutine(unsigned, UINT_MAX, false)
+test_compare_gen_subroutine(cx_ulong, ULONG_MAX, false)
+test_compare_gen_subroutine(cx_ulonglong, ULLONG_MAX, false)
+test_compare_gen_subroutine(uint16_t, UINT16_MAX, false)
+test_compare_gen_subroutine(uint32_t, UINT32_MAX, false)
+test_compare_gen_subroutine(uint64_t, UINT64_MAX, false)
+test_compare_gen_subroutine(float, FLT_MAX, true)
+test_compare_gen_subroutine(double, DBL_MAX, true)
+test_compare_gen_subroutine(intptr_t, INTPTR_MAX, true)
+test_compare_gen_subroutine(uintptr_t, UINTPTR_MAX, false)
+
+CX_TEST(test_compare_int) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int, cx_cmp_int);
+}
+
+CX_TEST(test_compare_long) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_long, cx_cmp_longint);
+}
+
+CX_TEST(test_compare_longlong) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_longlong, cx_cmp_longlong);
+}
+
+CX_TEST(test_compare_int16_t) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int16_t, cx_cmp_int16);
+}
+
+CX_TEST(test_compare_int32_t) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int32_t, cx_cmp_int32);
+}
+
+CX_TEST(test_compare_int64_t) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int64_t, cx_cmp_int64);
+}
+
+CX_TEST(test_compare_unsigned) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_unsigned, cx_cmp_uint);
+}
+
+CX_TEST(test_compare_ulong) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulong, cx_cmp_ulongint);
+}
+
+CX_TEST(test_compare_ulonglong) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulonglong, cx_cmp_ulonglong);
+}
+
+CX_TEST(test_compare_uint16_t) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint16_t, cx_cmp_uint16);
+}
+
+CX_TEST(test_compare_uint32_t) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint32_t, cx_cmp_uint32);
+}
+
+CX_TEST(test_compare_uint64_t) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint64_t, cx_cmp_uint64);
+}
+
+CX_TEST(test_compare_float) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_float, cx_cmp_float);
+}
+
+CX_TEST(test_compare_double) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_double, cx_cmp_double);
+}
+
+CX_TEST(test_compare_intptr_t) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_intptr_t, cx_cmp_intptr);
+}
+
+CX_TEST(test_compare_uintptr_t) {
+    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uintptr_t, cx_cmp_uintptr);
+}
+
+CX_TEST(test_compare_ptr) {
+    int data[3];
+    CX_TEST_DO {
+        CX_TEST_ASSERT(0 == cx_cmp_ptr(data, data));
+        CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[0], &data[1]));
+        CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[1], &data[2]));
+        CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[2], &data[1]));
+        CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[1], data));
+        CX_TEST_ASSERT(0 == cx_cmp_ptr(&data[1], &data[1]));
+    }
+}
+
+CxTestSuite *cx_test_suite_compare(void) {
+    CxTestSuite *suite = cx_test_suite_new("compare");
+
+    cx_test_register(suite, test_compare_int);
+    cx_test_register(suite, test_compare_long);
+    cx_test_register(suite, test_compare_longlong);
+    cx_test_register(suite, test_compare_int16_t);
+    cx_test_register(suite, test_compare_int32_t);
+    cx_test_register(suite, test_compare_int64_t);
+    cx_test_register(suite, test_compare_unsigned);
+    cx_test_register(suite, test_compare_ulong);
+    cx_test_register(suite, test_compare_ulonglong);
+    cx_test_register(suite, test_compare_uint16_t);
+    cx_test_register(suite, test_compare_uint32_t);
+    cx_test_register(suite, test_compare_uint64_t);
+    cx_test_register(suite, test_compare_float);
+    cx_test_register(suite, test_compare_double);
+    cx_test_register(suite, test_compare_intptr_t);
+    cx_test_register(suite, test_compare_uintptr_t);
+    cx_test_register(suite, test_compare_ptr);
+
+    return suite;
+}
--- a/tests/test_compare.cpp	Sun Dec 31 14:29:46 2023 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,137 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2022 Mike Becker, Olaf Wintermann All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "cx/compare.h"
-
-#include <gtest/gtest.h>
-
-template<typename T>
-static void test_compare(
-        int (*fnc)(
-                void const *,
-                void const *
-        )
-) {
-    auto m = std::numeric_limits<T>::max() / 400;
-    T x, y;
-
-    x = (std::is_signed_v<T> ? -3 : 3) * m;
-    y = 5 * m;
-    EXPECT_LT(fnc(&x, &y), 0);
-    EXPECT_GT(fnc(&y, &x), 0);
-
-    x = 120 * m;
-    y = 348 * m;
-    EXPECT_LT(fnc(&x, &y), 0);
-    EXPECT_GT(fnc(&y, &x), 0);
-
-    if constexpr (std::is_signed_v<T>) {
-        x = -120 * m;
-        y = -348 * m;
-        EXPECT_GT(fnc(&x, &y), 0);
-        EXPECT_LT(fnc(&y, &x), 0);
-    }
-
-    x = y;
-    EXPECT_EQ(fnc(&x, &y), 0);
-    EXPECT_EQ(fnc(&y, &x), 0);
-}
-
-TEST(Compare, Int) {
-    test_compare<int>(cx_cmp_int);
-}
-
-TEST(Compare, Longint) {
-    test_compare<long int>(cx_cmp_longint);
-}
-
-TEST(Compare, Longlong) {
-    test_compare<long long>(cx_cmp_longlong);
-}
-
-TEST(Compare, Int16) {
-    test_compare<int16_t>(cx_cmp_int16);
-}
-
-TEST(Compare, Int32) {
-    test_compare<int32_t>(cx_cmp_int32);
-}
-
-TEST(Compare, Int64) {
-    test_compare<int64_t>(cx_cmp_int64);
-}
-
-TEST(Compare, Uint) {
-    test_compare<unsigned int>(cx_cmp_uint);
-}
-
-TEST(Compare, Ulongint) {
-    test_compare<unsigned long int>(cx_cmp_ulongint);
-}
-
-TEST(Compare, Ulonglong) {
-    test_compare<unsigned long long>(cx_cmp_ulonglong);
-}
-
-TEST(Compare, Uint16) {
-    test_compare<uint16_t>(cx_cmp_uint16);
-}
-
-TEST(Compare, Uint32) {
-    test_compare<uint32_t>(cx_cmp_uint32);
-}
-
-TEST(Compare, Uint64) {
-    test_compare<uint64_t>(cx_cmp_uint64);
-}
-
-TEST(Compare, Float) {
-    test_compare<float>(cx_cmp_float);
-}
-
-TEST(Compare, Double) {
-    test_compare<double>(cx_cmp_double);
-}
-
-TEST(Compare, IntPtr) {
-    test_compare<intptr_t>(cx_cmp_intptr);
-}
-
-TEST(Compare, UintPtr) {
-    test_compare<uintptr_t>(cx_cmp_uintptr);
-}
-
-TEST(Compare, Ptr) {
-    int data[3];
-    EXPECT_EQ(0, cx_cmp_ptr(data, data));
-    EXPECT_EQ(-1, cx_cmp_ptr(&data[0], &data[1]));
-    EXPECT_EQ(-1, cx_cmp_ptr(&data[1], &data[2]));
-    EXPECT_EQ(1, cx_cmp_ptr(&data[2], &data[1]));
-    EXPECT_EQ(1, cx_cmp_ptr(&data[1], data));
-    EXPECT_EQ(0, cx_cmp_ptr(&data[1], &data[1]));
-}
--- a/tests/ucxtest.c	Sun Dec 31 14:29:46 2023 +0100
+++ b/tests/ucxtest.c	Sun Dec 31 15:02:11 2023 +0100
@@ -30,6 +30,7 @@
 
 CxTestSuite *cx_test_suite_testing_allocator(void);
 CxTestSuite *cx_test_suite_utils(void);
+CxTestSuite *cx_test_suite_compare(void);
 CxTestSuite *cx_test_suite_hash_key(void);
 CxTestSuite *cx_test_suite_allocator(void);
 CxTestSuite *cx_test_suite_string(void);
@@ -48,6 +49,7 @@
     execute_test_suites(
             cx_test_suite_testing_allocator(),
             cx_test_suite_utils(),
+            cx_test_suite_compare(),
             cx_test_suite_hash_key(),
             cx_test_suite_allocator(),
             cx_test_suite_string(),

mercurial