unittest_radix_tree.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "../tests/testutil.hpp"
4 
5 #include <radix_tree.hpp>
6 #include <stdint.hpp>
7 
8 #include <set>
9 #include <string>
10 #include <string.h>
11 #include <unity.h>
12 #include <vector>
13 
14 void setUp ()
15 {
16 }
17 void tearDown ()
18 {
19 }
20 
21 bool tree_add (zmq::radix_tree_t &tree_, const std::string &key_)
22 {
23  return tree_.add (reinterpret_cast<const unsigned char *> (key_.data ()),
24  key_.size ());
25 }
26 
27 bool tree_rm (zmq::radix_tree_t &tree_, const std::string &key_)
28 {
29  return tree_.rm (reinterpret_cast<const unsigned char *> (key_.data ()),
30  key_.size ());
31 }
32 
33 bool tree_check (zmq::radix_tree_t &tree_, const std::string &key_)
34 {
35  return tree_.check (reinterpret_cast<const unsigned char *> (key_.data ()),
36  key_.size ());
37 }
38 
39 void test_empty ()
40 {
42 
43  TEST_ASSERT_TRUE (tree.size () == 0);
44 }
45 
47 {
49 
50  TEST_ASSERT_TRUE (tree_add (tree, "foo"));
51 }
52 
54 {
56 
57  TEST_ASSERT_TRUE (tree_add (tree, "test"));
58  TEST_ASSERT_FALSE (tree_add (tree, "test"));
59 }
60 
62 {
64 
65  TEST_ASSERT_FALSE (tree_rm (tree, "test"));
66 }
67 
69 {
71 
72  tree_add (tree, "temporary");
73  TEST_ASSERT_TRUE (tree_rm (tree, "temporary"));
74 }
75 
77 {
79 
80  tree_add (tree, "test");
81  TEST_ASSERT_TRUE (tree_rm (tree, "test"));
82  TEST_ASSERT_FALSE (tree_rm (tree, "test"));
83 }
84 
86 {
88 
89  tree_add (tree, "test");
90  tree_add (tree, "test");
91  TEST_ASSERT_FALSE (tree_rm (tree, "test"));
92  TEST_ASSERT_TRUE (tree_rm (tree, "test"));
93 }
94 
96 {
98 
99  tree_add (tree, "checkpoint");
100  tree_add (tree, "checklist");
101  TEST_ASSERT_FALSE (tree_rm (tree, "check"));
102 }
103 
105 {
107 
108  tree_add (tree, "checkpoint");
109  tree_add (tree, "checklist");
110  tree_add (tree, "check");
111  TEST_ASSERT_TRUE (tree_rm (tree, "check"));
112 }
113 
115 {
117 
118  tree_add (tree, "");
119  TEST_ASSERT_TRUE (tree_rm (tree, ""));
120 }
121 
123 {
125 
126  TEST_ASSERT_FALSE (tree_check (tree, "foo"));
127 }
128 
130 {
132 
133  tree_add (tree, "entry");
134  TEST_ASSERT_TRUE (tree_check (tree, "entry"));
135 }
136 
138 {
140 
141  tree_add (tree, "introduce");
142  tree_add (tree, "introspect");
143  TEST_ASSERT_FALSE (tree_check (tree, "intro"));
144 }
145 
147 {
149 
150  tree_add (tree, "toasted");
151  TEST_ASSERT_FALSE (tree_check (tree, "toast"));
152  TEST_ASSERT_FALSE (tree_check (tree, "toaste"));
153  TEST_ASSERT_FALSE (tree_check (tree, "toaster"));
154 }
155 
157 {
159 
160  tree_add (tree, "red");
161  TEST_ASSERT_FALSE (tree_check (tree, "blue"));
162 }
163 
165 {
167 
168  tree_add (tree, "foo");
169  TEST_ASSERT_TRUE (tree_check (tree, "foobar"));
170 }
171 
173 {
175 
176  tree_add (tree, "");
177  TEST_ASSERT_TRUE (tree_check (tree, "all queries return true"));
178 }
179 
180 void test_size ()
181 {
183 
184  // Adapted from the example on wikipedia.
185  std::vector<std::string> keys;
186  keys.push_back ("tester");
187  keys.push_back ("water");
188  keys.push_back ("slow");
189  keys.push_back ("slower");
190  keys.push_back ("test");
191  keys.push_back ("team");
192  keys.push_back ("toast");
193 
194  for (size_t i = 0; i < keys.size (); ++i)
195  TEST_ASSERT_TRUE (tree_add (tree, keys[i]));
196  TEST_ASSERT_TRUE (tree.size () == keys.size ());
197  for (size_t i = 0; i < keys.size (); ++i)
198  TEST_ASSERT_FALSE (tree_add (tree, keys[i]));
199  TEST_ASSERT_TRUE (tree.size () == 2 * keys.size ());
200  for (size_t i = 0; i < keys.size (); ++i)
201  TEST_ASSERT_FALSE (tree_rm (tree, keys[i]));
202  TEST_ASSERT_TRUE (tree.size () == keys.size ());
203  for (size_t i = 0; i < keys.size (); ++i)
204  TEST_ASSERT_TRUE (tree_rm (tree, keys[i]));
205  TEST_ASSERT_TRUE (tree.size () == 0);
206 }
207 
208 void return_key (unsigned char *data_, size_t size_, void *arg_)
209 {
210  std::vector<std::string> *vec =
211  reinterpret_cast<std::vector<std::string> *> (arg_);
213  for (size_t i = 0; i < size_; ++i)
214  key.push_back (static_cast<char> (data_[i]));
215  vec->push_back (key);
216 }
217 
218 void test_apply ()
219 {
221 
222  std::set<std::string> keys;
223  keys.insert ("tester");
224  keys.insert ("water");
225  keys.insert ("slow");
226  keys.insert ("slower");
227  keys.insert ("test");
228  keys.insert ("team");
229  keys.insert ("toast");
230 
231  const std::set<std::string>::iterator end = keys.end ();
232  for (std::set<std::string>::iterator it = keys.begin (); it != end; ++it)
233  tree_add (tree, *it);
234 
235  std::vector<std::string> *vec = new std::vector<std::string> ();
236  tree.apply (return_key, static_cast<void *> (vec));
237  for (size_t i = 0; i < vec->size (); ++i)
238  TEST_ASSERT_TRUE (keys.count ((*vec)[i]) > 0);
239  delete vec;
240 }
241 
242 int main (void)
243 {
245 
246  UNITY_BEGIN ();
247 
251 
259 
267 
269 
271 
272  return UNITY_END ();
273 }
zmq::radix_tree_t::check
bool check(const unsigned char *key_, size_t key_size_)
Definition: radix_tree.cpp:503
test_apply
void test_apply()
Definition: unittest_radix_tree.cpp:218
test_check_empty
void test_check_empty()
Definition: unittest_radix_tree.cpp:122
data_
StringPiece data_
Definition: bytestream_unittest.cc:60
tree_check
bool tree_check(zmq::radix_tree_t &tree_, const std::string &key_)
Definition: unittest_radix_tree.cpp:33
end
GLuint GLuint end
Definition: glcorearb.h:2858
UNITY_END
return UNITY_END()
TEST_ASSERT_TRUE
#define TEST_ASSERT_TRUE(condition)
Definition: unity.h:121
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
test_check_query_longer_than_entry
void test_check_query_longer_than_entry()
Definition: unittest_radix_tree.cpp:164
test_rm_duplicate_entry
void test_rm_duplicate_entry()
Definition: unittest_radix_tree.cpp:85
setUp
void setUp()
Definition: unittest_radix_tree.cpp:14
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
TEST_ASSERT_FALSE
#define TEST_ASSERT_FALSE(condition)
Definition: unity.h:123
tearDown
void tearDown()
Definition: unittest_radix_tree.cpp:17
test_check_added_entry
void test_check_added_entry()
Definition: unittest_radix_tree.cpp:129
radix_tree.hpp
return_key
void return_key(unsigned char *data_, size_t size_, void *arg_)
Definition: unittest_radix_tree.cpp:208
zmq::radix_tree_t
Definition: radix_tree.hpp:89
test_add_single_entry
void test_add_single_entry()
Definition: unittest_radix_tree.cpp:46
stdint.hpp
test_check_prefix
void test_check_prefix()
Definition: unittest_radix_tree.cpp:146
test_rm_single_entry
void test_rm_single_entry()
Definition: unittest_radix_tree.cpp:68
test_check_null_entry_added
void test_check_null_entry_added()
Definition: unittest_radix_tree.cpp:172
test_check_common_prefix
void test_check_common_prefix()
Definition: unittest_radix_tree.cpp:137
tree_rm
bool tree_rm(zmq::radix_tree_t &tree_, const std::string &key_)
Definition: unittest_radix_tree.cpp:27
test_empty
void test_empty()
Definition: unittest_radix_tree.cpp:39
zmq::radix_tree_t::add
bool add(const unsigned char *key_, size_t key_size_)
Definition: radix_tree.cpp:259
key
const SETUP_TEARDOWN_TESTCONTEXT char * key
Definition: test_wss_transport.cpp:10
tree_add
bool tree_add(zmq::radix_tree_t &tree_, const std::string &key_)
Definition: unittest_radix_tree.cpp:21
i
int i
Definition: gmock-matchers_test.cc:764
unity.h
zmq::radix_tree_t::rm
bool rm(const unsigned char *key_, size_t key_size_)
Definition: radix_tree.cpp:383
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
main
int main(void)
Definition: unittest_radix_tree.cpp:242
UNITY_BEGIN
UNITY_BEGIN()
test_rm_null_entry
void test_rm_null_entry()
Definition: unittest_radix_tree.cpp:114
tree
Definition: wepoll.c:502
test_size
void test_size()
Definition: unittest_radix_tree.cpp:180
test_rm_common_prefix_entry
void test_rm_common_prefix_entry()
Definition: unittest_radix_tree.cpp:104
test_rm_unique_entry_twice
void test_rm_unique_entry_twice()
Definition: unittest_radix_tree.cpp:76
test_rm_common_prefix
void test_rm_common_prefix()
Definition: unittest_radix_tree.cpp:95
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
test_add_same_entry_twice
void test_add_same_entry_twice()
Definition: unittest_radix_tree.cpp:53
test_rm_when_empty
void test_rm_when_empty()
Definition: unittest_radix_tree.cpp:61
test_check_nonexistent_entry
void test_check_nonexistent_entry()
Definition: unittest_radix_tree.cpp:156


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00