xref: /spdk/test/unit/lib/blobfs/tree.c/tree_ut.c (revision 4d43844f4d50a7abd5b6029ff3f2baff329890a8)
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 #include "tree.h"
36 
37 #include "tree.c"
38 
39 void
40 spdk_cache_buffer_free(struct cache_buffer *cache_buffer)
41 {
42 	free(cache_buffer);
43 }
44 
45 static void
46 blobfs_tree_op_test(void)
47 {
48 	struct cache_tree *tree;
49 	struct cache_buffer *buffer[5];
50 	struct cache_buffer *tmp_buffer;
51 	int i;
52 
53 	for (i = 0; i < 5; i ++) {
54 		buffer[i] = calloc(1, sizeof(struct cache_buffer));
55 		SPDK_CU_ASSERT_FATAL(buffer[i]);
56 	}
57 
58 	tree = calloc(1, sizeof(*tree));
59 	SPDK_CU_ASSERT_FATAL(tree != NULL);
60 
61 	/* insert buffer[0] */
62 	buffer[0]->offset = 0;
63 	tree = spdk_tree_insert_buffer(tree, buffer[0]);
64 	SPDK_CU_ASSERT_FATAL(tree != NULL);
65 	CU_ASSERT(tree->level == 0);
66 	tmp_buffer = spdk_tree_find_buffer(tree, buffer[0]->offset);
67 	CU_ASSERT(tmp_buffer == buffer[0]);
68 
69 	/* insert buffer[1] */
70 	buffer[1]->offset = CACHE_BUFFER_SIZE;
71 	/* set the bytes_filled equal = bytes_filled with same non zero value, e.g., 32 */
72 	buffer[1]->bytes_filled = buffer[1]->bytes_flushed = 32;
73 	tree = spdk_tree_insert_buffer(tree, buffer[1]);
74 	SPDK_CU_ASSERT_FATAL(tree != NULL);
75 	CU_ASSERT(tree->level == 0);
76 	tmp_buffer = spdk_tree_find_filled_buffer(tree, buffer[1]->offset);
77 	CU_ASSERT(tmp_buffer == buffer[1]);
78 
79 	/* insert buffer[2] */
80 	buffer[2]->offset = (CACHE_TREE_WIDTH - 1) * CACHE_BUFFER_SIZE;
81 	tree = spdk_tree_insert_buffer(tree, buffer[2]);
82 	SPDK_CU_ASSERT_FATAL(tree != NULL);
83 	CU_ASSERT(tree->level == 0);
84 	tmp_buffer = spdk_tree_find_buffer(tree, buffer[2]->offset);
85 	CU_ASSERT(tmp_buffer == buffer[2]);
86 	tmp_buffer = spdk_tree_find_filled_buffer(tree, buffer[2]->offset);
87 	CU_ASSERT(tmp_buffer == NULL);
88 
89 	/* insert buffer[3], set an offset which can not be fit level 0 */
90 	buffer[3]->offset = CACHE_TREE_LEVEL_SIZE(1);
91 	tree = spdk_tree_insert_buffer(tree, buffer[3]);
92 	SPDK_CU_ASSERT_FATAL(tree != NULL);
93 	CU_ASSERT(tree->level == 1);
94 	tmp_buffer = spdk_tree_find_buffer(tree, buffer[3]->offset);
95 	CU_ASSERT(tmp_buffer == buffer[3]);
96 
97 	/*  insert buffer[4], set an offset which can not be fit level 1 */
98 	buffer[4]->offset = CACHE_TREE_LEVEL_SIZE(2);
99 	tree = spdk_tree_insert_buffer(tree, buffer[4]);
100 	SPDK_CU_ASSERT_FATAL(tree != NULL);
101 	CU_ASSERT(tree->level == 2);
102 	tmp_buffer = spdk_tree_find_buffer(tree, buffer[4]->offset);
103 	CU_ASSERT(tmp_buffer == buffer[4]);
104 
105 	/* delete buffer[0] */
106 	spdk_tree_remove_buffer(tree, buffer[0]);
107 	/* check whether buffer[0] is still existed or not */
108 	tmp_buffer = spdk_tree_find_buffer(tree, 0);
109 	CU_ASSERT(tmp_buffer == NULL);
110 
111 	/* delete buffer[3] */
112 	spdk_tree_remove_buffer(tree, buffer[3]);
113 	/* check whether buffer[3] is still existed or not */
114 	tmp_buffer = spdk_tree_find_buffer(tree, CACHE_TREE_LEVEL_SIZE(1));
115 	CU_ASSERT(tmp_buffer == NULL);
116 
117 	/* free all buffers in the tree */
118 	spdk_tree_free_buffers(tree);
119 
120 	/* check whether buffer[1] is still existed or not */
121 	tmp_buffer = spdk_tree_find_buffer(tree, CACHE_BUFFER_SIZE);
122 	CU_ASSERT(tmp_buffer == NULL);
123 	/* check whether buffer[2] is still existed or not */
124 	tmp_buffer = spdk_tree_find_buffer(tree, (CACHE_TREE_WIDTH - 1) * CACHE_BUFFER_SIZE);
125 	CU_ASSERT(tmp_buffer == NULL);
126 	/* check whether buffer[4] is still existed or not */
127 	tmp_buffer = spdk_tree_find_buffer(tree, CACHE_TREE_LEVEL_SIZE(2));
128 	CU_ASSERT(tmp_buffer == NULL);
129 
130 	/* According to spdk_tree_free_buffers, root will not be freed */
131 	free(tree);
132 }
133 
134 int main(int argc, char **argv)
135 {
136 	CU_pSuite	suite = NULL;
137 	unsigned int	num_failures;
138 
139 	if (CU_initialize_registry() != CUE_SUCCESS) {
140 		return CU_get_error();
141 	}
142 
143 	suite = CU_add_suite("tree", NULL, NULL);
144 	if (suite == NULL) {
145 		CU_cleanup_registry();
146 		return CU_get_error();
147 	}
148 
149 	if (CU_add_test(suite, "blobfs_tree_op_test", blobfs_tree_op_test) == NULL) {
150 		CU_cleanup_registry();
151 		return CU_get_error();
152 	}
153 
154 	CU_basic_set_mode(CU_BRM_VERBOSE);
155 	CU_basic_run_tests();
156 	num_failures = CU_get_number_of_failures();
157 	CU_cleanup_registry();
158 
159 	return num_failures;
160 }
161