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


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:56