unittest_mtrie.cpp 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
Copyright (c) 2018 Contributors as noted in the AUTHORS file

This file is part of 0MQ.

0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "../tests/testutil.hpp"

#if defined(min)
#undef min
#endif

#include <generic_mtrie_impl.hpp>

#include <unity.h>

void setUp ()
{
}
void tearDown ()
{
}

37 38
int getlen (const zmq::generic_mtrie_t<int>::prefix_t &data)
{
39
    return (int) strlen (reinterpret_cast<const char *> (data));
40 41
}

42 43 44 45 46
void test_create ()
{
    zmq::generic_mtrie_t<int> mtrie;
}

47
void mtrie_count (int *pipe, int *count)
48 49 50 51 52 53 54 55 56 57 58 59
{
    LIBZMQ_UNUSED (pipe);
    ++*count;
}

void test_check_empty_match_nonempty_data ()
{
    zmq::generic_mtrie_t<int> mtrie;
    const zmq::generic_mtrie_t<int>::prefix_t test_name =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");

    int count = 0;
60
    mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    TEST_ASSERT_EQUAL_INT (0, count);
}

void test_check_empty_match_empty_data ()
{
    zmq::generic_mtrie_t<int> mtrie;

    int count = 0;
    mtrie.match (NULL, 0, mtrie_count, &count);
    TEST_ASSERT_EQUAL_INT (0, count);
}

