Imre Kis | fdaaf8c | 2019-12-16 23:53:41 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | extern "C" { |
| 8 | #include "lib/object_pool.h" |
| 9 | } |
| 10 | #include "object_pool_allocator_wrapper.h" |
| 11 | #include "common/mock_debug.h" |
| 12 | #include "CppUTest/TestHarness.h" |
| 13 | #include "CppUTestExt/MockSupport.h" |
| 14 | #include <string.h> |
| 15 | |
| 16 | #define POOL_TEST_VALUE0 (0x01234567) |
| 17 | #define POOL_TEST_VALUE1 (0xf0123456) |
| 18 | |
| 19 | TEST_GROUP(object_pool) { |
| 20 | TEST_SETUP() { |
| 21 | memset(array, 0x00, sizeof(array)); |
| 22 | object_pool_allocate(&pool); // Invoking C wrapper |
| 23 | } |
| 24 | |
| 25 | TEST_TEARDOWN() { |
| 26 | mock().checkExpectations(); |
| 27 | mock().clear(); |
| 28 | } |
| 29 | |
| 30 | void check_remaining_fields(size_t start) { |
| 31 | for (size_t i = start; i < POOL_SIZE; i++) { |
| 32 | UNSIGNED_LONGS_EQUAL(0, array[i]); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | struct object_pool pool = {0}; |
| 37 | }; |
| 38 | |
| 39 | TEST(object_pool, allocate_one) { |
| 40 | uint32_t *first = (uint32_t *)pool_alloc(&pool); |
| 41 | *first = POOL_TEST_VALUE0; |
| 42 | |
| 43 | UNSIGNED_LONGS_EQUAL(POOL_TEST_VALUE0, array[0]); |
| 44 | check_remaining_fields(1); |
| 45 | } |
| 46 | |
| 47 | TEST(object_pool, allocate_two) { |
| 48 | uint32_t *first = (uint32_t *)pool_alloc(&pool); |
| 49 | *first = POOL_TEST_VALUE0; |
| 50 | |
| 51 | uint32_t *second = (uint32_t *)pool_alloc(&pool); |
| 52 | *second = POOL_TEST_VALUE1; |
| 53 | |
| 54 | UNSIGNED_LONGS_EQUAL(POOL_TEST_VALUE0, array[0]); |
| 55 | UNSIGNED_LONGS_EQUAL(POOL_TEST_VALUE1, array[1]); |
| 56 | check_remaining_fields(2); |
| 57 | } |
| 58 | |
| 59 | TEST(object_pool, allocate_two_at_once) { |
| 60 | uint32_t *items = (uint32_t *)pool_alloc_n(&pool, 2); |
| 61 | items[0] = POOL_TEST_VALUE0; |
| 62 | items[1] = POOL_TEST_VALUE1; |
| 63 | |
| 64 | UNSIGNED_LONGS_EQUAL(POOL_TEST_VALUE0, array[0]); |
| 65 | UNSIGNED_LONGS_EQUAL(POOL_TEST_VALUE1, array[1]); |
| 66 | check_remaining_fields(2); |
| 67 | } |
| 68 | |
| 69 | TEST(object_pool, allocate_one_then_overflow) { |
| 70 | panic_environment_t env; |
| 71 | |
| 72 | uint32_t *first = (uint32_t*) pool_alloc(&pool); |
| 73 | *first = POOL_TEST_VALUE0; |
| 74 | |
| 75 | if (SETUP_PANIC_ENVIRONMENT(env)) { |
| 76 | expect_tf_log( |
| 77 | "Cannot allocate 16 objects out of pool (15 objects left).\n"); |
| 78 | pool_alloc_n(&pool, POOL_SIZE); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | TEST(object_pool, overflow) { |
| 83 | panic_environment_t env; |
| 84 | |
| 85 | if (SETUP_PANIC_ENVIRONMENT(env)) { |
| 86 | expect_tf_log("Cannot allocate 17 objects out of pool (16 objects left).\n"); |
| 87 | pool_alloc_n(&pool, POOL_SIZE + 1); |
| 88 | } |
| 89 | } |