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
     1.1 --- a/test/dlist_tests.c	Wed Feb 27 09:41:17 2013 +0100
     1.2 +++ b/test/dlist_tests.c	Wed Feb 27 10:09:23 2013 +0100
     1.3 @@ -3,6 +3,7 @@
     1.4   */
     1.5  
     1.6  #include "dlist_tests.h"
     1.7 +#include "ucx/comparator.h"
     1.8  
     1.9  UCX_TEST_IMPLEMENT(test_ucx_dlist_append) {
    1.10      UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
    1.11 @@ -46,8 +47,8 @@
    1.12      list3 = ucx_dlist_prepend(list3, (void*)"Hallo");
    1.13      UCX_TEST_BEGIN
    1.14      
    1.15 -    UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed");
    1.16 -    UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed");
    1.17 +    UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, ucx_strcmp, NULL), "failed");
    1.18 +    UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, ucx_strcmp, NULL), "failed");
    1.19      
    1.20      UCX_TEST_END
    1.21      ucx_dlist_free(list3);
    1.22 @@ -160,7 +161,7 @@
    1.23      UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL);
    1.24      UCX_TEST_BEGIN
    1.25  
    1.26 -    UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, cmp_string, NULL), "failed");
    1.27 +    UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, ucx_strcmp, NULL), "failed");
    1.28      UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
    1.29      UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
    1.30  
    1.31 @@ -191,11 +192,11 @@
    1.32      expected = ucx_dlist_append(expected, (void*)"test");
    1.33      expected = ucx_dlist_append(expected, (void*)"this");
    1.34  
    1.35 -    list = ucx_dlist_sort(list, cmp_string, NULL);
    1.36 +    list = ucx_dlist_sort(list, ucx_strcmp, NULL);
    1.37  
    1.38      UCX_TEST_BEGIN
    1.39      UCX_TEST_ASSERT(
    1.40 -            ucx_dlist_equals(list, expected, cmp_string, NULL), "failed");
    1.41 +            ucx_dlist_equals(list, expected, ucx_strcmp, NULL), "failed");
    1.42      UcxDlist *l = list;
    1.43      UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
    1.44      while (l->next != NULL) {
     2.1 --- a/test/list_tests.c	Wed Feb 27 09:41:17 2013 +0100
     2.2 +++ b/test/list_tests.c	Wed Feb 27 10:09:23 2013 +0100
     2.3 @@ -3,6 +3,7 @@
     2.4   */
     2.5  
     2.6  #include "list_tests.h"
     2.7 +#include "ucx/comparator.h"
     2.8  
     2.9  UCX_TEST_IMPLEMENT(test_ucx_list_append) {
    2.10      UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    2.11 @@ -44,8 +45,8 @@
    2.12      list3 = ucx_list_prepend(list3, (void*)"Hallo");
    2.13      
    2.14      UCX_TEST_BEGIN
    2.15 -    UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed");
    2.16 -    UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed");
    2.17 +    UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
    2.18 +    UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
    2.19      UCX_TEST_END
    2.20      
    2.21      ucx_list_free(list3);
    2.22 @@ -148,7 +149,7 @@
    2.23      UcxList *copy = ucx_list_clone(list, copy_string, NULL);
    2.24      UCX_TEST_BEGIN
    2.25  
    2.26 -    UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed");
    2.27 +    UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
    2.28      UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
    2.29      UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
    2.30  
    2.31 @@ -179,11 +180,11 @@
    2.32      expected = ucx_list_append(expected, (void*)"test");
    2.33      expected = ucx_list_append(expected, (void*)"this");
    2.34  
    2.35 -    list = ucx_list_sort(list, cmp_string, NULL);
    2.36 +    list = ucx_list_sort(list, ucx_strcmp, NULL);
    2.37  
    2.38      UCX_TEST_BEGIN
    2.39      UCX_TEST_ASSERT(
    2.40 -            ucx_list_equals(list, expected, cmp_string, NULL), "failed");
    2.41 +            ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
    2.42      UCX_TEST_END
    2.43  
    2.44      ucx_list_free(expected);
     3.1 --- a/test/main.c	Wed Feb 27 09:41:17 2013 +0100
     3.2 +++ b/test/main.c	Wed Feb 27 10:09:23 2013 +0100
     3.3 @@ -41,10 +41,6 @@
     3.4  #include "map_tests.h"
     3.5  #include "buffer_tests.h"
     3.6  
     3.7 -int cmp_string(void* o1, void* o2, void* data) {
     3.8 -    return strcmp((char*)o1, (char*)o2);
     3.9 -}
    3.10 -
    3.11  void* copy_string(void* e, void* data) {
    3.12      char *str = (char*) e;
    3.13      size_t n = 1+strlen(str);
     4.1 --- a/ucx/Makefile	Wed Feb 27 09:41:17 2013 +0100
     4.2 +++ b/ucx/Makefile	Wed Feb 27 10:09:23 2013 +0100
     4.3 @@ -29,7 +29,8 @@
     4.4  include ../$(CONF).mk
     4.5  
     4.6  # list of source files
     4.7 -SRC  = list.c 
     4.8 +SRC  = comparator.c
     4.9 +SRC += list.c 
    4.10  SRC += dlist.c
    4.11  SRC += map.c
    4.12  SRC += mempool.c
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/ucx/comparator.c	Wed Feb 27 10:09:23 2013 +0100
     5.3 @@ -0,0 +1,27 @@
     5.4 +#include "comparator.h"
     5.5 +
     5.6 +int ucx_strcmp(void *s1, void *s2, void *data) {
     5.7 +    return strcmp((char*)s1, (char*)s2);
     5.8 +}
     5.9 +
    5.10 +int ucx_strncmp(void *s1, void *s2, void *n) {
    5.11 +    return strncmp((char*)s1, (char*)s2, *((size_t*) n));
    5.12 +}
    5.13 +
    5.14 +int ucx_intcmp(void *i1, void *i2, void *data) {
    5.15 +   int a = *((int*) i1);
    5.16 +   int b = *((int*) i2);
    5.17 +   if (a == b) {
    5.18 +       return 0;
    5.19 +   } else {
    5.20 +       return a < b ? -1 : 1;
    5.21 +   }
    5.22 +}
    5.23 +
    5.24 +int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
    5.25 +    if (ptr1 == ptr2) {
    5.26 +        return 0;
    5.27 +    } else {
    5.28 +        return ptr1 < ptr2 ? -1 : 1;
    5.29 +    }
    5.30 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/ucx/comparator.h	Wed Feb 27 10:09:23 2013 +0100
     6.3 @@ -0,0 +1,55 @@
     6.4 +#ifndef COMPARATOR_H
     6.5 +#define	COMPARATOR_H
     6.6 +
     6.7 +#ifdef	__cplusplus
     6.8 +extern "C" {
     6.9 +#endif
    6.10 +
    6.11 +#include "ucx.h"
    6.12 +#include <string.h>
    6.13 +
    6.14 +/**
    6.15 + * Wraps the strcmp function.
    6.16 + * @param s1 string one
    6.17 + * @param s2 string two
    6.18 + * @param data omitted
    6.19 + * @return the result of strcmp(s1, s2)
    6.20 + */
    6.21 +int ucx_strcmp(void *s1, void *s2, void *data);
    6.22 +
    6.23 +/**
    6.24 + * Wraps the strncmp function.
    6.25 + * @param s1 string one
    6.26 + * @param s2 string two
    6.27 + * @param n a pointer to the size_t containing the third strncmp parameter
    6.28 + * @return the result of strncmp(s1, s2, *n)
    6.29 + */
    6.30 +int ucx_strncmp(void *s1, void *s2, void *n);
    6.31 +
    6.32 +/**
    6.33 + * Compares two integers of type int.
    6.34 + * @param i1 pointer to integer one
    6.35 + * @param i2 pointer to integer two
    6.36 + * @param data omitted
    6.37 + * @return -1, if *i1 is less than *i2, 0 if both are equal,
    6.38 + * 1 if *i1 is greater than *i2
    6.39 + */
    6.40 +
    6.41 +int ucx_intcmp(void *i1, void *i2, void *data);
    6.42 +
    6.43 +/**
    6.44 + * Compares two pointers.
    6.45 + * @param ptr1 pointer one
    6.46 + * @param ptr2 pointer two
    6.47 + * @param data omitted
    6.48 + * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
    6.49 + * 1 if ptr1 is greater than ptr2
    6.50 + */
    6.51 +int ucx_ptrcmp(void *ptr1, void *ptr2, void *data);
    6.52 +
    6.53 +#ifdef	__cplusplus
    6.54 +}
    6.55 +#endif
    6.56 +
    6.57 +#endif	/* COMPARATOR_H */
    6.58 +

mercurial