xref: /spdk/test/unit/lib/blobfs/tree.c/tree_ut.c (revision 0ed85362c8132a2d1927757fbcade66b6660d26a)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk_cunit.h"
35 
36 #include "blobfs/tree.c"
37 
38 void
39 cache_buffer_free(struct cache_buffer *cache_buffer)
40 {
41 	free(cache_buffer);
42 }
43 
44 static void
45 blobfs_tree_op_test(void)
46 {
47 	struct cache_tree *tree;
48 	struct cache_buffer *buffer[5];
49 	struct cache_buffer *tmp_buffer;
50 	int i;
51 
52 	for (i = 0; i < 5; i ++) {
53 		buffer[i] = calloc(1, sizeof(struct cache_buffer));
54 		SPDK_CU_ASSERT_FATAL(buffer[i]);
55 	}
56 
57 	tree = calloc(1, sizeof(*tree));
58 	SPDK_CU_ASSERT_FATAL(tree != NULL);
59 
60 	/* insert buffer[0] */
61 	buffer[0]->offset = 0;
62 	tree = tree_insert_buffer(tree, buffer[0]);
63 	SPDK_CU_ASSERT_FATAL(tree != NULL);
64 	CU_ASSERT(tree->level == 0);
65 	tmp_buffer = tree_find_buffer(tree, buffer[0]->offset);
66 	CU_ASSERT(tmp_buffer == buffer[0]);
67 
68 	/* insert buffer[1] */
69 	buffer[1]->offset = CACHE_BUFFER_SIZE;
70 	/* set the bytes_filled equal = bytes_filled with same non zero value, e.g., 32 */
71 	buffer[1]->bytes_filled = buffer[1]->bytes_flushed = 32;
72 	tree = tree_insert_buffer(tree, buffer[1]);
73 	SPDK_CU_ASSERT_FATAL(tree != NULL);
74 	CU_ASSERT(tree->level == 0);
75 	tmp_buffer = tree_find_filled_buffer(tree, buffer[1]->offset);
76 	CU_ASSERT(tmp_buffer == buffer[1]);
77 
78 	/* insert buffer[2] */
79 	buffer[2]->offset = (CACHE_TREE_WIDTH - 1) * CACHE_BUFFER_SIZE;
80 	tree = tree_insert_buffer(tree, buffer[2]);
81 	SPDK_CU_ASSERT_FATAL(tree != NULL);
82 	CU_ASSERT(tree->level == 0);
83 	tmp_buffer = tree_find_buffer(tree, buffer[2]->offset);
84 	CU_ASSERT(tmp_buffer == buffer[2]);
85 	tmp_buffer = tree_find_filled_buffer(tree, buffer[2]->offset);
86 	CU_ASSERT(tmp_buffer == NULL);
87 
88 	/* insert buffer[3], set an offset which can not be fit level 0 */
89 	buffer[3]->offset = CACHE_TREE_LEVEL_SIZE(1);
90 	tree = tree_insert_buffer(tree, buffer[3]);
91 	SPDK_CU_ASSERT_FATAL(tree != NULL);
92 	CU_ASSERT(tree->level == 1);
93 	tmp_buffer = tree_find_buffer(tree, buffer[3]->offset);
94 	CU_ASSERT(tmp_buffer == buffer[3]);
95 
96 	/*  insert buffer[4], set an offset which can not be fit level 1 */
97 	buffer[4]->offset = CACHE_TREE_LEVEL_SIZE(2);
98 	tree = tree_insert_buffer(tree, buffer[4]);
99 	SPDK_CU_ASSERT_FATAL(tree != NULL);
100 	CU_ASSERT(tree->level == 2);
101 	tmp_buffer = tree_find_buffer(tree, buffer[4]->offset);
102 	CU_ASSERT(tmp_buffer == buffer[4]);
103 
104 	/* delete buffer[0] */
105 	tree_remove_buffer(tree, buffer[0]);
106 	/* check whether buffer[0] is still existed or not */
107 	tmp_buffer = tree_find_buffer(tree, 0);
108 	CU_ASSERT(tmp_buffer == NULL);
109 
110 	/* delete buffer[3] */
111 	tree_remove_buffer(tree, buffer[3]);
112 	/* check whether buffer[3] is still existed or not */
113 	tmp_buffer = tree_find_buffer(tree, CACHE_TREE_LEVEL_SIZE(1));
114 	CU_ASSERT(tmp_buffer == NULL);
115 
116 	/* free all buffers in the tree */
117 	tree_free_buffers(tree);
118 
119 	/* check whether buffer[1] is still existed or not */
120 	tmp_buffer = tree_find_buffer(tree, CACHE_BUFFER_SIZE);
121 	CU_ASSERT(tmp_buffer == NULL);
122 	/* check whether buffer[2] is still existed or not */
123 	tmp_buffer = tree_find_buffer(tree, (CACHE_TREE_WIDTH - 1) * CACHE_BUFFER_SIZE);
124 	CU_ASSERT(tmp_buffer == NULL);
125 	/* check whether buffer[4] is still existed or not */
126 	tmp_buffer = tree_find_buffer(tree, CACHE_TREE_LEVEL_SIZE(2));
127 	CU_ASSERT(tmp_buffer == NULL);
128 
129 	/* According to tree_free_buffers, root will not be freed */
130 	free(tree);
131 }
132 
133 int main(int argc, char **argv)
134 {
135 	CU_pSuite	suite = NULL;
136 	unsigned int	num_failures;
137 
138 	CU_set_error_action(CUEA_ABORT);
139 	CU_initialize_registry();
140 
141 	suite = CU_add_suite("tree", NULL, NULL);
142 	CU_ADD_TEST(suite, blobfs_tree_op_test);
143 
144 	CU_basic_set_mode(CU_BRM_VERBOSE);
145 	CU_basic_run_tests();
146 	num_failures = CU_get_number_of_failures();
147 	CU_cleanup_registry();
148 
149 	return num_failures;
150 }
151