forked from zeromq/cppzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipart.cpp
212 lines (166 loc) · 5.59 KB
/
multipart.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
#include <catch2/catch.hpp>
#include <zmq_addon.hpp>
#ifdef ZMQ_HAS_RVALUE_REFS
#ifdef ZMQ_CPP17
static_assert(std::is_invocable<decltype(&zmq::multipart_t::send),
zmq::multipart_t *,
zmq::socket_ref,
int>::value,
"Can't multipart_t::send with socket_ref");
static_assert(std::is_invocable<decltype(&zmq::multipart_t::recv),
zmq::multipart_t *,
zmq::socket_ref,
int>::value,
"Can't multipart_t::recv with socket_ref");
#endif
static_assert(std::is_constructible<zmq::multipart_t, zmq::socket_ref>::value,
"Can't construct with socket_ref");
/// \todo split this up into separate test cases
///
TEST_CASE("multipart legacy test", "[multipart]")
{
using namespace zmq;
bool ok = true;
(void) ok;
float num = 0;
(void) num;
std::string str = "";
message_t msg;
// Create two PAIR sockets and connect over inproc
context_t context(1);
socket_t output(context, ZMQ_PAIR);
socket_t input(context, ZMQ_PAIR);
output.bind("inproc://multipart.test");
input.connect("inproc://multipart.test");
// Test send and receive of single-frame message
multipart_t multipart;
assert(multipart.empty());
multipart.push(message_t("Hello", 5));
assert(multipart.size() == 1);
ok = multipart.send(output);
assert(multipart.empty());
assert(ok);
ok = multipart.recv(input);
assert(multipart.size() == 1);
assert(ok);
msg = multipart.pop();
assert(multipart.empty());
assert(std::string(msg.data<char>(), msg.size()) == "Hello");
// Test send and receive of multi-frame message
multipart.addstr("A");
multipart.addstr("BB");
multipart.addstr("CCC");
assert(multipart.size() == 3);
multipart_t copy = multipart.clone();
assert(copy.size() == 3);
ok = copy.send(output);
assert(copy.empty());
assert(ok);
ok = copy.recv(input);
assert(copy.size() == 3);
assert(ok);
assert(copy.equal(&multipart));
// Test equality operators
assert(copy == multipart);
assert(multipart == copy);
multipart.pop();
assert(copy != multipart);
assert(multipart != copy);
multipart_t emptyMessage1 {};
multipart_t emptyMessage2 {};
assert(emptyMessage1 == emptyMessage2);
assert(emptyMessage2 == emptyMessage1);
multipart.clear();
assert(multipart.empty());
// Test message frame manipulation
multipart.add(message_t("Frame5", 6));
multipart.addstr("Frame6");
multipart.addstr("Frame7");
multipart.addtyp(8.0f);
multipart.addmem("Frame9", 6);
multipart.push(message_t("Frame4", 6));
multipart.pushstr("Frame3");
multipart.pushstr("Frame2");
multipart.pushtyp(1.0f);
multipart.pushmem("Frame0", 6);
assert(multipart.size() == 10);
const message_t &front_msg = multipart.front();
assert(multipart.size() == 10);
assert(std::string(front_msg.data<char>(), front_msg.size()) == "Frame0");
const message_t &back_msg = multipart.back();
assert(multipart.size() == 10);
assert(std::string(back_msg.data<char>(), back_msg.size()) == "Frame9");
msg = multipart.remove();
assert(multipart.size() == 9);
assert(std::string(msg.data<char>(), msg.size()) == "Frame9");
msg = multipart.pop();
assert(multipart.size() == 8);
assert(std::string(msg.data<char>(), msg.size()) == "Frame0");
num = multipart.poptyp<float>();
assert(multipart.size() == 7);
assert(num == 1.0f);
str = multipart.popstr();
assert(multipart.size() == 6);
assert(str == "Frame2");
str = multipart.popstr();
assert(multipart.size() == 5);
assert(str == "Frame3");
str = multipart.popstr();
assert(multipart.size() == 4);
assert(str == "Frame4");
str = multipart.popstr();
assert(multipart.size() == 3);
assert(str == "Frame5");
str = multipart.popstr();
assert(multipart.size() == 2);
assert(str == "Frame6");
str = multipart.popstr();
assert(multipart.size() == 1);
assert(str == "Frame7");
num = multipart.poptyp<float>();
assert(multipart.empty());
assert(num == 8.0f);
// Test message constructors and concatenation
multipart_t head("One", 3);
head.addstr("Two");
assert(head.size() == 2);
multipart_t tail(std::string("One-hundred"));
tail.pushstr("Ninety-nine");
assert(tail.size() == 2);
multipart_t tmp(message_t("Fifty", 5));
assert(tmp.size() == 1);
multipart_t mid = multipart_t::create(49.0f);
mid.append(std::move(tmp));
assert(mid.size() == 2);
assert(tmp.empty());
multipart_t merged(std::move(mid));
merged.prepend(std::move(head));
merged.append(std::move(tail));
assert(merged.size() == 6);
assert(head.empty());
assert(tail.empty());
ok = merged.send(output);
assert(merged.empty());
assert(ok);
multipart_t received(input);
assert(received.size() == 6);
str = received.popstr();
assert(received.size() == 5);
assert(str == "One");
str = received.popstr();
assert(received.size() == 4);
assert(str == "Two");
num = received.poptyp<float>();
assert(received.size() == 3);
assert(num == 49.0f);
str = received.popstr();
assert(received.size() == 2);
assert(str == "Fifty");
str = received.popstr();
assert(received.size() == 1);
assert(str == "Ninety-nine");
str = received.popstr();
assert(received.empty());
assert(str == "One-hundred");
}
#endif