forked from zeromq/cppzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.cpp
246 lines (218 loc) · 6.23 KB
/
message.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
243
244
245
246
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <zmq.hpp>
#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
#if (__cplusplus >= 201703L)
static_assert(std::is_nothrow_swappable<zmq::message_t>::value,
"message_t should be nothrow swappable");
#endif
TEST_CASE("message default constructed", "[message]")
{
const zmq::message_t message;
CHECK(0u == message.size());
CHECK(message.empty());
}
#ifdef ZMQ_CPP11
TEST_CASE("message swap", "[message]")
{
const std::string data = "foo";
zmq::message_t message1;
zmq::message_t message2(data.data(), data.size());
using std::swap;
swap(message1, message2);
CHECK(message1.size() == data.size());
CHECK(message2.size() == 0);
swap(message1, message2);
CHECK(message1.size() == 0);
CHECK(message2.size() == data.size());
}
#endif
namespace
{
const char *const data = "Hi";
}
TEST_CASE("message constructor with iterators", "[message]")
{
const std::string hi(data);
const zmq::message_t hi_msg(hi.begin(), hi.end());
CHECK(2u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}
TEST_CASE("message constructor with size", "[message]")
{
const zmq::message_t msg(5);
CHECK(msg.size() == 5);
}
TEST_CASE("message constructor with buffer and size", "[message]")
{
const std::string hi(data);
const zmq::message_t hi_msg(hi.data(), hi.size());
CHECK(2u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}
TEST_CASE("message constructor with char array", "[message]")
{
const zmq::message_t hi_msg(data, strlen(data));
CHECK(2u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}
#if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
TEST_CASE("message constructor with container - deprecated", "[message]")
{
zmq::message_t hi_msg("Hi"); // deprecated
REQUIRE(3u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 3));
}
TEST_CASE("message constructor with container of trivial data", "[message]")
{
int buf[3] = {1, 2, 3};
zmq::message_t msg(buf);
REQUIRE(sizeof(buf) == msg.size());
CHECK(0 == memcmp(buf, msg.data(), msg.size()));
}
TEST_CASE("message constructor with strings", "[message]")
{
SECTION("string")
{
const std::string hi(data);
zmq::message_t hi_msg(hi);
CHECK(2u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}
#if CPPZMQ_HAS_STRING_VIEW
SECTION("string_view")
{
const std::string_view hi(data);
zmq::message_t hi_msg(hi);
CHECK(2u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}
#endif
}
#endif
#ifdef ZMQ_HAS_RVALUE_REFS
TEST_CASE("message move constructor", "[message]")
{
zmq::message_t hi_msg(zmq::message_t(data, strlen(data)));
}
TEST_CASE("message assign move empty before", "[message]")
{
zmq::message_t hi_msg;
hi_msg = zmq::message_t(data, strlen(data));
CHECK(2u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}
TEST_CASE("message assign move empty after", "[message]")
{
zmq::message_t hi_msg(data, strlen(data));
CHECK(!hi_msg.empty());
hi_msg = zmq::message_t();
CHECK(0u == hi_msg.size());
CHECK(hi_msg.empty());
}
TEST_CASE("message assign move empty before and after", "[message]")
{
zmq::message_t hi_msg;
hi_msg = zmq::message_t();
CHECK(0u == hi_msg.size());
}
#endif
TEST_CASE("message equality self", "[message]")
{
const zmq::message_t hi_msg(data, strlen(data));
CHECK(hi_msg == hi_msg);
}
TEST_CASE("message equality equal", "[message]")
{
const zmq::message_t hi_msg_a(data, strlen(data));
const zmq::message_t hi_msg_b(data, strlen(data));
CHECK(hi_msg_a == hi_msg_b);
}
TEST_CASE("message equality equal empty", "[message]")
{
const zmq::message_t msg_a;
const zmq::message_t msg_b;
CHECK(msg_a == msg_b);
}
TEST_CASE("message equality non equal", "[message]")
{
const zmq::message_t msg_a("Hi", 2);
const zmq::message_t msg_b("Hello", 5);
CHECK(msg_a != msg_b);
}
TEST_CASE("message equality non equal rhs empty", "[message]")
{
const zmq::message_t msg_a("Hi", 2);
const zmq::message_t msg_b;
CHECK(msg_a != msg_b);
}
TEST_CASE("message equality non equal lhs empty", "[message]")
{
const zmq::message_t msg_a;
const zmq::message_t msg_b("Hi", 2);
CHECK(msg_a != msg_b);
}
TEST_CASE("message to string", "[message]")
{
const zmq::message_t a;
const zmq::message_t b("Foo", 3);
CHECK(a.to_string() == "");
CHECK(b.to_string() == "Foo");
#if CPPZMQ_HAS_STRING_VIEW
CHECK(a.to_string_view() == "");
CHECK(b.to_string_view() == "Foo");
#endif
#if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
const zmq::message_t depr("Foo"); // deprecated
CHECK(depr.to_string() != "Foo");
CHECK(depr.to_string() == std::string("Foo", 4));
#endif
}
#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
TEST_CASE("message routing id persists", "[message]")
{
zmq::message_t msg;
msg.set_routing_id(123);
CHECK(123u == msg.routing_id());
}
TEST_CASE("message group persists", "[message]")
{
zmq::message_t msg;
msg.set_group("mygroup");
CHECK(std::string(msg.group()) == "mygroup");
}
#endif
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 2, 0)
TEST_CASE("message is not shared", "[message]")
{
zmq::message_t msg;
CHECK(msg.get(ZMQ_SHARED) == 0);
}
TEST_CASE("message is shared", "[message]")
{
size_t msg_sz = 1024; // large enough to be a type_lmsg
zmq::message_t msg1(msg_sz);
zmq::message_t msg2;
msg2.copy(msg1);
CHECK(msg1.get(ZMQ_SHARED) == 1);
CHECK(msg2.get(ZMQ_SHARED) == 1);
CHECK(msg1.size() == msg_sz);
CHECK(msg2.size() == msg_sz);
}
TEST_CASE("message move is not shared", "[message]")
{
size_t msg_sz = 1024; // large enough to be a type_lmsg
zmq::message_t msg1(msg_sz);
zmq::message_t msg2;
msg2.move(msg1);
CHECK(msg1.get(ZMQ_SHARED) == 0);
CHECK(msg2.get(ZMQ_SHARED) == 0);
CHECK(msg2.size() == msg_sz);
CHECK(msg1.size() == 0);
}
#endif