use ucx compare function in list tests

Sun, 06 Nov 2022 14:17:33 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 06 Nov 2022 14:17:33 +0100
changeset 602
3b071ea0e9cf
parent 601
95ba6014041b
child 603
c49104015a6b

use ucx compare function in list tests

test/test_list.cpp file | annotate | diff | comparison | revisions
--- a/test/test_list.cpp	Sat Nov 05 17:50:04 2022 +0100
+++ b/test/test_list.cpp	Sun Nov 06 14:17:33 2022 +0100
@@ -28,6 +28,7 @@
 
 #include "cx/linked_list.h"
 #include "cx/utils.h"
+#include "cx/compare.h"
 #include "util_allocator.h"
 
 #include <gtest/gtest.h>
@@ -36,16 +37,6 @@
 #include <unordered_set>
 #include <algorithm>
 
-static int cmp_int_impl(
-        int const *l,
-        int const *r
-) {
-    int left = *l, right = *r;
-    return left == right ? 0 : (left < right ? -1 : 1);
-}
-
-#define cmp_int ((CxListComparator) cmp_int_impl)
-
 struct node {
     node *next = nullptr;
     node *prev = nullptr;
@@ -177,17 +168,17 @@
     int s;
 
     s = 2;
-    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 0);
+    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 0);
     s = 4;
-    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 1);
+    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 1);
     s = 6;
-    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 2);
+    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 2);
     s = 8;
-    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 3);
+    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 3);
     s = 10;
-    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 4);
+    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 4);
     s = -2;
-    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cmp_int, &s), 4);
+    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, false, cx_cmp_int, &s), 4);
 }
 
 TEST(LinkedList_LowLevel, cx_linked_list_compare) {
@@ -196,11 +187,11 @@
     auto tc = create_nodes_test_data({2, 4, 6, 9});
     auto la = ta.begin, lb = tb.begin, lc = tc.begin;
 
-    EXPECT_GT(cx_linked_list_compare(la, lb, loc_next, loc_data, false, false, cmp_int), 0);
-    EXPECT_LT(cx_linked_list_compare(lb, la, loc_next, loc_data, false, false, cmp_int), 0);
-    EXPECT_GT(cx_linked_list_compare(lc, la, loc_next, loc_data, false, false, cmp_int), 0);
-    EXPECT_LT(cx_linked_list_compare(la, lc, loc_next, loc_data, false, false, cmp_int), 0);
-    EXPECT_EQ(cx_linked_list_compare(la, la, loc_next, loc_data, false, false, cmp_int), 0);
+    EXPECT_GT(cx_linked_list_compare(la, lb, loc_next, loc_data, false, false, cx_cmp_int), 0);
+    EXPECT_LT(cx_linked_list_compare(lb, la, loc_next, loc_data, false, false, cx_cmp_int), 0);
+    EXPECT_GT(cx_linked_list_compare(lc, la, loc_next, loc_data, false, false, cx_cmp_int), 0);
+    EXPECT_LT(cx_linked_list_compare(la, lc, loc_next, loc_data, false, false, cx_cmp_int), 0);
+    EXPECT_EQ(cx_linked_list_compare(la, la, loc_next, loc_data, false, false, cx_cmp_int), 0);
 }
 
 TEST(LinkedList_LowLevel, cx_linked_list_add) {
@@ -527,7 +518,7 @@
     void *end = cx_linked_list_last(begin, loc_next);
 
     cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data,
-                        false, cmp_int);
+                        false, cx_cmp_int);
 
     node *check = reinterpret_cast<node *>(begin);
     node *check_last = nullptr;
@@ -555,7 +546,7 @@
     cx_linked_list_reverse(&begin, &end, loc_prev, loc_next);
     EXPECT_EQ(end, orig_begin);
     EXPECT_EQ(begin, orig_end);