void test_add_single_entry_match_exact ()
{
    int pipe;

    zmq::generic_mtrie_t<int> mtrie;
    const zmq::generic_mtrie_t<int>::prefix_t test_name =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");

81
    bool res = mtrie.add (test_name, getlen (test_name), &pipe);
82 83 84
    TEST_ASSERT_TRUE (res);

    int count = 0;
85
    mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
86 87 88
    TEST_ASSERT_EQUAL_INT (1, count);
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
void test_add_single_entry_twice_match_exact ()
{
    int pipe;

    zmq::generic_mtrie_t<int> mtrie;
    const zmq::generic_mtrie_t<int>::prefix_t test_name =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");

    bool res = mtrie.add (test_name, getlen (test_name), &pipe);
    TEST_ASSERT_TRUE (res);

    res = mtrie.add (test_name, getlen (test_name), &pipe);
    TEST_ASSERT_FALSE (res);

    int count = 0;
    mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
    TEST_ASSERT_EQUAL_INT (1, count);
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
void test_add_two_entries_with_same_name_match_exact ()
{
    int pipe_1, pipe_2;

    zmq::generic_mtrie_t<int> mtrie;
    const zmq::generic_mtrie_t<int>::prefix_t test_name =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");

    bool res = mtrie.add (test_name, getlen (test_name), &pipe_1);
    TEST_ASSERT_TRUE (res);

    res = mtrie.add (test_name, getlen (test_name), &pipe_2);
    TEST_ASSERT_FALSE (res);

    int count = 0;
    mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
    TEST_ASSERT_EQUAL_INT (2, count);
}

void test_add_two_entries_match_prefix_and_exact ()
{
    int pipe_1, pipe_2;

    zmq::generic_mtrie_t<int> mtrie;
    const zmq::generic_mtrie_t<int>::prefix_t test_name_prefix =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");
    const zmq::generic_mtrie_t<int>::prefix_t test_name_full =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foobar");

    bool res = mtrie.add (test_name_prefix, getlen (test_name_prefix), &pipe_1);
    TEST_ASSERT_TRUE (res);

    res = mtrie.add (test_name_full, getlen (test_name_full), &pipe_2);
    TEST_ASSERT_TRUE (res);

    int count = 0;
    mtrie.match (test_name_full, getlen (test_name_full), mtrie_count, &count);
    TEST_ASSERT_EQUAL_INT (2, count);
}

148 149 150 151 152 153 154
void test_add_rm_single_entry_match_exact ()
{
    int pipe;
    zmq::generic_mtrie_t<int> mtrie;
    const zmq::generic_mtrie_t<int>::prefix_t test_name =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");

155
    mtrie.add (test_name, getlen (test_name), &pipe);
156 157 158
    zmq::generic_mtrie_t<int>::rm_result res =
      mtrie.rm (test_name, getlen (test_name), &pipe);
    TEST_ASSERT_EQUAL (zmq::generic_mtrie_t<int>::last_value_removed, res);
159 160

    int count = 0;
161
    mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
162 163 164
    TEST_ASSERT_EQUAL_INT (0, count);
}

165 166 167 168 169
void test_rm_nonexistent_0_size_empty ()
{
    int pipe;
    zmq::generic_mtrie_t<int> mtrie;

170 171
    zmq::generic_mtrie_t<int>::rm_result res = mtrie.rm (0, 0, &pipe);
    TEST_ASSERT_EQUAL (zmq::generic_mtrie_t<int>::not_found, res);
172 173
}

174
void test_rm_nonexistent_empty ()
175 176 177 178 179 180
{
    int pipe;
    zmq::generic_mtrie_t<int> mtrie;
    const zmq::generic_mtrie_t<int>::prefix_t test_name =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> ("foo");

181 182 183
    zmq::generic_mtrie_t<int>::rm_result res =
      mtrie.rm (test_name, getlen (test_name), &pipe);
    TEST_ASSERT_EQUAL (zmq::generic_mtrie_t<int>::not_found, res);
184 185 186 187 188 189

    int count = 0;
    mtrie.match (test_name, getlen (test_name), mtrie_count, &count);
    TEST_ASSERT_EQUAL_INT (0, count);
}

190 191 192 193 194 195 196 197 198 199 200
void test_add_and_rm_other (const char *add_name, const char *rm_name)
{
    int addpipe, rmpipe;
    zmq::generic_mtrie_t<int> mtrie;
    const zmq::generic_mtrie_t<int>::prefix_t add_name_data =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (add_name);
    const zmq::generic_mtrie_t<int>::prefix_t rm_name_data =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (rm_name);

    mtrie.add (add_name_data, getlen (add_name_data), &addpipe);

201 202 203
    zmq::generic_mtrie_t<int>::rm_result res =
      mtrie.rm (rm_name_data, getlen (rm_name_data), &rmpipe);
    TEST_ASSERT_EQUAL (zmq::generic_mtrie_t<int>::not_found, res);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

    {
        int count = 0;
        mtrie.match (add_name_data, getlen (add_name_data), mtrie_count,
                     &count);
        TEST_ASSERT_EQUAL_INT (1, count);
    }

    if (strncmp (add_name, rm_name,
                 std::min (strlen (add_name), strlen (rm_name) + 1))
        != 0) {
        int count = 0;
        mtrie.match (rm_name_data, getlen (rm_name_data), mtrie_count, &count);
        TEST_ASSERT_EQUAL_INT (0, count);
    }
}

void test_rm_nonexistent_nonempty_samename ()
{
    // TODO this triggers an assertion
    test_add_and_rm_other ("foo", "foo");
}

void test_rm_nonexistent_nonempty_differentname ()
{
    test_add_and_rm_other ("foo", "bar");
}

void test_rm_nonexistent_nonempty_prefix ()
{
    // TODO here, a test assertion fails
    test_add_and_rm_other ("foobar", "foo");
}

void test_rm_nonexistent_nonempty_prefixed ()
{
    test_add_and_rm_other ("foo", "foobar");
}

243 244 245
void add_indexed_expect_unique (zmq::generic_mtrie_t<int> &mtrie,
                                int *pipes,
                                const char **names,
246
                                size_t i)
247 248 249 250 251
{
    const zmq::generic_mtrie_t<int>::prefix_t name_data =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names[i]);

    bool res = mtrie.add (name_data, getlen (name_data), &pipes[i]);
252
    TEST_ASSERT_EQUAL (zmq::generic_mtrie_t<int>::last_value_removed, res);
253 254 255 256 257
}

void test_rm_nonexistent_between ()
{
    int pipes[3];
258
    const char *names[] = {"foo1", "foo2", "foo3"};
259 260 261 262 263 264 265 266

    zmq::generic_mtrie_t<int> mtrie;
    add_indexed_expect_unique (mtrie, pipes, names, 0);
    add_indexed_expect_unique (mtrie, pipes, names, 2);

    const zmq::generic_mtrie_t<int>::prefix_t name_data =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names[1]);

267 268 269
    zmq::generic_mtrie_t<int>::rm_result res =
      mtrie.rm (name_data, getlen (name_data), &pipes[1]);
    TEST_ASSERT_EQUAL (zmq::generic_mtrie_t<int>::not_found, res);
270 271
}

