1- diff --git a/test/test-api-uk-port.c b/test/test-api-uk-port.c
2- new file mode 100644
3- index 0000000..6b10fb5
4- --- /dev/null
5- +++ b/test/test-api-uk-port.c
6- @@ -0,0 +1,241 @@
7- + /* ----------------------------------------------------------------------------
8- + Copyright (c) 2018, Microsoft Research, Daan Leijen
9- + This is free software; you can redistribute it and/or modify it under the
10- + terms of the MIT license. A copy of the license can be found in the file
11- + "LICENSE" at the root of this distribution.
12- + -----------------------------------------------------------------------------*/
13- +
14- + /*
15- + Testing allocators is difficult as bugs may only surface after particular
16- + allocation patterns. The main approach to testing _mimalloc_ is therefore
17- + to have extensive internal invariant checking (see `page_is_valid` in `page.c`
18- + for example), which is enabled in debug mode with `-DMI_DEBUG_FULL=ON`.
19- + The main testing is then to run `mimalloc-bench` [1] using full invariant checking
20- + to catch any potential problems over a wide range of intensive allocation bench
21- + marks.
22- +
23- + However, this does not test well for the entire API surface. In this test file
24- + we therefore test the API over various inputs. Please add more tests :-)
25- +
26- + [1] https://github.com/daanx/mimalloc-bench
27- + */
28- +
29- + #include <stdio.h>
30- + #include <assert.h>
31- + #include <stdbool.h>
32- + #include <stdint.h>
33- + #include <errno.h>
34- +
1+ diff --git a/test/test-api.c b/test/test-api.c
2+ index 166cfca..5be2b6e 100644
3+ --- a/test/test-api.c
4+ +++ b/test/test-api.c
5+ @@ -26,6 +26,8 @@ we therefore test the API over various inputs. Please add more tests :-)
6+ #include <stdint.h>
7+ #include <errno.h>
8+
359+ #include <uk/test.h>
3610+
37- + #ifdef __cplusplus
38- + #include <vector>
39- + #endif
40- +
41- + #include "mimalloc.h"
42- + // #include "mimalloc-internal.h"
43- +
11+ #ifdef __cplusplus
12+ #include <vector>
13+ #endif
14+ @@ -33,184 +35,167 @@ we therefore test the API over various inputs. Please add more tests :-)
15+ #include "mimalloc.h"
16+ // #include "mimalloc-internal.h"
17+
18+ - // ---------------------------------------------------------------------------
19+ - // Test macros: CHECK(name,predicate) and CHECK_BODY(name,body)
20+ - // ---------------------------------------------------------------------------
21+ - static int ok = 0;
22+ - static int failed = 0;
23+ -
24+ - #define CHECK_BODY(name,body) \
25+ - do { \
26+ - fprintf(stderr,"test: %s... ", name ); \
27+ - bool result = true; \
28+ - do { body } while(false); \
29+ - if (!(result)) { \
30+ - failed++; \
31+ - fprintf(stderr, \
32+ - "\n FAILED: %s:%d:\n %s\n", \
33+ - __FILE__, \
34+ - __LINE__, \
35+ - #body); \
36+ - /* exit(1); */ \
37+ - } \
38+ - else { \
39+ - ok++; \
40+ - fprintf(stderr,"ok.\n"); \
41+ - } \
42+ - } while (false)
43+ -
44+ - #define CHECK(name,expr) CHECK_BODY(name,{ result = (expr); })
45+ -
46+ - // ---------------------------------------------------------------------------
47+ - // Test functions
48+ - // ---------------------------------------------------------------------------
49+ - bool test_heap1();
50+ - bool test_heap2();
51+ - bool test_stl_allocator1();
52+ - bool test_stl_allocator2();
53+ -
54+ - // ---------------------------------------------------------------------------
55+ - // Main testing
56+ - // ---------------------------------------------------------------------------
57+ - int main() {
58+ - mi_option_disable(mi_option_verbose);
59+ -
60+ - // ---------------------------------------------------
61+ - // Malloc
62+ - // ---------------------------------------------------
63+ -
64+ - CHECK_BODY("malloc-zero",{
4465+ // ---------------------------------------------------
4566+ // Malloc
4667+ // ---------------------------------------------------
4768+
4869+ UK_TESTCASE(libmimalloc_api_testsuite, malloc_zero)
4970+ {
50- + void* p = mi_malloc(0); mi_free(p);
71+ void* p = mi_malloc(0); mi_free(p);
72+ - });
73+ - CHECK_BODY("malloc-nomem1",{
74+ - result = (mi_malloc(SIZE_MAX/2) == NULL);
75+ - });
76+ - CHECK_BODY("malloc-null",{
5177+ }
5278+
5379+ UK_TESTCASE(libmimalloc_api_testsuite, malloc_nomem1)
@@ -57,24 +83,133 @@ index 0000000..6b10fb5
5783+
5884+ UK_TESTCASE(libmimalloc_api_testsuite, malloc_null)
5985+ {
60- + mi_free(NULL);
86+ mi_free(NULL);
87+ - });
88+ - CHECK_BODY("calloc-overflow",{
6189+ }
6290+
6391+ UK_TESTCASE(libmimalloc_api_testsuite, calloc_overflow)
6492+ {
65- + // use (size_t)&mi_calloc to get some number without triggering compiler warnings
93+ // use (size_t)&mi_calloc to get some number without triggering compiler warnings
94+ - result = (mi_calloc((size_t)&mi_calloc,SIZE_MAX/1000) == NULL);
95+ - });
96+ - CHECK_BODY("calloc0",{
97+ - result = (mi_usable_size(mi_calloc(0,1000)) <= 16);
98+ - });
99+ -
100+ - // ---------------------------------------------------
101+ - // Extended
102+ - // ---------------------------------------------------
103+ - CHECK_BODY("posix_memalign1", {
104+ - void* p = &p;
105+ - int err = mi_posix_memalign(&p, sizeof(void*), 32);
106+ - result = ((err==0 && (uintptr_t)p % sizeof(void*) == 0) || p==&p);
107+ - mi_free(p);
108+ - });
109+ - CHECK_BODY("posix_memalign_no_align", {
110+ - void* p = &p;
111+ - int err = mi_posix_memalign(&p, 3, 32);
112+ - result = (err==EINVAL && p==&p);
113+ - });
114+ - CHECK_BODY("posix_memalign_zero", {
115+ - void* p = &p;
116+ - int err = mi_posix_memalign(&p, sizeof(void*), 0);
117+ - mi_free(p);
118+ - result = (err==0);
119+ - });
120+ - CHECK_BODY("posix_memalign_nopow2", {
121+ - void* p = &p;
122+ - int err = mi_posix_memalign(&p, 3*sizeof(void*), 32);
123+ - result = (err==EINVAL && p==&p);
124+ - });
125+ - CHECK_BODY("posix_memalign_nomem", {
126+ - void* p = &p;
127+ - int err = mi_posix_memalign(&p, sizeof(void*), SIZE_MAX);
128+ - result = (err==ENOMEM && p==&p);
129+ - });
130+ -
131+ - // ---------------------------------------------------
132+ - // Aligned API
133+ - // ---------------------------------------------------
134+ - CHECK_BODY("malloc-aligned1", {
135+ - void* p = mi_malloc_aligned(32,32); result = (p != NULL && (uintptr_t)(p) % 32 == 0); mi_free(p);
136+ - });
137+ - CHECK_BODY("malloc-aligned2", {
138+ - void* p = mi_malloc_aligned(48,32); result = (p != NULL && (uintptr_t)(p) % 32 == 0); mi_free(p);
139+ - });
140+ - CHECK_BODY("malloc-aligned3", {
141+ - void* p1 = mi_malloc_aligned(48,32); bool result1 = (p1 != NULL && (uintptr_t)(p1) % 32 == 0);
142+ - void* p2 = mi_malloc_aligned(48,32); bool result2 = (p2 != NULL && (uintptr_t)(p2) % 32 == 0);
143+ - mi_free(p2);
144+ - mi_free(p1);
145+ - result = (result1&&result2);
146+ - });
147+ - CHECK_BODY("malloc-aligned4", {
148+ - void* p;
149+ - bool ok = true;
150+ - for (int i = 0; i < 8 && ok; i++) {
151+ - p = mi_malloc_aligned(8, 16);
152+ - ok = (p != NULL && (uintptr_t)(p) % 16 == 0); mi_free(p);
153+ - }
154+ - result = ok;
155+ - });
156+ - CHECK_BODY("malloc-aligned-at1", {
157+ - void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);
158+ - });
159+ - CHECK_BODY("malloc-aligned-at2", {
160+ - void* p = mi_malloc_aligned_at(50,32,8); result = (p != NULL && ((uintptr_t)(p) + 8) % 32 == 0); mi_free(p);
161+ - });
162+ - CHECK_BODY("memalign1", {
163+ - void* p;
164+ - bool ok = true;
165+ - for (int i = 0; i < 8 && ok; i++) {
166+ - p = mi_memalign(16,8);
167+ - ok = (p != NULL && (uintptr_t)(p) % 16 == 0); mi_free(p);
168+ - }
169+ - result = ok;
170+ - });
171+ -
172+ - // ---------------------------------------------------
173+ - // Heaps
174+ - // ---------------------------------------------------
175+ - CHECK("heap_destroy", test_heap1());
176+ - CHECK("heap_delete", test_heap2());
177+ -
178+ - //mi_stats_print(NULL);
179+ -
180+ - // ---------------------------------------------------
181+ - // various
182+ - // ---------------------------------------------------
183+ - CHECK_BODY("realpath", {
184+ - char* s = mi_realpath( ".", NULL );
185+ - // printf("realpath: %s\n",s);
186+ - mi_free(s);
187+ - });
188+ -
189+ - CHECK("stl_allocator1", test_stl_allocator1());
190+ - CHECK("stl_allocator2", test_stl_allocator2());
191+ -
192+ - // ---------------------------------------------------
193+ - // Done
194+ - // ---------------------------------------------------[]
195+ - fprintf(stderr,"\n\n---------------------------------------------\n"
196+ - "succeeded: %i\n"
197+ - "failed : %i\n\n", ok, failed);
198+ - return failed;
66199+ UK_TEST_EXPECT_PTR_EQ(mi_calloc((size_t)&mi_calloc,SIZE_MAX/1000), NULL);
67200+ }
68201+
69202+ UK_TESTCASE(libmimalloc_api_testsuite, calloc0)
70203+ {
71204+ UK_TEST_EXPECT(mi_usable_size(mi_calloc(0,1000)) <= 16);
72- + }
73- +
74- + // ---------------------------------------------------
205+ }
206+
207+ // ---------------------------------------------------
208+ - // Larger test functions
75209+ // Extended
76- + // ---------------------------------------------------
77- +
210+ // ---------------------------------------------------
211+
212+ - bool test_heap1() {
78213+ UK_TESTCASE(libmimalloc_api_testsuite, posix_memalign1)
79214+ {
80215+ void* p = &p;
@@ -193,24 +328,28 @@ index 0000000..6b10fb5
193328+ // ---------------------------------------------------
194329+ UK_TESTCASE(libmimalloc_api_testsuite, heap_destroy)
195330+ {
196- + mi_heap_t* heap = mi_heap_new();
197- + int* p1 = mi_heap_malloc_tp(heap,int);
198- + int* p2 = mi_heap_malloc_tp(heap,int);
199- + *p1 = *p2 = 43;
200- + mi_heap_destroy(heap);
201- + }
202- +
331+ mi_heap_t* heap = mi_heap_new();
332+ int* p1 = mi_heap_malloc_tp(heap,int);
333+ int* p2 = mi_heap_malloc_tp(heap,int);
334+ *p1 = *p2 = 43;
335+ mi_heap_destroy(heap);
336+ - return true;
337+ }
338+
339+ - bool test_heap2() {
203340+ UK_TESTCASE(libmimalloc_api_testsuite, heap_delete)
204341+ {
205- + mi_heap_t* heap = mi_heap_new();
206- + int* p1 = mi_heap_malloc_tp(heap,int);
207- + int* p2 = mi_heap_malloc_tp(heap,int);
208- + mi_heap_delete(heap);
209- + *p1 = 42;
210- + mi_free(p1);
211- + mi_free(p2);
212- + }
213- +
342+ mi_heap_t* heap = mi_heap_new();
343+ int* p1 = mi_heap_malloc_tp(heap,int);
344+ int* p2 = mi_heap_malloc_tp(heap,int);
345+ @@ -218,29 +203,39 @@ bool test_heap2() {
346+ *p1 = 42;
347+ mi_free(p1);
348+ mi_free(p2);
349+ - return true;
350+ }
351+
352+ - bool test_stl_allocator1() {
214353+ // ---------------------------------------------------
215354+ // various
216355+ // ---------------------------------------------------
@@ -220,26 +359,36 @@ index 0000000..6b10fb5
220359+ mi_free(s);
221360+ }
222361+
223- + #ifdef __cplusplus
224- + UK_TESTCASE(various_test_suite , stl_allocator1)
362+ #ifdef __cplusplus
363+ + UK_TESTCASE(libmimalloc_api_testsuite , stl_allocator1)
225364+ {
226- + std::vector<int, mi_stl_allocator<int> > vec;
227- + vec.push_back(1);
228- + vec.pop_back();
365+ std::vector<int, mi_stl_allocator<int> > vec;
366+ vec.push_back(1);
367+ vec.pop_back();
368+ - return vec.size() == 0;
369+ - #else
370+ - return true;
371+ - #endif
229372+ UK_TEST_EXPECT_ZERO(vec.size());
230- + }
373+ }
231374+ #endif
232- +
375+
233376+ #ifdef __cplusplus
234- + struct some_struct { int i; int j; double z; };
235- +
377+ struct some_struct { int i; int j; double z; };
378+
379+ - bool test_stl_allocator2() {
380+ - #ifdef __cplusplus
236381+ UK_TESTCASE(libmimalloc_api_testsuite, stl_allocator2)
237382+ {
238- + std::vector<some_struct, mi_stl_allocator<some_struct> > vec;
239- + vec.push_back(some_struct());
240- + vec.pop_back();
383+ std::vector<some_struct, mi_stl_allocator<some_struct> > vec;
384+ vec.push_back(some_struct());
385+ vec.pop_back();
386+ - return vec.size() == 0;
387+ - #else
388+ - return true;
389+ - #endif
241390+ UK_TEST_EXPECT_ZERO(vec.size());
242- + }
391+ }
243392+ #endif
244393+
245394+ #if defined(CONFIG_LIBMIMALLOC_TEST_API)
0 commit comments