-    EXPECT_EQ(cx_linked_list_compare(begin, expected.begin, loc_next, loc_data, false, false, cmp_int), 0);
+    EXPECT_EQ(cx_linked_list_compare(begin, expected.begin, loc_next, loc_data, false, false, cx_cmp_int), 0);
 }
 
 class HighLevelTest : public ::testing::Test {
@@ -580,7 +571,7 @@
         return autofree(
                 cxLinkedListFromArray(
                         &testingAllocator,
-                        cmp_int,
+                        cx_cmp_int,
                         sizeof(int),
                         testdata_len,
                         testdata.data.data()
@@ -589,7 +580,7 @@
     }
 
     auto pointerLinkedListFromTestData() const -> CxList * {
-        auto list = autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int));
+        auto list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
         cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
         return list;
     }
@@ -599,7 +590,7 @@
         EXPECT_EQ(list->size, 0);
         EXPECT_EQ(list->capacity, (size_t) -1);
         EXPECT_EQ(list->allocator, &testingAllocator);
-        EXPECT_EQ(list->cmpfunc, cmp_int);
+        EXPECT_EQ(list->cmpfunc, cx_cmp_int);
     }
 
     void verifyAdd(
@@ -779,41 +770,41 @@
 };
 
 TEST_F(LinkedList, cxLinkedListCreate) {
-    CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cmp_int, sizeof(int)));
+    CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
     EXPECT_EQ(list->itemsize, sizeof(int));
     verifyCreate(list);
 }
 
 TEST_F(PointerLinkedList, cxPointerLinkedListCreate) {
-    CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int));
+    CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
     EXPECT_EQ(list->itemsize, sizeof(void *));
     verifyCreate(list);
 }
 
 TEST_F(LinkedList, cxLinkedListFromArray) {
-    CxList *expected = autofree(cxLinkedListCreate(&testingAllocator, cmp_int, sizeof(int)));
+    CxList *expected = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
     cx_for_n (i, testdata_len) cxListAdd(expected, &testdata.data[i]);
-    CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cmp_int, sizeof(int),
+    CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int),
                                                   testdata_len, testdata.data.data()));
     EXPECT_EQ(cxListCompare(list, expected), 0);
 }
 
 TEST_F(LinkedList, cxListAdd) {
-    CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cmp_int, sizeof(int)));
+    CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
     verifyAdd(list, false);
 }
 
 TEST_F(PointerLinkedList, cxListAdd) {
-    CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int));
+    CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
     verifyAdd(list, true);
 }
 
 TEST_F(LinkedList, cxListInsert) {
-    verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cmp_int, sizeof(int))));
+    verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
 }
 
 TEST_F(PointerLinkedList, cxListInsert) {
-    verifyInsert(autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int)));
+    verifyInsert(autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)));
 }
 
 TEST_F(LinkedList, cxListRemove) {
@@ -858,13 +849,13 @@
 
 TEST_F(LinkedList, InsertViaIterator) {
     int fivenums[] = {0, 1, 2, 3, 4, 5};
-    CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cmp_int, sizeof(int), 5, fivenums));
+    CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int), 5, fivenums));
     verifyInsertViaIterator(list);
 }
 
 TEST_F(PointerLinkedList, InsertViaIterator) {
     int fivenums[] = {0, 1, 2, 3, 4, 5};
-    CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cmp_int));
+    CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
     cx_for_n (i, 5) cxListAdd(list, &fivenums[i]);
     verifyInsertViaIterator(list);
 }
@@ -903,7 +894,7 @@
 
 TEST_F(PointerLinkedList, NoDestructor) {
     void *item = cxMalloc(&testingAllocator, sizeof(int));
-    auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cmp_int);
+    auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int);
     cxListAdd(list, item);
     ASSERT_FALSE(testingAllocator.verify());
     cxListDestroy(list);
@@ -914,7 +905,7 @@
 
 TEST_F(PointerLinkedList, SimpleDestructor) {
     int item = 0;
-    auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cmp_int);
+    auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int);
     list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
     list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
     cxListAdd(list, &item);
@@ -924,7 +915,7 @@
 
 TEST_F(PointerLinkedList, AdvancedDestructor) {
     void *item = cxMalloc(&testingAllocator, sizeof(int));
-    auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cmp_int);
+    auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int);
     list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
     list->advanced_destructor.data = &testingAllocator;
     list->advanced_destructor.func = (cx_destructor_func2) cxFree;

mercurial