|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2025 Intel Corporation. All rights reserved. |
| 4 | +// |
| 5 | +// These contents may have been developed with support from one or more Intel-operated |
| 6 | +// generative artificial intelligence solutions. |
| 7 | + |
| 8 | +#include <zephyr/ztest.h> |
| 9 | +#include <sof/lib/fast-get.h> |
| 10 | +#include <stdlib.h> |
| 11 | + |
| 12 | +static const int testdata[33][100] = { |
| 13 | + { |
| 14 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 15 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 16 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 17 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 18 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 19 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 20 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 21 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 22 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 23 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 24 | + }, |
| 25 | + { 2 }, |
| 26 | + { 3 }, |
| 27 | + { 4 }, |
| 28 | + { 5 }, |
| 29 | + { 6 }, |
| 30 | + { 7 }, |
| 31 | + { 8 }, |
| 32 | + { 9 }, |
| 33 | + { 10 }, |
| 34 | + { 11 }, |
| 35 | + { 12 }, |
| 36 | + { 13 }, |
| 37 | + { 14 }, |
| 38 | + { 15 }, |
| 39 | + { 16 }, |
| 40 | + { 17 }, |
| 41 | + { 18 }, |
| 42 | + { 19 }, |
| 43 | + { 20 }, |
| 44 | + { 21 }, |
| 45 | + { 23 }, |
| 46 | + { 24 }, |
| 47 | + { 25 }, |
| 48 | + { 26 }, |
| 49 | + { 27 }, |
| 50 | + { 28 }, |
| 51 | + { 29 }, |
| 52 | + { 30 }, |
| 53 | + { 31 }, |
| 54 | + { 32 }, |
| 55 | + { 33 }, |
| 56 | +}; |
| 57 | + |
| 58 | +/* Mock memory allocation functions for testing purposes */ |
| 59 | + |
| 60 | +void *__wrap_rzalloc(uint32_t flags, size_t bytes) |
| 61 | +{ |
| 62 | + void *ret; |
| 63 | + (void)flags; |
| 64 | + |
| 65 | + ret = malloc(bytes); |
| 66 | + |
| 67 | + zassert_not_null(ret, "Memory allocation should not fail"); |
| 68 | + |
| 69 | + memset(ret, 0, bytes); |
| 70 | + |
| 71 | + return ret; |
| 72 | +} |
| 73 | + |
| 74 | +void *__wrap_rmalloc(uint32_t flags, size_t bytes) |
| 75 | +{ |
| 76 | + void *ret; |
| 77 | + (void)flags; |
| 78 | + |
| 79 | + ret = malloc(bytes); |
| 80 | + |
| 81 | + zassert_not_null(ret, "Memory allocation should not fail"); |
| 82 | + |
| 83 | + return ret; |
| 84 | +} |
| 85 | + |
| 86 | +void __wrap_rfree(void *ptr) |
| 87 | +{ |
| 88 | + free(ptr); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief Test basic fast_get and fast_put functionality |
| 93 | + * |
| 94 | + * Tests that fast_get can allocate memory for data and fast_put can free it. |
| 95 | + * Verifies that the returned pointer is valid and data is correctly copied. |
| 96 | + */ |
| 97 | +ZTEST(fast_get_suite, test_simple_fast_get_put) |
| 98 | +{ |
| 99 | + const void *ret; |
| 100 | + |
| 101 | + ret = fast_get(testdata[0], sizeof(testdata[0])); |
| 102 | + |
| 103 | + zassert_not_null(ret, "fast_get should return valid pointer"); |
| 104 | + zassert_mem_equal(ret, testdata[0], sizeof(testdata[0]), |
| 105 | + "Returned data should match original data"); |
| 106 | + |
| 107 | + fast_put(ret); |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * @brief Test fast_get size mismatch behavior |
| 112 | + * |
| 113 | + * Tests that fast_get returns NULL when requested size doesn't match |
| 114 | + * previously allocated size for the same data pointer. |
| 115 | + */ |
| 116 | +ZTEST(fast_get_suite, test_fast_get_size_missmatch_test) |
| 117 | +{ |
| 118 | + const void *ret[2]; |
| 119 | + |
| 120 | + ret[0] = fast_get(testdata[0], sizeof(testdata[0])); |
| 121 | + |
| 122 | + zassert_not_null(ret[0], "First fast_get should succeed"); |
| 123 | + zassert_mem_equal(ret[0], testdata[0], sizeof(testdata[0]), |
| 124 | + "Returned data should match original data"); |
| 125 | + |
| 126 | + ret[1] = fast_get(testdata[0], sizeof(testdata[0]) + 1); |
| 127 | + zassert_is_null(ret[1], "fast_get with different size should return NULL"); |
| 128 | + |
| 129 | + fast_put(ret[0]); |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * @brief Test multiple fast_get and fast_put operations |
| 134 | + * |
| 135 | + * Tests that fast_get can handle more than 32 allocations and that |
| 136 | + * all data is correctly stored and can be retrieved. |
| 137 | + */ |
| 138 | +ZTEST(fast_get_suite, test_over_32_fast_gets_and_puts) |
| 139 | +{ |
| 140 | + const void *copy[ARRAY_SIZE(testdata)]; |
| 141 | + int i; |
| 142 | + |
| 143 | + for (i = 0; i < ARRAY_SIZE(copy); i++) |
| 144 | + copy[i] = fast_get(testdata[i], sizeof(testdata[0])); |
| 145 | + |
| 146 | + for (i = 0; i < ARRAY_SIZE(copy); i++) |
| 147 | + zassert_mem_equal(copy[i], testdata[i], sizeof(testdata[0]), |
| 148 | + "Data at index %d should match original", i); |
| 149 | + |
| 150 | + for (i = 0; i < ARRAY_SIZE(copy); i++) |
| 151 | + fast_put(copy[i]); |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * @brief Test fast_get reference counting functionality |
| 156 | + * |
| 157 | + * Tests that fast_get implements proper reference counting - multiple |
| 158 | + * fast_get calls for the same data should return the same pointer, |
| 159 | + * and the data should remain valid until all references are released. |
| 160 | + */ |
| 161 | +ZTEST(fast_get_suite, test_fast_get_refcounting) |
| 162 | +{ |
| 163 | + const void *copy[2][ARRAY_SIZE(testdata)]; |
| 164 | + int i; |
| 165 | + |
| 166 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 167 | + copy[0][i] = fast_get(testdata[i], sizeof(testdata[0])); |
| 168 | + |
| 169 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 170 | + copy[1][i] = fast_get(testdata[i], sizeof(testdata[0])); |
| 171 | + |
| 172 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 173 | + zassert_equal_ptr(copy[0][i], copy[1][i], |
| 174 | + "Same data should return same pointer (refcounting)"); |
| 175 | + |
| 176 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 177 | + zassert_mem_equal(copy[0][i], testdata[i], sizeof(testdata[0]), |
| 178 | + "Data should match original after multiple fast_get calls"); |
| 179 | + |
| 180 | + /* Release first set of references */ |
| 181 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 182 | + fast_put(copy[0][i]); |
| 183 | + |
| 184 | + /* Data should still be valid through second set of references */ |
| 185 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 186 | + zassert_mem_equal(copy[1][i], testdata[i], sizeof(testdata[0]), |
| 187 | + "Data should remain valid after partial fast_put"); |
| 188 | + |
| 189 | + /* Release second set of references */ |
| 190 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 191 | + fast_put(copy[1][i]); |
| 192 | +} |
| 193 | + |
| 194 | +/** |
| 195 | + * @brief Define and initialize the fast_get test suite |
| 196 | + */ |
| 197 | +ZTEST_SUITE(fast_get_suite, NULL, NULL, NULL, NULL, NULL); |
0 commit comments