diff --git a/tests/test-malgo.c b/tests/test-malgo.c index 3138b52b..32e65241 100644 --- a/tests/test-malgo.c +++ b/tests/test-malgo.c @@ -20,6 +20,7 @@ * (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 "test-obj.h" #include "m-list.h" #include "m-i-list.h" #include "m-array.h" @@ -29,8 +30,6 @@ #include "m-tuple.h" #include "m-algo.h" -#include "test-obj.h" - typedef struct over_s { unsigned long data; ILIST_INTERFACE(ilist_over, over_s); diff --git a/tests/test-mbitset.c b/tests/test-mbitset.c index dcb712f0..ae688f41 100644 --- a/tests/test-mbitset.c +++ b/tests/test-mbitset.c @@ -20,10 +20,9 @@ * (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 "test-obj.h" #include "m-string.h" #include "coverage.h" - #include "m-bitset.h" static void test1(void) diff --git a/tests/test-mbptree.c b/tests/test-mbptree.c index 6ee08a84..60c5ba46 100644 --- a/tests/test-mbptree.c +++ b/tests/test-mbptree.c @@ -20,11 +20,9 @@ * (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 "test-obj.h" #include "m-string.h" #include "m-bptree.h" -#include "test-obj.h" #include "coverage.h" START_COVERAGE diff --git a/tests/test-mbstring.c b/tests/test-mbstring.c index 2d00103e..5602d3df 100644 --- a/tests/test-mbstring.c +++ b/tests/test-mbstring.c @@ -20,9 +20,8 @@ * (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 "m-core.h" +#include "test-obj.h" #include "coverage.h" - #include "m-bstring.h" static void test0(void) diff --git a/tests/test-mbuffer.c b/tests/test-mbuffer.c index 3474d670..6fc2032d 100644 --- a/tests/test-mbuffer.c +++ b/tests/test-mbuffer.c @@ -20,10 +20,10 @@ * (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 "m-buffer.h" - #include "test-obj.h" +#include "m-buffer.h" #include "coverage.h" + START_COVERAGE // Define a fixed queue of unsigned int BUFFER_DEF(buffer_uint, unsigned int, 10, BUFFER_QUEUE|BUFFER_BLOCKING) diff --git a/tests/test-mconcurrent.c b/tests/test-mconcurrent.c index 234c89c9..4c84c509 100644 --- a/tests/test-mconcurrent.c +++ b/tests/test-mconcurrent.c @@ -22,7 +22,6 @@ */ #include #include "test-obj.h" -#include "coverage.h" #include "m-array.h" #include "m-bptree.h" @@ -35,6 +34,7 @@ #include "m-variant.h" #include "m-concurrent.h" +#include "coverage.h" TUPLE_DEF2(point, (x, int), (y, int)) #define M_OPL_point_t() TUPLE_OPLIST(point, M_BASIC_OPLIST, M_BASIC_OPLIST) diff --git a/tests/test-mcore.c b/tests/test-mcore.c index 6f80a16c..54419440 100644 --- a/tests/test-mcore.c +++ b/tests/test-mcore.c @@ -29,9 +29,8 @@ #define M_F(a,b) M_OVERRIDE_F(a,b) #define M_OVERRIDE__method() , _get_method , #define M_OVERRIDE__method2() , _get_method2 -#include "m-core.h" - #include "test-obj.h" +#include "m-core.h" static bool tmp_init(void) { return true; } static bool tmp_get_method(void) { return true; } diff --git a/tests/test-mdict.c b/tests/test-mdict.c index fbc779cc..6d847007 100644 --- a/tests/test-mdict.c +++ b/tests/test-mdict.c @@ -21,17 +21,16 @@ * (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 "test-obj.h" #include "m-dict.h" #include "m-array.h" #include "m-string.h" -#include "test-obj.h" +#include "coverage.h" static inline bool oor_equal_p(int k, unsigned char n) { return k == (int)-n-1; } static inline void oor_set(int *k, unsigned char n) { *k = (int)-n-1; } static inline void update_value (int *p, const int p1, const int p2) { *p = p1 + p2; } -#include "coverage.h" + START_COVERAGE DICT_DEF2(dict_str, string_t, STRING_OPLIST, string_t, STRING_OPLIST) DICT_OA_DEF2(dict_oa_int, int, M_OPEXTEND(M_BASIC_OPLIST, OOR_EQUAL(oor_equal_p), OOR_SET(oor_set M_IPTR)), int, M_OPEXTEND(M_BASIC_OPLIST, ADD(update_value M_IPTR))) diff --git a/tests/test-mfuncobj.c b/tests/test-mfuncobj.c index af2b63be..14fc2d3f 100644 --- a/tests/test-mfuncobj.c +++ b/tests/test-mfuncobj.c @@ -20,7 +20,6 @@ * (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 "test-obj.h" #include "m-string.h" #include "m-funcobj.h" diff --git a/tests/test-mgeneric.c b/tests/test-mgeneric.c index 9e814baa..79abb7bd 100644 --- a/tests/test-mgeneric.c +++ b/tests/test-mgeneric.c @@ -21,14 +21,12 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include "coverage.h" - +#include "test-obj.h" #include "m-string.h" #include "m-array.h" #include "m-list.h" #include "m-generic.h" +#include "coverage.h" // Generic is not supported if not C11 // TCC has some issues in the preprocessing. diff --git a/tests/test-mgenint.c b/tests/test-mgenint.c index b55e56c5..69839a18 100644 --- a/tests/test-mgenint.c +++ b/tests/test-mgenint.c @@ -20,12 +20,11 @@ * (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 "test-obj.h" #include "m-thread.h" #include "m-atomic.h" #include "m-core.h" #include "coverage.h" - #include "m-genint.h" #define MAX_N 256 diff --git a/tests/test-milist.c b/tests/test-milist.c index 72b28153..df3a3849 100644 --- a/tests/test-milist.c +++ b/tests/test-milist.c @@ -22,9 +22,9 @@ * (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 "test-obj.h" #include "m-i-list.h" +#include "coverage.h" typedef struct test_s { int n; @@ -37,7 +37,6 @@ static inline bool test_equal_p(const test_t *i1, const test_t *i2) return i1->n == i2->n; } -#include "coverage.h" START_COVERAGE ILIST_DEF(ilist_tname, test_t, M_OPEXTEND(M_POD_OPLIST, EQUAL(API_6(test_equal_p)))) END_COVERAGE diff --git a/tests/test-mishared.c b/tests/test-mishared.c index 7d75a538..b65fc754 100644 --- a/tests/test-mishared.c +++ b/tests/test-mishared.c @@ -20,11 +20,8 @@ * (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 "test-obj.h" - #include "m-i-shared.h" - #include "m-thread.h" #include "coverage.h" diff --git a/tests/test-mlist.c b/tests/test-mlist.c index 49f264d2..599c1dd1 100644 --- a/tests/test-mlist.c +++ b/tests/test-mlist.c @@ -20,12 +20,11 @@ * (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 "test-obj.h" #include "m-string.h" #include "m-list.h" - #include "coverage.h" + START_COVERAGE LIST_DEF(list_uint, unsigned int) LIST_DEF(list_mpz, testobj_t, TESTOBJ_OPLIST) diff --git a/tests/test-mmempool.c b/tests/test-mmempool.c index 4d95ba37..b9513861 100644 --- a/tests/test-mmempool.c +++ b/tests/test-mmempool.c @@ -21,12 +21,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include - +#include "test-obj.h" #include "m-mempool.h" - #include "coverage.h" + START_COVERAGE MEMPOOL_DEF(mempool_uint, unsigned int) END_COVERAGE diff --git a/tests/test-mprioqueue.c b/tests/test-mprioqueue.c index b25b9228..40335953 100644 --- a/tests/test-mprioqueue.c +++ b/tests/test-mprioqueue.c @@ -20,13 +20,11 @@ * (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 "test-obj.h" #include "m-string.h" #include "m-prioqueue.h" - #include "coverage.h" + START_COVERAGE PRIOQUEUE_DEF(int_pqueue, int) END_COVERAGE diff --git a/tests/test-mrbtree.c b/tests/test-mrbtree.c index 7a781f77..c2f01878 100644 --- a/tests/test-mrbtree.c +++ b/tests/test-mrbtree.c @@ -20,10 +20,10 @@ * (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 "test-obj.h" #include "m-string.h" #include "m-rbtree.h" +#include "coverage.h" static bool uint_in_str(unsigned int *u, FILE *f) { @@ -49,7 +49,6 @@ static void uint_get_str(string_t str, unsigned int u, bool append) (append ? string_cat_printf : string_printf) (str, "%u", u); } -#include "coverage.h" START_COVERAGE RBTREE_DEF(rbtree_uint, unsigned int, M_OPEXTEND(M_BASIC_OPLIST, IN_STR(uint_in_str M_IPTR), OUT_STR(uint_out_str), GET_STR(uint_get_str), PARSE_STR(uint_parse_str M_IPTR)) ) END_COVERAGE diff --git a/tests/test-mserial-bin.c b/tests/test-mserial-bin.c index bbc368ae..56719c31 100644 --- a/tests/test-mserial-bin.c +++ b/tests/test-mserial-bin.c @@ -20,6 +20,7 @@ * (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 "test-obj.h" #include "m-tuple.h" #include "m-array.h" #include "m-variant.h" diff --git a/tests/test-mserial-json.c b/tests/test-mserial-json.c index b47ed40a..fcb542f9 100644 --- a/tests/test-mserial-json.c +++ b/tests/test-mserial-json.c @@ -20,6 +20,7 @@ * (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 "test-obj.h" #include "m-tuple.h" #include "m-array.h" #include "m-variant.h" diff --git a/tests/test-mshared-ptr.c b/tests/test-mshared-ptr.c index 71d9aa5f..f13f47ab 100644 --- a/tests/test-mshared-ptr.c +++ b/tests/test-mshared-ptr.c @@ -20,10 +20,8 @@ * (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 "test-obj.h" #include "m-shared-ptr.h" - #include "m-string.h" #include "m-array.h" #include "m-bptree.h" @@ -34,7 +32,6 @@ #include "m-rbtree.h" #include "m-tuple.h" #include "m-variant.h" - #include "coverage.h" // TEST WITH INT diff --git a/tests/test-mshared.c b/tests/test-mshared.c index f7d8dd37..98bd78c3 100644 --- a/tests/test-mshared.c +++ b/tests/test-mshared.c @@ -20,12 +20,10 @@ * (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 "test-obj.h" - #include "m-shared.h" - #include "coverage.h" + START_COVERAGE SHARED_PTR_DEF(shared_int, int) SHARED_RESOURCE_DEF(shared_ressource, testobj_t, TESTOBJ_OPLIST) diff --git a/tests/test-msnapshot.c b/tests/test-msnapshot.c index 51d82930..7794a447 100644 --- a/tests/test-msnapshot.c +++ b/tests/test-msnapshot.c @@ -21,10 +21,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test-obj.h" - #include "m-snapshot.h" #include "m-thread.h" - #include "coverage.h" // Long Structure with a simple CRC to check for proper passing diff --git a/tests/test-mstring.c b/tests/test-mstring.c index 5e84d8a8..78bd195b 100644 --- a/tests/test-mstring.c +++ b/tests/test-mstring.c @@ -22,9 +22,8 @@ */ #define M_USE_ADDITIONAL_CHECKS 1 -#include "m-core.h" +#include "test-obj.h" #include "coverage.h" - #include "m-string.h" BOUNDED_STRING_DEF(string16, 16) @@ -609,16 +608,18 @@ static void test0(void) assert (string_equal_str_p(s1, "QWERTYQWERTY")); string_clear (s2); + size_t cap = string_capacity(s1); char *s = string_clear_get_str (s1); assert(strcmp(s, "QWERTYQWERTY") == 0); - free(s); + overloaded_free(1, s, cap <= sizeof (m_str1ng_heap_ct) - 1 ? strlen(s)+1 : cap ); string_t s3; string_init(s3); string_cat_str(s3, "ABC"); + cap = string_capacity(s3); s = string_clear_get_str(s3); assert(strcmp(s, "ABC") == 0); - free(s); + overloaded_free(1, s, cap <= sizeof (m_str1ng_heap_ct) - 1 ? strlen(s)+1 : cap ); string_init_set_str(s1, "RESTART"); assert (string_equal_str_p(s1, "RESTART")); diff --git a/tests/test-mtree.c b/tests/test-mtree.c index 9e02b997..6017b112 100644 --- a/tests/test-mtree.c +++ b/tests/test-mtree.c @@ -20,11 +20,8 @@ * (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 "m-tree.h" - #include "test-obj.h" +#include "m-tree.h" #include "m-string.h" #include "coverage.h" diff --git a/tests/test-mtry.c b/tests/test-mtry.c index 49179c1f..bcb44027 100644 --- a/tests/test-mtry.c +++ b/tests/test-mtry.c @@ -51,7 +51,6 @@ static bool test_final_in_progress; #include "test-obj.h" #include "coverage.h" - #include "m-try.h" M_TRY_DEF_ONCE() diff --git a/tests/test-mtuple.c b/tests/test-mtuple.c index 1d72ef13..68029cd9 100644 --- a/tests/test-mtuple.c +++ b/tests/test-mtuple.c @@ -20,13 +20,11 @@ * (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 "test-obj.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))), diff --git a/tests/test-mvariant.c b/tests/test-mvariant.c index b9120515..96f2ac8d 100644 --- a/tests/test-mvariant.c +++ b/tests/test-mvariant.c @@ -20,13 +20,11 @@ * (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 "test-obj.h" #include "m-string.h" - #include "m-variant.h" - #include "coverage.h" + START_COVERAGE VARIANT_DEF2(pair, (key, int, M_BASIC_OPLIST), diff --git a/tests/test-mworker.c b/tests/test-mworker.c index 74aa12db..f0743d45 100644 --- a/tests/test-mworker.c +++ b/tests/test-mworker.c @@ -20,9 +20,8 @@ * (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 "m-core.h" +#include "test-obj.h" #include "coverage.h" - #include "m-worker.h" /* Compute Fibonacci number using thread systems. */ diff --git a/tests/test-obj.h b/tests/test-obj.h index eac9e982..933499c0 100644 --- a/tests/test-obj.h +++ b/tests/test-obj.h @@ -25,6 +25,52 @@ #include #include +#include +#include + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +// First overwrite memory handling function so that we can check if everything is ok +#ifdef M_MEMORY_REALLOC +# error ERROR: Do not include M*LIB headers before this header. +#endif + +static size_t amount_allocated = 0; + +static inline void *overloaded_realloc(size_t st, void *ptr, size_t o, size_t n) +{ + size_t *p = (size_t *) ptr; + if (p != NULL) { + p -= 2; + assert(p[0] == st); + assert(p[1] == o); + amount_allocated -= st * o; + } + p = (size_t *) realloc(p, 2*sizeof(size_t) + st * n); + assert(p != NULL); + p[0] = st; + p[1] = n; + amount_allocated += st * n; + return (void*) (&p[2]); +} + +static inline void overloaded_free(size_t st, void *ptr, size_t n) +{ + size_t *p = (size_t *) ptr; + if (p == NULL) { + return; + } + p -= 2; + assert(p[0] == st); + assert(p[1] == n); + amount_allocated -= st * n; + free(p); +} + +#define M_MEMORY_REALLOC(type, ptr, o, n) (type *) overloaded_realloc(sizeof(type), ptr, o, n) +#define M_MEMORY_FREE(type, ptr, o) overloaded_free(sizeof(type), ptr, o) + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include "m-string.h" @@ -45,6 +91,8 @@ static inline void testobj_final_check(void) { // All created testobj shall have been cleared at the end. assert(testobj_init_counter == 0); + // All memory allocations shall be cleared + assert(amount_allocated == 0); } static inline void testobj_init(testobj_t z)