Wed, 08 Feb 2012 23:43:02 +0100
changed make clean + added dlist_clone with copy test + added va_end statements to string.c
Makefile | file | annotate | diff | comparison | revisions | |
test/list_tests.c | file | annotate | diff | comparison | revisions | |
ucx/list.c | file | annotate | diff | comparison | revisions | |
ucx/string.c | file | annotate | diff | comparison | revisions |
1.1 --- a/Makefile Sun Jan 15 14:20:25 2012 +0100 1.2 +++ b/Makefile Wed Feb 08 23:43:02 2012 +0100 1.3 @@ -53,7 +53,7 @@ 1.4 ./build/test$(APP_EXT) 1.5 1.6 clean: FORCE 1.7 - $(RM) $(RMFLAGS) build/* 1.8 + $(RM) $(RMFLAGS) build/*.${OBJ_EXT} 1.9 1.10 FORCE: 1.11
2.1 --- a/test/list_tests.c Sun Jan 15 14:20:25 2012 +0100 2.2 +++ b/test/list_tests.c Wed Feb 08 23:43:02 2012 +0100 2.3 @@ -8,14 +8,22 @@ 2.4 #include "ucx/list.h" 2.5 #include "ucx/dlist.h" 2.6 2.7 -struct test1_data { 2.8 +struct foreach_testdata { 2.9 int values[3]; 2.10 int i; 2.11 }; 2.12 2.13 +void* int_cpy(void* source, void* data) { 2.14 + void *dest = malloc(sizeof(int)); 2.15 + if (dest != NULL) { 2.16 + *((int*)dest) = *((int*)source); 2.17 + } 2.18 + return dest; 2.19 +} 2.20 + 2.21 int int_cmp(void* e1, void *e2, void *data) { 2.22 if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1; 2.23 - 2.24 + 2.25 int *i1 = (int*)e1, *i2 = (int*)e2; 2.26 int r = (*i1) - (*i2); 2.27 return (r < 0) ? -1 : (r == 0 ? 0 : 1); 2.28 @@ -23,11 +31,17 @@ 2.29 2.30 int dlist_tests_foreach(void *v, void *custom) { 2.31 UcxDlist *dl = (UcxDlist*)v; 2.32 - struct test1_data *tdata = (struct test1_data*)custom; 2.33 + struct foreach_testdata *tdata = (struct foreach_testdata*)custom; 2.34 2.35 tdata->values[tdata->i] = *(int*)dl->data; 2.36 tdata->i++; 2.37 - 2.38 + 2.39 + return 0; 2.40 +} 2.41 + 2.42 +int dlist_free_content(void *v, void *custom) { 2.43 + UcxDlist *dl = (UcxDlist*)v; 2.44 + free(dl->data); 2.45 return 0; 2.46 } 2.47 2.48 @@ -50,15 +64,15 @@ 2.49 if(elm == NULL) { 2.50 fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); 2.51 r--; 2.52 - } 2.53 - if(elm->data == NULL) { 2.54 + } else if(elm->data == NULL) { 2.55 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); 2.56 r--; 2.57 - } 2.58 - int *data = (int*)elm->data; 2.59 - if(*data != i) { 2.60 - fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); 2.61 - r--; 2.62 + } else { 2.63 + int *data = (int*)elm->data; 2.64 + if(*data != i) { 2.65 + fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); 2.66 + r--; 2.67 + } 2.68 } 2.69 } 2.70 2.71 @@ -73,7 +87,7 @@ 2.72 dl = ucx_dlist_prepend(dl, &v[4]); 2.73 dl = ucx_dlist_append(dl, &v[4]); 2.74 2.75 - struct test1_data tdata; 2.76 + struct foreach_testdata tdata; 2.77 tdata.i = 0; 2.78 ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata); 2.79 2.80 @@ -108,7 +122,7 @@ 2.81 r--; 2.82 } 2.83 ucx_dlist_free(dl2); 2.84 - 2.85 + 2.86 printf(" Test ucx_dlist_clone\n"); 2.87 dl2 = ucx_dlist_clone(dl, NULL, NULL); 2.88 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { 2.89 @@ -116,8 +130,20 @@ 2.90 r--; 2.91 } 2.92 ucx_dlist_free(dl2); 2.93 - 2.94 - printf(" TODO: test clone with copy\n"); 2.95 + 2.96 + printf(" Test ucx_dlist_clone with copy\n"); 2.97 + dl2 = ucx_dlist_clone(dl, int_cpy, NULL); 2.98 + if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { 2.99 + if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { 2.100 + fprintf(stderr, "ucx_dlist_clone (with copy) failed (compare)\n"); 2.101 + r--; 2.102 + } 2.103 + } else { 2.104 + fprintf(stderr, "ucx_dlist_clone (with copy) failed (identity)\n"); 2.105 + r--; 2.106 + } 2.107 + ucx_dlist_foreach(dl, dlist_free_content, NULL); 2.108 + ucx_dlist_free(dl2); 2.109 2.110 ucx_dlist_free(dl); 2.111
3.1 --- a/ucx/list.c Sun Jan 15 14:20:25 2012 +0100 3.2 +++ b/ucx/list.c Wed Feb 08 23:43:02 2012 +0100 3.3 @@ -135,4 +135,4 @@ 3.4 } 3.5 } 3.6 return l; 3.7 -} 3.8 \ No newline at end of file 3.9 +}
4.1 --- a/ucx/string.c Sun Jan 15 14:20:25 2012 +0100 4.2 +++ b/ucx/string.c Wed Feb 08 23:43:02 2012 +0100 4.3 @@ -34,6 +34,7 @@ 4.4 sstr_t str = va_arg(ap, sstr_t); 4.5 size += str.length; 4.6 } 4.7 + va_end(ap); 4.8 4.9 return size; 4.10 } 4.11 @@ -48,6 +49,7 @@ 4.12 s.ptr = strncat (s.ptr, str.ptr, s.length); 4.13 str = va_arg (ap, sstr_t); 4.14 } 4.15 + va_end(ap); 4.16 4.17 return s; 4.18 } 4.19 @@ -62,6 +64,7 @@ 4.20 sstr_t str = va_arg (ap, sstr_t); 4.21 s.ptr = strncat (s.ptr, str.ptr, s.length); 4.22 } 4.23 + va_end(ap); 4.24 4.25 return s; 4.26 } 4.27 @@ -89,11 +92,13 @@ 4.28 4.29 sstr_t sstrdub(sstr_t s) { 4.30 sstr_t newstring; 4.31 - newstring.ptr = malloc(s.length + 1); 4.32 - newstring.length = s.length; 4.33 - newstring.ptr[newstring.length] = 0; 4.34 + newstring.ptr = (char*) malloc(s.length + 1); 4.35 + if (newstring.ptr != NULL) { 4.36 + newstring.length = s.length; 4.37 + newstring.ptr[newstring.length] = 0; 4.38 4.39 - memcpy(newstring.ptr, s.ptr, s.length); 4.40 + memcpy(newstring.ptr, s.ptr, s.length); 4.41 + } 4.42 4.43 return newstring; 4.44 }