From 4a92419bbe335c0f2f049d6935a74eb9868fd2fe Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Tue, 20 Feb 2018 21:55:16 +0100 Subject: [PATCH] Add tests --- m-shared.h | 2 +- tests/test-mshared.c | 47 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/m-shared.h b/m-shared.h index 970e79f9..2afbb571 100644 --- a/m-shared.h +++ b/m-shared.h @@ -1,5 +1,5 @@ /* - * M*LIB - SHARED PTR Module + * M*LIB - SHARED Module * * Copyright (c) 2017-2018, Patrick Pelissier * All rights reserved. diff --git a/tests/test-mshared.c b/tests/test-mshared.c index 9cdae98f..80a9d3d4 100644 --- a/tests/test-mshared.c +++ b/tests/test-mshared.c @@ -28,10 +28,10 @@ #include "coverage.h" START_COVERAGE SHARED_PTR_DEF(shared_int, int) +SHARED_RESSOURCE_DEF(shared_ressource, mpz_t, M_CLASSIC_OPLIST(mpz)) END_COVERAGE SHARED_PTR_DEF(shared_mpz, mpz_t, M_CLASSIC_OPLIST(mpz)) SHARED_PTR_RELAXED_DEF(shared_relaxed_int, int) -SHARED_RESSOURCE_DEF(shared_ressource, mpz_t, M_CLASSIC_OPLIST(mpz)) static int f(const shared_int_t p) { @@ -130,9 +130,54 @@ static void test2(void) shared_int_clear(p1); } +static void test_ressource(int n) +{ + shared_ressource_t ressource; + shared_ressource_it_t it[n]; + shared_ressource_it_t copy[n]; + shared_ressource_it_t onemore; + + shared_ressource_init (ressource, 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]); + assert (z != NULL); + 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]); + assert (z != NULL); + assert (mpz_cmp_ui(*z, i) == 0); + } + shared_ressource_it (onemore, ressource); + assert (shared_ressource_end_p(onemore)); + for(int i = 0; i < n ; i++) { + shared_ressource_end (it[i], ressource); + } + shared_ressource_it (onemore, ressource); + assert (shared_ressource_end_p(onemore)); + for(int i = n-1; i >= 0 ; i--) { + shared_ressource_end (copy[i], ressource); + shared_ressource_it (onemore, ressource); + assert (!shared_ressource_end_p(onemore)); + shared_ressource_end (onemore, ressource); + shared_ressource_end (copy[i], ressource); + } + + shared_ressource_clear(ressource); +} + + int main(void) { test1(); test2(); + for(int i = 1; i < 10; i++) + test_ressource(i); exit(0); }