From 374f8264350bfc2b39e9325e30c8701d50080d7e Mon Sep 17 00:00:00 2001 From: glenrendes Date: Wed, 17 Apr 2019 18:48:31 +0200 Subject: [PATCH] da_exists and da_all with tests --- module/da/src/da.lib.c | 20 ++++----- module/da/test/src/test_da.lib.c | 71 ++++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/module/da/src/da.lib.c b/module/da/src/da.lib.c index 8535d4c..191fc05 100644 --- a/module/da/src/da.lib.c +++ b/module/da/src/da.lib.c @@ -289,7 +289,6 @@ bool da_equal(Da *da_el, Da *test_el){ 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); @@ -297,10 +296,10 @@ bool da_exists (Da **dar, int dar_size, Da *dap){ } void da_big_map(Da **dar, int dar_size, void f(void *, void *), void *closure){ - Da *pt = *dar; + Da **pt = dar; int i = 0; while( i < dar_size ){ - f(pt, closure); + f(*pt, closure); pt++; i++; } @@ -310,14 +309,15 @@ void da_big_map(Da **dar, int dar_size, void f(void *, void *), void *closure){ //∀, 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; + Da **tdar = dap; + Da *test_da = *tdar; + bool result = true; int i = 0; - while(dec.found && (i < dar_size)){ - da_big_map(dar, dar_size, da_present, &dec); - dec.da++; + while(result && (i < dar_size)){ + result = da_exists(dar, dar_size, test_da); + tdar++; + test_da = *tdar; i++; } - return dec.found; + return result; } diff --git a/module/da/test/src/test_da.lib.c b/module/da/test/src/test_da.lib.c index f34d953..589d774 100644 --- a/module/da/test/src/test_da.lib.c +++ b/module/da/test/src/test_da.lib.c @@ -385,25 +385,25 @@ bool test_da_exists_0(){ Da *dap0; { da_alloc(dap0, sizeof(char)); - //char a = 'y'; - //da_push(dap0, &a); + char a = 'y'; + da_push(dap0, &a); } Da *dap1; { da_alloc(dap1, sizeof(char)); - //char a = 'u'; - //da_push(dap1, &a); + char a = 'u'; + da_push(dap1, &a); } Da *dap2; { da_alloc(dap2, sizeof(char)); - //char a = 'n'; - //da_push(dap2, &a); + char a = 'n'; + da_push(dap2, &a); } - void *darr[3]; + Da *darr[3]; int dar_size = 0; //add dap0, dap1 to darr @@ -412,9 +412,9 @@ bool test_da_exists_0(){ //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); + bool f1 = da_exists(darr, dar_size, dap0); + bool f2 = da_exists(darr, dar_size, dap1); + bool f3 = !da_exists(darr, dar_size, dap2); //add dap2 to darr darr[dar_size] = dap2; dar_size++; @@ -427,9 +427,56 @@ bool test_da_exists_0(){ //tests da_all bool test_da_all_0(){ - return true; -} + 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); + } + + Da **darr0 = malloc(3 * sizeof(Da *)); + int dar_size0 = 0; + + //add dap0, dap1 to darr0 (array being tested) + darr0[dar_size0] = dap0; dar_size0++; + darr0[dar_size0] = dap1; dar_size0++; + darr0[dar_size0] = dap0; + //has to have same amount of elements as test array or will core dump + + + Da **darr1 = malloc(3 * sizeof(Da *)); + int dar_size1 = 0; + //add dap0,1,2 to darr1 (test array, to test against) + darr1[dar_size1] = dap0; dar_size1++; + darr1[dar_size1] = dap1; dar_size1++; + darr1[dar_size1] = dap2; dar_size1++; + + //tests that darr0 doesn't have all (dap0,1,2) + bool f1 = !da_all(darr0, dar_size1, darr1); + + //add dap2 to darr0 + darr0[dar_size0] = dap2; dar_size0++; + + //tests that darr0 has all (dap0,1,2) + bool f2 = da_all(darr0, dar_size1, darr1); + + return f1 && f2; +} + /* Functions -- 2.20.1