1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) Intel Corporation. 3 * All rights reserved. 4 */ 5 6 #include "spdk/stdinc.h" 7 8 #include "spdk_cunit.h" 9 #include "common/lib/test_env.c" 10 11 #include "ftl/utils/ftl_mempool.c" 12 13 #define COUNT 16 14 #define ALIGNMENT 64 15 #define SIZE (ALIGNMENT * 2) 16 #define SOCKET_ID_ANY -1 17 18 static struct ftl_mempool *g_mpool; 19 20 static void 21 test_ftl_mempool_create(void) 22 { 23 struct ftl_mempool *mpool; 24 25 /* improper value of alignment */ 26 mpool = ftl_mempool_create(COUNT, SIZE, ALIGNMENT + 1, SOCKET_ID_ANY); 27 CU_ASSERT_EQUAL(mpool, NULL); 28 } 29 30 static void 31 test_ftl_mempool_get_put(void) 32 { 33 void *elem[COUNT]; 34 void *elem_empty; 35 void *elem_first = SLIST_FIRST(&g_mpool->list); 36 struct ftl_mempool_element *ftl_elem; 37 int i; 38 for (i = 0; i < COUNT; i++) { 39 elem[i] = ftl_mempool_get(g_mpool); 40 ftl_elem = elem[i]; 41 CU_ASSERT_EQUAL(ftl_elem->entry.sle_next, SLIST_FIRST(&g_mpool->list)); 42 } 43 44 CU_ASSERT(SLIST_EMPTY(&g_mpool->list)); 45 46 elem_empty = ftl_mempool_get(g_mpool); 47 CU_ASSERT_EQUAL(elem_empty, NULL); 48 49 for (i = COUNT - 1; i >= 0; i--) { 50 ftl_mempool_put(g_mpool, elem[i]); 51 CU_ASSERT_EQUAL(SLIST_FIRST(&g_mpool->list), elem[i]); 52 } 53 54 CU_ASSERT_EQUAL(SLIST_FIRST(&g_mpool->list), elem_first); 55 } 56 57 static int 58 test_setup(void) 59 { 60 g_mpool = ftl_mempool_create(COUNT, SIZE, ALIGNMENT, SOCKET_ID_ANY); 61 if (!g_mpool) { 62 return -ENOMEM; 63 } 64 65 return 0; 66 } 67 68 static int 69 test_cleanup(void) 70 { 71 ftl_mempool_destroy(g_mpool); 72 g_mpool = NULL; 73 return 0; 74 } 75 76 int 77 main(int argc, char **argv) 78 { 79 CU_pSuite suite = NULL; 80 unsigned int num_failures; 81 82 CU_set_error_action(CUEA_ABORT); 83 CU_initialize_registry(); 84 85 suite = CU_add_suite("ftl_mempool", test_setup, test_cleanup); 86 CU_ADD_TEST(suite, test_ftl_mempool_create); 87 CU_ADD_TEST(suite, test_ftl_mempool_get_put); 88 89 CU_basic_set_mode(CU_BRM_VERBOSE); 90 CU_basic_run_tests(); 91 num_failures = CU_get_number_of_failures(); 92 CU_cleanup_registry(); 93 94 return num_failures; 95 } 96