272 273 274 275 276 277 278 279 280 281
template <size_t N>
void add_entries (zmq::generic_mtrie_t<int> &mtrie,
                  int (&pipes)[N],
                  const char *(&names)[N])
{
    for (size_t i = 0; i < N; ++i) {
        add_indexed_expect_unique (mtrie, pipes, names, i);
    }
}

282 283 284
void test_add_multiple ()
{
    int pipes[3];
285
    const char *names[] = {"foo1", "foo2", "foo3"};
286 287

    zmq::generic_mtrie_t<int> mtrie;
288
    add_entries (mtrie, pipes, names);
289

290
    for (size_t i = 0; i < sizeof (names) / sizeof (names[0]); ++i) {
291 292 293 294 295 296 297 298 299 300 301
        const zmq::generic_mtrie_t<int>::prefix_t name_data =
          reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names[i]);
        int count = 0;
        mtrie.match (name_data, getlen (name_data), mtrie_count, &count);
        TEST_ASSERT_EQUAL_INT (1, count);
    }
}

void test_add_multiple_reverse ()
{
    int pipes[3];
302
    const char *names[] = {"foo1", "foo2", "foo3"};
303 304 305

    zmq::generic_mtrie_t<int> mtrie;
    for (int i = 2; i >= 0; --i) {
306
        add_indexed_expect_unique (mtrie, pipes, names, (size_t) i);
307 308
    }

309
    for (size_t i = 0; i < 3; ++i) {
310 311 312 313 314 315
        const zmq::generic_mtrie_t<int>::prefix_t name_data =
          reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names[i]);
        int count = 0;
        mtrie.match (name_data, getlen (name_data), mtrie_count, &count);
        TEST_ASSERT_EQUAL_INT (1, count);
    }
316 317
}

318
template <size_t N> void add_and_rm_entries (const char *(&names)[N])
319
{
320
    int pipes[N];
321
    zmq::generic_mtrie_t<int> mtrie;
322
    add_entries (mtrie, pipes, names);
323

324
    for (size_t i = 0; i < N; ++i) {
325 326 327
        const zmq::generic_mtrie_t<int>::prefix_t name_data =
          reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (names[i]);

328 329 330
        zmq::generic_mtrie_t<int>::rm_result res =
          mtrie.rm (name_data, getlen (name_data), &pipes[i]);
        TEST_ASSERT_EQUAL (zmq::generic_mtrie_t<int>::last_value_removed, res);
331 332 333
    }
}

334 335 336 337 338 339
void test_rm_multiple_in_order ()
{
    const char *names[] = {"foo1", "foo2", "foo3"};
    add_and_rm_entries (names);
}

340 341
void test_rm_multiple_reverse_order ()
{
342 343 344
    const char *names[] = {"foo3", "foo2", "foo1"};
    add_and_rm_entries (names);
}
345

346 347
void check_name (zmq::generic_mtrie_t<int>::prefix_t data_,
                 size_t len_,
348
                 const char *name_)
349
{
350 351
    TEST_ASSERT_EQUAL_UINT (strlen (name_), len_);
    TEST_ASSERT_EQUAL_STRING_LEN (name_, data_, len_);
352 353 354 355 356
}

