|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2024 Intel Corporation. All rights reserved. |
| 4 | +// |
| 5 | +// Author: Jyri Sarha <jyri.sarha@linux.intel.com> |
| 6 | + |
| 7 | +#include <sof/lib/fast-get.h> |
| 8 | +#include <rtos/sof.h> |
| 9 | +#include <rtos/alloc.h> |
| 10 | +#include <sof/lib/mm_heap.h> |
| 11 | +#include <sof/lib/memory.h> |
| 12 | +#include <sof/common.h> |
| 13 | + |
| 14 | +#include <stdarg.h> |
| 15 | +#include <stddef.h> |
| 16 | +#include <setjmp.h> |
| 17 | +#include <stdint.h> |
| 18 | +#include <stdlib.h> |
| 19 | +#include <string.h> |
| 20 | +#include <cmocka.h> |
| 21 | +#include <assert.h> |
| 22 | + |
| 23 | +static const int testdata[33][100] = { |
| 24 | + { |
| 25 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 26 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 27 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 28 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 29 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 30 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 31 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 32 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 33 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 34 | + 1, 2, 3, 4, 5, 6, 7, 9, 0, |
| 35 | + }, |
| 36 | + { 2 }, |
| 37 | + { 3 }, |
| 38 | + { 4 }, |
| 39 | + { 5 }, |
| 40 | + { 6 }, |
| 41 | + { 7 }, |
| 42 | + { 8 }, |
| 43 | + { 9 }, |
| 44 | + { 10 }, |
| 45 | + { 11 }, |
| 46 | + { 12 }, |
| 47 | + { 13 }, |
| 48 | + { 14 }, |
| 49 | + { 15 }, |
| 50 | + { 16 }, |
| 51 | + { 17 }, |
| 52 | + { 18 }, |
| 53 | + { 19 }, |
| 54 | + { 20 }, |
| 55 | + { 21 }, |
| 56 | + { 23 }, |
| 57 | + { 24 }, |
| 58 | + { 25 }, |
| 59 | + { 26 }, |
| 60 | + { 27 }, |
| 61 | + { 28 }, |
| 62 | + { 29 }, |
| 63 | + { 30 }, |
| 64 | + { 31 }, |
| 65 | + { 32 }, |
| 66 | + { 33 }, |
| 67 | +}; |
| 68 | + |
| 69 | +static void test_simple_fast_get_put(void **state) |
| 70 | +{ |
| 71 | + const void *ret; |
| 72 | + |
| 73 | + (void)state; /* unused */ |
| 74 | + |
| 75 | + ret = fast_get(testdata[0], sizeof(testdata[0])); |
| 76 | + |
| 77 | + assert(ret); |
| 78 | + assert(!memcmp(ret, testdata[0], sizeof(testdata[0]))); |
| 79 | + |
| 80 | + fast_put(ret); |
| 81 | +} |
| 82 | + |
| 83 | +static void test_fast_get_size_missmatch_test(void **state) |
| 84 | +{ |
| 85 | + const void *ret[2]; |
| 86 | + |
| 87 | + (void)state; /* unused */ |
| 88 | + |
| 89 | + ret[0] = fast_get(testdata[0], sizeof(testdata[0])); |
| 90 | + |
| 91 | + assert(ret[0]); |
| 92 | + assert(!memcmp(ret[0], testdata[0], sizeof(testdata[0]))); |
| 93 | + |
| 94 | + ret[1] = fast_get(testdata[0], sizeof(testdata[0]) + 1); |
| 95 | + assert(!ret[1]); |
| 96 | + |
| 97 | + fast_put(ret); |
| 98 | +} |
| 99 | + |
| 100 | +static void test_over_32_fast_gets_and_puts(void **state) |
| 101 | +{ |
| 102 | + const void *copy[ARRAY_SIZE(testdata)]; |
| 103 | + int i; |
| 104 | + |
| 105 | + (void)state; /* unused */ |
| 106 | + |
| 107 | + for (i = 0; i < ARRAY_SIZE(copy); i++) |
| 108 | + copy[i] = fast_get(testdata[i], sizeof(testdata[0])); |
| 109 | + |
| 110 | + for (i = 0; i < ARRAY_SIZE(copy); i++) |
| 111 | + assert(!memcmp(copy[i], testdata[i], sizeof(testdata[0]))); |
| 112 | + |
| 113 | + for (i = 0; i < ARRAY_SIZE(copy); i++) |
| 114 | + fast_put(copy[i]); |
| 115 | +} |
| 116 | + |
| 117 | +static void test_fast_get_refcounting(void **state) |
| 118 | +{ |
| 119 | + const void *copy[2][ARRAY_SIZE(testdata)]; |
| 120 | + int i; |
| 121 | + (void)state; /* unused */ |
| 122 | + |
| 123 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 124 | + copy[0][i] = fast_get(testdata[i], sizeof(testdata[0])); |
| 125 | + |
| 126 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 127 | + copy[1][i] = fast_get(testdata[i], sizeof(testdata[0])); |
| 128 | + |
| 129 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 130 | + assert(copy[0][i] == copy[1][i]); |
| 131 | + |
| 132 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 133 | + assert(!memcmp(copy[0][i], testdata[i], sizeof(testdata[0]))); |
| 134 | + |
| 135 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 136 | + fast_put(copy[0][i]); |
| 137 | + |
| 138 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 139 | + assert(!memcmp(copy[1][i], testdata[i], sizeof(testdata[0]))); |
| 140 | + |
| 141 | + for (i = 0; i < ARRAY_SIZE(copy[0]); i++) |
| 142 | + fast_put(copy[1][i]); |
| 143 | +} |
| 144 | + |
| 145 | +int main(void) |
| 146 | +{ |
| 147 | + const struct CMUnitTest tests[] = { |
| 148 | + cmocka_unit_test(test_simple_fast_get_put), |
| 149 | + cmocka_unit_test(test_fast_get_size_missmatch_test), |
| 150 | + cmocka_unit_test(test_over_32_fast_gets_and_puts), |
| 151 | + cmocka_unit_test(test_fast_get_refcounting), |
| 152 | + }; |
| 153 | + |
| 154 | + cmocka_set_message_output(CM_OUTPUT_TAP); |
| 155 | + |
| 156 | + return cmocka_run_group_tests(tests, NULL, NULL); |
| 157 | +} |
| 158 | + |
| 159 | +void *__wrap_rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes); |
| 160 | +void *__wrap_rmalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes); |
| 161 | +void __wrap_rfree(void *ptr); |
| 162 | + |
| 163 | +void *__wrap_rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes) |
| 164 | +{ |
| 165 | + void *ret; |
| 166 | + (void)zone; |
| 167 | + (void)flags; |
| 168 | + (void)caps; |
| 169 | + |
| 170 | + ret = malloc(bytes); |
| 171 | + |
| 172 | + assert(ret); |
| 173 | + |
| 174 | + memset(ret, 0, bytes); |
| 175 | + |
| 176 | + return ret; |
| 177 | +} |
| 178 | + |
| 179 | +void *__wrap_rmalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes) |
| 180 | +{ |
| 181 | + void *ret; |
| 182 | + (void)zone; |
| 183 | + (void)flags; |
| 184 | + (void)caps; |
| 185 | + |
| 186 | + ret = malloc(bytes); |
| 187 | + |
| 188 | + assert(ret); |
| 189 | + |
| 190 | + return ret; |
| 191 | +} |
| 192 | + |
| 193 | +void __wrap_rfree(void *ptr) |
| 194 | +{ |
| 195 | + free(ptr); |
| 196 | +} |
0 commit comments