unittest_mtrie.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "../tests/testutil.hpp"
4 
5 #if defined(min)
6 #undef min
7 #endif
8 
9 #include <generic_mtrie_impl.hpp>
10 
11 #include <unity.h>
12 
13 void setUp ()
14 {
15 }
16 void tearDown ()
17 {
18 }
19 
21 {
22  return static_cast<int> (strlen (reinterpret_cast<const char *> (data_)));
23 }
24 
25 void test_create ()
26 {
28 }
29 
30 void mtrie_count (int *pipe_, int *count_)
31 {
32  LIBZMQ_UNUSED (pipe_);
33  ++*count_;
34 }
35 
37 {
39  const zmq::generic_mtrie_t<int>::prefix_t test_name =
40  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");
41 
42  int count = 0;
43  mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
45 }
46 
48 {
50 
51  int count = 0;
52  mtrie.match (NULL, 0, mtrie_count, &count);
54 }
55 
57 {
58  int pipe;
59 
61  const zmq::generic_mtrie_t<int>::prefix_t test_name =
62  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");
63 
64  bool res = mtrie.add (test_name, getlen (test_name), &pipe);
65  TEST_ASSERT_TRUE (res);
66 
67  TEST_ASSERT_EQUAL_INT (1, mtrie.num_prefixes ());
68 
69  int count = 0;
70  mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
72 }
73 
75 {
76  int pipe;
77 
79  const zmq::generic_mtrie_t<int>::prefix_t test_name =
80  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");
81 
82  bool res = mtrie.add (test_name, getlen (test_name), &pipe);
83  TEST_ASSERT_TRUE (res);
84  TEST_ASSERT_EQUAL_INT (1, mtrie.num_prefixes ());
85 
86  res = mtrie.add (test_name, getlen (test_name), &pipe);
87  TEST_ASSERT_FALSE (res);
88  TEST_ASSERT_EQUAL_INT (1, mtrie.num_prefixes ());
89 
90  int count = 0;
91  mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
93 }
94 
96 {
97  int pipe_1, pipe_2;
98 
100  const zmq::generic_mtrie_t<int>::prefix_t test_name =
101  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");
102 
103  bool res = mtrie.add (test_name, getlen (test_name), &pipe_1);
104  TEST_ASSERT_TRUE (res);
105  TEST_ASSERT_EQUAL_INT (1, mtrie.num_prefixes ());
106 
107  res = mtrie.add (test_name, getlen (test_name), &pipe_2);
108  TEST_ASSERT_FALSE (res);
109  TEST_ASSERT_EQUAL_INT (1, mtrie.num_prefixes ());
110 
111  int count = 0;
112  mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
114 }
115 
117 {
118  int pipe_1, pipe_2;
119 
121  const zmq::generic_mtrie_t<int>::prefix_t test_name_prefix =
122  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");
123  const zmq::generic_mtrie_t<int>::prefix_t test_name_full =
124  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foobar");
125 
126  bool res = mtrie.add (test_name_prefix, getlen (test_name_prefix), &pipe_1);
127  TEST_ASSERT_TRUE (res);
128  TEST_ASSERT_EQUAL_INT (1, mtrie.num_prefixes ());
129 
130  res = mtrie.add (test_name_full, getlen (test_name_full), &pipe_2);
131  TEST_ASSERT_TRUE (res);
132  TEST_ASSERT_EQUAL_INT (2, mtrie.num_prefixes ());
133 
134  int count = 0;
135  mtrie.match (test_name_full, getlen (test_name_full), mtrie_count, &count);
137 }
138 
140 {
141  int pipe;
143  const zmq::generic_mtrie_t<int>::prefix_t test_name =
144  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");
145 
146  mtrie.add (test_name, getlen (test_name), &pipe);
147  TEST_ASSERT_EQUAL_INT (1, mtrie.num_prefixes ());
149  mtrie.rm (test_name, getlen (test_name), &pipe);
151  TEST_ASSERT_EQUAL_INT (0, mtrie.num_prefixes ());
152 
153  int count = 0;
154  mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
156 }
157 
159 {
160  int pipe;
162 
163  zmq::generic_mtrie_t<int>::rm_result res = mtrie.rm (0, 0, &pipe);
165  TEST_ASSERT_EQUAL_INT (0, mtrie.num_prefixes ());
166 }
167 
169 {
170  int pipe;
172  const zmq::generic_mtrie_t<int>::prefix_t test_name =
173  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");
174 
176  mtrie.rm (test_name, getlen (test_name), &pipe);
178  TEST_ASSERT_EQUAL_INT (0, mtrie.num_prefixes ());
179 
180  int count = 0;
181  mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
183 }
184 
185 void test_add_and_rm_other (const char *add_name_, const char *rm_name_)
186 {
187  int addpipe, rmpipe;
189  const zmq::generic_mtrie_t<int>::prefix_t add_name_data =
190  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (add_name_);
191  const zmq::generic_mtrie_t<int>::prefix_t rm_name_data =
192  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (rm_name_);
193 
194  mtrie.add (add_name_data, getlen (add_name_data), &addpipe);
195  TEST_ASSERT_EQUAL_INT (1, mtrie.num_prefixes ());
196 
198  mtrie.rm (rm_name_data, getlen (rm_name_data), &rmpipe);
200  TEST_ASSERT_EQUAL_INT (1, mtrie.num_prefixes ());
201 
202  {
203  int count = 0;
204  mtrie.match (add_name_data, getlen (add_name_data), mtrie_count,
205  &count);
207  }
208 
209  if (strncmp (add_name_, rm_name_,
210  std::min (strlen (add_name_), strlen (rm_name_) + 1))
211  != 0) {
212  int count = 0;
213  mtrie.match (rm_name_data, getlen (rm_name_data), mtrie_count, &count);
215  }
216 }
217 
219 {
220  // TODO this triggers an assertion
221  test_add_and_rm_other ("foo", "foo");
222 }
223 
225 {
226  test_add_and_rm_other ("foo", "bar");
227 }
228 
230 {
231  // TODO here, a test assertion fails
232  test_add_and_rm_other ("foobar", "foo");
233 }
234 
236 {
237  test_add_and_rm_other ("foo", "foobar");
238 }
239 
241  int *pipes_,
242  const char **names_,
243  size_t i_)
244 {
245  const zmq::generic_mtrie_t<int>::prefix_t name_data =
246  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names_[i_]);
247 
248  bool res = mtrie_.add (name_data, getlen (name_data), &pipes_[i_]);
251  res); // FIXME asserting equality between enum and bool? I think first arg for macro should be "true"
252 }
253 
255 {
256  int pipes[3];
257  const char *names[] = {"foo1", "foo2", "foo3"};
258 
260  add_indexed_expect_unique (mtrie, pipes, names, 0);
261  add_indexed_expect_unique (mtrie, pipes, names, 2);
262  TEST_ASSERT_EQUAL_INT (2, mtrie.num_prefixes ());
263 
264  const zmq::generic_mtrie_t<int>::prefix_t name_data =
265  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names[1]);
266 
268  mtrie.rm (name_data, getlen (name_data), &pipes[1]);
270  TEST_ASSERT_EQUAL_INT (2, mtrie.num_prefixes ());
271 }
272 
273 template <size_t N>
275  int (&pipes_)[N],
276  const char *(&names_)[N])
277 {
278  for (size_t i = 0; i < N; ++i) {
279  add_indexed_expect_unique (mtrie_, pipes_, names_, i);
280  }
281  TEST_ASSERT_EQUAL_INT (N, mtrie_.num_prefixes ());
282 }
283 
285 {
286  int pipes[3];
287  const char *names[] = {"foo1", "foo2", "foo3"};
288 
290  add_entries (mtrie, pipes, names);
291 
292  for (size_t i = 0; i < sizeof (names) / sizeof (names[0]); ++i) {
293  const zmq::generic_mtrie_t<int>::prefix_t name_data =
294  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names[i]);
295  int count = 0;
296  mtrie.match (name_data, getlen (name_data), mtrie_count, &count);
298  }
299 }
300 
302 {
303  int pipes[3];
304  const char *names[] = {"foo1", "foo2", "foo3"};
305 
307  for (int i = 2; i >= 0; --i) {
308  add_indexed_expect_unique (mtrie, pipes, names,
309  static_cast<size_t> (i));
310  }
311  TEST_ASSERT_EQUAL_INT (3, mtrie.num_prefixes ());
312 
313  for (size_t i = 0; i < 3; ++i) {
314  const zmq::generic_mtrie_t<int>::prefix_t name_data =
315  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names[i]);
316  int count = 0;
317  mtrie.match (name_data, getlen (name_data), mtrie_count, &count);
319  }
320 }
321 
322 template <size_t N> void add_and_rm_entries (const char *(&names_)[N])
323 {
324  int pipes[N];
326  add_entries (mtrie, pipes, names_);
327 
328  for (size_t i = 0; i < N; ++i) {
329  const zmq::generic_mtrie_t<int>::prefix_t name_data =
330  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names_[i]);
331 
333  mtrie.rm (name_data, getlen (name_data), &pipes[i]);
335  }
336  TEST_ASSERT_EQUAL_INT (0, mtrie.num_prefixes ());
337 }
338 
340 {
341  const char *names[] = {"foo1", "foo2", "foo3"};
342  add_and_rm_entries (names);
343 }
344 
346 {
347  const char *names[] = {"foo3", "foo2", "foo1"};
348  add_and_rm_entries (names);
349 }
350 
352  size_t len_,
353  const char *name_)
354 {
355  TEST_ASSERT_EQUAL_UINT (strlen (name_), len_);
357 }
358 
359 template <size_t N> void add_entries_rm_pipes_unique (const char *(&names_)[N])
360 {
361  int pipes[N];
363  add_entries (mtrie, pipes, names_);
364 
365  for (size_t i = 0; i < N; ++i) {
366  mtrie.rm (&pipes[i], check_name, names_[i], false);
367  }
368 }
369 
371 {
372  const char *names[] = {"foo1", "foo2", "foo3"};
373 
375 }
376 
378 {
379  const char *names[] = {"foo3", "foo2", "foo1"};
380 
382 }
383 
385  size_t len_,
386  int *count_)
387 {
389  LIBZMQ_UNUSED (len_);
390 
391  --(*count_);
392  TEST_ASSERT_GREATER_OR_EQUAL (0, *count_);
393 }
394 
395 void add_duplicate_entry (zmq::generic_mtrie_t<int> &mtrie_, int (&pipes_)[2])
396 {
397  const char *name = "foo";
398 
399  const zmq::generic_mtrie_t<int>::prefix_t name_data =
400  reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (name);
401 
402  bool res = mtrie_.add (name_data, getlen (name_data), &pipes_[0]);
403  TEST_ASSERT_TRUE (res);
404  TEST_ASSERT_EQUAL_INT (1, mtrie_.num_prefixes ());
405  res = mtrie_.add (name_data, getlen (name_data), &pipes_[1]);
406  TEST_ASSERT_FALSE (res);
407  TEST_ASSERT_EQUAL_INT (1, mtrie_.num_prefixes ());
408 }
409 
411 {
412  int pipes[2];
414  add_duplicate_entry (mtrie, pipes);
415 
416  int count = 1;
417  mtrie.rm (&pipes[0], check_count, &count, false);
418  count = 1;
419  mtrie.rm (&pipes[1], check_count, &count, false);
420 }
421 
423 {
424  int pipes[2];
426  add_duplicate_entry (mtrie, pipes);
427 
428  int count = 0;
429  mtrie.rm (&pipes[0], check_count, &count, true);
430  count = 1;
431  mtrie.rm (&pipes[1], check_count, &count, true);
432 }
433 
434 int main (void)
435 {
437 
438  UNITY_BEGIN ();
447 
455 
460 
465 
466  return UNITY_END ();
467 }
test_rm_nonexistent_nonempty_prefix
void test_rm_nonexistent_nonempty_prefix()
Definition: unittest_mtrie.cpp:229
zmq::generic_mtrie_t::rm
void rm(value_t *value_, void(*func_)(const unsigned char *data_, size_t size_, Arg arg_), Arg arg_, bool call_on_uniq_)
check_count
void check_count(zmq::generic_mtrie_t< int >::prefix_t data_, size_t len_, int *count_)
Definition: unittest_mtrie.cpp:384
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
data_
StringPiece data_
Definition: bytestream_unittest.cc:60
test_rm_multiple_reverse_order
void test_rm_multiple_reverse_order()
Definition: unittest_mtrie.cpp:345
TEST_ASSERT_EQUAL_STRING_LEN
#define TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len)
Definition: unity.h:236
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
getlen
int getlen(const zmq::generic_mtrie_t< int >::prefix_t &data_)
Definition: unittest_mtrie.cpp:20
test_add_single_entry_match_exact
void test_add_single_entry_match_exact()
Definition: unittest_mtrie.cpp:56
TEST_ASSERT_TRUE
#define TEST_ASSERT_TRUE(condition)
Definition: unity.h:121
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
TEST_ASSERT_EQUAL_UINT
#define TEST_ASSERT_EQUAL_UINT(expected, actual)
Definition: unity.h:135
add_indexed_expect_unique
void add_indexed_expect_unique(zmq::generic_mtrie_t< int > &mtrie_, int *pipes_, const char **names_, size_t i_)
Definition: unittest_mtrie.cpp:240
add_and_rm_entries
void add_and_rm_entries(const char *(&names_)[N])
Definition: unittest_mtrie.cpp:322
test_add_rm_single_entry_match_exact
void test_add_rm_single_entry_match_exact()
Definition: unittest_mtrie.cpp:139
test_rm_nonexistent_nonempty_differentname
void test_rm_nonexistent_nonempty_differentname()
Definition: unittest_mtrie.cpp:224
add_entries
void add_entries(zmq::generic_mtrie_t< int > &mtrie_, int(&pipes_)[N], const char *(&names_)[N])
Definition: unittest_mtrie.cpp:274
add_entries_rm_pipes_unique
void add_entries_rm_pipes_unique(const char *(&names_)[N])
Definition: unittest_mtrie.cpp:359
TEST_ASSERT_FALSE
#define TEST_ASSERT_FALSE(condition)
Definition: unity.h:123
zmq::generic_mtrie_t::num_prefixes
uint32_t num_prefixes() const
Definition: generic_mtrie.hpp:62
check_name
void check_name(zmq::generic_mtrie_t< int >::prefix_t data_, size_t len_, const char *name_)
Definition: unittest_mtrie.cpp:351
main
int main(void)
Definition: unittest_mtrie.cpp:434
setUp
void setUp()
Definition: unittest_mtrie.cpp:13
tearDown
void tearDown()
Definition: unittest_mtrie.cpp:16
mtrie_count
void mtrie_count(int *pipe_, int *count_)
Definition: unittest_mtrie.cpp:30
test_create
void test_create()
Definition: unittest_mtrie.cpp:25
test_check_empty_match_nonempty_data
void test_check_empty_match_nonempty_data()
Definition: unittest_mtrie.cpp:36
test_check_empty_match_empty_data
void test_check_empty_match_empty_data()
Definition: unittest_mtrie.cpp:47
test_rm_nonexistent_empty
void test_rm_nonexistent_empty()
Definition: unittest_mtrie.cpp:168
zmq::generic_mtrie_t::rm_result
rm_result
Definition: generic_mtrie.hpp:22
LIBZMQ_UNUSED
#define LIBZMQ_UNUSED(object)
Definition: macros.hpp:6
test_rm_nonexistent_nonempty_samename
void test_rm_nonexistent_nonempty_samename()
Definition: unittest_mtrie.cpp:218
zmq::generic_mtrie_t::add
bool add(prefix_t prefix_, size_t size_, value_t *value_)
Definition: generic_mtrie_impl.hpp:41
test_add_single_entry_twice_match_exact
void test_add_single_entry_twice_match_exact()
Definition: unittest_mtrie.cpp:74
name_
string name_
Definition: googletest.cc:182
test_rm_with_callback_duplicate_uniq_only
void test_rm_with_callback_duplicate_uniq_only()
Definition: unittest_mtrie.cpp:422
test_rm_nonexistent_nonempty_prefixed
void test_rm_nonexistent_nonempty_prefixed()
Definition: unittest_mtrie.cpp:235
TEST_ASSERT_EQUAL
#define TEST_ASSERT_EQUAL(expected, actual)
Definition: unity.h:133
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
test_rm_nonexistent_between
void test_rm_nonexistent_between()
Definition: unittest_mtrie.cpp:254
i
int i
Definition: gmock-matchers_test.cc:764
test_add_and_rm_other
void test_add_and_rm_other(const char *add_name_, const char *rm_name_)
Definition: unittest_mtrie.cpp:185
unity.h
test_rm_nonexistent_0_size_empty
void test_rm_nonexistent_0_size_empty()
Definition: unittest_mtrie.cpp:158
test_add_two_entries_with_same_name_match_exact
void test_add_two_entries_with_same_name_match_exact()
Definition: unittest_mtrie.cpp:95
test_add_multiple_reverse
void test_add_multiple_reverse()
Definition: unittest_mtrie.cpp:301
i_
int i_
Definition: gmock-matchers_test.cc:1114
test_rm_with_callback_multiple_in_order
void test_rm_with_callback_multiple_in_order()
Definition: unittest_mtrie.cpp:370
add_duplicate_entry
void add_duplicate_entry(zmq::generic_mtrie_t< int > &mtrie_, int(&pipes_)[2])
Definition: unittest_mtrie.cpp:395
test_rm_with_callback_multiple_reverse_order
void test_rm_with_callback_multiple_reverse_order()
Definition: unittest_mtrie.cpp:377
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
zmq::generic_mtrie_t
Definition: generic_mtrie.hpp:16
TEST_ASSERT_GREATER_OR_EQUAL
#define TEST_ASSERT_GREATER_OR_EQUAL(threshold, actual)
Definition: unity.h:184
generic_mtrie_impl.hpp
test_add_multiple
void test_add_multiple()
Definition: unittest_mtrie.cpp:284
test_add_two_entries_match_prefix_and_exact
void test_add_two_entries_match_prefix_and_exact()
Definition: unittest_mtrie.cpp:116
count
GLint GLsizei count
Definition: glcorearb.h:2830
test_rm_with_callback_duplicate
void test_rm_with_callback_duplicate()
Definition: unittest_mtrie.cpp:410
test_rm_multiple_in_order
void test_rm_multiple_in_order()
Definition: unittest_mtrie.cpp:339
zmq::generic_mtrie_t::match
void match(prefix_t data_, size_t size_, void(*func_)(value_t *value_, Arg arg_), Arg arg_)


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