From: glenrendes Date: Tue, 16 Apr 2019 18:48:53 +0000 (+0200) Subject: a few new tests for da X-Git-Url: https://git.reasoningtechnology.com/style/rt_dark_doc.css?a=commitdiff_plain;h=89916cb698829d4fe198e5be917a43e546ec92ba;p=subu a few new tests for da --- diff --git a/module/da/doc/da_lib_doc.txt b/module/da/doc/da_lib_doc.txt index 11d20e8..a98f53c 100644 --- a/module/da/doc/da_lib_doc.txt +++ b/module/da/doc/da_lib_doc.txt @@ -81,16 +81,16 @@ bool da_pop - The primary goal of this function is to pop the end pointer of a D - It returns the flag bool value to confirm whether or not a pop was performed. void da_map - The primary goal of this function is to create closure for the Da. It takes the results of functions for all the elements of a Da and copies them to a closure pointer to keep them at global scope even after exiting the namespace the function is in. -- This function takes a Da pointer called cap, a void function pointer which takes two void pointer as arguments, and a void pointer called closure. +- This function takes a Da pointer called dap, a pointer to a void function which takes two void pointers as arguments, and a void pointer called closure. - The arguments of f are intended to be an element pointer and another arg pointer. - It initializes a char pointer called pt with the address of the base pointer of dap. -- As long as pt does not equal the end pointer of dap, it passes pt and closure as arguments to f. Then it adds a dap element_size worth to the pt address. +- As long as pt does not equal the end pointer of dap, it passes pt and closure as arguments to f (which is intended to be da_free_element as passed in by da_free_elements). Then it adds a dap element_size worth to the pt address. static void da_free_element - The primary goal of this function is to free up space taken by a Da becuase these lists "are sometimes used as resource managers." It is one substep in the process, which is used by the da_map function during a closure procedure to free up the pointer. - This function takes two void pointers called pt and closure as arguments. - It frees the memory at the address of pt. -void da_free_element - The primary goal of this function is to free up space taken by a Da because these lists "are sometimes used as resource managers." It is the main call for this function, sending the dap through the da_free_element function for every element and then rewinding the dap. This frees up the memory without deleting the Da itself. +void da_free_elements - The primary goal of this function is to free up space taken by a Da because these lists "are sometimes used as resource managers." It is the main call for this function, sending the dap through the da_free_element function for every element and then rewinding the dap. This frees up the memory without deleting the Da itself. - This function takes a Da called dap as an argument. - It sends the dap pointer, the function da_free_element and the closure pointer of NULL to da_map. - It then sends dap to da_rewind. @@ -110,7 +110,7 @@ void da_ints_print void da_cat - The primary goal of this function is to concatenate the contents of one array onto another. It adds it right to the end of where the last started, doubling if need be. "If dap0 has had a terminatating zero added, that must be popped off before the call. Similarly if a terminating zero is desired, it should be pushed after the call." - This function takes two Da pointers called dap0 and dap1 as arguments. -- It returns if the end pointer of dap 1 already point to the same address as the end pointer of dap0. This prevents redundant concatenation. +- It returns if the end pointer of dap 1 already points to the same address as the end pointer of dap0. This prevents redundant concatenation. - It initializes a size_t called dap0_size with the difference between the end and base of dap0, then does the same thing for dap1. - It adds the size of dap1 to the end pointer of dap0. - If the end pointer of dap0 has now run off the edge of dap0's allocated size, it expands/doubles dap0 to accomodate. diff --git a/module/da/makefile-flags b/module/da/makefile-flags index 25ae25d..308602e 100644 --- a/module/da/makefile-flags +++ b/module/da/makefile-flags @@ -23,7 +23,7 @@ ECHO= echo # compiler and flags C=gcc -CFLAGS=-std=gnu11 -fPIC -I$(SRCDIR) -I$(INCDIR) -I$(SHAREDIR)/include -ggdb -Werror -DDEBUG -DDEBUGDB +CFLAGS=-std=gnu11 -fPIC -I$(SRCDIR) -I$(INCDIR) -I$(SHAREDIR)/include -ggdb -O0 -Werror -DDEBUG -DDEBUGDB LINKFLAGS=-L$(LIBDIR) -l$(MODULE) diff --git a/module/da/src/da.lib.c b/module/da/src/da.lib.c index 543e95d..e10ebae 100644 --- a/module/da/src/da.lib.c +++ b/module/da/src/da.lib.c @@ -2,7 +2,6 @@ Dynamic Array */ - #include "da.lib.h" #include @@ -57,7 +56,7 @@ char *da_expand(Da *dap){ // true when end has run off the allocated area bool da_boundq(Da *dap){ - return dap->end >= dap->base + dap->size; + return dap->end > (dap->base + dap->size); } //-------------------------------------------------------------------------------- @@ -111,6 +110,12 @@ void da_map(Da *dap, void f(void *, void *), void *closure){ } } +void test_map(void *pt, void *closure){ + bool *f1 = (bool *)closure; + *f1 = true; +} + + //-------------------------------------------------------------------------------- // da being used as a resource manager @@ -119,6 +124,7 @@ void da_map(Da *dap, void f(void *, void *), void *closure){ static void da_free_element(void *pt, void *closure){ free(*(char **)pt); // free does not care about the pointer type } + void da_free_elements(Da *dap){ da_map(dap, da_free_element, NULL); da_rewind(dap); @@ -161,6 +167,17 @@ typedef struct { char *string; bool found; } da_strings_exists_closure; +static void string_equal(void *sp, void *closure){ + const char *string_element = *(char **)sp; + da_strings_exists_closure *ss = (da_strings_exists_closure *)closure; + const char *str = ss->string; + if( ss->found ) return; + ss->found = !strcmp(string_element, str); + return; +} +//added consts to see if it was a strcmp syntax issue + +/*original static void string_equal(void *sp, void *closure){ char *string_element = *(char **)sp; da_strings_exists_closure *ss = (da_strings_exists_closure *)closure; @@ -168,6 +185,8 @@ static void string_equal(void *sp, void *closure){ ss->found = !strcmp(string_element, ss->string); return; } +*/ + bool da_strings_exists(Da *string_arrp, char *test_string){ da_strings_exists_closure sec; sec.string = test_string; diff --git a/module/da/src/da.lib.h b/module/da/src/da.lib.h index 8195c4a..d1bc80c 100644 --- a/module/da/src/da.lib.h +++ b/module/da/src/da.lib.h @@ -31,12 +31,14 @@ bool da_pop(Da *dap, void *element); bool da_endq(Da *dap, void *pt); void da_map(Da *dap, void f(void *, void *), void *closure); - +void test_map(void *pt, void *closure); + void da_free_elements(Da *dap); void da_ints_print(Da *dap, char *sep); void da_strings_print(Da *dap, char *sep); + bool da_strings_exists(Da *string_arrp, char *test_string); void da_strings_set_insert(Da *string_arrp, char *proffered_string, void destruct(void *)); void da_strings_set_union(Da *string_arrp, Da *proffered_string_arrp, void destruct(void *)); diff --git a/module/da/test/src/test_da.cli.c b/module/da/test/src/test_da.cli.c index 6023121..4081cd6 100644 --- a/module/da/test/src/test_da.cli.c +++ b/module/da/test/src/test_da.cli.c @@ -4,15 +4,41 @@ #include "test_da.lib.h" int main(){ - bool da_0_passed = test_da_0(); + bool da_0_passed = test_da_push_0(); unsigned int passed = 0; unsigned int failed = 0; // enumeration of tests typedef bool (*test_fun)(); - test_fun tests[] = {test_da_0, test_da_1, test_da_2, test_da_3, test_da_4, test_da_5, NULL}; - char *test_names[] = {"test_da_0", "test_da_1", "test_da_2", "test_da_3", "test_da_4", "test_da_5", NULL}; + test_fun tests[] = + { + test_da_push_0, + test_da_expand_0, + test_da_string_input_0, + test_da_pop_0, + test_da_cat_0, + test_da_cat_1, + test_da_rewind_0, + test_da_index_0, + test_da_rebase_0, + test_da_boundq_0, + test_da_map_0, + NULL}; + char *test_names[] = + { + "test_da_push_0", + "test_da_expand_0", + "test_da_string_input_0", + "test_da_pop_0", + "test_da_cat_0", + "test_da_cat_1", + "test_da_rewind_0", + "test_da_index_0", + "test_da_rebase_0", + "test_da_boundq_0", + "test_da_map_0", + NULL}; // call tests test_fun *tfp = tests; diff --git a/module/da/test/src/test_da.lib.c b/module/da/test/src/test_da.lib.c index 08efbbe..d198210 100644 --- a/module/da/test/src/test_da.lib.c +++ b/module/da/test/src/test_da.lib.c @@ -11,7 +11,7 @@ Tests for Da. #include "test_da.lib.h" // tests push -bool test_da_0(){ +bool test_da_push_0(){ Da da; da_alloc(&da, sizeof(int)); // leaves room for 4 ints int i = 0; @@ -39,7 +39,7 @@ bool test_da_0(){ } // tests manual expansion -bool test_da_1(){ +bool test_da_expand_0(){ Da da; da_alloc(&da, sizeof(int)); // leaves room for 4 ints int i = 0; @@ -71,8 +71,8 @@ bool test_da_1(){ return f0 && f1 && f2 && f3; } -// da_fgets -bool test_da_2(){ +// da_fgets via da_string_input +bool test_da_string_input_0(){ char *data_file_name = "../lib/test.dat"; FILE *file = fopen(data_file_name,"r"); @@ -106,8 +106,8 @@ bool test_da_2(){ return f0 && f1 && f2 && f3; } -// da_fgets -bool test_da_3(){ +// da_pop +bool test_da_pop_0(){ Da da; da_alloc(&da, sizeof(int)); @@ -129,7 +129,7 @@ bool test_da_3(){ } // da_cat -bool test_da_4(){ +bool test_da_cat_0(){ Da da0, da1; da_alloc(&da0, sizeof(int)); @@ -165,32 +165,255 @@ bool test_da_4(){ return result; } +//Glenda: this was already tested much better above, where each element was actually tested, not just one, but it was a good one to test for practice +bool test_da_cat_1(){ + Da dar0; + Da dar1; + da_alloc(&dar0, sizeof(int)); + da_alloc(&dar1, sizeof(int)); + int a = 2; + for (int b=0; b<4; b++){ + da_push(&dar0, &a); + a+=2; + } + size_t off0 = dar0.end - dar0.base; + bool f1 = dar0.base != dar0.end; + for (int b=0; b<4; b++){ + da_push(&dar1, &a); + a+=2; + } + size_t off1 = dar1.end - dar1.base; + bool f2 = dar1.base != dar1.end; + da_cat(&dar0, &dar1); + bool f3 = dar0.end == dar0.base + off0 +off1; + bool f4 = *(dar0.base + (5*dar0.element_size)) == *(dar1.base + dar1.element_size); + return f1 && f2 && f3 && f4; +} + + //Glenda's tests +//tested output for success and artificial failure //tests da_rewind -bool test_da_5(){ - int i = 0; - Da dap; - da_alloc(&dap, sizeof(int)); - while(i < 8){ - da_push(&dap, &i); +bool test_da_rewind_0(){ + int i = 10; + Da dar; + da_alloc(&dar, sizeof(int)); + while(i < 18){ + da_push(&dar, &i); ++i; } - bool f = dap.end != dap.base; - da_rewind(&dap); - int j = *(dap.end); - bool g = dap.end == dap.base; - bool h = j == 0; - bool result = f && g && h; + bool f1 = dar.end != dar.base; + da_rewind(&dar); + int j = *(dar.end); + bool f2 = dar.end == dar.base; + bool f3 = j == 10; + // bool f4 = *dar.end == 10 && *(dar.end + 9 * sizeof(int)) = 19; + bool result = f1 && f2 && f3; // && f4; return result; } -/* //tests da_index -bool test_da_6(){ - Da dap; - da_alloc(&dap, sizeof(int)); - int i = 0; - da_push(& +bool test_da_index_0(){ + Da dar; + da_alloc(&dar, sizeof(int)); + int j = 0; + char *k[2]; + while(j < 4){ + da_push(&dar, &j); + j++; + } + j--; + k[0] = da_index(&dar, j); + bool f1 = k[0] == dar.base + (3*dar.element_size); + return f1; +} + +//tests da_free_elements +//cannot test sub-functions bc da_free_elements needs to be static +/*bool test_da_7(){ + Da dar; + da_alloc(&dar, sizeof(int)); + int j = 0; + while(j < 4){ + da_push(&dar, &j); + ++j; + } + da_free_elements(&dar); + bool g = 1; + return g; + } +causes errors: +test_da_2, could not open data file ../lib/test.dat for reading +test_da_2 failed - these were because in wrong directory, core dump is actual issue here and with 7 +Segmentation fault (core dumped) +*/ +/* +//tests da_strings_exist +bool test_da_7(){ + Da dar; + da_alloc(&dar, sizeof(char *)); + char *j = "test"; + da_push(&dar, j); + // da_strings_exists(&dar, j); + bool f1 = da_strings_exists(&dar, j); + return f1; +} +//same errors, something to do with calling fucntions that call static functions I believe +*/ + +//tests rebase +bool test_da_rebase_0(){ + Da dar; + da_alloc(&dar,sizeof(char)); + + char **el_pt = (char **)malloc(4*(sizeof(char*))); + {//push 2-5 into dar, leave with pointer to last element + int i = 0; + char arr[4] = {'t','e','s','t'}; + while(i<4){ + char c = arr[i]; + *el_pt = da_push(&dar, &c); + el_pt++; + i++; + } + el_pt--; + } + + //check that push worked, that element pointer is in right place, and that expand works + // + bool f1 = dar.base != dar.end; + bool f2 = *el_pt == dar.end - dar.element_size; + + char *obase = dar.base; + char *old_base = da_expand(&dar); + bool f3 = obase == old_base; + + bool f4[4]; + {//check that each pointer is properly rebased + int i = 0; + while(i<4 && *el_pt >= old_base){ + size_t old_offset = *el_pt - old_base; + da_rebase(&dar, old_base, el_pt); + size_t new_offset = *el_pt - dar.base; + f4[i] = old_offset == new_offset; + el_pt--; + i++; + } + } + + bool result = f4[0]; + {//now check that all pointers are properly rebased + int i = 1; + while(result && i < 4){ + result = f4[i]; + i++; + } + } + + return f1 && f2 && f3 && result; +} + + +//tests da_boundq +bool test_da_boundq_0(){ + Da dar; + da_alloc(&dar,sizeof(char)); + + bool f[5]; + {//pushes onto dar and tests no runoff + char arr[4] = {'r','e','s','t'}; + int i = 0; + char c; + while(i<4){ + c = arr[i]; + da_push(&dar, &c); + f[i] = (dar.end != dar.base) && !da_boundq(&dar); + i++; + } + } + + //makes end pointer run off allocated area, tests boundq detects runoff + dar.end++; // = dar.base + dar.size; + f[4] = da_boundq(&dar); + + bool result = true; + {//check all test results + int i = 0; + while (result && i < 5){ + result = f[i]; + i++; + } + } + + return result; +} + +//tests map +bool test_da_map_0(){ + Da dar; + da_alloc(&dar,sizeof(char)); + + {//pushes onto dar + char arr[4] = {'t','r','u','e'}; + int i = 0; + char c; + while(i<4){ + c = arr[i]; + da_push(&dar, &c); + i++; + } + } + + bool *closure; + bool result = true; + {//tests map via test_map + int i = 0; + while (result && i<4){ + da_map(&dar, test_map, closure); + result = *closure; + i++; + *closure = false; + } + } + + return result; } + + + +/* + Functions +da_alloc +da_free +-da_rewind +da_emptyq +da_length +-da_rebase +-da_expand +-da_boundq +-da_index +da_push_alloc +-da_push +-da_pop +da_endq +-da_map Had to put test_map function in da src code... +da_free_elements +da_ints_print +da_strings_print +da_strings_exists +da_strings_set_insert +da_strings_set_union +-da_string_input +da_string_push +-da_cat + +test first +map +boundq + +add +da_exists (OR map, ∃) +da_all (AND map, ∀) + */ diff --git a/module/da/test/src/test_da.lib.h b/module/da/test/src/test_da.lib.h index 0fd98db..c5a6264 100644 --- a/module/da/test/src/test_da.lib.h +++ b/module/da/test/src/test_da.lib.h @@ -1,12 +1,17 @@ #ifndef DA_LIB_H #define DA_LIB_H -bool test_da_0(); -bool test_da_1(); -bool test_da_2(); -bool test_da_3(); -bool test_da_4(); -bool test_da_5(); -bool test_da_6(); + +bool test_da_push_0(); +bool test_da_expand_0(); +bool test_da_string_input_0(); +bool test_da_pop_0(); +bool test_da_cat_0(); +bool test_da_cat_1(); +bool test_da_rewind_0(); +bool test_da_index_0(); +bool test_da_rebase_0(); +bool test_da_boundq_0(); +bool test_da_map_0(); #endif