RoveComm C++ 3.0.0
Welcome to the RoveComm C++ repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of RoveComm C++. The RoveComm C++ project is the RoveComm interface for high level systems and should not be used for microcontrollers.
Loading...
Searching...
No Matches
udp.cc File Reference

Unit test for UDP functionality in RoveComm. More...

Include dependency graph for udp.cc:

Functions

 TEST (RoveCommUDP, InitSocket)
 Test Initialization of a UDP Socket.
 TEST (RoveCommUDP, SendMultipleDataTypes)
 Test Sending Multiple Data Types via UDP.
 TEST (RoveCommUDP, CallbackMultipleDataTypes)
 Main function for running the tests.
 TEST (RoveCommUDP, ManifestIntegration)
 Main function for running the tests.
 TEST (RoveCommUDP, CallbacksBelongToTheirNode)
 TEST (RoveCommUDP, ClearRemovesOnlyThatCallback)

Detailed Description

Unit test for UDP functionality in RoveComm.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-02-07

Function Documentation

◆ TEST() [1/6]

TEST ( RoveCommUDP ,
CallbackMultipleDataTypes  )

Main function for running the tests.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-04-21
344{
345 // Run the test via the RunTimedTest function to allow for retries and timeouts.
347 []()
348 {
349 // Create RoveComm Nodes
350 rovecomm::RoveCommUDP RoveCommUDPNode;
351
352 // Flag to check if the initialization was successful
353 bool bInitSuccess = false;
354
355 // Give the node three chances to initialize the socket
356 for (int nI = 0; nI < 3; ++nI)
357 {
358 if (RoveCommUDPNode.Init(11004))
359 {
360 bInitSuccess = true;
361 break;
362 }
363 else
364 {
365 std::this_thread::sleep_for(std::chrono::seconds(1));
366 }
367 }
368
369 ASSERT_TRUE(bInitSuccess) << "Failed to initialize the UDP socket";
370
371 // Setup dummy address for callbacks
372 struct sockaddr_in saUDPClientAddr;
373 memset(&saUDPClientAddr, 0, sizeof(saUDPClientAddr));
374 saUDPClientAddr.sin_family = AF_INET;
375 saUDPClientAddr.sin_port = htons(11004);
376 inet_pton(AF_INET, "127.0.0.1", &saUDPClientAddr.sin_addr);
377
378 // Constants for test
379 const uint16_t unTestDataId = 1300;
380
381 // Test flags for each data type
382 bool bUint8CallbackInvoked = false;
383 bool bUint16CallbackInvoked = false;
384 bool bUint32CallbackInvoked = false;
385 bool bInt8CallbackInvoked = false;
386 bool bInt16CallbackInvoked = false;
387 bool bInt32CallbackInvoked = false;
388 bool bFloatCallbackInvoked = false;
389 bool bDoubleCallbackInvoked = false;
390 bool bCharCallbackInvoked = false;
391
392 // ======================== UINT8_T CALLBACK ========================
393 {
394 // Test data
395 std::vector<uint8_t> vExpectedData = {1, 2, 3, 255};
396
397 // Add callback
398 RoveCommUDPNode.On<uint8_t>(unTestDataId,
399 [&](const rovecomm::RoveCommPacket<uint8_t>& stPacket)
400 {
401 bUint8CallbackInvoked = true;
402 EXPECT_EQ(stPacket.unDataId, unTestDataId);
403 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
404 EXPECT_EQ(stPacket.eDataType, manifest::DataTypes::UINT8_T);
405
406 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
407 {
408 EXPECT_EQ(stPacket.vData[siI], vExpectedData[siI]);
409 }
410 });
411
412 // Create packet and test callback
413 rovecomm::RoveCommPacket<uint8_t> stPacket{.unDataId = unTestDataId, .vData = vExpectedData};
414
415 // Pack the packet
416 std::vector<uint8_t> vData = rovecomm::PackPacket(stPacket);
417
418 // Process the packet
419 RoveCommUDPNode.ProcessPacket<uint8_t>(vData, saUDPClientAddr);
420 }
421
422 // ======================== UINT16_T CALLBACK ========================
423 {
424 // Test data
425 std::vector<uint16_t> vExpectedData = {1000, 2000, 3000, 65535};
426
427 // Add callback
428 RoveCommUDPNode.On<uint16_t>(unTestDataId,
429 [&](const rovecomm::RoveCommPacket<uint16_t>& stPacket)
430 {
431 bUint16CallbackInvoked = true;
432 EXPECT_EQ(stPacket.unDataId, unTestDataId);
433 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
434 EXPECT_EQ(stPacket.eDataType, manifest::DataTypes::UINT16_T);
435
436 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
437 {
438 EXPECT_EQ(stPacket.vData[siI], vExpectedData[siI]);
439 }
440 });
441
442 // Create packet and test callback
443 rovecomm::RoveCommPacket<uint16_t> stPacket{.unDataId = unTestDataId, .vData = vExpectedData};
444
445 // Pack the packet
446 std::vector<uint8_t> vData = rovecomm::PackPacket(stPacket);
447
448 // Process the packet
449 RoveCommUDPNode.ProcessPacket<uint16_t>(vData, saUDPClientAddr);
450 }
451
452 // ======================== INT32_T CALLBACK ========================
453 {
454 // Test data
455 std::vector<int32_t> vExpectedData = {-100000, -1, 0, 1, 100000};
456
457 // Add callback
458 RoveCommUDPNode.On<int32_t>(unTestDataId,
459 [&](const rovecomm::RoveCommPacket<int32_t>& stPacket)
460 {
461 bInt32CallbackInvoked = true;
462 EXPECT_EQ(stPacket.unDataId, unTestDataId);
463 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
464 EXPECT_EQ(stPacket.eDataType, manifest::DataTypes::INT32_T);
465
466 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
467 {
468 EXPECT_EQ(stPacket.vData[siI], vExpectedData[siI]);
469 }
470 });
471
472 // Create packet and test callback
473 rovecomm::RoveCommPacket<int32_t> stPacket{.unDataId = unTestDataId, .vData = vExpectedData};
474
475 // Pack the packet
476 std::vector<uint8_t> vData = rovecomm::PackPacket(stPacket);
477
478 // Process the packet
479 RoveCommUDPNode.ProcessPacket<int32_t>(vData, saUDPClientAddr);
480 }
481
482 // ======================== FLOAT CALLBACK ========================
483 {
484 // Test data
485 std::vector<float> vExpectedData = {-3.14f, 0.0f, 2.718f, 1000.5f};
486
487 // Add callback
488 RoveCommUDPNode.On<float>(unTestDataId,
489 [&](const rovecomm::RoveCommPacket<float>& stPacket)
490 {
491 bFloatCallbackInvoked = true;
492 EXPECT_EQ(stPacket.unDataId, unTestDataId);
493 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
494 EXPECT_EQ(stPacket.eDataType, manifest::DataTypes::FLOAT_T);
495
496 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
497 {
498 EXPECT_FLOAT_EQ(stPacket.vData[siI], vExpectedData[siI]);
499 }
500 });
501
502 // Create packet and test callback
503 rovecomm::RoveCommPacket<float> stPacket{.unDataId = unTestDataId, .vData = vExpectedData};
504
505 // Pack the packet
506 std::vector<uint8_t> vData = rovecomm::PackPacket(stPacket);
507
508 // Process the packet
509 RoveCommUDPNode.ProcessPacket<float>(vData, saUDPClientAddr);
510 }
511
512 // ======================== DOUBLE CALLBACK ========================
513 {
514 // Test data
515 std::vector<double> vExpectedData = {-3.14, 0.0, 2.718, 1000.5};
516
517 // Add callback
518 RoveCommUDPNode.On<double>(unTestDataId,
519 [&](const rovecomm::RoveCommPacket<double>& stPacket)
520 {
521 bDoubleCallbackInvoked = true;
522 EXPECT_EQ(stPacket.unDataId, unTestDataId);
523 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
524 EXPECT_EQ(stPacket.eDataType, manifest::DataTypes::DOUBLE_T);
525
526 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
527 {
528 EXPECT_DOUBLE_EQ(stPacket.vData[siI], vExpectedData[siI]);
529 }
530 });
531
532 // Create packet and test callback
533 rovecomm::RoveCommPacket<double> stPacket{.unDataId = unTestDataId, .vData = vExpectedData};
534
535 // Pack the packet
536 std::vector<uint8_t> vData = rovecomm::PackPacket(stPacket);
537
538 // Process the packet
539 RoveCommUDPNode.ProcessPacket<double>(vData, saUDPClientAddr);
540 }
541
542 // ======================== CHAR CALLBACK ========================
543 {
544 // Test data - "Hello"
545 std::string szMessage = "Hello";
546 std::vector<char> vExpectedData(szMessage.begin(), szMessage.end());
547
548 // Add callback
549 RoveCommUDPNode.On<char>(unTestDataId,
550 [&](const rovecomm::RoveCommPacket<char>& stPacket)
551 {
552 bCharCallbackInvoked = true;
553 EXPECT_EQ(stPacket.unDataId, unTestDataId);
554 EXPECT_EQ(stPacket.GetDataCount(), vExpectedData.size());
555 EXPECT_EQ(stPacket.eDataType, manifest::DataTypes::CHAR);
556
557 for (size_t siI = 0; siI < stPacket.vData.size(); siI++)
558 {
559 EXPECT_EQ(stPacket.vData[siI], vExpectedData[siI]);
560 }
561 });
562
563 // Create packet and test callback
564 rovecomm::RoveCommPacket<char> stPacket{.unDataId = unTestDataId, .vData = vExpectedData};
565
566 // Pack the packet
567 std::vector<uint8_t> vData = rovecomm::PackPacket(stPacket);
568
569 // Process the packet
570 RoveCommUDPNode.ProcessPacket<char>(vData, saUDPClientAddr);
571 }
572
573 // Check if all the callbacks were invoked
574 EXPECT_TRUE(bUint8CallbackInvoked) << "uint8_t callback was not invoked";
575 EXPECT_TRUE(bUint16CallbackInvoked) << "uint16_t callback was not invoked";
576 EXPECT_TRUE(bInt32CallbackInvoked) << "int32_t callback was not invoked";
577 EXPECT_TRUE(bFloatCallbackInvoked) << "float callback was not invoked";
578 EXPECT_TRUE(bCharCallbackInvoked) << "char callback was not invoked";
579
580 // Close the socket
581 RoveCommUDPNode.Close();
582 },
583 3, // 3 total attempts
584 30000); // 30 second timeout (30,000 ms)
585}
The RoveCommUDP class is used to send and receive data over a UDP connection.
Definition RoveCommUDP.h:52
void ProcessPacket(std::span< const uint8_t > stData, const sockaddr_in &saClientAddr)
Process a UDP packet and invoke the appropriate callback function. This function is called from the R...
Definition RoveCommUDP.cpp:433
void Close()
Close the UDP socket. This method is called when the RoveCommUDP object is destroyed....
Definition RoveCommUDP.cpp:696
bool Init(int nPort=manifest::General::ETHERNET_UDP_PORT)
Initialize the UDP socket and bind it to the specified port. This method also starts the thread that ...
Definition RoveCommUDP.cpp:110
CallbackHandle On(const uint16_t unDataId, std::function< void(const RoveCommPacket< T > &)> fnCallback)
Register a callback for UDP packets with the given data ID. The callback belongs to this node: packet...
Definition RoveCommUDP.cpp:333
@ INT32_T
Definition RoveCommManifest.h:39
@ UINT8_T
Definition RoveCommManifest.h:36
@ FLOAT_T
Definition RoveCommManifest.h:41
@ UINT16_T
Definition RoveCommManifest.h:38
@ DOUBLE_T
Definition RoveCommManifest.h:42
@ CHAR
Definition RoveCommManifest.h:43
std::vector< uint8_t > PackPacket(const RoveCommPacket< T > &stPacket)
Create a RoveCommData structure from a RoveCommPacket structure.
Definition RoveCommPacket.cpp:44
void RunTimedTest(const std::function< void()> &fnTestCode, int nMaxRetries, int nTimeoutMS)
Run a test with a timeout and retries.
Definition TestUtils.cc:31
The RoveCommPacket struct is used to store data in a packet format for transmission over a network....
Definition RoveCommPacket.h:75
Here is the call graph for this function:

