Branch data Line data Source code
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 : 0 : DEFINE_STUB(ftl_bitmap_create, struct ftl_bitmap *, (void *buf, size_t size), NULL);
19 : 0 : DEFINE_STUB_V(ftl_bitmap_destroy, (struct ftl_bitmap *bitmap));
20 [ # # ]: 0 : DEFINE_STUB(ftl_bitmap_get, bool, (const struct ftl_bitmap *bitmap, uint64_t bit), true);
21 : 0 : DEFINE_STUB_V(ftl_bitmap_set, (struct ftl_bitmap *bitmap, uint64_t bit));
22 : 0 : 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 : 4 : test_ftl_mempool_create(void)
28 : : {
29 : : struct ftl_mempool *mpool;
30 : :
31 : : /* improper value of alignment */
32 : 4 : mpool = ftl_mempool_create(COUNT, SIZE, ALIGNMENT + 1, SOCKET_ID_ANY);
33 : 4 : CU_ASSERT_EQUAL(mpool, NULL);
34 : 4 : }
35 : :
36 : : static void
37 : 4 : test_ftl_mempool_get_put(void)
38 : : {
39 : 4 : void *elem[COUNT];
40 : : void *elem_empty;
41 : 4 : void *elem_first = SLIST_FIRST(&g_mpool->list);
42 : : struct ftl_mempool_element *ftl_elem;
43 : : int i;
44 [ + + ]: 68 : for (i = 0; i < COUNT; i++) {
45 : 64 : elem[i] = ftl_mempool_get(g_mpool);
46 : 64 : ftl_elem = elem[i];
47 : 64 : CU_ASSERT_EQUAL(ftl_elem->entry.sle_next, SLIST_FIRST(&g_mpool->list));
48 : : }
49 : :
50 : 4 : CU_ASSERT(SLIST_EMPTY(&g_mpool->list));
51 : :
52 : 4 : elem_empty = ftl_mempool_get(g_mpool);
53 : 4 : CU_ASSERT_EQUAL(elem_empty, NULL);
54 : :
55 [ + + ]: 68 : for (i = COUNT - 1; i >= 0; i--) {
56 : 64 : ftl_mempool_put(g_mpool, elem[i]);
57 : 64 : CU_ASSERT_EQUAL(SLIST_FIRST(&g_mpool->list), elem[i]);
58 : : }
59 : :
60 : 4 : CU_ASSERT_EQUAL(SLIST_FIRST(&g_mpool->list), elem_first);
61 : 4 : }
62 : :
63 : : static int
64 : 4 : test_setup(void)
65 : : {
66 : 4 : g_mpool = ftl_mempool_create(COUNT, SIZE, ALIGNMENT, SOCKET_ID_ANY);
67 [ - + ]: 4 : if (!g_mpool) {
68 : 0 : return -ENOMEM;
69 : : }
70 : :
71 : 4 : return 0;
72 : : }
73 : :
74 : : static int
75 : 4 : test_cleanup(void)
76 : : {
77 : 4 : ftl_mempool_destroy(g_mpool);
78 : 4 : g_mpool = NULL;
79 : 4 : return 0;
80 : : }
81 : :
82 : : int
83 : 4 : main(int argc, char **argv)
84 : : {
85 : 4 : CU_pSuite suite = NULL;
86 : : unsigned int num_failures;
87 : :
88 : 4 : CU_initialize_registry();
89 : :
90 : 4 : suite = CU_add_suite("ftl_mempool", test_setup, test_cleanup);
91 : 4 : CU_ADD_TEST(suite, test_ftl_mempool_create);
92 : 4 : CU_ADD_TEST(suite, test_ftl_mempool_get_put);
93 : :
94 : 4 : num_failures = spdk_ut_run_tests(argc, argv, NULL);
95 : 4 : CU_cleanup_registry();
96 : :
97 : 4 : return num_failures;
98 : : }
|