diff --git a/tests/test-malgo.c b/tests/test-malgo.c index d880230d..4cce823c 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 890eb018..ae89d36e 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 e5ede6d1..2e3daf99 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 38c567f6..d4327d6e 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 420efccc..ac2bc2dc 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 0d9f6f63..9460b840 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 e4010d28..b579eeb5 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 e2c2f993..ac84005a 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 f569d8c6..462e60f1 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 4e9b7198..65f5cb4c 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 bc787e51..cd9b939c 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 46a69d48..8790004a 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 17f7cf3e..54671a96 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 c825c4ac..78b9391a 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 8a52d7fb..d2a0e45e 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 080259b8..d933a7a9 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 0417d874..9064ee70 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 e2a2333c..87a8694f 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 672ee833..90fd8a5a 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 90dac082..584ce46e 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 67d3f34b..f8783705 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 14ff058e..d3d2029e 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 3e7f7292..0e4867da 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 e05c8768..08c02177 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 24e508a8..a0161b43 100644 --- a/tests/test-mtry.c +++ b/tests/test-mtry.c @@ -40,7 +40,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 55a526fa..a1138323 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 67040187..ca7beaf3 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 4a359b5c..2d3eaab3 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 f6b7cfbd..4b28d2f0 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)