add cx_cmp_ptr() - fix #340

Mon, 18 Dec 2023 16:04:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 16:04:21 +0100
changeset 762
4523f6d42512
parent 761
61d5197d612b
child 763
741a2040fa33

add cx_cmp_ptr() - fix #340

CHANGELOG file | annotate | diff | comparison | revisions
src/compare.c file | annotate | diff | comparison | revisions
src/cx/compare.h file | annotate | diff | comparison | revisions
tests/test_compare.cpp file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Mon Dec 18 15:13:26 2023 +0100
+++ b/CHANGELOG	Mon Dec 18 16:04:21 2023 +0100
@@ -1,6 +1,7 @@
 Version 3.1 - tbd.
 ------------------------
  * adds cxBufferReset()
+ * adds cx_cmp_ptr()
  * fixes wrong link from UCX 2 documentation to UCX 3 documentation
  * removes CMake, except for tests at the moment, in favor of uwproj
 
--- a/src/compare.c	Mon Dec 18 15:13:26 2023 +0100
+++ b/src/compare.c	Mon Dec 18 16:04:21 2023 +0100
@@ -199,3 +199,15 @@
     }
 }
 
+int cx_cmp_ptr(
+        void const *ptr1,
+        void const *ptr2
+) {
+    uintptr_t p1 = (uintptr_t) ptr1;
+    uintptr_t p2 = (uintptr_t) ptr2;
+    if (p1 == p2) {
+        return 0;
+    } else {
+        return p1 < p2 ? -1 : 1;
+    }
+}
--- a/src/cx/compare.h	Mon Dec 18 15:13:26 2023 +0100
+++ b/src/cx/compare.h	Mon Dec 18 16:04:21 2023 +0100
@@ -212,6 +212,19 @@
         void const *ptr2
 );
 
+/**
+ * Compares the pointers specified in the arguments without de-referencing.
+ *
+ * @param ptr1 pointer one
+ * @param ptr2 pointer two
+ * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
+ * 1 if ptr1 is greater than ptr2
+ */
+int cx_cmp_ptr(
+        void const *ptr1,
+        void const *ptr2
+);
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
--- a/tests/test_compare.cpp	Mon Dec 18 15:13:26 2023 +0100
+++ b/tests/test_compare.cpp	Mon Dec 18 16:04:21 2023 +0100
@@ -125,3 +125,13 @@
 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]));
+}

mercurial