From bc83fe43e542d10eae0f40449ac8fd84edebe78c Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 29 Mar 2018 20:01:45 +0200 Subject: [PATCH] Remove dependency on GMP, so that it is easier to run the bench suite. --- tests/Makefile | 3 +- tests/mympz.h | 97 ++++++++++++++++++++++++++++++++++++------- tests/test-marray.c | 14 +++---- tests/test-mcore.c | 6 +-- tests/test-mdeque.c | 2 +- tests/test-mlist.c | 34 +++++++-------- tests/test-mshared.c | 16 +++---- tests/test-mtuple.c | 28 ++++++------- tests/test-mvariant.c | 16 +++---- 9 files changed, 142 insertions(+), 74 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 1b034992..1d58e25b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,10 +1,9 @@ -#NOTE: Neither GMP not pthread is not a real dependency. It is only used for the test suite. CC=cc -std=c99 WCFLAGS=-Wall -W -pedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wswitch-default -Wswitch-enum -Wcast-align -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 -Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wunreachable-code -Wlogical-op -Wstrict-aliasing=2 -Wredundant-decls -Wold-style-definition -Wno-unused-function -ftrapv XCFLAGS= CFLAGS=$(WCFLAGS) $(XCFLAGS) CPPFLAGS=-I.. -LDFLAGS=-pthread -lgmp +LDFLAGS=-pthread RM=rm -f all: diff --git a/tests/mympz.h b/tests/mympz.h index f1810a9d..18a88b3a 100644 --- a/tests/mympz.h +++ b/tests/mympz.h @@ -1,35 +1,104 @@ #ifndef MY_MPZ_T #define MY_MPZ_T -#include #include "m-string.h" -static void my_mpz_out_str(FILE *f, const mpz_t z) +/* This is a trivial encapsulation of an opquae structure. + Used for test purpose only.*/ +typedef struct my_mpz_s { + unsigned int n; + unsigned int a; + unsigned int *ptr; +} my_mpz_t[1]; + +static void my_mpz_init(my_mpz_t z) { - mpz_out_str (f, 10, z); + z->n = 1; + z->a = 1; + z->ptr = calloc(1, sizeof(unsigned int)); + if (!z->ptr) abort(); } -static bool my_mpz_in_str(mpz_t z, FILE *f) +static void my_mpz_clear(my_mpz_t z) { - return mpz_inp_str (z, f, 10) > 0; + free(z->ptr); + z->ptr = NULL; } -static bool my_mpz_equal_p(const mpz_t z1, const mpz_t z2) +static void my_mpz_init_set(my_mpz_t d, const my_mpz_t s) { - return mpz_cmp (z1, z2) == 0; + d->n = s->n; + d->a = s->n; + d->ptr = calloc (s->n, sizeof(unsigned int)); + memcpy(d->ptr, s->ptr, sizeof(unsigned int) * s->n); } -static void my_mpz_str(string_t str, const mpz_t z, bool append) +static void my_mpz_set(my_mpz_t d, const my_mpz_t s) { - char *s = mpz_get_str(NULL, 10, z); - assert (s != NULL); - if (append) string_cat_str (str, s); - else string_set_str (str, s); - free(s); + my_mpz_clear(d); + my_mpz_init_set(d, s); +} + +static void my_mpz_set_ui(my_mpz_t d, unsigned int v) +{ + d->n = 1; + d->ptr[0] = v; +} + +static void my_mpz_init_set_ui(my_mpz_t d, unsigned int v) +{ + my_mpz_init(d); + my_mpz_set_ui(d, v); +} + +static unsigned int my_mpz_get_ui(const my_mpz_t z) +{ + return z->ptr[0]; +} + +static void my_mpz_add(my_mpz_t d, const my_mpz_t a, const my_mpz_t b) +{ + d->ptr[0] = a->ptr[0] + b->ptr[0]; +} + +static void my_mpz_out_str(FILE *f, const my_mpz_t z) +{ + assert(z->n == 1); + fprintf(f, "%u", z->ptr[0]); +} + +static bool my_mpz_in_str(my_mpz_t z, FILE *f) +{ + z->n = 1; + return fscanf(f, "%u", &z->ptr[0]) == 1; +} + +static bool my_mpz_equal_p(const my_mpz_t z1, const my_mpz_t z2) +{ + if (z1->n != z2->n) return false; + return memcmp(z1->ptr, z2->ptr, z1->n*sizeof(unsigned int)) == 0; +} + +static int my_mpz_cmp(const my_mpz_t z1, const my_mpz_t z2) +{ + if (z1->n != z2->n) return z1-> n < z2->n ? -1 : 1; + return memcmp(z1->ptr, z2->ptr, z1->n*sizeof(unsigned int)); +} + +static int my_mpz_cmp_ui(const my_mpz_t z1, unsigned int z2) +{ + if (z1->n != 1) return z1-> n < 1 ? -1 : 1; + return memcmp(z1->ptr, &z2, sizeof(unsigned int)); +} + +static void my_mpz_str(string_t str, const my_mpz_t z, bool append) +{ + if (append) string_cat_printf (str, "%u", z->ptr[0]); + else string_printf (str, "%u", z->ptr[0]); } #define MYMPZ_OPLIST \ - (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear), \ + (INIT(my_mpz_init), INIT_SET(my_mpz_init_set), SET(my_mpz_set), CLEAR(my_mpz_clear), \ OUT_STR(my_mpz_out_str), IN_STR(my_mpz_in_str), EQUAL(my_mpz_equal_p), \ GET_STR(my_mpz_str) ) diff --git a/tests/test-marray.c b/tests/test-marray.c index d87311fb..4b654782 100644 --- a/tests/test-marray.c +++ b/tests/test-marray.c @@ -27,7 +27,7 @@ START_COVERAGE ARRAY_DEF(array_uint, unsigned int) -ARRAY_DEF(array_mpz, mpz_t, MYMPZ_OPLIST) +ARRAY_DEF(array_mpz, my_mpz_t, MYMPZ_OPLIST) END_COVERAGE #define ARRAY_UINT_OPLIST ARRAY_OPLIST(array_uint) @@ -154,11 +154,11 @@ static void test_uint(void) static void test_mpz(void) { array_mpz_t array1, array2; - mpz_t z; + my_mpz_t z; array_mpz_init (array1); array_mpz_init (array2); - mpz_init (z); + my_mpz_init (z); // Test empty FILE *f = fopen ("a-marray.dat", "wt"); @@ -175,7 +175,7 @@ static void test_mpz(void) // Test non empty for(int n = 0; n < 1000; n++) { - mpz_set_ui (z, n); + my_mpz_set_ui (z, n); array_mpz_push_back (array1, z); } @@ -196,18 +196,18 @@ static void test_mpz(void) M_LET(str, STRING_OPLIST) { array_mpz_get_str(str, array1, false); assert (string_cmp_str (str, "[]") == 0); - mpz_set_ui (z, 17); + my_mpz_set_ui (z, 17); array_mpz_push_back (array1, z); array_mpz_get_str(str, array1, false); assert (string_cmp_str (str, "[17]") == 0); - mpz_set_ui (z, 42); + my_mpz_set_ui (z, 42); array_mpz_push_back (array1, z); array_mpz_get_str(str, array1, true); assert (string_cmp_str (str, "[17][17,42]") == 0); // NOTE: Not the same order than list... } - mpz_clear (z); + my_mpz_clear (z); array_mpz_clear(array2); array_mpz_clear(array1); } diff --git a/tests/test-mcore.c b/tests/test-mcore.c index 60763956..f013202b 100644 --- a/tests/test-mcore.c +++ b/tests/test-mcore.c @@ -23,7 +23,7 @@ #include #include -#include +#include "mympz.h" #include "m-core.h" @@ -158,8 +158,8 @@ static void test_map(void) static void test_let(void) { - M_LET(z, M_CLASSIC_OPLIST(mpz)) { - mpz_add(z,z,z); + M_LET(z, M_CLASSIC_OPLIST(my_mpz)) { + my_mpz_add(z,z,z); } } diff --git a/tests/test-mdeque.c b/tests/test-mdeque.c index 5805c0c3..75fee147 100644 --- a/tests/test-mdeque.c +++ b/tests/test-mdeque.c @@ -31,7 +31,7 @@ START_COVERAGE DEQUE_DEF(deque, int) END_COVERAGE -DEQUE_DEF(deque_mpz, mpz_t, MYMPZ_OPLIST) +DEQUE_DEF(deque_mpz, my_mpz_t, MYMPZ_OPLIST) #define OPL DEQUE_OPLIST(deque) diff --git a/tests/test-mlist.c b/tests/test-mlist.c index c8596e50..e9da8ef9 100644 --- a/tests/test-mlist.c +++ b/tests/test-mlist.c @@ -28,10 +28,10 @@ #include "coverage.h" START_COVERAGE LIST_DEF(list_uint, unsigned int) -LIST_DEF(list_mpz, mpz_t, MYMPZ_OPLIST) +LIST_DEF(list_mpz, my_mpz_t, MYMPZ_OPLIST) LIST_DUAL_PUSH_DEF(list2_double, double) END_COVERAGE -LIST_DUAL_PUSH_DEF(list2_mpz, mpz_t, MYMPZ_OPLIST) +LIST_DUAL_PUSH_DEF(list2_mpz, my_mpz_t, MYMPZ_OPLIST) #define LIST_UINT_OPLIST LIST_OPLIST(list_uint) static void test_uint(void) @@ -188,36 +188,36 @@ static void test_uint(void) static void test_mpz(void) { list_mpz_t v; - mpz_t z, x, s; + my_mpz_t z, x, s; - mpz_init (z); - mpz_init (x); - mpz_init (s); + my_mpz_init (z); + my_mpz_init (x); + my_mpz_init (s); list_mpz_init(v); assert (list_mpz_empty_p(v) == true); - mpz_set_ui (z, 2); + my_mpz_set_ui (z, 2); list_mpz_push_back(v, z); assert (list_mpz_empty_p(v) == false); list_mpz_pop_back(&x, v); - assert( mpz_cmp (x, z) == 0); + assert( my_mpz_equal_p (x, z)); assert (list_mpz_empty_p(v) == true); assert (list_mpz_size(v) == 0); for(unsigned int i = 0; i < 1000; i ++) { - mpz_set_ui (z, i); + my_mpz_set_ui (z, i); list_mpz_push_back(v, z); } assert (list_mpz_size(v) == 1000); list_mpz_it_t u; - mpz_set_ui (s, 0); + my_mpz_set_ui (s, 0); for(list_mpz_it(u, v); !list_mpz_end_p(u); list_mpz_next(u)) { - mpz_add (s, s, *list_mpz_cref(u)); + my_mpz_add (s, s, *list_mpz_cref(u)); } - assert (mpz_get_ui(s) == 1000*999/2); + assert (my_mpz_get_ui(s) == 1000*999/2); FILE *f = fopen ("a-mlist.dat", "wt"); if (!f) abort(); @@ -237,11 +237,11 @@ static void test_mpz(void) M_LET(str, STRING_OPLIST) { list_mpz_get_str(str, v, false); assert (string_cmp_str (str, "[]") == 0); - mpz_set_ui (z, 17); + my_mpz_set_ui (z, 17); list_mpz_push_back (v, z); list_mpz_get_str(str, v, false); assert (string_cmp_str (str, "[17]") == 0); - mpz_set_ui (z, 42); + my_mpz_set_ui (z, 42); list_mpz_push_back (v, z); list_mpz_get_str(str, v, true); assert (string_cmp_str (str, "[17][42,17]") == 0); @@ -249,9 +249,9 @@ static void test_mpz(void) list_mpz_clear(v); - mpz_clear (z); - mpz_clear (x); - mpz_clear (s); + my_mpz_clear (z); + my_mpz_clear (x); + my_mpz_clear (s); } static void test_dual_push1(void) diff --git a/tests/test-mshared.c b/tests/test-mshared.c index 63411fb2..ee5c03d3 100644 --- a/tests/test-mshared.c +++ b/tests/test-mshared.c @@ -20,17 +20,17 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include +#include "mympz.h" #include "m-shared.h" #include "coverage.h" START_COVERAGE SHARED_PTR_DEF(shared_int, int) -SHARED_RESOURCE_DEF(shared_ressource, mpz_t, M_CLASSIC_OPLIST(mpz)) +SHARED_RESOURCE_DEF(shared_ressource, my_mpz_t, M_CLASSIC_OPLIST(my_mpz)) END_COVERAGE -SHARED_PTR_DEF(shared_mpz, mpz_t, M_CLASSIC_OPLIST(mpz)) +SHARED_PTR_DEF(shared_mpz, my_mpz_t, M_CLASSIC_OPLIST(my_mpz)) SHARED_PTR_RELAXED_DEF(shared_relaxed_int, int) static int f(const shared_int_t p) @@ -143,18 +143,18 @@ static void test_ressource(int n) for(int i = 0; i < n ; i++) { shared_ressource_it (it[i], ressource); assert (!shared_ressource_end_p(it[i])); - mpz_t *z = shared_ressource_ref(it[i]); + my_mpz_t *z = shared_ressource_ref(it[i]); assert (z != NULL); - mpz_set_ui(*z, i); + my_mpz_set_ui(*z, i); } shared_ressource_it (onemore, ressource); assert (shared_ressource_end_p(onemore)); for(int i = 0; i < n ; i++) { shared_ressource_it_set (copy[i], it[i]); assert (!shared_ressource_end_p(copy[i])); - const mpz_t *z = shared_ressource_cref(copy[i]); + const my_mpz_t *z = shared_ressource_cref(copy[i]); assert (z != NULL); - assert (mpz_cmp_ui(*z, i) == 0); + assert (my_mpz_cmp_ui(*z, i) == 0); } shared_ressource_it (onemore, ressource); assert (shared_ressource_end_p(onemore)); @@ -164,7 +164,7 @@ static void test_ressource(int n) shared_ressource_it (onemore, ressource); assert (shared_ressource_end_p(onemore)); for(int i = n-1; i >= 0 ; i--) { - const mpz_t *z = shared_ressource_cref(copy[i]); + const my_mpz_t *z = shared_ressource_cref(copy[i]); assert (z != NULL); shared_ressource_end (copy[i], ressource); assert (shared_ressource_end_p(copy[i])); diff --git a/tests/test-mtuple.c b/tests/test-mtuple.c index 5cc191b6..50b21213 100644 --- a/tests/test-mtuple.c +++ b/tests/test-mtuple.c @@ -21,28 +21,28 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include -#include - +#include "mympz.h" #include "m-string.h" + #include "m-tuple.h" #include "coverage.h" START_COVERAGE TUPLE_DEF2(pair, (key, string_t, (INIT(string_init), INIT_SET(string_init_set), SET(string_set), CLEAR(string_clear))), - (value, mpz_t, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear) ))) + (value, my_mpz_t, MYMPZ_OPLIST) ) END_COVERAGE // No cmp defined TUPLE_DEF2(triple, (key, string_t, (INIT(string_init), INIT_SET(string_init_set), SET(string_set), CLEAR(string_clear), CMP(string_cmp))), - (value, mpz_t, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear) )), + (value, my_mpz_t, MYMPZ_OPLIST ), (data, const char *, ())) // cmp shall be defined TUPLE_DEF2(pair3, (key, string_t, (INIT(string_init), INIT_SET(string_init_set), SET(string_set), CLEAR(string_clear), CMP(string_cmp), HASH(string_hash), EQUAL(string_equal_p))), - (value, mpz_t, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear), CMP(mpz_cmp) ))) + (value, my_mpz_t, M_OPEXTEND( MYMPZ_OPLIST, CMP(my_mpz_cmp)) ) ) TUPLE_DEF2(pair_str, (vala, string_t, STRING_OPLIST), @@ -98,33 +98,33 @@ int main(void) pair_init (p1); string_set_str(p1->key, "HELLO"); - mpz_set_str(p1->value, "1742", 10); + my_mpz_set_ui(p1->value, 1742); pair_init_set (p2, p1); - assert(mpz_cmp_ui(p2->value, 1742) == 0); + assert(my_mpz_cmp_ui(p2->value, 1742) == 0); assert(string_equal_str_p(p2->key, "HELLO")); string_set_str(p2->key, "HELLO WORLD"); - mpz_set_str(p2->value, "174217", 10); + my_mpz_set_ui(p2->value, 174217); pair_set(p1, p1); pair_set(p1, p2); - assert(mpz_cmp_ui(p1->value, 174217) == 0); + assert(my_mpz_cmp_ui(p1->value, 174217) == 0); assert(string_equal_str_p(p1->key, "HELLO WORLD")); pair_clear(p1); pair_clear(p2); string_t s; - mpz_t z; + my_mpz_t z; pair3_t p3, p4; string_init_set_str(s, "HELLO"); - mpz_init_set_ui (z, 1442); + my_mpz_init_set_ui (z, 1442); pair3_init_set2(p3, s, z); pair3_init_set2(p4, s, z); int i = pair3_cmp (p3, p4); assert (i == 0); - mpz_set_ui (z, 1443); + my_mpz_set_ui (z, 1443); pair3_set_value(p4, z); i = pair3_cmp (p3, p4); assert (i < 0); - mpz_set_ui (z, 1442); + my_mpz_set_ui (z, 1442); string_set_str(s, "HELLN"); pair3_set_key(p4, s); i = pair3_cmp (p3, p4); @@ -132,7 +132,7 @@ int main(void) pair3_clear(p3); pair3_clear(p4); string_clear(s); - mpz_clear(z); + my_mpz_clear(z); check_io(); check_swap(); diff --git a/tests/test-mvariant.c b/tests/test-mvariant.c index 55bf36e3..c940b8b2 100644 --- a/tests/test-mvariant.c +++ b/tests/test-mvariant.c @@ -21,9 +21,9 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include -#include - +#include "mympz.h" #include "m-string.h" + #include "m-variant.h" #include "coverage.h" @@ -138,7 +138,7 @@ static void test_pair(void) VARIANT_DEF2(triple, (s, string_t, STRING_OPLIST), - (z, mpz_t, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear) )), + (z, my_mpz_t, (INIT(my_mpz_init), INIT_SET(my_mpz_init_set), SET(my_mpz_set), CLEAR(my_mpz_clear) )), (i, int, M_DEFAULT_OPLIST) ) static void @@ -148,10 +148,10 @@ test_triple(void) triple_init(t); string_t s; string_init(s); - mpz_t z; - mpz_init(z); + my_mpz_t z; + my_mpz_init(z); - mpz_set_str(z, "125567890456789345784567812345678", 10); + my_mpz_set_ui(z, 2556789045); string_set_str(s, "FHZKJHFKZUHFKZHFUHZFUHURHYERUYEUIRYEIURYIEYRIU"); assert( triple_type(t) == triple_EMPTY); @@ -165,9 +165,9 @@ test_triple(void) assert( triple_type(t) == triple_z); triple_set_z(t, z); assert( triple_type(t) == triple_z); - assert( mpz_cmp (z, *triple_get_z(t)) == 0); + assert( my_mpz_cmp (z, *triple_get_z(t)) == 0); - mpz_clear(z); + my_mpz_clear(z); string_clear(s); triple_clear(t); }