template <size_t N> void add_entries_rm_pipes_unique (const char *(&names)[N])
{
    int pipes[N];
357
    zmq::generic_mtrie_t<int> mtrie;
358 359 360
    add_entries (mtrie, pipes, names);

    for (size_t i = 0; i < N; ++i) {
361
        mtrie.rm (&pipes[i], check_name, names[i], false);
362
    }
363
}
364

365 366 367
void test_rm_with_callback_multiple_in_order ()
{
    const char *names[] = {"foo1", "foo2", "foo3"};
368

369 370 371 372 373 374 375 376
    add_entries_rm_pipes_unique (names);
}

void test_rm_with_callback_multiple_reverse_order ()
{
    const char *names[] = {"foo3", "foo2", "foo1"};

    add_entries_rm_pipes_unique (names);
377 378
}

379 380
void check_count (zmq::generic_mtrie_t<int>::prefix_t data_,
                  size_t len_,
381
                  int *count_)
382
{
383 384
    --(*count_);
    TEST_ASSERT_GREATER_OR_EQUAL (0, *count_);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
}

void add_duplicate_entry (zmq::generic_mtrie_t<int> &mtrie, int (&pipes)[2])
{
    const char *name = "foo";

    const zmq::generic_mtrie_t<int>::prefix_t name_data =
      reinterpret_cast<zmq::generic_mtrie_t<int>::prefix_t> (name);

    bool res = mtrie.add (name_data, getlen (name_data), &pipes[0]);
    TEST_ASSERT_TRUE (res);
    res = mtrie.add (name_data, getlen (name_data), &pipes[1]);
    TEST_ASSERT_FALSE (res);
}

void test_rm_with_callback_duplicate ()
{
    int pipes[2];
    zmq::generic_mtrie_t<int> mtrie;
    add_duplicate_entry (mtrie, pipes);

    int count = 1;
    mtrie.rm (&pipes[0], check_count, &count, false);
    count = 1;
    mtrie.rm (&pipes[1], check_count, &count, false);
}

void test_rm_with_callback_duplicate_uniq_only ()
{
    int pipes[2];
    zmq::generic_mtrie_t<int> mtrie;
    add_duplicate_entry (mtrie, pipes);

    int count = 0;
    mtrie.rm (&pipes[0], check_count, &count, true);
    count = 1;
    mtrie.rm (&pipes[1], check_count, &count, true);
}

424 425 426 427 428 429 430 431 432
int main (void)
{
    setup_test_environment ();

    UNITY_BEGIN ();
    RUN_TEST (test_create);
    RUN_TEST (test_check_empty_match_nonempty_data);
    RUN_TEST (test_check_empty_match_empty_data);
    RUN_TEST (test_add_single_entry_match_exact);
433
    RUN_TEST (test_add_single_entry_twice_match_exact);
434
    RUN_TEST (test_add_rm_single_entry_match_exact);
435 436
    RUN_TEST (test_add_two_entries_match_prefix_and_exact);
    RUN_TEST (test_add_two_entries_with_same_name_match_exact);
437

438
    RUN_TEST (test_rm_nonexistent_0_size_empty);
439
    RUN_TEST (test_rm_nonexistent_empty);
440 441 442 443
    RUN_TEST (test_rm_nonexistent_nonempty_samename);
    RUN_TEST (test_rm_nonexistent_nonempty_differentname);
    RUN_TEST (test_rm_nonexistent_nonempty_prefix);
    RUN_TEST (test_rm_nonexistent_nonempty_prefixed);
444
    RUN_TEST (test_rm_nonexistent_between);
445 446

    RUN_TEST (test_add_multiple);
447
    RUN_TEST (test_add_multiple_reverse);
448 449
    RUN_TEST (test_rm_multiple_in_order);
    RUN_TEST (test_rm_multiple_reverse_order);
450

451 452
    RUN_TEST (test_rm_with_callback_multiple_in_order);
    RUN_TEST (test_rm_with_callback_multiple_reverse_order);
453 454
    RUN_TEST (test_rm_with_callback_duplicate);
    RUN_TEST (test_rm_with_callback_duplicate_uniq_only);
455

456 457
    return UNITY_END ();
}