From: glenrendes Date: Wed, 17 Apr 2019 10:33:33 +0000 (+0200) Subject: da_exist and da_all added, half done testing X-Git-Url: https://git.reasoningtechnology.com/style/static/git-favicon.png?a=commitdiff_plain;h=6065fa2182af9ae43527c0601536e543b41a2f39;p=subu da_exist and da_all added, half done testing --- diff --git a/module/da/src/da.lib.c b/module/da/src/da.lib.c index e10ebae..8535d4c 100644 --- a/module/da/src/da.lib.c +++ b/module/da/src/da.lib.c @@ -110,7 +110,7 @@ void da_map(Da *dap, void f(void *, void *), void *closure){ } } -void test_map(void *pt, void *closure){ +void da_test_map(void *pt, void *closure){ bool *f1 = (bool *)closure; *f1 = true; } @@ -260,4 +260,64 @@ void da_cat(Da *dap0, Da *dap1){ memcpy(dap0->base + dap0_size, dap1->base, dap1_size); } +//array of Das +//∃, OR map +//checks that some Da is present in Da of Das + +typedef struct{ + Da *da; + bool found; +} da_exists_closure; + +void da_present(void *da_el, void *closure){ + Da *da_element = (Da *)da_el; + da_exists_closure *dd = (da_exists_closure *)closure; + Da *test_element = dd->da; + if (dd->found) return; + dd->found = da_equal(da_element, test_element); + return; +}//may need to be static? + +bool da_equal(Da *da_el, Da *test_el){ + bool f1 = da_el->base == test_el->base; + bool f2 = da_el->end == test_el->end; + bool f3 = da_el->size == test_el->size; + bool f4 = da_el->element_size == test_el->element_size; + return f1 && f2 && f3 && f4; +} + +bool da_exists (Da **dar, int dar_size, Da *dap){ + da_exists_closure dec; + Da *da_first = *dar; + dec.da = dap; + dec.found = false; + da_big_map(dar, dar_size, da_present, &dec); + return dec.found; +} + +void da_big_map(Da **dar, int dar_size, void f(void *, void *), void *closure){ + Da *pt = *dar; + int i = 0; + while( i < dar_size ){ + f(pt, closure); + pt++; + i++; + } + return; +} + +//∀, AND map +//checks that all Das are present in Da of Das +bool da_all (Da **dar, int dar_size, Da **dap){ + da_exists_closure dec; + dec.da = *dap; + dec.found = true; + int i = 0; + while(dec.found && (i < dar_size)){ + da_big_map(dar, dar_size, da_present, &dec); + dec.da++; + i++; + } + return dec.found; +} diff --git a/module/da/src/da.lib.h b/module/da/src/da.lib.h index d1bc80c..218d048 100644 --- a/module/da/src/da.lib.h +++ b/module/da/src/da.lib.h @@ -31,7 +31,7 @@ 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_test_map(void *pt, void *closure); void da_free_elements(Da *dap); @@ -49,5 +49,11 @@ void da_string_push(Da *dap0, char *string); void da_cat(Da *dap_base, Da *dap_cat); +void da_present(void *da_el, void *closure); +bool da_equal(Da *da_el, Da *test_el); +void da_big_map(Da **dar, int dar_size, void f(void *,void*), void *closure); +bool da_exists(Da **dar, int dar_size, Da *dap); +bool da_all(Da **dar, int dar_size, Da **dap); + #endif diff --git a/module/da/test/src/test_da.cli.c b/module/da/test/src/test_da.cli.c index 4081cd6..ab50ed9 100644 --- a/module/da/test/src/test_da.cli.c +++ b/module/da/test/src/test_da.cli.c @@ -24,6 +24,8 @@ int main(){ test_da_rebase_0, test_da_boundq_0, test_da_map_0, + test_da_exists_0, + test_da_all_0, NULL}; char *test_names[] = { @@ -38,6 +40,8 @@ int main(){ "test_da_rebase_0", "test_da_boundq_0", "test_da_map_0", + "test_da_exists_0", + "test_da_all_0", NULL}; // call tests diff --git a/module/da/test/src/test_da.lib.c b/module/da/test/src/test_da.lib.c index d198210..f34d953 100644 --- a/module/da/test/src/test_da.lib.c +++ b/module/da/test/src/test_da.lib.c @@ -367,10 +367,10 @@ bool test_da_map_0(){ bool *closure; bool result = true; - {//tests map via test_map + {//tests map via da_test_map int i = 0; while (result && i<4){ - da_map(&dar, test_map, closure); + da_map(&dar, da_test_map, closure); result = *closure; i++; *closure = false; @@ -379,7 +379,56 @@ bool test_da_map_0(){ return result; } + +//tests da_exists +bool test_da_exists_0(){ + Da *dap0; + { + da_alloc(dap0, sizeof(char)); + //char a = 'y'; + //da_push(dap0, &a); + } + + Da *dap1; + { + da_alloc(dap1, sizeof(char)); + //char a = 'u'; + //da_push(dap1, &a); + } + + Da *dap2; + { + da_alloc(dap2, sizeof(char)); + //char a = 'n'; + //da_push(dap2, &a); + } + + void *darr[3]; + int dar_size = 0; + + //add dap0, dap1 to darr + darr[dar_size] = dap0; dar_size++; + darr[dar_size] = dap1; dar_size++; + + //test that dap0 and dap1 exist in darr but not dap2 + bool f1 = da_exists((Da **)darr, dar_size, dap0); + bool f2 = da_exists((Da **)darr, dar_size, dap1); + bool f3 = !da_exists((Da **)darr, dar_size, dap2); + + //add dap2 to darr + darr[dar_size] = dap2; dar_size++; + + //test that dap2 exists in darr + bool f4 = da_exists((Da **)darr, dar_size, dap2); + + return f1 && f2 && f3 && f4; +} + +//tests da_all +bool test_da_all_0(){ + return true; +} /* @@ -397,7 +446,7 @@ da_push_alloc -da_push -da_pop da_endq --da_map Had to put test_map function in da src code... +-da_map Had to put da_test_map function in da src code... da_free_elements da_ints_print da_strings_print @@ -407,13 +456,16 @@ da_strings_set_union -da_string_input da_string_push -da_cat +da_exists +da_all + test first -map -boundq +-map +-boundq add -da_exists (OR map, ∃) -da_all (AND map, ∀) +-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 c5a6264..533be98 100644 --- a/module/da/test/src/test_da.lib.h +++ b/module/da/test/src/test_da.lib.h @@ -13,5 +13,7 @@ bool test_da_index_0(); bool test_da_rebase_0(); bool test_da_boundq_0(); bool test_da_map_0(); +bool test_da_exists_0(); +bool test_da_all_0(); #endif