◆ TEST() [2/6]

TEST ( RoveCommUDP ,
CallbacksBelongToTheirNode  )
660{
661 // Run the test via the RunTimedTest function to allow for retries and timeouts.
663 []()
664 {
665 // Two nodes in one process. Neither needs a socket: packets are fed straight to ProcessPacket.
666 rovecomm::RoveCommUDP RoveCommUDPNodeA;
667 rovecomm::RoveCommUDP RoveCommUDPNodeB;
668
669 // Setup dummy address for callbacks
670 struct sockaddr_in saUDPClientAddr;
671 memset(&saUDPClientAddr, 0, sizeof(saUDPClientAddr));
672 saUDPClientAddr.sin_family = AF_INET;
673 saUDPClientAddr.sin_port = htons(11005);
674 inet_pton(AF_INET, "127.0.0.1", &saUDPClientAddr.sin_addr);
675
676 const uint16_t unTestDataId = 1400;
677 int nCallsA = 0;
678 int nCallsB = 0;
679 std::vector<uint8_t> vData = rovecomm::PackPacket(rovecomm::RoveCommPacket<uint8_t>{.unDataId = unTestDataId, .vData = {1, 2, 3}});
680
681 // Only node A registers a callback.
682 RoveCommUDPNodeA.On<uint8_t>(unTestDataId, [&](const rovecomm::RoveCommPacket<uint8_t>&) { ++nCallsA; });
683
684 // A packet received by node B must not reach node A's callback.
685 RoveCommUDPNodeB.ProcessPacket<uint8_t>(vData, saUDPClientAddr);
686 EXPECT_EQ(nCallsA, 0) << "Node B invoked a callback registered on node A";
687
688 // Node A still receives its own packets.
689 RoveCommUDPNodeA.ProcessPacket<uint8_t>(vData, saUDPClientAddr);
690 EXPECT_EQ(nCallsA, 1);
691
692 // Clearing the data ID on node B must leave node A's callback registered.
693 rovecomm::CallbackHandle stCallbackB = RoveCommUDPNodeB.On<uint8_t>(unTestDataId, [&](const rovecomm::RoveCommPacket<uint8_t>&) { ++nCallsB; });
694 RoveCommUDPNodeB.Clear(stCallbackB);
695 RoveCommUDPNodeB.ProcessPacket<uint8_t>(vData, saUDPClientAddr);
696 RoveCommUDPNodeA.ProcessPacket<uint8_t>(vData, saUDPClientAddr);
697 EXPECT_EQ(nCallsB, 0) << "Clear() left a callback registered on node B";
698 EXPECT_EQ(nCallsA, 2) << "Clear() on node B removed node A's callback";
699 },
700 3, // 3 total attempts
701 30000); // 30 second timeout (30,000 ms)
702}
void Clear(const CallbackHandle &stHandle)
Remove the one callback identified by a handle from On(). Other callbacks for the same data ID stay r...
Definition RoveCommUDP.cpp:357
Identifies one callback registered with a node's On(), so Clear() can remove that callback without to...
Definition RoveCommPacket.h:104
Here is the call graph for this function:

