23
23
#error [NOT_SUPPORTED] MemoryPool test cases require a RTOS to run.
24
24
#else
25
25
26
+ #define TEST_ASSERT_DURATION_WITHIN (delta, expected, actual ) \
27
+ do { \
28
+ using ct = std::common_type_t <decltype (delta), decltype (expected), decltype (actual)>; \
29
+ TEST_ASSERT_INT_WITHIN (ct (delta).count (), ct (expected).count (), ct (actual).count ()); \
30
+ } while (0 )
31
+
32
+ #define TEST_ASSERT_TIME_POINT_WITHIN (delta, expected, actual ) \
33
+ do { \
34
+ using ct_tp = std::common_type_t <decltype (expected), decltype (actual)>; \
35
+ using ct = std::common_type_t <decltype (delta), ct_tp::duration>; \
36
+ TEST_ASSERT_INT_WITHIN (ct (delta).count (), ct (expected.time_since_epoch ()).count (), ct (actual.time_since_epoch ()).count ()); \
37
+ } while (0 )
38
+
26
39
using namespace utest ::v1;
40
+ using namespace std ::chrono;
27
41
28
42
#define THREAD_STACK_SIZE 512
29
- #define TEST_TIMEOUT 50
43
+ #define TEST_TIMEOUT 50ms
30
44
31
45
/* Enum used to select block allocation method. */
32
46
typedef enum {
@@ -68,7 +82,7 @@ void comp_set(COMPLEX_TYPE *object, int a, char b, int c)
68
82
object->c = c;
69
83
}
70
84
71
- /* Template for functional tests for alloc (), calloc () functions
85
+ /* Template for functional tests for try_alloc (), try_calloc () functions
72
86
* of MemoryPool object.
73
87
*
74
88
* Given MemoryPool object of the specified type and queue size has
@@ -89,9 +103,9 @@ void test_mem_pool_alloc_success(AllocType atype)
89
103
for (i = 0 ; i < numOfEntries; i++) {
90
104
/* Allocate memory block. */
91
105
if (atype == ALLOC) {
92
- p_blocks[i] = mem_pool.alloc ();
106
+ p_blocks[i] = mem_pool.try_alloc ();
93
107
} else {
94
- p_blocks[i] = mem_pool.calloc ();
108
+ p_blocks[i] = mem_pool.try_calloc ();
95
109
}
96
110
97
111
/* Show that memory pool block has been allocated. */
@@ -112,7 +126,7 @@ void test_mem_pool_alloc_success(AllocType atype)
112
126
}
113
127
}
114
128
115
- /* Template for functional tests for alloc (), calloc () functions
129
+ /* Template for functional tests for try_alloc (), try_calloc () functions
116
130
* of MemoryPool object.
117
131
*
118
132
* Complex memory pool block type is used.
@@ -135,9 +149,9 @@ void test_mem_pool_alloc_success_complex(AllocType atype)
135
149
for (i = 0 ; i < numOfEntries; i++) {
136
150
/* Allocate memory block. */
137
151
if (atype == ALLOC) {
138
- p_blocks[i] = mem_pool.alloc ();
152
+ p_blocks[i] = mem_pool.try_alloc ();
139
153
} else {
140
- p_blocks[i] = mem_pool.calloc ();
154
+ p_blocks[i] = mem_pool.try_calloc ();
141
155
}
142
156
143
157
/* Show that memory pool block has been allocated. */
@@ -158,7 +172,7 @@ void test_mem_pool_alloc_success_complex(AllocType atype)
158
172
}
159
173
}
160
174
161
- /* Template for functional tests for alloc (), calloc () functions
175
+ /* Template for functional tests for try_alloc (), try_calloc () functions
162
176
* of MemoryPool object.
163
177
*
164
178
* Given MemoryPool has already max number of blocks allocated from the pool.
@@ -177,9 +191,9 @@ void test_mem_pool_alloc_fail(AllocType atype)
177
191
/* Allocate all available blocks. */
178
192
for (i = 0 ; i < numOfEntries; i++) {
179
193
if (atype == ALLOC) {
180
- p_blocks[i] = mem_pool.alloc ();
194
+ p_blocks[i] = mem_pool.try_alloc ();
181
195
} else {
182
- p_blocks[i] = mem_pool.calloc ();
196
+ p_blocks[i] = mem_pool.try_calloc ();
183
197
}
184
198
185
199
/* Show that memory pool block has been allocated. */
@@ -188,9 +202,9 @@ void test_mem_pool_alloc_fail(AllocType atype)
188
202
189
203
/* There are no more blocks available. Try to allocate another block. */
190
204
if (atype == ALLOC) {
191
- p_extra_block = mem_pool.alloc ();
205
+ p_extra_block = mem_pool.try_alloc ();
192
206
} else {
193
- p_extra_block = mem_pool.calloc ();
207
+ p_extra_block = mem_pool.try_calloc ();
194
208
}
195
209
196
210
/* Show that memory pool block has NOT been allocated. */
@@ -216,9 +230,9 @@ void test_mem_pool_free_success(AllocType atype)
216
230
/* Allocate all available blocks. */
217
231
for (i = 0 ; i < numOfEntries; i++) {
218
232
if (atype == ALLOC) {
219
- p_blocks[i] = mem_pool.alloc ();
233
+ p_blocks[i] = mem_pool.try_alloc ();
220
234
} else {
221
- p_blocks[i] = mem_pool.calloc ();
235
+ p_blocks[i] = mem_pool.try_calloc ();
222
236
}
223
237
224
238
/* Show that memory pool block has been allocated. */
@@ -234,7 +248,7 @@ void test_mem_pool_free_success(AllocType atype)
234
248
}
235
249
}
236
250
237
- /* Template for functional tests for alloc (), calloc () functions
251
+ /* Template for functional tests for try_alloc (), try_calloc () functions
238
252
* of MemoryPool object.
239
253
*
240
254
* Basic memory pool block type is used.
@@ -256,9 +270,9 @@ void test_mem_pool_free_realloc_last(AllocType atype)
256
270
/* Allocate all available blocks. */
257
271
for (i = 0 ; i < numOfEntries; i++) {
258
272
if (atype == ALLOC) {
259
- p_blocks[i] = mem_pool.alloc ();
273
+ p_blocks[i] = mem_pool.try_alloc ();
260
274
} else {
261
- p_blocks[i] = mem_pool.calloc ();
275
+ p_blocks[i] = mem_pool.try_calloc ();
262
276
}
263
277
264
278
/* Init block. */
@@ -276,9 +290,9 @@ void test_mem_pool_free_realloc_last(AllocType atype)
276
290
277
291
/* Try to allocate another block (one block is now available). */
278
292
if (atype == ALLOC) {
279
- p_blocks[numOfEntries - 1 ] = mem_pool.alloc ();
293
+ p_blocks[numOfEntries - 1 ] = mem_pool.try_alloc ();
280
294
} else {
281
- p_blocks[numOfEntries - 1 ] = mem_pool.calloc ();
295
+ p_blocks[numOfEntries - 1 ] = mem_pool.try_calloc ();
282
296
}
283
297
284
298
/* Show that memory pool block has been now allocated. */
@@ -290,7 +304,7 @@ void test_mem_pool_free_realloc_last(AllocType atype)
290
304
}
291
305
}
292
306
293
- /* Template for functional tests for alloc (), calloc () functions
307
+ /* Template for functional tests for try_alloc (), try_calloc () functions
294
308
* of MemoryPool object.
295
309
*
296
310
* Complex memory pool block type is used.
@@ -312,9 +326,9 @@ void test_mem_pool_free_realloc_last_complex(AllocType atype)
312
326
/* Allocate all available blocks. */
313
327
for (i = 0 ; i < numOfEntries; i++) {
314
328
if (atype == ALLOC) {
315
- p_blocks[i] = mem_pool.alloc ();
329
+ p_blocks[i] = mem_pool.try_alloc ();
316
330
} else {
317
- p_blocks[i] = mem_pool.calloc ();
331
+ p_blocks[i] = mem_pool.try_calloc ();
318
332
}
319
333
320
334
/* Init block. */
@@ -332,9 +346,9 @@ void test_mem_pool_free_realloc_last_complex(AllocType atype)
332
346
333
347
/* Try to allocate another block (one block is now available). */
334
348
if (atype == ALLOC) {
335
- p_blocks[numOfEntries - 1 ] = mem_pool.alloc ();
349
+ p_blocks[numOfEntries - 1 ] = mem_pool.try_alloc ();
336
350
} else {
337
- p_blocks[numOfEntries - 1 ] = mem_pool.calloc ();
351
+ p_blocks[numOfEntries - 1 ] = mem_pool.try_calloc ();
338
352
}
339
353
340
354
/* Show that memory pool block has been now allocated. */
@@ -346,7 +360,7 @@ void test_mem_pool_free_realloc_last_complex(AllocType atype)
346
360
}
347
361
}
348
362
349
- /* Template for functional tests for alloc (), calloc () functions
363
+ /* Template for functional tests for try_alloc (), try_calloc () functions
350
364
* of MemoryPool object.
351
365
*
352
366
* Basic memory pool block type is used.
@@ -368,9 +382,9 @@ void test_mem_pool_free_realloc_first(AllocType atype)
368
382
/* Allocate all available blocks. */
369
383
for (i = 0 ; i < numOfEntries; i++) {
370
384
if (atype == ALLOC) {
371
- p_blocks[i] = mem_pool.alloc ();
385
+ p_blocks[i] = mem_pool.try_alloc ();
372
386
} else {
373
- p_blocks[i] = mem_pool.calloc ();
387
+ p_blocks[i] = mem_pool.try_calloc ();
374
388
}
375
389
376
390
/* Init block. */
@@ -388,9 +402,9 @@ void test_mem_pool_free_realloc_first(AllocType atype)
388
402
389
403
/* Try to allocate another block (one block is now available). */
390
404
if (atype == ALLOC) {
391
- p_blocks[0 ] = mem_pool.alloc ();
405
+ p_blocks[0 ] = mem_pool.try_alloc ();
392
406
} else {
393
- p_blocks[0 ] = mem_pool.calloc ();
407
+ p_blocks[0 ] = mem_pool.try_calloc ();
394
408
}
395
409
396
410
/* Show that memory pool block has been now allocated. */
@@ -402,7 +416,7 @@ void test_mem_pool_free_realloc_first(AllocType atype)
402
416
}
403
417
}
404
418
405
- /* Template for functional tests for alloc (), calloc () functions
419
+ /* Template for functional tests for try_alloc (), try_calloc () functions
406
420
* of MemoryPool object.
407
421
*
408
422
* Complex memory pool block type is used.
@@ -424,9 +438,9 @@ void test_mem_pool_free_realloc_first_complex(AllocType atype)
424
438
/* Allocate all available blocks. */
425
439
for (i = 0 ; i < numOfEntries; i++) {
426
440
if (atype == ALLOC) {
427
- p_blocks[i] = mem_pool.alloc ();
441
+ p_blocks[i] = mem_pool.try_alloc ();
428
442
} else {
429
- p_blocks[i] = mem_pool.calloc ();
443
+ p_blocks[i] = mem_pool.try_calloc ();
430
444
}
431
445
432
446
/* Init block. */
@@ -444,9 +458,9 @@ void test_mem_pool_free_realloc_first_complex(AllocType atype)
444
458
445
459
/* Try to allocate another block (one block is now available). */
446
460
if (atype == ALLOC) {
447
- p_blocks[0 ] = mem_pool.alloc ();
461
+ p_blocks[0 ] = mem_pool.try_alloc ();
448
462
} else {
449
- p_blocks[0 ] = mem_pool.calloc ();
463
+ p_blocks[0 ] = mem_pool.try_calloc ();
450
464
}
451
465
452
466
/* Show that memory pool block has been now allocated. */
@@ -458,7 +472,7 @@ void test_mem_pool_free_realloc_first_complex(AllocType atype)
458
472
}
459
473
}
460
474
461
- /* Test alloc timeout
475
+ /* Test try_alloc_for/try_alloc_until timeout
462
476
*
463
477
* Given a pool with one slot for int data
464
478
* When a thread tries to allocate two blocks with @ TEST_TIMEOUT timeout
@@ -471,18 +485,18 @@ void test_mem_pool_timeout()
471
485
Timer timer;
472
486
timer.start ();
473
487
474
- int *item = mem_pool.alloc_for (TEST_TIMEOUT);
488
+ int *item = mem_pool.try_alloc_for (TEST_TIMEOUT);
475
489
TEST_ASSERT_NOT_NULL (item);
476
- TEST_ASSERT_UINT32_WITHIN (TEST_TIMEOUT * 100 , 0 , timer.read_us ());
490
+ TEST_ASSERT_DURATION_WITHIN (TEST_TIMEOUT / 10 , 0ms , timer.elapsed_time ());
477
491
478
- item = mem_pool.alloc_for (TEST_TIMEOUT);
492
+ item = mem_pool.try_alloc_for (TEST_TIMEOUT);
479
493
TEST_ASSERT_NULL (item);
480
- TEST_ASSERT_UINT32_WITHIN (TEST_TIMEOUT * 100 , TEST_TIMEOUT * 1000 , timer.read_us ());
494
+ TEST_ASSERT_DURATION_WITHIN (TEST_TIMEOUT / 10 , TEST_TIMEOUT, timer.elapsed_time ());
481
495
482
- uint64_t end_time = Kernel::get_ms_count () + TEST_TIMEOUT;
483
- item = mem_pool.alloc_until (end_time);
496
+ auto end_time = Kernel::Clock::now () + TEST_TIMEOUT;
497
+ item = mem_pool.try_alloc_until (end_time);
484
498
TEST_ASSERT_NULL (item);
485
- TEST_ASSERT_UINT64_WITHIN (TEST_TIMEOUT * 100 , end_time, Kernel::get_ms_count ());
499
+ TEST_ASSERT_TIME_POINT_WITHIN (TEST_TIMEOUT / 10 , end_time, Kernel::Clock::now ());
486
500
}
487
501
488
502
namespace {
@@ -516,18 +530,18 @@ void test_mem_pool_waitforever()
516
530
Timer timer;
517
531
timer.start ();
518
532
519
- int *item = pool.alloc_for (osWaitForever );
533
+ int *item = pool.try_alloc_for (Kernel::wait_for_u32_forever );
520
534
TEST_ASSERT_NOT_NULL (item);
521
- TEST_ASSERT_UINT32_WITHIN (TEST_TIMEOUT * 100 , 0 , timer.read_us ());
535
+ TEST_ASSERT_DURATION_WITHIN (TEST_TIMEOUT / 10 , 0ms , timer.elapsed_time ());
522
536
523
537
struct free_capture to_free;
524
538
to_free.pool = &pool;
525
539
to_free.item = item;
526
540
t.start (callback (free_int_item, &to_free));
527
541
528
- item = pool.alloc_for (osWaitForever );
542
+ item = pool.try_alloc_for (Kernel::wait_for_u32_forever );
529
543
TEST_ASSERT_EQUAL (item, to_free.item );
530
- TEST_ASSERT_UINT32_WITHIN (TEST_TIMEOUT * 100 , TEST_TIMEOUT * 1000 , timer.read_us ());
544
+ TEST_ASSERT_DURATION_WITHIN (TEST_TIMEOUT / 10 , TEST_TIMEOUT, timer.elapsed_time ());
531
545
532
546
t.join ();
533
547
}
@@ -632,12 +646,12 @@ void test_mem_pool_alloc_fail_wrapper()
632
646
}
633
647
634
648
Case cases[] = {
635
- Case (" Test: alloc ()/calloc () - success, 4 bytes b_type, q_size equal to 1." , test_mem_pool_alloc_success_wrapper<int , 1 >),
636
- Case (" Test: alloc ()/calloc () - success, 4 bytes b_type, q_size equal to 3." , test_mem_pool_alloc_success_wrapper<int , 3 >),
637
- Case (" Test: alloc ()/calloc () - success, 1 bytes b_type, q_size equal to 1." , test_mem_pool_alloc_success_wrapper<char , 1 >),
638
- Case (" Test: alloc ()/calloc () - success, 1 bytes b_type, q_size equal to 3." , test_mem_pool_alloc_success_wrapper<char , 3 >),
639
- Case (" Test: alloc ()/calloc () - success, complex b_type, q_size equal to 1." , test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 1 >),
640
- Case (" Test: alloc ()/calloc () - success, complex b_type, q_size equal to 3." , test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 3 >),
649
+ Case (" Test: try_alloc ()/try_calloc () - success, 4 bytes b_type, q_size equal to 1." , test_mem_pool_alloc_success_wrapper<int , 1 >),
650
+ Case (" Test: try_alloc ()/try_calloc () - success, 4 bytes b_type, q_size equal to 3." , test_mem_pool_alloc_success_wrapper<int , 3 >),
651
+ Case (" Test: try_alloc ()/try_calloc () - success, 1 bytes b_type, q_size equal to 1." , test_mem_pool_alloc_success_wrapper<char , 1 >),
652
+ Case (" Test: try_alloc ()/try_calloc () - success, 1 bytes b_type, q_size equal to 3." , test_mem_pool_alloc_success_wrapper<char , 3 >),
653
+ Case (" Test: try_alloc ()/try_calloc () - success, complex b_type, q_size equal to 1." , test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 1 >),
654
+ Case (" Test: try_alloc ()/try_calloc () - success, complex b_type, q_size equal to 3." , test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 3 >),
641
655
642
656
Case (" Test: free() - success, 4 bytes b_type, q_size equal to 1." , test_mem_pool_free_success_wrapper<int , 1 >),
643
657
Case (" Test: free() - success, 4 bytes b_type, q_size equal to 3." , test_mem_pool_free_success_wrapper<int , 3 >),
0 commit comments