Wed, 27 Feb 2013 11:48:29 +0100
renamed comparator to utils module and added copy functions
Makefile | file | annotate | diff | comparison | revisions | |
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 | |
ucx/utils.c | file | annotate | diff | comparison | revisions | |
ucx/utils.h | file | annotate | diff | comparison | revisions |
1.1 --- a/Makefile Wed Feb 27 11:37:27 2013 +0100 1.2 +++ b/Makefile Wed Feb 27 11:48:29 2013 +0100 1.3 @@ -54,6 +54,7 @@ 1.4 1.5 clean: FORCE 1.6 $(RM) $(RMFLAGS) build/*.${OBJ_EXT} 1.7 + $(RM) $(RMFLAGS) build/*.${LIB_EXT} 1.8 1.9 FORCE: 1.10
2.1 --- a/test/dlist_tests.c Wed Feb 27 11:37:27 2013 +0100 2.2 +++ b/test/dlist_tests.c Wed Feb 27 11:48:29 2013 +0100 2.3 @@ -3,7 +3,7 @@ 2.4 */ 2.5 2.6 #include "dlist_tests.h" 2.7 -#include "ucx/comparator.h" 2.8 +#include "ucx/utils.h" 2.9 2.10 UCX_TEST_IMPLEMENT(test_ucx_dlist_append) { 2.11 UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello"); 2.12 @@ -171,7 +171,7 @@ 2.13 UcxDlist *list = ucx_dlist_append(NULL, hello); 2.14 list = ucx_dlist_append(list, world); 2.15 2.16 - UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL); 2.17 + UcxDlist *copy = ucx_dlist_clone(list, ucx_strcpy, NULL); 2.18 UCX_TEST_BEGIN 2.19 2.20 UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, ucx_strcmp, NULL), "failed");
3.1 --- a/test/list_tests.c Wed Feb 27 11:37:27 2013 +0100 3.2 +++ b/test/list_tests.c Wed Feb 27 11:48:29 2013 +0100 3.3 @@ -3,7 +3,7 @@ 3.4 */ 3.5 3.6 #include "list_tests.h" 3.7 -#include "ucx/comparator.h" 3.8 +#include "ucx/utils.h" 3.9 3.10 UCX_TEST_IMPLEMENT(test_ucx_list_append) { 3.11 UcxList *list = ucx_list_append(NULL, (void*)"Hello"); 3.12 @@ -159,7 +159,7 @@ 3.13 UcxList *list = ucx_list_append(NULL, hello); 3.14 list = ucx_list_append(list, world); 3.15 3.16 - UcxList *copy = ucx_list_clone(list, copy_string, NULL); 3.17 + UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL); 3.18 UCX_TEST_BEGIN 3.19 3.20 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
4.1 --- a/test/main.c Wed Feb 27 11:37:27 2013 +0100 4.2 +++ b/test/main.c Wed Feb 27 11:48:29 2013 +0100 4.3 @@ -41,14 +41,6 @@ 4.4 #include "map_tests.h" 4.5 #include "buffer_tests.h" 4.6 4.7 -void* copy_string(void* e, void* data) { 4.8 - char *str = (char*) e; 4.9 - size_t n = 1+strlen(str); 4.10 - char *cpy = (char*) malloc(n); 4.11 - memcpy(cpy, str, n); 4.12 - return cpy; 4.13 -} 4.14 - 4.15 UCX_TEST_IMPLEMENT(testTestSuitePositive) { 4.16 UCX_TEST_BEGIN 4.17 UCX_TEST_ASSERT(2*2 == 4, "the test framework fails");
5.1 --- a/ucx/Makefile Wed Feb 27 11:37:27 2013 +0100 5.2 +++ b/ucx/Makefile Wed Feb 27 11:48:29 2013 +0100 5.3 @@ -29,7 +29,7 @@ 5.4 include ../$(CONF).mk 5.5 5.6 # list of source files 5.7 -SRC = comparator.c 5.8 +SRC = utils.c 5.9 SRC += list.c 5.10 SRC += dlist.c 5.11 SRC += map.c
6.1 --- a/ucx/comparator.c Wed Feb 27 11:37:27 2013 +0100 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,54 +0,0 @@ 6.4 -#include "comparator.h" 6.5 -#include "math.h" 6.6 - 6.7 -int ucx_strcmp(void *s1, void *s2, void *data) { 6.8 - return strcmp((char*)s1, (char*)s2); 6.9 -} 6.10 - 6.11 -int ucx_strncmp(void *s1, void *s2, void *n) { 6.12 - return strncmp((char*)s1, (char*)s2, *((size_t*) n)); 6.13 -} 6.14 - 6.15 -int ucx_intcmp(void *i1, void *i2, void *data) { 6.16 - int a = *((int*) i1); 6.17 - int b = *((int*) i2); 6.18 - if (a == b) { 6.19 - return 0; 6.20 - } else { 6.21 - return a < b ? -1 : 1; 6.22 - } 6.23 -} 6.24 - 6.25 -int ucx_floatcmp(void *f1, void *f2, void *epsilon) { 6.26 - float a = *((float*) f1); 6.27 - float b = *((float*) f2); 6.28 - float e = !epsilon ? 1e-6f : *((float*)epsilon); 6.29 - if (fabsf(a - b) < e) { 6.30 - return 0; 6.31 - } else { 6.32 - return a < b ? -1 : 1; 6.33 - } 6.34 -} 6.35 - 6.36 -int ucx_doublecmp(void *d1, void *d2, void *epsilon) { 6.37 - double a = *((float*) d1); 6.38 - double b = *((float*) d2); 6.39 - double e = !epsilon ? 1e-14 : *((double*)epsilon); 6.40 - if (fabs(a - b) < e) { 6.41 - return 0; 6.42 - } else { 6.43 - return a < b ? -1 : 1; 6.44 - } 6.45 -} 6.46 - 6.47 -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { 6.48 - if (ptr1 == ptr2) { 6.49 - return 0; 6.50 - } else { 6.51 - return ptr1 < ptr2 ? -1 : 1; 6.52 - } 6.53 -} 6.54 - 6.55 -int ucx_memcmp(void *ptr1, void *ptr2, void *n) { 6.56 - return memcmp(ptr1, ptr2, *((size_t*)n)); 6.57 -}
7.1 --- a/ucx/comparator.h Wed Feb 27 11:37:27 2013 +0100 7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 @@ -1,86 +0,0 @@ 7.4 -#ifndef COMPARATOR_H 7.5 -#define COMPARATOR_H 7.6 - 7.7 -#ifdef __cplusplus 7.8 -extern "C" { 7.9 -#endif 7.10 - 7.11 -#include "ucx.h" 7.12 -#include <string.h> 7.13 - 7.14 -/** 7.15 - * Wraps the strcmp function. 7.16 - * @param s1 string one 7.17 - * @param s2 string two 7.18 - * @param data omitted 7.19 - * @return the result of strcmp(s1, s2) 7.20 - */ 7.21 -int ucx_strcmp(void *s1, void *s2, void *data); 7.22 - 7.23 -/** 7.24 - * Wraps the strncmp function. 7.25 - * @param s1 string one 7.26 - * @param s2 string two 7.27 - * @param n a pointer to the size_t containing the third strncmp parameter 7.28 - * @return the result of strncmp(s1, s2, *n) 7.29 - */ 7.30 -int ucx_strncmp(void *s1, void *s2, void *n); 7.31 - 7.32 -/** 7.33 - * Compares two integers of type int. 7.34 - * @param i1 pointer to integer one 7.35 - * @param i2 pointer to integer two 7.36 - * @param data omitted 7.37 - * @return -1, if *i1 is less than *i2, 0 if both are equal, 7.38 - * 1 if *i1 is greater than *i2 7.39 - */ 7.40 - 7.41 -int ucx_intcmp(void *i1, void *i2, void *data); 7.42 - 7.43 -/** 7.44 - * Compares two real numbers of type float. 7.45 - * @param f1 pointer to float one 7.46 - * @param f2 pointer to float two 7.47 - * @param if provided: a pointer to precision (default: 1e-6f) 7.48 - * @return -1, if *f1 is less than *f2, 0 if both are equal, 7.49 - * 1 if *f1 is greater than *f2 7.50 - */ 7.51 - 7.52 -int ucx_floatcmp(void *f1, void *f2, void *data); 7.53 - 7.54 -/** 7.55 - * Compares two real numbers of type double. 7.56 - * @param f1 pointer to double one 7.57 - * @param f2 pointer to double two 7.58 -* @param if provided: a pointer to precision (default: 1e-14) 7.59 - * @return -1, if *d1 is less than *d2, 0 if both are equal, 7.60 - * 1 if *d1 is greater than *d2 7.61 - */ 7.62 - 7.63 -int ucx_doublecmp(void *d1, void *d2, void *data); 7.64 - 7.65 -/** 7.66 - * Compares two pointers. 7.67 - * @param ptr1 pointer one 7.68 - * @param ptr2 pointer two 7.69 - * @param data omitted 7.70 - * @return -1 if ptr1 is less than ptr2, 0 if both are equal, 7.71 - * 1 if ptr1 is greater than ptr2 7.72 - */ 7.73 -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); 7.74 - 7.75 -/** 7.76 - * Compares two memory areas. 7.77 - * @param ptr1 pointer one 7.78 - * @param ptr2 pointer two 7.79 - * @param n a pointer to the size_t containing the third parameter for memcmp 7.80 - * @return the result of memcmp(ptr1, ptr2, *n) 7.81 - */ 7.82 -int ucx_memcmp(void *ptr1, void *ptr2, void *n); 7.83 - 7.84 -#ifdef __cplusplus 7.85 -} 7.86 -#endif 7.87 - 7.88 -#endif /* COMPARATOR_H */ 7.89 -
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/ucx/utils.c Wed Feb 27 11:48:29 2013 +0100 8.3 @@ -0,0 +1,72 @@ 8.4 +#include "utils.h" 8.5 +#include "math.h" 8.6 + 8.7 +/* COPY FUCNTIONS */ 8.8 +void* ucx_strcpy(void* s, void* data) { 8.9 + char *str = (char*) s; 8.10 + size_t n = 1+strlen(str); 8.11 + char *cpy = (char*) malloc(n); 8.12 + memcpy(cpy, str, n); 8.13 + return cpy; 8.14 +} 8.15 + 8.16 +void* ucx_memcpy(void* m, void* n) { 8.17 + size_t k = *((size_t*)n); 8.18 + void *cpy = malloc(k); 8.19 + memcpy(cpy, m, k); 8.20 + return cpy; 8.21 +} 8.22 + 8.23 +/* COMPARE FUNCTION */ 8.24 + 8.25 +int ucx_strcmp(void *s1, void *s2, void *data) { 8.26 + return strcmp((char*)s1, (char*)s2); 8.27 +} 8.28 + 8.29 +int ucx_strncmp(void *s1, void *s2, void *n) { 8.30 + return strncmp((char*)s1, (char*)s2, *((size_t*) n)); 8.31 +} 8.32 + 8.33 +int ucx_intcmp(void *i1, void *i2, void *data) { 8.34 + int a = *((int*) i1); 8.35 + int b = *((int*) i2); 8.36 + if (a == b) { 8.37 + return 0; 8.38 + } else { 8.39 + return a < b ? -1 : 1; 8.40 + } 8.41 +} 8.42 + 8.43 +int ucx_floatcmp(void *f1, void *f2, void *epsilon) { 8.44 + float a = *((float*) f1); 8.45 + float b = *((float*) f2); 8.46 + float e = !epsilon ? 1e-6f : *((float*)epsilon); 8.47 + if (fabsf(a - b) < e) { 8.48 + return 0; 8.49 + } else { 8.50 + return a < b ? -1 : 1; 8.51 + } 8.52 +} 8.53 + 8.54 +int ucx_doublecmp(void *d1, void *d2, void *epsilon) { 8.55 + double a = *((float*) d1); 8.56 + double b = *((float*) d2); 8.57 + double e = !epsilon ? 1e-14 : *((double*)epsilon); 8.58 + if (fabs(a - b) < e) { 8.59 + return 0; 8.60 + } else { 8.61 + return a < b ? -1 : 1; 8.62 + } 8.63 +} 8.64 + 8.65 +int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { 8.66 + if (ptr1 == ptr2) { 8.67 + return 0; 8.68 + } else { 8.69 + return ptr1 < ptr2 ? -1 : 1; 8.70 + } 8.71 +} 8.72 + 8.73 +int ucx_memcmp(void *ptr1, void *ptr2, void *n) { 8.74 + return memcmp(ptr1, ptr2, *((size_t*)n)); 8.75 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/ucx/utils.h Wed Feb 27 11:48:29 2013 +0100 9.3 @@ -0,0 +1,103 @@ 9.4 +#ifndef COMPARATOR_H 9.5 +#define COMPARATOR_H 9.6 + 9.7 +#ifdef __cplusplus 9.8 +extern "C" { 9.9 +#endif 9.10 + 9.11 +#include "ucx.h" 9.12 +#include <string.h> 9.13 + 9.14 +/** 9.15 + * Copies a string. 9.16 + * @param s the string to copy 9.17 + * @param data omitted 9.18 + * @return a pointer to a copy of s1 that can be passed to free(void*) 9.19 + */ 9.20 +void *ucx_strcpy(void *s, void *data); 9.21 + 9.22 +/** 9.23 + * Copies a memory area. 9.24 + * @param m a pointer to the memory area 9.25 + * @param n a pointer to the size_t containing the size of the memory area 9.26 + * @return a pointer to a copy of the specified memory area that can 9.27 + * be passed to free(void*) 9.28 + */ 9.29 +void *ucx_memcpy(void *m, void *n); 9.30 + 9.31 +/** 9.32 + * Wraps the strcmp function. 9.33 + * @param s1 string one 9.34 + * @param s2 string two 9.35 + * @param data omitted 9.36 + * @return the result of strcmp(s1, s2) 9.37 + */ 9.38 +int ucx_strcmp(void *s1, void *s2, void *data); 9.39 + 9.40 +/** 9.41 + * Wraps the strncmp function. 9.42 + * @param s1 string one 9.43 + * @param s2 string two 9.44 + * @param n a pointer to the size_t containing the third strncmp parameter 9.45 + * @return the result of strncmp(s1, s2, *n) 9.46 + */ 9.47 +int ucx_strncmp(void *s1, void *s2, void *n); 9.48 + 9.49 +/** 9.50 + * Compares two integers of type int. 9.51 + * @param i1 pointer to integer one 9.52 + * @param i2 pointer to integer two 9.53 + * @param data omitted 9.54 + * @return -1, if *i1 is less than *i2, 0 if both are equal, 9.55 + * 1 if *i1 is greater than *i2 9.56 + */ 9.57 + 9.58 +int ucx_intcmp(void *i1, void *i2, void *data); 9.59 + 9.60 +/** 9.61 + * Compares two real numbers of type float. 9.62 + * @param f1 pointer to float one 9.63 + * @param f2 pointer to float two 9.64 + * @param if provided: a pointer to precision (default: 1e-6f) 9.65 + * @return -1, if *f1 is less than *f2, 0 if both are equal, 9.66 + * 1 if *f1 is greater than *f2 9.67 + */ 9.68 + 9.69 +int ucx_floatcmp(void *f1, void *f2, void *data); 9.70 + 9.71 +/** 9.72 + * Compares two real numbers of type double. 9.73 + * @param f1 pointer to double one 9.74 + * @param f2 pointer to double two 9.75 +* @param if provided: a pointer to precision (default: 1e-14) 9.76 + * @return -1, if *d1 is less than *d2, 0 if both are equal, 9.77 + * 1 if *d1 is greater than *d2 9.78 + */ 9.79 + 9.80 +int ucx_doublecmp(void *d1, void *d2, void *data); 9.81 + 9.82 +/** 9.83 + * Compares two pointers. 9.84 + * @param ptr1 pointer one 9.85 + * @param ptr2 pointer two 9.86 + * @param data omitted 9.87 + * @return -1 if ptr1 is less than ptr2, 0 if both are equal, 9.88 + * 1 if ptr1 is greater than ptr2 9.89 + */ 9.90 +int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); 9.91 + 9.92 +/** 9.93 + * Compares two memory areas. 9.94 + * @param ptr1 pointer one 9.95 + * @param ptr2 pointer two 9.96 + * @param n a pointer to the size_t containing the third parameter for memcmp 9.97 + * @return the result of memcmp(ptr1, ptr2, *n) 9.98 + */ 9.99 +int ucx_memcmp(void *ptr1, void *ptr2, void *n); 9.100 + 9.101 +#ifdef __cplusplus 9.102 +} 9.103 +#endif 9.104 + 9.105 +#endif /* COMPARATOR_H */ 9.106 +