◆ TEST() [3/6]

TEST ( RoveCommUDP ,
ClearRemovesOnlyThatCallback  )
705{
706 // Run the test via the RunTimedTest function to allow for retries and timeouts.
708 []()
709 {
710 // Create RoveComm Node. It needs no socket: packets are fed straight to ProcessPacket.
711 rovecomm::RoveCommUDP RoveCommUDPNode;
712
713 // Setup dummy address for callbacks
714 struct sockaddr_in saUDPClientAddr;
715 memset(&saUDPClientAddr, 0, sizeof(saUDPClientAddr));
716 saUDPClientAddr.sin_family = AF_INET;
717 saUDPClientAddr.sin_port = htons(11006);
718 inet_pton(AF_INET, "127.0.0.1", &saUDPClientAddr.sin_addr);
719
720 const uint16_t unTestDataId = 1401;
721 int nCallsFirst = 0;
722 int nCallsSecond = 0;
723 std::vector<uint8_t> vData = rovecomm::PackPacket(rovecomm::RoveCommPacket<float>{.unDataId = unTestDataId, .vData = {1.5f, 2.5f}});
724
725 // Two callbacks for the same data ID.
726 rovecomm::CallbackHandle stFirst = RoveCommUDPNode.On<float>(unTestDataId, [&](const rovecomm::RoveCommPacket<float>&) { ++nCallsFirst; });
727 rovecomm::CallbackHandle stSecond = RoveCommUDPNode.On<float>(unTestDataId, [&](const rovecomm::RoveCommPacket<float>&) { ++nCallsSecond; });
728 EXPECT_EQ(stFirst.unDataId, unTestDataId);
729 EXPECT_EQ(stFirst.eDataType, manifest::DataTypes::FLOAT_T);
730 EXPECT_NE(stFirst.ullID, stSecond.ullID);
731
732 RoveCommUDPNode.ProcessPacket<float>(vData, saUDPClientAddr);
733 EXPECT_EQ(nCallsFirst, 1);
734 EXPECT_EQ(nCallsSecond, 1);
735
736 // Clear() removes the first callback and keeps the second.
737 RoveCommUDPNode.Clear(stFirst);
738 RoveCommUDPNode.ProcessPacket<float>(vData, saUDPClientAddr);
739 EXPECT_EQ(nCallsFirst, 1) << "Clear() left its callback registered";
740 EXPECT_EQ(nCallsSecond, 2) << "Clear() removed a different callback";
741
742 // Removing it again, or passing an empty handle, does nothing.
743 RoveCommUDPNode.Clear(stFirst);
744 RoveCommUDPNode.Clear(rovecomm::CallbackHandle{});
745 RoveCommUDPNode.ProcessPacket<float>(vData, saUDPClientAddr);
746 EXPECT_EQ(nCallsSecond, 3);
747
748 // Clear() removes the second callback.
749 RoveCommUDPNode.Clear(stSecond);
750 RoveCommUDPNode.ProcessPacket<float>(vData, saUDPClientAddr);
751 EXPECT_EQ(nCallsSecond, 3) << "Clear() left a callback registered";
752 },
753 3, // 3 total attempts
754 30000); // 30 second timeout (30,000 ms)
755}
Here is the call graph for this function:

◆ TEST() [4/6]

TEST ( RoveCommUDP ,
InitSocket  )

Test Initialization of a UDP Socket.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-02-08
31{
32 // Run the test via the RunTimedTest function to allow for retries and timeouts.
34 []()
35 {
36 // Create RoveComm Node
37 rovecomm::RoveCommUDP RoveCommUDPNode;
38
39 // Flag to check if the initialization was successful
40 bool bInitSuccess = false;
41
42 // Give the node three chances to initialize the socket
43 // Since the socket is bound to a specific port, it may take a few tries to find an available port
44 for (int i = 0; i < 3; ++i)
45 {
46 // Initialize the UDP node
47 if (RoveCommUDPNode.Init(11000))
48 {
49 bInitSuccess = true;
50 break;
51 }
52 else
53 {
54 std::this_thread::sleep_for(std::chrono::seconds(1));
55 }
56 }
57
58 // Initialize the UDP node
59 EXPECT_TRUE(bInitSuccess);
60
61 // Close the socket
62 RoveCommUDPNode.Close();
63 },
64 3, // 3 total attempts
65 30000); // 30 second timeout (30,000 ms)
66}
Here is the call graph for this function:

◆ TEST() [5/6]

TEST ( RoveCommUDP ,
ManifestIntegration  )

Main function for running the tests.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-04-21
595{
596 // Run the test via the RunTimedTest function to allow for retries and timeouts.
598 []()
599 {
600 // Create RoveComm Nodes
601 rovecomm::RoveCommUDP RoveCommUDPNode;
602
603 // Flag to check if the initialization was successful
604 bool bInitSuccess = false;
605
606 // Give the node three chances to initialize the socket
607 for (int nI = 0; nI < 3; ++nI)
608 {
609 if (RoveCommUDPNode.Init(11004))
610 {
611 bInitSuccess = true;
612 break;
613 }
614 else
615 {
616 std::this_thread::sleep_for(std::chrono::seconds(1));
617 }
618 }
619
620 ASSERT_TRUE(bInitSuccess) << "Failed to initialize the UDP socket";
621
622 // Setup dummy address for callbacks
623 struct sockaddr_in saUDPClientAddr;
624 memset(&saUDPClientAddr, 0, sizeof(saUDPClientAddr));
625 saUDPClientAddr.sin_family = AF_INET;
626 saUDPClientAddr.sin_port = htons(11004);
627 inet_pton(AF_INET, "127.0.0.1", &saUDPClientAddr.sin_addr);
628
629 {
630 constexpr manifest::ManifestEntry stTestEntry{
631 .DATA_ID = 1000,
632 .DATA_COUNT = 3,
633 .DATA_TYPE = manifest::DataTypes::INT32_T,
634 };
635
636 ssize_t siBytesSent = RoveCommUDPNode.WriteTo<stTestEntry>({-100, 0, 100}, "127.0.0.1", 11004);
637 EXPECT_EQ(siBytesSent, stTestEntry.DATA_COUNT * sizeof(int32_t) + rovecomm::ROVECOMM_PACKET_HEADER_SIZE)
638 << "Failed to send int32_t packet using manifest entry";
639 }
640
641 {
642 constexpr manifest::ManifestEntry stTestEntry{
643 .DATA_ID = 1001,
644 .DATA_COUNT = 256,
645 .DATA_TYPE = manifest::DataTypes::CHAR,
646 };
647
648 ssize_t siBytesSent = RoveCommUDPNode.WriteTo<stTestEntry>("TEST MESSAGE", "127.0.0.1", 11004);
649 EXPECT_EQ(siBytesSent, 256 + rovecomm::ROVECOMM_PACKET_HEADER_SIZE) << "Failed to send small char packet using manifest entry";
650 }
651
652 // Close the socket
653 RoveCommUDPNode.Close();
654 },
655 3, // 3 total attempts
656 30000); // 30 second timeout (30,000 ms)
657}
ssize_t WriteTo(const RoveCommPacket< T > &stPacket, const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Sends a packet to a specific destination. This is often used to send a command to a specific device.
Definition RoveCommUDP.h:143
const int ROVECOMM_PACKET_HEADER_SIZE
Definition RoveCommConsts.h:26
Manifest Entry Object for RoveComm.
Definition RoveCommManifest.h:106
int DATA_COUNT
Definition RoveCommManifest.h:109
Here is the call graph for this function:

◆ TEST() [6/6]

TEST ( RoveCommUDP ,
SendMultipleDataTypes  )

Test Sending Multiple Data Types via UDP.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-04-21
76{
77 // Run the test via the RunTimedTest function to allow for retries and timeouts.
79 []()
80 {
81 // Create RoveComm Node
82 rovecomm::RoveCommUDP RoveCommUDPNode;
83
84 // Flag to check if the initialization was successful
85 bool bInitSuccess = false;
86
87 // Give the node three chances to initialize the socket
88 // Since the socket is bound to a specific port, it may take a few tries to find an available port
89 for (int nI = 0; nI < 3; ++nI)
90 {
91 // Initialize the UDP node
92 if (RoveCommUDPNode.Init(11003))
93 {
94 bInitSuccess = true;
95 break;
96 }
97 else
98 {
99 std::this_thread::sleep_for(std::chrono::seconds(1));
100 }
101 }
102
103 ASSERT_TRUE(bInitSuccess) << "Failed to initialize the UDP socket";
104
105 // Test constants
106 const uint16_t unTestDataId = 1200;
107 const std::string szIpAddr = "127.0.0.1";
108 const int nPort = 11003;
109
110 // ======================== UINT8_T TESTS ========================
111 {
112 // Small packet (1 element)
113 rovecomm::RoveCommPacket<uint8_t> stSmallPacket{.unDataId = unTestDataId, .vData = {UINT8_MAX}};
114
115 // Medium packet (10 elements)
116 rovecomm::RoveCommPacket<uint8_t> stMediumPacket{.unDataId = unTestDataId, .vData = {}};
117 for (uint8_t ucI = 0; ucI < 10; ucI++)
118 {
119 stMediumPacket.vData.push_back(ucI * 25); // Values 0, 25, 50, 75, 100, 125, 150, 175, 200, 225
120 }
121
122 // Large packet (100 elements)
123 rovecomm::RoveCommPacket<uint8_t> stLargePacket{.unDataId = unTestDataId, .vData = {}};
124 for (int nI = 0; nI < 100; nI++)
125 {
126 stLargePacket.vData.push_back(nI % 256);
127 }
128
129 // Send and verify
130 ssize_t siBytesSent = RoveCommUDPNode.WriteTo(stSmallPacket, szIpAddr, nPort);
131 EXPECT_EQ(siBytesSent, 7) << "Failed to send small uint8_t packet";
132
133 siBytesSent = RoveCommUDPNode.WriteTo(stMediumPacket, szIpAddr, nPort);
134 EXPECT_EQ(siBytesSent, 16) << "Failed to send medium uint8_t packet";
135
136 siBytesSent = RoveCommUDPNode.WriteTo(stLargePacket, szIpAddr, nPort);
137 EXPECT_EQ(siBytesSent, 106) << "Failed to send large uint8_t packet";
138 }
139
140 // ======================== UINT16_T TESTS ========================
141 {
142 // Small packet (1 element)
143 rovecomm::RoveCommPacket<uint16_t> stSmallPacket{.unDataId = unTestDataId, .vData = {UINT16_MAX}};
144
145 // Medium packet (10 elements)
146 rovecomm::RoveCommPacket<uint16_t> stMediumPacket{.unDataId = unTestDataId, .vData = {}};
147 for (uint16_t usI = 0; usI < 10; usI++)
148 {
149 stMediumPacket.vData.push_back(usI * 6000);
150 }
151
152 // Large packet (50 elements)
153 rovecomm::RoveCommPacket<uint16_t> stLargePacket{.unDataId = unTestDataId, .vData = {}};
154 for (int nI = 0; nI < 50; nI++)
155 {
156 stLargePacket.vData.push_back(nI * 1000);
157 }
158
159 // Send and verify
160 ssize_t siBytesSent = RoveCommUDPNode.WriteTo(stSmallPacket, szIpAddr, nPort);
161 EXPECT_EQ(siBytesSent, 8) << "Failed to send small uint16_t packet";
162
163 siBytesSent = RoveCommUDPNode.WriteTo(stMediumPacket, szIpAddr, nPort);
164 EXPECT_EQ(siBytesSent, 26) << "Failed to send medium uint16_t packet";
165
166 siBytesSent = RoveCommUDPNode.WriteTo(stLargePacket, szIpAddr, nPort);
167 EXPECT_EQ(siBytesSent, 106) << "Failed to send large uint16_t packet";
168 }
169
170 // ======================== UINT32_T TESTS ========================
171 {
172 // Small packet (1 element)
174 .unDataId = unTestDataId,
175 .vData = {UINT32_MAX},
176 };
177
178 // Medium packet (10 elements)
179 rovecomm::RoveCommPacket<uint32_t> stMediumPacket{.unDataId = unTestDataId, .vData = {}};
180 for (uint32_t unI = 0; unI < 10; unI++)
181 {
182 stMediumPacket.vData.push_back(unI * 400000000);
183 }
184
185 // Send and verify
186 ssize_t siBytesSent = RoveCommUDPNode.WriteTo(stSmallPacket, szIpAddr, nPort);
187 EXPECT_EQ(siBytesSent, 10) << "Failed to send small uint32_t packet";
188
189 siBytesSent = RoveCommUDPNode.WriteTo(stMediumPacket, szIpAddr, nPort);
190 EXPECT_EQ(siBytesSent, 46) << "Failed to send medium uint32_t packet";
191 }
192
193 // ======================== INT8_T TESTS ========================
194 {
195 // Small packet (1 element)
197 .unDataId = unTestDataId,
198 .vData = {INT8_MIN},
199 };
200
201 // Medium packet (10 elements) with positive and negative values
202 rovecomm::RoveCommPacket<int8_t> stMediumPacket{.unDataId = unTestDataId, .vData = {}};
203 for (int nI = 0; nI < 10; nI++)
204 {
205 stMediumPacket.vData.push_back(nI < 5 ? -nI * 20 : nI * 10);
206 }
207
208 // Send and verify
209 ssize_t siBytesSent = RoveCommUDPNode.WriteTo(stSmallPacket, szIpAddr, nPort);
210 EXPECT_EQ(siBytesSent, 7) << "Failed to send small int8_t packet";
211
212 siBytesSent = RoveCommUDPNode.WriteTo(stMediumPacket, szIpAddr, nPort);
213 EXPECT_EQ(siBytesSent, 16) << "Failed to send medium int8_t packet";
214 }
215
216 // ======================== INT16_T TESTS ========================
217 {
218 // Small packet (1 element)
219 rovecomm::RoveCommPacket<int16_t> stSmallPacket{.unDataId = unTestDataId, .vData = {INT16_MIN}};
220
221 // Medium packet (10 elements)
222 rovecomm::RoveCommPacket<int16_t> stMediumPacket{.unDataId = unTestDataId, .vData = {}};
223 for (int nI = 0; nI < 10; nI++)
224 {
225 stMediumPacket.vData.push_back(nI < 5 ? -nI * 5000 : nI * 3000);
226 }
227
228 // Send and verify
229 ssize_t siBytesSent = RoveCommUDPNode.WriteTo(stSmallPacket, szIpAddr, nPort);
230 EXPECT_EQ(siBytesSent, 8) << "Failed to send small int16_t packet";
231
232 siBytesSent = RoveCommUDPNode.WriteTo(stMediumPacket, szIpAddr, nPort);
233 EXPECT_EQ(siBytesSent, 26) << "Failed to send medium int16_t packet";
234 }
235
236 // ======================== INT32_T TESTS ========================
237 {
238 // Small packet (1 element)
239 rovecomm::RoveCommPacket<int32_t> stSmallPacket{.unDataId = unTestDataId, .vData = {INT32_MIN}};
240
241 // Medium packet (10 elements)
242 rovecomm::RoveCommPacket<int32_t> stMediumPacket{.unDataId = unTestDataId, .vData = {}};
243 for (int nI = 0; nI < 10; nI++)
244 {
245 stMediumPacket.vData.push_back(nI < 5 ? -nI * 400000000 : nI * 300000000);
246 }
247
248 // Send and verify
249 ssize_t siBytesSent = RoveCommUDPNode.WriteTo(stSmallPacket, szIpAddr, nPort);
250 EXPECT_EQ(siBytesSent, 10) << "Failed to send small int32_t packet";
251
252 siBytesSent = RoveCommUDPNode.WriteTo(stMediumPacket, szIpAddr, nPort);
253 EXPECT_EQ(siBytesSent, 46) << "Failed to send medium int32_t packet";
254 }
255
256 // ======================== FLOAT TESTS ========================
257 {
258 // Small packet (1 element)
259 rovecomm::RoveCommPacket<float> stSmallPacket{.unDataId = unTestDataId, .vData = {3.14159f}};
260
261 // Medium packet (10 elements)
262 rovecomm::RoveCommPacket<float> stMediumPacket{.unDataId = unTestDataId, .vData = {}};
263 for (int nI = 0; nI < 10; nI++)
264 {
265 stMediumPacket.vData.push_back(nI * 0.5f);
266 }
267
268 // Send and verify
269 ssize_t siBytesSent = RoveCommUDPNode.WriteTo(stSmallPacket, szIpAddr, nPort);
270 EXPECT_EQ(siBytesSent, 10) << "Failed to send small float packet";
271
272 siBytesSent = RoveCommUDPNode.WriteTo(stMediumPacket, szIpAddr, nPort);
273 EXPECT_EQ(siBytesSent, 46) << "Failed to send medium float packet";
274 }
275
276 // ======================== DOUBLE TESTS ========================
277 {
278 // Small packet (1 element)
279 rovecomm::RoveCommPacket<double> stSmallPacket{.unDataId = unTestDataId, .vData = {3.14159265358979323846}};
280
281 // Medium packet (10 elements)
282 rovecomm::RoveCommPacket<double> stMediumPacket{.unDataId = unTestDataId, .vData = {}};
283 for (int nI = 0; nI < 10; nI++)
284 {
285 stMediumPacket.vData.push_back(nI * 0.333333333333333);
286 }
287
288 // Send and verify
289 ssize_t siBytesSent = RoveCommUDPNode.WriteTo(stSmallPacket, szIpAddr, nPort);
290 EXPECT_EQ(siBytesSent, 14) << "Failed to send small double packet";
291
292 siBytesSent = RoveCommUDPNode.WriteTo(stMediumPacket, szIpAddr, nPort);
293 EXPECT_EQ(siBytesSent, 86) << "Failed to send medium double packet";
294 }
295
296 // ======================== CHAR TESTS ========================
297 {
298 // Small packet (1 element)
299 rovecomm::RoveCommPacket<char> stSmallPacket{
300 .unDataId = unTestDataId,
301 .vData = {'X'},
302 };
303
304 // Medium packet - string "Hello World!"
305 std::string szMessage = "Hello World!";
306 rovecomm::RoveCommPacket<char> stMediumPacket{
307 .unDataId = unTestDataId,
308 .vData = {szMessage.begin(), szMessage.end()},
309 };
310
311 // Large packet - Lorem ipsum
312 std::string szLoremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
313 rovecomm::RoveCommPacket<char> stLargePacket{
314 .unDataId = unTestDataId,
315 .vData = {szLoremIpsum.begin(), szLoremIpsum.end()},
316 };
317
318 // Send and verify
319 ssize_t siBytesSent = RoveCommUDPNode.WriteTo(stSmallPacket, szIpAddr, nPort);
320 EXPECT_EQ(siBytesSent, 7) << "Failed to send small char packet";
321
322 siBytesSent = RoveCommUDPNode.WriteTo(stMediumPacket, szIpAddr, nPort);
323 EXPECT_EQ(siBytesSent, szMessage.size() + 6) << "Failed to send medium char packet";
324
325 siBytesSent = RoveCommUDPNode.WriteTo(stLargePacket, szIpAddr, nPort);
326 EXPECT_EQ(siBytesSent, szLoremIpsum.size() + 6) << "Failed to send large char packet";
327 }
328
329 // Close the socket
330 RoveCommUDPNode.Close();
331 },
332 3, // 3 total attempts
333 60000); // 60 second timeout (60,000 ms) - increased for multiple packet tests
334}
std::vector< T > vData
Definition RoveCommPacket.h:78
Here is the call graph for this function: