Commit a9712c0b authored by Simon Giesecke's avatar Simon Giesecke

Problem: no unittests for mtrie

Solution: made mtrie generic (to remove complex dependency on pipe_t),
and added some unit tests
parent 9fc3692e
......@@ -611,6 +611,8 @@ set (cxx-sources
fd.hpp
fq.hpp
gather.hpp
generic_mtrie.hpp
generic_mtrie_impl.hpp
gssapi_client.hpp
gssapi_mechanism_base.hpp
gssapi_server.hpp
......
......@@ -64,6 +64,8 @@ src_libzmq_la_SOURCES = \
src/fq.hpp \
src/gather.cpp \
src/gather.hpp \
src/generic_mtrie.hpp \
src/generic_mtrie_impl.hpp \
src/gssapi_mechanism_base.cpp \
src/gssapi_mechanism_base.hpp \
src/gssapi_client.cpp \
......@@ -865,7 +867,8 @@ if ENABLE_STATIC
# unit tests - these include individual source files and test the internal functions
test_apps += \
unittests/unittest_poller \
unittests/unittest_ypipe
unittests/unittest_ypipe \
unittests/unittest_mtrie
unittests_unittest_poller_SOURCES = unittests/unittest_poller.cpp
unittests_unittest_poller_CPPFLAGS = -I$(top_srcdir)/src ${UNITY_CPPFLAGS}
......@@ -878,6 +881,12 @@ unittests_unittest_ypipe_CPPFLAGS = -I$(top_srcdir)/src ${UNITY_CPPFLAGS}
unittests_unittest_ypipe_LDADD = $(top_builddir)/src/.libs/libzmq.a \
${src_libzmq_la_LIBADD} \
${UNITY_LIBS}
unittests_unittest_mtrie_SOURCES = unittests/unittest_mtrie.cpp
unittests_unittest_mtrie_CPPFLAGS = -I$(top_srcdir)/src ${UNITY_CPPFLAGS}
unittests_unittest_mtrie_LDADD = $(top_builddir)/src/.libs/libzmq.a \
${src_libzmq_la_LIBADD} \
${UNITY_LIBS}
endif
check_PROGRAMS = ${test_apps}
......
/*
Copyright (c) 2018 Contributors as noted in the AUTHORS file
This file is part of libzmq, the ZeroMQ core engine in C++.
libzmq is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
As a special exception, the Contributors give you permission to link
this library with independent modules to produce an executable,
regardless of the license terms of these independent modules, and to
copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the
terms and conditions of the license of that module. An independent
module is a module which is not derived from or based on this library.
If you modify this library, you must extend this exception to your
version of the library.
libzmq 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/>.
*/
#ifndef __ZMQ_GENERIC_MTRIE_HPP_INCLUDED__
#define __ZMQ_GENERIC_MTRIE_HPP_INCLUDED__
#include <stddef.h>
#include <set>
#include "stdint.hpp"
namespace zmq
{
// Multi-trie. Each node in the trie is a set of pointers to pipes.
template <typename T> class generic_mtrie_t
{
public:
typedef T value_t;
typedef const unsigned char *prefix_t;
generic_mtrie_t ();
~generic_mtrie_t ();
// Add key to the trie. Returns true if it's a new subscription
// rather than a duplicate.
bool add (prefix_t prefix_, size_t size_, value_t *pipe_);
// Remove all subscriptions for a specific peer from the trie.
// The call_on_uniq_ flag controls if the callback is invoked
// when there are no subscriptions left on some topics or on
// every removal.
void
rm (value_t *pipe_,
void (*func_) (const unsigned char *data_, size_t size_, void *arg_),
void *arg_,
bool call_on_uniq_);
// Remove specific subscription from the trie. Return true is it was
// actually removed rather than de-duplicated.
bool rm (prefix_t prefix_, size_t size_, value_t *pipe_);
// Signal all the matching pipes.
void match (prefix_t data_,
size_t size_,
void (*func_) (value_t *pipe_, void *arg_),
void *arg_);
private:
bool add_helper (prefix_t prefix_, size_t size_, value_t *pipe_);
void rm_helper (value_t *pipe_,
unsigned char **buff_,
size_t buffsize_,
size_t maxbuffsize_,
void (*func_) (prefix_t data_, size_t size_, void *arg_),
void *arg_,
bool call_on_uniq_);
bool rm_helper (prefix_t prefix_, size_t size_, value_t *pipe_);
bool is_redundant () const;
typedef std::set<value_t *> pipes_t;
pipes_t *pipes;
unsigned char min;
unsigned short count;
unsigned short live_nodes;
union
{
class generic_mtrie_t<value_t> *node;
class generic_mtrie_t<value_t> **table;
} next;
generic_mtrie_t (const generic_mtrie_t<value_t> &);
const generic_mtrie_t<value_t> &
operator= (const generic_mtrie_t<value_t> &);
};
}
#endif
This diff is collapsed.
This diff is collapsed.
......@@ -30,76 +30,23 @@
#ifndef __ZMQ_MTRIE_HPP_INCLUDED__
#define __ZMQ_MTRIE_HPP_INCLUDED__
#include <stddef.h>
#include <set>
#include "generic_mtrie.hpp"
#include "stdint.hpp"
#if __cplusplus >= 201103L || defined(_MSC_VER)
#define ZMQ_HAS_EXTERN_TEMPLATE 1
#else
#define ZMQ_HAS_EXTERN_TEMPLATE 0
#endif
namespace zmq
{
class pipe_t;
// Multi-trie. Each node in the trie is a set of pointers to pipes.
class mtrie_t
{
public:
typedef const unsigned char *prefix_t;
mtrie_t ();
~mtrie_t ();
// Add key to the trie. Returns true if it's a new subscription
// rather than a duplicate.
bool add (prefix_t prefix_, size_t size_, zmq::pipe_t *pipe_);
// Remove all subscriptions for a specific peer from the trie.
// The call_on_uniq_ flag controls if the callback is invoked
// when there are no subscriptions left on some topics or on
// every removal.
void
rm (zmq::pipe_t *pipe_,
void (*func_) (const unsigned char *data_, size_t size_, void *arg_),
void *arg_,
bool call_on_uniq_);
// Remove specific subscription from the trie. Return true is it was
// actually removed rather than de-duplicated.
bool rm (prefix_t prefix_, size_t size_, zmq::pipe_t *pipe_);
// Signal all the matching pipes.
void match (prefix_t data_,
size_t size_,
void (*func_) (zmq::pipe_t *pipe_, void *arg_),
void *arg_);
private:
bool add_helper (prefix_t prefix_, size_t size_, zmq::pipe_t *pipe_);
void rm_helper (zmq::pipe_t *pipe_,
unsigned char **buff_,
size_t buffsize_,
size_t maxbuffsize_,
void (*func_) (prefix_t data_, size_t size_, void *arg_),
void *arg_,
bool call_on_uniq_);
bool rm_helper (prefix_t prefix_, size_t size_, zmq::pipe_t *pipe_);
bool is_redundant () const;
typedef std::set<zmq::pipe_t *> pipes_t;
pipes_t *pipes;
unsigned char min;
unsigned short count;
unsigned short live_nodes;
union
{
class mtrie_t *node;
class mtrie_t **table;
} next;
#if ZMQ_HAS_EXTERN_TEMPLATE
extern template class generic_mtrie_t<pipe_t>;
#endif
mtrie_t (const mtrie_t &);
const mtrie_t &operator= (const mtrie_t &);
};
typedef generic_mtrie_t<pipe_t> mtrie_t;
}
#endif
......@@ -4,6 +4,7 @@ cmake_minimum_required(VERSION "2.8.1")
set(unittests
unittest_ypipe
unittest_poller
unittest_mtrie
)
#IF (ENABLE_DRAFTS)
......
/*
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 ()
{
}
void test_create ()
{
zmq::generic_mtrie_t<int> mtrie;
}
void mtrie_count (int *pipe, void *arg)
{
LIBZMQ_UNUSED (pipe);
int *count = static_cast<int *> (arg);
++*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;
mtrie.match (test_name, 3, mtrie_count, &count);
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");
bool res = mtrie.add (test_name, 3, &pipe);
TEST_ASSERT_TRUE (res);
int count = 0;
mtrie.match (test_name, 3, mtrie_count, &count);
TEST_ASSERT_EQUAL_INT (1, count);
}
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");
mtrie.add (test_name, 3, &pipe);
bool res = mtrie.rm (test_name, 3, &pipe);
TEST_ASSERT_TRUE (res);
int count = 0;
mtrie.match (test_name, 3, mtrie_count, &count);
TEST_ASSERT_EQUAL_INT (0, count);
}
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);
RUN_TEST (test_add_rm_single_entry_match_exact);
return UNITY_END ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment