51 #include <gtest/gtest.h> 92 EXPECT_TRUE(bytes.
init(&buffer[0], SIZE));
99 char bigBuffer[TOO_BIG];
100 EXPECT_FALSE(bytes.
init(&bigBuffer[0], TOO_BIG));
103 std::cout << std::string(15,
' ')
104 <<
"ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl;
116 ASSERT_TRUE(bytes.
init(&buffer[0], SIZE));
123 EXPECT_TRUE(bytes.
load(bIN));
125 EXPECT_TRUE(bytes.
unload(bOUT));
127 EXPECT_EQ(bOUT, bIN);
130 EXPECT_TRUE(bytes.
load(iIN));
132 EXPECT_TRUE(bytes.
unload(iOUT));
134 EXPECT_EQ(iOUT, iIN);
137 EXPECT_TRUE(bytes.
load(rIN));
139 EXPECT_TRUE(bytes.
unload(rOUT));
141 EXPECT_EQ(rOUT, rIN);
144 EXPECT_TRUE(empty.
load(bIN));
146 EXPECT_TRUE(empty.
unload(bOUT));
148 EXPECT_EQ(bOUT, bIN);
153 EXPECT_TRUE(empty.
load(rIN));
155 EXPECT_TRUE(empty.
load(iIN));
158 EXPECT_EQ(rOUT, rIN);
159 EXPECT_TRUE(empty.
unload(iOUT));
161 EXPECT_EQ(iOUT, iIN);
164 TEST(ByteArraySuite, byteSwapping)
166 if(ByteArray::isByteSwapEnabled())
168 ASSERT_TRUE(ByteArray::isByteSwapEnabled());
171 unsigned char buffer[] = {
172 0x00, 0x00, 0x00, 0x38,
173 0x00, 0x00, 0x00, 0x0a,
174 0x00, 0x00, 0x00, 0x01,
176 0x3e, 0x81, 0x32, 0x64,
177 0x3f, 0x30, 0x4b, 0x75,
178 0x3f, 0xa8, 0x9d, 0xd2,
179 0x3f, 0x85, 0x93, 0xdd,
180 0xbf, 0xf4, 0x8c, 0xc5,
183 const unsigned int bufferLength = 32;
187 swapped.
init((
const char*) buffer, bufferLength);
190 ASSERT_TRUE(swapped.
unload(tempReal));
191 EXPECT_FLOAT_EQ(tempReal, -1.9105459451675415);
193 ASSERT_TRUE(swapped.
unload(tempReal));
194 EXPECT_FLOAT_EQ(tempReal, 1.0435749292373657);
196 ASSERT_TRUE(swapped.
unload(tempReal));
197 EXPECT_FLOAT_EQ(tempReal, 1.3173162937164307);
199 ASSERT_TRUE(swapped.
unload(tempReal));
200 EXPECT_FLOAT_EQ(tempReal, 0.68865138292312622);
202 ASSERT_TRUE(swapped.
unload(tempReal));
203 EXPECT_FLOAT_EQ(tempReal, 0.25233757495880127);
205 ASSERT_TRUE(swapped.
unload(tempInt));
206 EXPECT_EQ(tempInt, 1);
208 ASSERT_TRUE(swapped.
unload(tempInt));
209 EXPECT_EQ(tempInt, 10);
211 ASSERT_TRUE(swapped.
unload(tempInt));
212 EXPECT_EQ(tempInt, 56);
229 EXPECT_TRUE(copyFrom.
init(&buffer[0], SIZE));
230 EXPECT_TRUE(copyTo.
load(copyFrom));
232 EXPECT_TRUE(copyTo.
load(copyFrom));
237 if (tooBig.getMaxBufferSize()-1 <= std::numeric_limits<shared_int>::max())
240 char bigBuffer[TOO_BIG];
242 EXPECT_TRUE(tooBig.init(&bigBuffer[0], TOO_BIG));
243 EXPECT_FALSE(copyTo.
load(tooBig));
248 std::cout << std::string(15,
' ')
249 <<
"ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl;
259 return TcpClient::sendBytes(buffer);
267 return TcpServer::receiveBytes(buffer, num_bytes);
276 return UdpClient::sendBytes(buffer);
284 return UdpServer::receiveBytes(buffer, num_bytes);
299 const int port = TEST_PORT_BASE;
300 char ipAddr[] =
"127.0.0.1";
310 ASSERT_TRUE(server.
init(port));
313 ASSERT_TRUE(client.
init(&ipAddr[0], port));
314 pthread_t serverConnectThrd;
318 pthread_join(serverConnectThrd, NULL);
320 ASSERT_TRUE(send.
load(DATA));
346 const int DATA = 256;
357 TEST(SocketSuite, splitPackets)
359 const int port = TEST_PORT_BASE + 1;
360 char ipAddr[] =
"127.0.0.1";
361 const int RECV_LENGTH = 64;
367 ASSERT_TRUE(server.
init(port));
370 ASSERT_TRUE(client.
init(&ipAddr[0], port));
371 pthread_t serverConnectThrd;
375 pthread_join(serverConnectThrd, NULL);
377 pthread_t senderThrd;
378 pthread_create(&senderThrd, NULL,
spinSender, &client);
383 pthread_cancel(senderThrd);
384 pthread_join(senderThrd, NULL);
412 EXPECT_FALSE(ping.
init(msg));
418 EXPECT_TRUE(ping.init(msg));
422 TEST(PingMessageSuite, toMessage)
439 EXPECT_FALSE(ping.
toTopic(msg));
448 ASSERT_TRUE(handler.
init(&client));
451 EXPECT_FALSE(handler.
init(NULL));
455 TEST(MessageManagerSuite, init)
460 EXPECT_TRUE(manager.
init(&client));
461 EXPECT_FALSE(manager.
init(NULL));
465 TEST(MessageManagerSuite, addHandler)
473 ASSERT_TRUE(manager.
init(&client));
475 EXPECT_FALSE(manager.
add(NULL));
477 ASSERT_TRUE(handler.
init(&client));
478 EXPECT_FALSE(manager.
add(&handler));
557 int main(
int argc,
char **argv)
560 testing::InitGoogleTest(&argc, argv);
561 return RUN_ALL_TESTS();
Defines TCP server functions.
bool init(char *buff, int port_num)
initializes TCP client socket. Object can either be a client OR a server, NOT BOTH.
void * spinFunc(void *arg)
bool init(industrial::simple_message::SimpleMessage &msg)
Initializes message from a simple message.
Contains platform specific type definitions that guarantee the size of primitive data types...
void init()
Initializes or Reinitializes an empty buffer.
bool init(industrial::smpl_msg_connection::SmplMsgConnection *connection)
Class initializer.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
This class defines a simple messaging protocol for communicating with an industrial robot controller...
int getMessageType()
Gets message type(see StandardMsgType)
bool unloadFront(industrial::shared_types::shared_real &value)
unloads a double value from the beginning of the byte array. If byte swapping is enabled, then the bytes are swapped.
bool load(industrial::shared_types::shared_bool value)
loads a boolean into the byte array
virtual bool toRequest(industrial::simple_message::SimpleMessage &msg)
creates a simple_message request
Class encapsulated ping message generation methods (either to or from a SimpleMessage type...
unsigned int getNumHandlers()
Gets number of handlers.
bool receiveBytes(ByteArray &buffer, shared_int num_bytes)
Method used by receive message interface method. This should be overridden for the specific connectio...
bool makeConnect()
connects to the remote host
int getMsgType()
Gets message type that callback expects.
void spin()
Perform a indefinite execution of the message manager.
bool add(industrial::message_handler::MessageHandler *handler, bool allow_replace=false)
Adds a message handler to the manager.
Message handler that handles ping messages.
The byte array wraps a dynamic array of bytes (i.e. char).
bool sendBytes(ByteArray &buffer)
Method used by send message interface method. This should be overridden for the specific connection t...
int main(int argc, char **argv)
bool toTopic(industrial::simple_message::SimpleMessage &msg)
The ping message overrides the base method toTopic to always return false. A ping cannot be sent as a...
unsigned int getMaxBufferSize()
gets current buffer size
The message manager handles communications for a simple server.
bool init(int msgType, int commType, int replyCode, industrial::byte_array::ByteArray &data)
Initializes a fully populated simple message.
int getMessageType() const
gets message type (enumeration)
virtual bool toReply(industrial::simple_message::SimpleMessage &msg, industrial::simple_message::ReplyType reply)
creates a simple_message reply
bool makeConnect()
connects to the remote host
void * connectServerFunc(void *arg)
unsigned int getBufferSize()
gets current buffer size
TEST(ByteArraySuite, init)
bool unload(industrial::shared_types::shared_bool &value)
unloads a boolean value from the byte array
void * spinSender(void *arg)
bool init(int port_num)
initializes TCP server socket. The connect method must be called following initialization in order to...
bool init(industrial::smpl_msg_connection::SmplMsgConnection *connection)
Class initializer.
Defines TCP client functions.
int getReplyCode()
Gets reply code(see ReplyType)
int getCommType()
Gets message type(see CommType)