Merge pull request #205 from sigiesec/split-testcases

Improved tests and implementation of message_t
This commit is contained in:
Luca Boccassi 2018-04-16 14:52:51 +01:00 committed by GitHub
commit 19da7a4cf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 107 additions and 32 deletions

View File

@ -1,30 +1,112 @@
#include <gtest/gtest.h>
#include <zmq.hpp>
TEST (message, create_destroy)
#if defined(ZMQ_CPP11)
static_assert(!std::is_copy_constructible<zmq::message_t>::value, "message_t should not be copy-constructible");
static_assert(!std::is_copy_assignable<zmq::message_t>::value, "message_t should not be copy-assignable");
#endif
TEST (message, constructor_default)
{
zmq::message_t message;
const zmq::message_t message;
ASSERT_EQ (0u, message.size ());
}
TEST (message, constructors)
const char* const data = "Hi";
TEST (message, constructor_iterators)
{
const std::string hi ("Hi");
zmq::message_t hi_msg_a (hi.begin (), hi.end ());
ASSERT_EQ (hi_msg_a.size (), hi.size ());
zmq::message_t hi_msg_b (hi.data (), hi.size ());
ASSERT_EQ (hi_msg_b.size (), hi.size ());
ASSERT_EQ (hi_msg_a, hi_msg_b);
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
zmq::message_t hello_msg_a ("Hello");
ASSERT_NE (hi_msg_a, hello_msg_a);
ASSERT_NE (hi_msg_b, hello_msg_a);
zmq::message_t hi_msg_c (hi);
ASSERT_EQ (hi_msg_c, hi_msg_a);
ASSERT_EQ (hi_msg_c, hi_msg_b);
ASSERT_NE (hi_msg_c, hello_msg_a);
#endif
#ifdef ZMQ_HAS_RVALUE_REFS
zmq::message_t hello_msg_b(zmq::message_t("Hello"));
ASSERT_EQ (hello_msg_a, hello_msg_b);
#endif
const std::string hi (data);
const zmq::message_t hi_msg (hi.begin (), hi.end ());
ASSERT_EQ (2u, hi_msg.size ());
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
}
TEST (message, constructor_pointer_size)
{
const std::string hi (data);
const zmq::message_t hi_msg (hi.data (), hi.size ());
ASSERT_EQ (2u, hi_msg.size ());
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
}
TEST (message, constructor_char_array) {
const zmq::message_t hi_msg (data, strlen (data));
ASSERT_EQ (2u, hi_msg.size ());
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
}
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
TEST (message, constructor_container)
{
const std::string hi (data);
zmq::message_t hi_msg (hi);
ASSERT_EQ (2u, hi_msg.size ());
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
}
#endif
#ifdef ZMQ_HAS_RVALUE_REFS
TEST (message, constructor_move)
{
zmq::message_t hi_msg (zmq::message_t(data, strlen (data)));
}
TEST (message, assign_move_empty_before)
{
zmq::message_t hi_msg;
hi_msg = zmq::message_t (data, strlen (data));
ASSERT_EQ (2u, hi_msg.size ());
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
}
TEST (message, assign_move_empty_after)
{
zmq::message_t hi_msg (data, strlen (data));
hi_msg = zmq::message_t();
ASSERT_EQ (0u, hi_msg.size ());
}
TEST (message, assign_move_empty_before_and_after)
{
zmq::message_t hi_msg;
hi_msg = zmq::message_t();
ASSERT_EQ (0u, hi_msg.size ());
}
#endif
TEST (message, equality_self) {
const zmq::message_t hi_msg (data, strlen (data));
ASSERT_EQ (hi_msg, hi_msg);
}
TEST (message, equality_equal) {
const zmq::message_t hi_msg_a (data, strlen (data));
const zmq::message_t hi_msg_b (data, strlen (data));
ASSERT_EQ (hi_msg_a, hi_msg_b);
}
TEST (message, equality_equal_empty) {
const zmq::message_t msg_a;
const zmq::message_t msg_b;
ASSERT_EQ (msg_a, msg_b);
}
TEST (message, equality_non_equal) {
const zmq::message_t msg_a ("Hi", 2);
const zmq::message_t msg_b ("Hello", 5);
ASSERT_NE (msg_a, msg_b);
}
TEST (message, equality_non_equal_rhs_empty) {
const zmq::message_t msg_a ("Hi", 2);
const zmq::message_t msg_b;
ASSERT_NE (msg_a, msg_b);
}
TEST (message, equality_non_equal_lhs_empty) {
const zmq::message_t msg_a;
const zmq::message_t msg_b ("Hi", 2);
ASSERT_NE (msg_a, msg_b);
}

13
zmq.hpp
View File

@ -388,20 +388,13 @@ namespace zmq
ZMQ_DEPRECATED("from 4.3.0, use operator== instead")
inline bool equal(const message_t* other) const ZMQ_NOTHROW
{
if (size() != other->size())
return false;
const std::string a(data<char>(), size());
const std::string b(other->data<char>(), other->size());
return a == b;
return *this == *other;
}
inline bool operator==(const message_t &other) const ZMQ_NOTHROW
{
if (size () != other.size ())
return false;
const std::string a(data<char>(), size());
const std::string b(other.data<char>(), other.size());
return a == b;
const size_t my_size = size ();
return my_size == other.size () && 0 == memcmp (data (), other.data (), my_size);
}
inline bool operator!=(const message_t &other) const ZMQ_NOTHROW