Skip to content

Commit 19da7a4

Browse files
authored
Merge pull request #205 from sigiesec/split-testcases
Improved tests and implementation of message_t
2 parents a186ed6 + 2aac1df commit 19da7a4

File tree

2 files changed

+103
-28
lines changed

2 files changed

+103
-28
lines changed

tests/message.cpp

Lines changed: 100 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,112 @@
11
#include <gtest/gtest.h>
22
#include <zmq.hpp>
33

4-
TEST (message, create_destroy)
4+
#if defined(ZMQ_CPP11)
5+
static_assert(!std::is_copy_constructible<zmq::message_t>::value, "message_t should not be copy-constructible");
6+
static_assert(!std::is_copy_assignable<zmq::message_t>::value, "message_t should not be copy-assignable");
7+
#endif
8+
9+
TEST (message, constructor_default)
510
{
6-
zmq::message_t message;
11+
const zmq::message_t message;
12+
ASSERT_EQ (0u, message.size ());
713
}
814

9-
TEST (message, constructors)
15+
const char* const data = "Hi";
16+
17+
TEST (message, constructor_iterators)
1018
{
11-
const std::string hi ("Hi");
12-
zmq::message_t hi_msg_a (hi.begin (), hi.end ());
13-
ASSERT_EQ (hi_msg_a.size (), hi.size ());
14-
zmq::message_t hi_msg_b (hi.data (), hi.size ());
15-
ASSERT_EQ (hi_msg_b.size (), hi.size ());
16-
ASSERT_EQ (hi_msg_a, hi_msg_b);
19+
const std::string hi (data);
20+
const zmq::message_t hi_msg (hi.begin (), hi.end ());
21+
ASSERT_EQ (2u, hi_msg.size ());
22+
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
23+
}
24+
25+
TEST (message, constructor_pointer_size)
26+
{
27+
const std::string hi (data);
28+
const zmq::message_t hi_msg (hi.data (), hi.size ());
29+
ASSERT_EQ (2u, hi_msg.size ());
30+
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
31+
}
32+
33+
TEST (message, constructor_char_array) {
34+
const zmq::message_t hi_msg (data, strlen (data));
35+
ASSERT_EQ (2u, hi_msg.size ());
36+
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
37+
}
38+
1739
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
18-
zmq::message_t hello_msg_a ("Hello");
19-
ASSERT_NE (hi_msg_a, hello_msg_a);
20-
ASSERT_NE (hi_msg_b, hello_msg_a);
21-
zmq::message_t hi_msg_c (hi);
22-
ASSERT_EQ (hi_msg_c, hi_msg_a);
23-
ASSERT_EQ (hi_msg_c, hi_msg_b);
24-
ASSERT_NE (hi_msg_c, hello_msg_a);
40+
TEST (message, constructor_container)
41+
{
42+
const std::string hi (data);
43+
zmq::message_t hi_msg (hi);
44+
ASSERT_EQ (2u, hi_msg.size ());
45+
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
46+
}
2547
#endif
48+
2649
#ifdef ZMQ_HAS_RVALUE_REFS
27-
zmq::message_t hello_msg_b(zmq::message_t("Hello"));
28-
ASSERT_EQ (hello_msg_a, hello_msg_b);
50+
TEST (message, constructor_move)
51+
{
52+
zmq::message_t hi_msg (zmq::message_t(data, strlen (data)));
53+
}
54+
55+
TEST (message, assign_move_empty_before)
56+
{
57+
zmq::message_t hi_msg;
58+
hi_msg = zmq::message_t (data, strlen (data));
59+
ASSERT_EQ (2u, hi_msg.size ());
60+
ASSERT_EQ (0, memcmp (data, hi_msg.data (), 2));
61+
}
62+
63+
TEST (message, assign_move_empty_after)
64+
{
65+
zmq::message_t hi_msg (data, strlen (data));
66+
hi_msg = zmq::message_t();
67+
ASSERT_EQ (0u, hi_msg.size ());
68+
}
69+
70+
TEST (message, assign_move_empty_before_and_after)
71+
{
72+
zmq::message_t hi_msg;
73+
hi_msg = zmq::message_t();
74+
ASSERT_EQ (0u, hi_msg.size ());
75+
}
2976
#endif
77+
78+
TEST (message, equality_self) {
79+
const zmq::message_t hi_msg (data, strlen (data));
80+
ASSERT_EQ (hi_msg, hi_msg);
81+
}
82+
83+
TEST (message, equality_equal) {
84+
const zmq::message_t hi_msg_a (data, strlen (data));
85+
const zmq::message_t hi_msg_b (data, strlen (data));
86+
ASSERT_EQ (hi_msg_a, hi_msg_b);
3087
}
88+
89+
TEST (message, equality_equal_empty) {
90+
const zmq::message_t msg_a;
91+
const zmq::message_t msg_b;
92+
ASSERT_EQ (msg_a, msg_b);
93+
}
94+
95+
TEST (message, equality_non_equal) {
96+
const zmq::message_t msg_a ("Hi", 2);
97+
const zmq::message_t msg_b ("Hello", 5);
98+
ASSERT_NE (msg_a, msg_b);
99+
}
100+
101+
TEST (message, equality_non_equal_rhs_empty) {
102+
const zmq::message_t msg_a ("Hi", 2);
103+
const zmq::message_t msg_b;
104+
ASSERT_NE (msg_a, msg_b);
105+
}
106+
107+
TEST (message, equality_non_equal_lhs_empty) {
108+
const zmq::message_t msg_a;
109+
const zmq::message_t msg_b ("Hi", 2);
110+
ASSERT_NE (msg_a, msg_b);
111+
}
112+

zmq.hpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -388,20 +388,13 @@ namespace zmq
388388
ZMQ_DEPRECATED("from 4.3.0, use operator== instead")
389389
inline bool equal(const message_t* other) const ZMQ_NOTHROW
390390
{
391-
if (size() != other->size())
392-
return false;
393-
const std::string a(data<char>(), size());
394-
const std::string b(other->data<char>(), other->size());
395-
return a == b;
391+
return *this == *other;
396392
}
397393

398394
inline bool operator==(const message_t &other) const ZMQ_NOTHROW
399395
{
400-
if (size () != other.size ())
401-
return false;
402-
const std::string a(data<char>(), size());
403-
const std::string b(other.data<char>(), other.size());
404-
return a == b;
396+
const size_t my_size = size ();
397+
return my_size == other.size () && 0 == memcmp (data (), other.data (), my_size);
405398
}
406399

407400
inline bool operator!=(const message_t &other) const ZMQ_NOTHROW

0 commit comments

Comments
 (0)