xref: /spdk/test/unit/lib/ftl/ftl_mempool.c/ftl_mempool_ut.c (revision 45a053c5777494f4e8ce4bc1191c9de3920377f7)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2022 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk_internal/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 DEFINE_STUB(ftl_bitmap_create, struct ftl_bitmap *, (void *buf, size_t size), NULL);
19 DEFINE_STUB_V(ftl_bitmap_destroy, (struct ftl_bitmap *bitmap));
20 DEFINE_STUB(ftl_bitmap_get, bool, (const struct ftl_bitmap *bitmap, uint64_t bit), true);
21 DEFINE_STUB_V(ftl_bitmap_set, (struct ftl_bitmap *bitmap, uint64_t bit));
22 DEFINE_STUB_V(ftl_bitmap_clear, (struct ftl_bitmap *bitmap, uint64_t bit));
23 
24 static struct ftl_mempool *g_mpool;
25 
26 static void
27 test_ftl_mempool_create(void)
28 {
29 	struct ftl_mempool *mpool;
30 
31 	/* improper value of alignment */
32 	mpool = ftl_mempool_create(COUNT, SIZE, ALIGNMENT + 1, SOCKET_ID_ANY);
33 	CU_ASSERT_EQUAL(mpool, NULL);
34 }
35 
36 static void
37 test_ftl_mempool_get_put(void)
38 {
39 	void *elem[COUNT];
40 	void *elem_empty;
41 	void *elem_first = SLIST_FIRST(&g_mpool->list);
42 	struct ftl_mempool_element *ftl_elem;
43 	int i;
44 	for (i = 0; i < COUNT; i++) {
45 		elem[i] = ftl_mempool_get(g_mpool);
46 		ftl_elem = elem[i];
47 		CU_ASSERT_EQUAL(ftl_elem->entry.sle_next, SLIST_FIRST(&g_mpool->list));
48 	}
49 
50 	CU_ASSERT(SLIST_EMPTY(&g_mpool->list));
51 
52 	elem_empty = ftl_mempool_get(g_mpool);
53 	CU_ASSERT_EQUAL(elem_empty, NULL);
54 
55 	for (i = COUNT - 1; i >= 0; i--) {
56 		ftl_mempool_put(g_mpool, elem[i]);
57 		CU_ASSERT_EQUAL(SLIST_FIRST(&g_mpool->list), elem[i]);
58 	}
59 
60 	CU_ASSERT_EQUAL(SLIST_FIRST(&g_mpool->list), elem_first);
61 }
62 
63 static int
64 test_setup(void)
65 {
66 	g_mpool = ftl_mempool_create(COUNT, SIZE, ALIGNMENT, SOCKET_ID_ANY);
67 	if (!g_mpool) {
68 		return -ENOMEM;
69 	}
70 
71 	return 0;
72 }
73 
74 static int
75 test_cleanup(void)
76 {
77 	ftl_mempool_destroy(g_mpool);
78 	g_mpool = NULL;
79 	return 0;
80 }
81 
82 int
83 main(int argc, char **argv)
84 {
85 	CU_pSuite suite = NULL;
86 	unsigned int num_failures;
87 
88 	CU_initialize_registry();
89 
90 	suite = CU_add_suite("ftl_mempool", test_setup, test_cleanup);
91 	CU_ADD_TEST(suite, test_ftl_mempool_create);
92 	CU_ADD_TEST(suite, test_ftl_mempool_get_put);
93 
94 	num_failures = spdk_ut_run_tests(argc, argv, NULL);
95 	CU_cleanup_registry();
96 
97 	return num_failures;
98 }
99