comparator module

Wed, 27 Feb 2013 10:09:23 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 10:09:23 +0100
changeset 89
47f7fdbddb62
parent 88
18823857ce79
child 90
ef3163857e88

comparator module

test/dlist_tests.c file | annotate | diff | comparison | revisions
test/list_tests.c file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
ucx/Makefile file | annotate | diff | comparison | revisions
ucx/comparator.c file | annotate | diff | comparison | revisions
ucx/comparator.h file | annotate | diff | comparison | revisions
--- a/test/dlist_tests.c	Wed Feb 27 09:41:17 2013 +0100
+++ b/test/dlist_tests.c	Wed Feb 27 10:09:23 2013 +0100
@@ -3,6 +3,7 @@
  */
 
 #include "dlist_tests.h"
+#include "ucx/comparator.h"
 
 UCX_TEST_IMPLEMENT(test_ucx_dlist_append) {
     UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
@@ -46,8 +47,8 @@
     list3 = ucx_dlist_prepend(list3, (void*)"Hallo");
     UCX_TEST_BEGIN
     
-    UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed");
-    UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed");
+    UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, ucx_strcmp, NULL), "failed");
+    UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, ucx_strcmp, NULL), "failed");
     
     UCX_TEST_END
     ucx_dlist_free(list3);
@@ -160,7 +161,7 @@
     UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL);
     UCX_TEST_BEGIN
 
-    UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, cmp_string, NULL), "failed");
+    UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, ucx_strcmp, NULL), "failed");
     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
 
@@ -191,11 +192,11 @@
     expected = ucx_dlist_append(expected, (void*)"test");
     expected = ucx_dlist_append(expected, (void*)"this");
 
-    list = ucx_dlist_sort(list, cmp_string, NULL);
+    list = ucx_dlist_sort(list, ucx_strcmp, NULL);
 
     UCX_TEST_BEGIN
     UCX_TEST_ASSERT(
-            ucx_dlist_equals(list, expected, cmp_string, NULL), "failed");
+            ucx_dlist_equals(list, expected, ucx_strcmp, NULL), "failed");
     UcxDlist *l = list;
     UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
     while (l->next != NULL) {
--- a/test/list_tests.c	Wed Feb 27 09:41:17 2013 +0100
+++ b/test/list_tests.c	Wed Feb 27 10:09:23 2013 +0100
@@ -3,6 +3,7 @@
  */
 
 #include "list_tests.h"
+#include "ucx/comparator.h"
 
 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
     UcxList *list = ucx_list_append(NULL, (void*)"Hello");
@@ -44,8 +45,8 @@
     list3 = ucx_list_prepend(list3, (void*)"Hallo");
     
     UCX_TEST_BEGIN
-    UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed");
-    UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed");
+    UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
+    UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
     UCX_TEST_END
     
     ucx_list_free(list3);
@@ -148,7 +149,7 @@
     UcxList *copy = ucx_list_clone(list, copy_string, NULL);
     UCX_TEST_BEGIN
 
-    UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed");
+    UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
 
@@ -179,11 +180,11 @@
     expected = ucx_list_append(expected, (void*)"test");
     expected = ucx_list_append(expected, (void*)"this");
 
-    list = ucx_list_sort(list, cmp_string, NULL);
+    list = ucx_list_sort(list, ucx_strcmp, NULL);
 
     UCX_TEST_BEGIN
     UCX_TEST_ASSERT(
-            ucx_list_equals(list, expected, cmp_string, NULL), "failed");
+            ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
     UCX_TEST_END
 
     ucx_list_free(expected);
--- a/test/main.c	Wed Feb 27 09:41:17 2013 +0100
+++ b/test/main.c	Wed Feb 27 10:09:23 2013 +0100
@@ -41,10 +41,6 @@
 #include "map_tests.h"
 #include "buffer_tests.h"
 
-int cmp_string(void* o1, void* o2, void* data) {
-    return strcmp((char*)o1, (char*)o2);
-}
-
 void* copy_string(void* e, void* data) {
     char *str = (char*) e;
     size_t n = 1+strlen(str);
--- a/ucx/Makefile	Wed Feb 27 09:41:17 2013 +0100
+++ b/ucx/Makefile	Wed Feb 27 10:09:23 2013 +0100
@@ -29,7 +29,8 @@
 include ../$(CONF).mk
 
 # list of source files
-SRC  = list.c 
+SRC  = comparator.c
+SRC += list.c 
 SRC += dlist.c
 SRC += map.c
 SRC += mempool.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ucx/comparator.c	Wed Feb 27 10:09:23 2013 +0100
@@ -0,0 +1,27 @@
+#include "comparator.h"
+
+int ucx_strcmp(void *s1, void *s2, void *data) {
+    return strcmp((char*)s1, (char*)s2);
+}
+
+int ucx_strncmp(void *s1, void *s2, void *n) {
+    return strncmp((char*)s1, (char*)s2, *((size_t*) n));
+}
+
+int ucx_intcmp(void *i1, void *i2, void *data) {
+   int a = *((int*) i1);
+   int b = *((int*) i2);
+   if (a == b) {
+       return 0;
+   } else {
+       return a < b ? -1 : 1;
+   }
+}
+
+int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
+    if (ptr1 == ptr2) {
+        return 0;
+    } else {
+        return ptr1 < ptr2 ? -1 : 1;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ucx/comparator.h	Wed Feb 27 10:09:23 2013 +0100
@@ -0,0 +1,55 @@
+#ifndef COMPARATOR_H
+#define	COMPARATOR_H
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+#include "ucx.h"
+#include <string.h>
+
+/**
+ * Wraps the strcmp function.
+ * @param s1 string one
+ * @param s2 string two
+ * @param data omitted
+ * @return the result of strcmp(s1, s2)
+ */
+int ucx_strcmp(void *s1, void *s2, void *data);
+
+/**
+ * Wraps the strncmp function.
+ * @param s1 string one
+ * @param s2 string two
+ * @param n a pointer to the size_t containing the third strncmp parameter
+ * @return the result of strncmp(s1, s2, *n)
+ */
+int ucx_strncmp(void *s1, void *s2, void *n);
+
+/**
+ * Compares two integers of type int.
+ * @param i1 pointer to integer one
+ * @param i2 pointer to integer two
+ * @param data omitted
+ * @return -1, if *i1 is less than *i2, 0 if both are equal,
+ * 1 if *i1 is greater than *i2
+ */
+
+int ucx_intcmp(void *i1, void *i2, void *data);
+
+/**
+ * Compares two pointers.
+ * @param ptr1 pointer one
+ * @param ptr2 pointer two
+ * @param data omitted
+ * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
+ * 1 if ptr1 is greater than ptr2
+ */
+int ucx_ptrcmp(void *ptr1, void *ptr2, void *data);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* COMPARATOR_H */
+

mercurial