diff options
author | Dmitri Pal <dpal@redhat.com> | 2010-11-25 17:36:36 -0500 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2010-12-21 11:16:08 -0500 |
commit | 8c201905d5f0720b62d036eb2308f81f4530cfad (patch) | |
tree | 3a556b7c4881e04bbd266a92d322005918223b0a /refarray/ref_array_ut.c | |
parent | 86564695945fa1892fe990ba8b7bbc1af70cc48a (diff) | |
download | ding-libs-8c201905d5f0720b62d036eb2308f81f4530cfad.tar.gz ding-libs-8c201905d5f0720b62d036eb2308f81f4530cfad.tar.xz ding-libs-8c201905d5f0720b62d036eb2308f81f4530cfad.zip |
Adding ref_array copy method
* Added a method to copy ref array
* Added unit test to check copy functionality
* Took advantage of the new trace macros
* Reworked the debug function to be able to
trace arrays with different types of data.
Diffstat (limited to 'refarray/ref_array_ut.c')
-rw-r--r-- | refarray/ref_array_ut.c | 210 |
1 files changed, 197 insertions, 13 deletions
diff --git a/refarray/ref_array_ut.c b/refarray/ref_array_ut.c index 6e6009a..5347634 100644 --- a/refarray/ref_array_ut.c +++ b/refarray/ref_array_ut.c @@ -36,7 +36,7 @@ int verbose = 0; if (verbose) foo; \ } while(0) -extern void ref_array_debug(struct ref_array *ra); +extern void ref_array_debug(struct ref_array *ra, int num); typedef int (*test_fn)(void); @@ -65,7 +65,7 @@ int ref_array_basic_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); error = ref_array_append(ra, &line1); if (error) { @@ -74,7 +74,7 @@ int ref_array_basic_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); error = ref_array_append(ra, &line2); if (error) { @@ -83,7 +83,7 @@ int ref_array_basic_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); error = ref_array_append(ra, &line3); if (error) { @@ -92,7 +92,7 @@ int ref_array_basic_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); error = ref_array_append(ra, &line4); if (error) { @@ -101,7 +101,7 @@ int ref_array_basic_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); error = ref_array_append(ra, &line5); if (error) { @@ -110,7 +110,7 @@ int ref_array_basic_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); error = ref_array_append(ra, &line6); if (error) { @@ -119,7 +119,7 @@ int ref_array_basic_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); RAOUT(printf("\n\nTest 1 - Printing lines.\n\n")); @@ -247,7 +247,7 @@ int ref_array_free_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); str = strdup(line1); @@ -258,7 +258,7 @@ int ref_array_free_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); str = strdup(line2); @@ -269,7 +269,7 @@ int ref_array_free_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); str = strdup(line3); @@ -280,7 +280,7 @@ int ref_array_free_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); str = strdup(line4); @@ -291,7 +291,7 @@ int ref_array_free_test(void) return error; } - RAOUT(ref_array_debug(ra)); + RAOUT(ref_array_debug(ra, 0)); i = 0; for (;;) { @@ -560,7 +560,189 @@ int ref_array_adv_test(void) return EOK; } +int copy_cb(void *elem, + void *new_elem) +{ + char *ne = NULL; + + ne = strdup(*((char **)elem)); + *((char **)new_elem) = ne; + + RAOUT(printf("Source: %s\nCopy:%s\n", *((char **)elem), ne)); + + + return EOK; +} + +int ref_array_copy_test(void) +{ + const char *line1 = "line1"; + const char *line2 = "line2"; + const char *line3 = "line3"; + const char *line4 = "line4"; + const char *line5 = "line5"; + const char *line6 = "line6"; + uint32_t i; + struct ref_array *ra; + struct ref_array *ra2; + int error = EOK; + uint32_t len = 0; + char text[] = "Deleting: "; + + error = ref_array_create(&ra, sizeof(char *), 1, NULL, NULL); + if (error) { + printf("Failed to create array %d\n", error); + return error; + } + + RAOUT(ref_array_debug(ra, 0)); + + error = ref_array_append(ra, &line1); + if (error) { + ref_array_destroy(ra); + printf("Failed to append to array line 1 %d\n", error); + return error; + } + + RAOUT(ref_array_debug(ra, 0)); + + error = ref_array_append(ra, &line2); + if (error) { + ref_array_destroy(ra); + printf("Failed to append to array line 2 %d\n", error); + return error; + } + + RAOUT(ref_array_debug(ra, 0)); + + error = ref_array_append(ra, &line3); + if (error) { + ref_array_destroy(ra); + printf("Failed to append to array line 3 %d\n", error); + return error; + } + + RAOUT(ref_array_debug(ra, 0)); + + error = ref_array_append(ra, &line4); + if (error) { + ref_array_destroy(ra); + printf("Failed to append to array line 4 %d\n", error); + return error; + } + + RAOUT(ref_array_debug(ra, 0)); + + error = ref_array_append(ra, &line5); + if (error) { + ref_array_destroy(ra); + printf("Failed to append to array line 5 %d\n", error); + return error; + } + + RAOUT(ref_array_debug(ra, 0)); + + error = ref_array_append(ra, &line6); + if (error) { + ref_array_destroy(ra); + printf("Failed to append to array line 6 %d\n", error); + return error; + } + + RAOUT(ref_array_debug(ra, 0)); + + RAOUT(printf("\n\nCopy lines.\n\n")); + + error = ref_array_copy(ra, copy_cb, array_cleanup, (char *)text, &ra2); + if (error) { + ref_array_destroy(ra); + printf("Failed to copy array %d\n", error); + return error; + } + + for (i = 0; i < len; i++) { + if (strcmp(ref_array_get(ra, i, NULL), + ref_array_get(ra2, i, NULL)) != 0) { + printf("\nRetrieved strings were expected to be same,\n"); + printf("but they are not:\n"); + printf("First:[%s]\nSecond:[%s]\n", + (char *)ref_array_get(ra, i, NULL), + (char *)ref_array_get(ra2, i, NULL)); + ref_array_destroy(ra); + ref_array_destroy(ra2); + return EFAULT; + } + } + + RAOUT(printf("\n\nSource array.\n\n")); + RAOUT(ref_array_debug(ra, 0)); + ref_array_destroy(ra); + + RAOUT(printf("\n\nAbout to destroy a copy.\n\n")); + RAOUT(ref_array_debug(ra2, 0)); + ref_array_destroy(ra2); + + RAOUT(printf("\n\nDone!!!\n\n")); + return EOK; + +} + +int ref_array_copy_num_test(void) +{ + uint32_t i,j,k; + struct ref_array *ra; + struct ref_array *ra2; + int error = EOK; + uint32_t len = 0; + + error = ref_array_create(&ra, sizeof(uint32_t), 1, NULL, NULL); + if (error) { + printf("Failed to create array %d\n", error); + return error; + } + + RAOUT(ref_array_debug(ra, 1)); + + for (i=0; i<5; i++) { + error = ref_array_append(ra, &i); + if (error) { + ref_array_destroy(ra); + printf("Failed to append number to array %d\n", error); + return error; + } + + RAOUT(ref_array_debug(ra, 1)); + } + RAOUT(printf("\n\nCopy num test.\n\n")); + + error = ref_array_copy(ra, NULL, NULL, NULL, &ra2); + if (error) { + ref_array_destroy(ra); + printf("Failed to copy array %d\n", error); + return error; + } + + for (i = 0; i < len; i++) { + j = *((uint32_t *)(ref_array_get(ra, i, NULL))); + k = *((uint32_t *)(ref_array_get(ra2, i, NULL))); + if (j != k) { + printf("\nRetrieved values were expected to be same,\n"); + printf("but they are not:\n"); + printf("First:[%d]\nSecond:[%d]\n", j, k); + ref_array_destroy(ra); + ref_array_destroy(ra2); + return EFAULT; + } + } + + ref_array_destroy(ra); + ref_array_destroy(ra2); + + RAOUT(printf("\n\nDone!!!\n\n")); + return EOK; + +} /* Main function of the unit test */ int main(int argc, char *argv[]) @@ -569,6 +751,8 @@ int main(int argc, char *argv[]) test_fn tests[] = { ref_array_basic_test, ref_array_free_test, ref_array_adv_test, + ref_array_copy_test, + ref_array_copy_num_test, NULL }; test_fn t; int i = 0; |