51 #include <gtest/gtest.h>
91 EXPECT_TRUE(bytes.
init(&buffer[0], SIZE));
98 std::vector<char> bigBuffer(TOO_BIG);
99 EXPECT_FALSE(bytes.
init(&bigBuffer[0], TOO_BIG));
102 std::cout << std::string(15,
' ')
103 <<
"ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl;
115 ASSERT_TRUE(bytes.
init(&buffer[0], SIZE));
122 EXPECT_TRUE(bytes.
load(bIN));
124 EXPECT_TRUE(bytes.
unload(bOUT));
126 EXPECT_EQ(bOUT, bIN);
129 EXPECT_TRUE(bytes.
load(iIN));
131 EXPECT_TRUE(bytes.
unload(iOUT));
133 EXPECT_EQ(iOUT, iIN);
136 EXPECT_TRUE(bytes.
load(rIN));
138 EXPECT_TRUE(bytes.
unload(rOUT));
140 EXPECT_EQ(rOUT, rIN);
143 EXPECT_TRUE(empty.
load(bIN));
145 EXPECT_TRUE(empty.
unload(bOUT));
147 EXPECT_EQ(bOUT, bIN);
152 EXPECT_TRUE(empty.
load(rIN));
154 EXPECT_TRUE(empty.
load(iIN));
157 EXPECT_EQ(rOUT, rIN);
158 EXPECT_TRUE(empty.
unload(iOUT));
160 EXPECT_EQ(iOUT, iIN);
163 TEST(ByteArraySuite, byteSwapping)
165 if(ByteArray::isByteSwapEnabled())
167 ASSERT_TRUE(ByteArray::isByteSwapEnabled());
170 unsigned char buffer[] = {
171 0x00, 0x00, 0x00, 0x38,
172 0x00, 0x00, 0x00, 0x0a,
173 0x00, 0x00, 0x00, 0x01,
175 0x3e, 0x81, 0x32, 0x64,
176 0x3f, 0x30, 0x4b, 0x75,
177 0x3f, 0xa8, 0x9d, 0xd2,
178 0x3f, 0x85, 0x93, 0xdd,
179 0xbf, 0xf4, 0x8c, 0xc5,
182 const unsigned int bufferLength = 32;
186 swapped.
init((
const char*) buffer, bufferLength);
189 ASSERT_TRUE(swapped.
unload(tempReal));
190 EXPECT_FLOAT_EQ(tempReal, -1.9105459451675415);
192 ASSERT_TRUE(swapped.
unload(tempReal));
193 EXPECT_FLOAT_EQ(tempReal, 1.0435749292373657);
195 ASSERT_TRUE(swapped.
unload(tempReal));
196 EXPECT_FLOAT_EQ(tempReal, 1.3173162937164307);
198 ASSERT_TRUE(swapped.
unload(tempReal));
199 EXPECT_FLOAT_EQ(tempReal, 0.68865138292312622);
201 ASSERT_TRUE(swapped.
unload(tempReal));
202 EXPECT_FLOAT_EQ(tempReal, 0.25233757495880127);
204 ASSERT_TRUE(swapped.
unload(tempInt));
205 EXPECT_EQ(tempInt, 1);
207 ASSERT_TRUE(swapped.
unload(tempInt));
208 EXPECT_EQ(tempInt, 10);
210 ASSERT_TRUE(swapped.
unload(tempInt));
211 EXPECT_EQ(tempInt, 56);
228 EXPECT_TRUE(copyFrom.
init(&buffer[0], SIZE));
229 EXPECT_TRUE(copyTo.
load(copyFrom));
231 EXPECT_TRUE(copyTo.
load(copyFrom));
239 std::vector<char> bigBuffer(TOO_BIG);
241 EXPECT_TRUE(tooBig.
init(&bigBuffer[0], TOO_BIG));
242 EXPECT_FALSE(copyTo.
load(tooBig));
247 std::cout << std::string(15,
' ')
248 <<
"ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl;
258 return TcpClient::sendBytes(buffer);
266 return TcpServer::receiveBytes(buffer, num_bytes, -1);
271 return TcpServer::receiveBytes(buffer, num_bytes, timeout_ms);
280 return UdpClient::sendBytes(buffer);
288 return UdpServer::receiveBytes(buffer, num_bytes, -1);
293 return UdpServer::receiveBytes(buffer, num_bytes, timeout_ms);
308 const int port = TEST_PORT_BASE;
309 char ipAddr[] =
"127.0.0.1";
319 ASSERT_TRUE(server.
init(port));
322 ASSERT_TRUE(client.
init(&ipAddr[0], port));
327 serverConnectThrd.join();
329 ASSERT_TRUE(send.
load(DATA));
334 std::this_thread::sleep_for(std::chrono::seconds(2));
350 const int port = TEST_PORT_BASE;
351 char ipAddr[] =
"127.0.0.1";
362 ASSERT_TRUE(server.
init(port));
365 ASSERT_TRUE(client.
init(&ipAddr[0], port));
366 pthread_t serverConnectThrd;
370 pthread_join(serverConnectThrd, NULL);
372 ASSERT_TRUE(send.
load(DATA));
375 ASSERT_FALSE(server.
receiveBytes(recv, TWO_INTS, TIMEOUT_MS));
381 ASSERT_FALSE(server.
receiveBytes(recv, TWO_INTS, TIMEOUT_MS));
391 const int DATA = 256;
398 std::this_thread::sleep_for(std::chrono::milliseconds(100));
402 TEST(SocketSuite, splitPackets)
404 const int port = TEST_PORT_BASE + 1;
405 char ipAddr[] =
"127.0.0.1";
406 const int RECV_LENGTH = 64;
412 ASSERT_TRUE(server.
init(port));
415 ASSERT_TRUE(client.
init(&ipAddr[0], port));
419 serverConnectThrd.join();
421 bool senderRunning =
true;
422 std::thread senderThrd(
spinSender, &client, std::ref(senderRunning));
427 senderRunning =
false;
456 EXPECT_FALSE(ping.
init(msg));
462 EXPECT_TRUE(ping.
init(msg));
466 TEST(PingMessageSuite, toMessage)
483 EXPECT_FALSE(ping.
toTopic(msg));
492 ASSERT_TRUE(handler.
init(&client));
495 EXPECT_FALSE(handler.
init(NULL));
499 TEST(MessageManagerSuite, init)
504 EXPECT_TRUE(manager.
init(&client));
505 EXPECT_FALSE(manager.
init(NULL));
509 TEST(MessageManagerSuite, addHandler)
517 ASSERT_TRUE(manager.
init(&client));
519 EXPECT_FALSE(manager.
add(NULL));
521 ASSERT_TRUE(handler.
init(&client));
522 EXPECT_FALSE(manager.
add(&handler));
601 int main(
int argc,
char **argv)
604 testing::InitGoogleTest(&argc, argv);
605 return RUN_ALL_TESTS();