Debian Codegraph @

tests/message.cpp

raw
1 #define CATCH_CONFIG_MAIN
2 #include <catch2/catch.hpp>
3 #include <zmq.hpp>
4
5 #if defined(ZMQ_CPP11)
6 static_assert(!std::is_copy_constructible<zmq::message_t>::value,
7 "message_t should not be copy-constructible");
8 static_assert(!std::is_copy_assignable<zmq::message_t>::value,
9 "message_t should not be copy-assignable");
10 #endif
11 #if (__cplusplus >= 201703L)
12 static_assert(std::is_nothrow_swappable<zmq::message_t>::value,
13 "message_t should be nothrow swappable");
14 #endif
15
16 TEST_CASE("message default constructed", "[message]")
17 {
18 const zmq::message_t message;
19 CHECK(0u == message.size());
20 CHECK(message.empty());
21 }
22
23 #ifdef ZMQ_CPP11
24 TEST_CASE("message swap", "[message]")
25 {
26 const std::string data = "foo";
27 zmq::message_t message1;
28 zmq::message_t message2(data.data(), data.size());
29 using std::swap;
30 swap(message1, message2);
31 CHECK(message1.size() == data.size());
32 CHECK(message2.size() == 0);
33 swap(message1, message2);
34 CHECK(message1.size() == 0);
35 CHECK(message2.size() == data.size());
36 }
37 #endif
38
39 namespace
40 {
41 const char *const data = "Hi";
42 }
43
44 TEST_CASE("message constructor with iterators", "[message]")
45 {
46 const std::string hi(data);
47 const zmq::message_t hi_msg(hi.begin(), hi.end());
48 CHECK(2u == hi_msg.size());
49 CHECK(0 == memcmp(data, hi_msg.data(), 2));
50 }
51
52 TEST_CASE("message constructor with size", "[message]")
53 {
54 const zmq::message_t msg(5);
55 CHECK(msg.size() == 5);
56 }
57
58 TEST_CASE("message constructor with buffer and size", "[message]")
59 {
60 const std::string hi(data);
61 const zmq::message_t hi_msg(hi.data(), hi.size());
62 CHECK(2u == hi_msg.size());
63 CHECK(0 == memcmp(data, hi_msg.data(), 2));
64 }
65
66 TEST_CASE("message constructor with char array", "[message]")
67 {
68 const zmq::message_t hi_msg(data, strlen(data));
69 CHECK(2u == hi_msg.size());
70 CHECK(0 == memcmp(data, hi_msg.data(), 2));
71 }
72
73 #if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
74 TEST_CASE("message constructor with container - deprecated", "[message]")
75 {
76 zmq::message_t hi_msg("Hi"); // deprecated
77 REQUIRE(3u == hi_msg.size());
78 CHECK(0 == memcmp(data, hi_msg.data(), 3));
79 }
80
81 TEST_CASE("message constructor with container of trivial data", "[message]")
82 {
83 int buf[3] = {1, 2, 3};
84 zmq::message_t msg(buf);
85 REQUIRE(sizeof(buf) == msg.size());
86 CHECK(0 == memcmp(buf, msg.data(), msg.size()));
87 }
88
89 TEST_CASE("message constructor with strings", "[message]")
90 {
91 SECTION("string")
92 {
93 const std::string hi(data);
94 zmq::message_t hi_msg(hi);
95 CHECK(2u == hi_msg.size());
96 CHECK(0 == memcmp(data, hi_msg.data(), 2));
97 }
98 #if CPPZMQ_HAS_STRING_VIEW
99 SECTION("string_view")
100 {
101 const std::string_view hi(data);
102 zmq::message_t hi_msg(hi);
103 CHECK(2u == hi_msg.size());
104 CHECK(0 == memcmp(data, hi_msg.data(), 2));
105 }
106 #endif
107 }
108 #endif
109
110 #ifdef ZMQ_HAS_RVALUE_REFS
111 TEST_CASE("message move constructor", "[message]")
112 {
113 zmq::message_t hi_msg(zmq::message_t(data, strlen(data)));
114 }
115
116 TEST_CASE("message assign move empty before", "[message]")
117 {
118 zmq::message_t hi_msg;
119 hi_msg = zmq::message_t(data, strlen(data));
120 CHECK(2u == hi_msg.size());
121 CHECK(0 == memcmp(data, hi_msg.data(), 2));
122 }
123
124 TEST_CASE("message assign move empty after", "[message]")
125 {
126 zmq::message_t hi_msg(data, strlen(data));
127 CHECK(!hi_msg.empty());
128 hi_msg = zmq::message_t();
129 CHECK(0u == hi_msg.size());
130 CHECK(hi_msg.empty());
131 }
132
133 TEST_CASE("message assign move empty before and after", "[message]")
134 {
135 zmq::message_t hi_msg;
136 hi_msg = zmq::message_t();
137 CHECK(0u == hi_msg.size());
138 }
139 #endif
140
141 TEST_CASE("message equality self", "[message]")
142 {
143 const zmq::message_t hi_msg(data, strlen(data));
144 CHECK(hi_msg == hi_msg);
145 }
146
147 TEST_CASE("message equality equal", "[message]")
148 {
149 const zmq::message_t hi_msg_a(data, strlen(data));
150 const zmq::message_t hi_msg_b(data, strlen(data));
151 CHECK(hi_msg_a == hi_msg_b);
152 }
153
154 TEST_CASE("message equality equal empty", "[message]")
155 {
156 const zmq::message_t msg_a;
157 const zmq::message_t msg_b;
158 CHECK(msg_a == msg_b);
159 }
160
161 TEST_CASE("message equality non equal", "[message]")
162 {
163 const zmq::message_t msg_a("Hi", 2);
164 const zmq::message_t msg_b("Hello", 5);
165 CHECK(msg_a != msg_b);
166 }
167
168 TEST_CASE("message equality non equal rhs empty", "[message]")
169 {
170 const zmq::message_t msg_a("Hi", 2);
171 const zmq::message_t msg_b;
172 CHECK(msg_a != msg_b);
173 }
174
175 TEST_CASE("message equality non equal lhs empty", "[message]")
176 {
177 const zmq::message_t msg_a;
178 const zmq::message_t msg_b("Hi", 2);
179 CHECK(msg_a != msg_b);
180 }
181
182 TEST_CASE("message to string", "[message]")
183 {
184 const zmq::message_t a;
185 const zmq::message_t b("Foo", 3);
186 CHECK(a.to_string() == "");
187 CHECK(b.to_string() == "Foo");
188 #if CPPZMQ_HAS_STRING_VIEW
189 CHECK(a.to_string_view() == "");
190 CHECK(b.to_string_view() == "Foo");
191 #endif
192
193 #if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
194 const zmq::message_t depr("Foo"); // deprecated
195 CHECK(depr.to_string() != "Foo");
196 CHECK(depr.to_string() == std::string("Foo", 4));
197 #endif
198 }
199
200 #if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
201 TEST_CASE("message routing id persists", "[message]")
202 {
203 zmq::message_t msg;
204 msg.set_routing_id(123);
205 CHECK(123u == msg.routing_id());
206 }
207
208 TEST_CASE("message group persists", "[message]")
209 {
210 zmq::message_t msg;
211 msg.set_group("mygroup");
212 CHECK(std::string(msg.group()) == "mygroup");
213 }
214 #endif
215
216 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 2, 0)
217 TEST_CASE("message is not shared", "[message]")
218 {
219 zmq::message_t msg;
220 CHECK(msg.get(ZMQ_SHARED) == 0);
221 }
222
223 TEST_CASE("message is shared", "[message]")
224 {
225 size_t msg_sz = 1024; // large enough to be a type_lmsg
226 zmq::message_t msg1(msg_sz);
227 zmq::message_t msg2;
228 msg2.copy(msg1);
229 CHECK(msg1.get(ZMQ_SHARED) == 1);
230 CHECK(msg2.get(ZMQ_SHARED) == 1);
231 CHECK(msg1.size() == msg_sz);
232 CHECK(msg2.size() == msg_sz);
233 }
234
235 TEST_CASE("message move is not shared", "[message]")
236 {
237 size_t msg_sz = 1024; // large enough to be a type_lmsg
238 zmq::message_t msg1(msg_sz);
239 zmq::message_t msg2;
240 msg2.move(msg1);
241 CHECK(msg1.get(ZMQ_SHARED) == 0);
242 CHECK(msg2.get(ZMQ_SHARED) == 0);
243 CHECK(msg2.size() == msg_sz);
244 CHECK(msg1.size() == 0);
245 }
246 #endif