test/mpool_tests.c

Sun, 03 Nov 2019 17:03:10 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Nov 2019 17:03:10 +0100
changeset 365
72da0e4cbf4a
parent 270
3d80d425543b
permissions
-rw-r--r--

fix: missing documentation for first param in ucx_array_init_a()

13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
259
2f5dea574a75 modules documentation
Mike Becker <universe@uap-core.de>
parents: 253
diff changeset
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
5 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
6 * Redistribution and use in source and binary forms, with or without
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
7 * modification, are permitted provided that the following conditions are met:
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
8 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
10 * notice, this list of conditions and the following disclaimer.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
11 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
14 * documentation and/or other materials provided with the distribution.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
15 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
219
2df780a4482b fixed misaligned memory access in test_ucx_buffer_write
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
29 #include <inttypes.h>
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
30
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include "mpool_tests.h"
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
134
4d320dc3a7af documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
33 UCX_TEST(test_ucx_mempool_new) {
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
34 UcxMempool *pool = ucx_mempool_new(16);
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
35 UCX_TEST_BEGIN
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
36 UCX_TEST_ASSERT(pool->size == 16, "wrong size");
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
37 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter");
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
38 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed");
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
39 UCX_TEST_END
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
40 ucx_mempool_destroy(pool);
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
41 }
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
134
4d320dc3a7af documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
43 UCX_TEST(test_ucx_mempool_malloc) {
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
44
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
45 UcxMempool *pool = ucx_mempool_new(1);
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
46 UCX_TEST_BEGIN
32
c7af4ec56e19 consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
47 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
48
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
49 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented");
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
50 UCX_TEST_ASSERT(pool->size == 1, "chcap called");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
51
32
c7af4ec56e19 consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
52 intptr_t *pooladdr =
c7af4ec56e19 consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
53 (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor));
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
54 *pooladdr = 5;
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
55
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
56 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
57
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
58 UCX_TEST_END
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
59 ucx_mempool_destroy(pool);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
134
4d320dc3a7af documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
62 UCX_TEST(test_ucx_mempool_malloc_with_chcap) {
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
63
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
64 UcxMempool *pool = ucx_mempool_new(1);
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
65 UCX_TEST_BEGIN
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
66 ucx_mempool_malloc(pool, sizeof(int));
32
c7af4ec56e19 consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
67 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
68
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
69 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented");
241
661f33ef20d8 removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two
Mike Becker <universe@uap-core.de>
parents: 225
diff changeset
70 UCX_TEST_ASSERT(pool->size == 2, "chcap not called");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
71
32
c7af4ec56e19 consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
72 intptr_t *pooladdr =
c7af4ec56e19 consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
73 (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor));
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
74 *pooladdr = 5;
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
75
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
76 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
77
270
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
78 // overflow test
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
79 void *n0 = ucx_mempool_malloc(pool, (size_t)-1);
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
80 void *n1 = ucx_mempool_malloc(pool, ((size_t)-1) - sizeof(void*)/2);
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
81
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
82 UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes");
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
83 UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow");
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
84
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
85 UCX_TEST_END
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
86 ucx_mempool_destroy(pool);
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
87 }
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
88
134
4d320dc3a7af documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
89 UCX_TEST(test_ucx_mempool_calloc) {
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
90
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
91 UcxMempool *pool = ucx_mempool_new(1);
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
92 UCX_TEST_BEGIN
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
93
32
c7af4ec56e19 consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
94 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
95
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
96 UCX_TEST_ASSERT(test != NULL, "no memory for test data");
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
97 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
98
270
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
99 // overflow test
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
100 void *n0 = ucx_mempool_calloc(pool, (size_t)-1, 1);
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
101 void *n1 = ucx_mempool_calloc(pool, ((size_t)-1)/2, 3);
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
102
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
103 UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes");
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
104 UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow");
3d80d425543b adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 259
diff changeset
105
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
106 UCX_TEST_END
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
107 ucx_mempool_destroy(pool);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
108 }
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
109
134
4d320dc3a7af documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
110 UCX_TEST(test_ucx_mempool_free) {
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
111 UcxMempool *pool = ucx_mempool_new(16);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
112 void *mem1;
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
113 void *mem2;
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
114
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
115 UCX_TEST_BEGIN
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
116
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
117 mem1 = ucx_mempool_malloc(pool, 16);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
118 ucx_mempool_free(pool, mem1);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
119
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
120 UCX_TEST_ASSERT(pool->ndata == 0, "mempool not empty");
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
121
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
122 ucx_mempool_malloc(pool, 16);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
123 ucx_mempool_malloc(pool, 16);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
124 mem1 = ucx_mempool_malloc(pool, 16);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
125 ucx_mempool_malloc(pool, 16);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
126 mem2 = ucx_mempool_malloc(pool, 16);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
127
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
128 ucx_mempool_free(pool, mem1);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
129
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
130 UCX_TEST_ASSERT(pool->ndata == 4, "wrong mempool size");
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
131
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
132 ucx_mempool_free(pool, mem2);
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
133
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
134 UCX_TEST_ASSERT(pool->ndata == 3, "wrong mempool size");
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
135
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
136 UCX_TEST_END
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
137 ucx_mempool_destroy(pool);
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
138 }
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
139
253
e19825a1430a removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
140 #ifdef __cplusplus
e19825a1430a removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
141 extern "C"
e19825a1430a removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
142 #endif
e19825a1430a removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
143 void test_setdestr(void* elem) {
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
144 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
145 *cb = 42;
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147
134
4d320dc3a7af documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
148 UCX_TEST(test_ucx_mempool_set_destr) {
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
149
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
150 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
151 UCX_TEST_BEGIN
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
152 UcxMempool *pool = ucx_mempool_new(2);
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
153
32
c7af4ec56e19 consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
154 ucx_mempool_malloc(pool, sizeof(intptr_t));
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
155 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
156
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
157 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
158
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
159 test[0] = 5; test[1] = (intptr_t) cb;
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
160 *cb = 13;
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
161
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
162 ucx_mempool_set_destr(test, test_setdestr);
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
163 UCX_TEST_ASSERT(
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
164 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
165 UCX_TEST_ASSERT(
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
166 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
167
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
168 ucx_mempool_destroy(pool);
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
169
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
170 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
171
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
172 UCX_TEST_END
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
173 if (cb != NULL) free(cb);
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
174 }
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176
134
4d320dc3a7af documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
177 UCX_TEST(test_ucx_mempool_reg_destr) {
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
178
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
179 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
180 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
181 UCX_TEST_BEGIN
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
182 UcxMempool *pool = ucx_mempool_new(1);
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
183
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
184 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
185
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
186 test[0] = 5; test[1] = (intptr_t) cb;
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
187 *cb = 13;
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
188
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
189 ucx_mempool_reg_destr(pool, test, test_setdestr);
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
190
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
191 ucx_destructor *pooladdr = (ucx_destructor*)
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
192 ((char*)pool->data[0] + sizeof(ucx_destructor));
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
193
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
194 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
195
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
196 ucx_mempool_destroy(pool);
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
197 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
198 UCX_TEST_END
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
199
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
200 if (test != NULL) free(test);
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
201 if (cb != NULL) free(cb);
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
202 }
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203
134
4d320dc3a7af documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents: 131
diff changeset
204 UCX_TEST(test_ucx_mempool_realloc) {
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
205
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
206 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
207 UCX_TEST_BEGIN
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
208 UcxMempool *pool = ucx_mempool_new(2);
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
209
32
c7af4ec56e19 consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
210 ucx_mempool_malloc(pool, sizeof(intptr_t));
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
211 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
212
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
213 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
214
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
215 test[0] = 5; test[1] = (intptr_t) cb;
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
216 *cb = 13;
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
217
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
218 ucx_mempool_set_destr(test, test_setdestr);
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
219
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
220 intptr_t *rtest, n = 2;
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
221 do {
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
222 n *= 2;
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
223 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
224 rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
225 } while (rtest == test);
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
226 test = rtest;
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
227
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
228 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
229 "realloc killed destructor")
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
230 UCX_TEST_ASSERT(
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
231 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
232
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
233 ucx_mempool_destroy(pool);
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
234
40
583718dd4cf3 added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
235 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236
28
1666cbeb1db8 new mempool tests
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
237 UCX_TEST_END
33
9c219a62070d major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
238 if (cb != NULL) free(cb);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 }

mercurial