RoboidControl-cpp/test/thing_test.cc
2025-01-02 12:48:14 +01:00

102 lines
2.5 KiB
C++

#if GTEST
// #include <gmock/gmock.h>
// not supported using Visual Studio 2022 compiler...
#include <gtest/gtest.h>
#include <chrono>
#include <ws2tcpip.h>
#include "Participant.h"
#include "Thing.h"
namespace Passer {
// Function to get the current time in milliseconds as unsigned long
unsigned long get_time_ms() {
auto now = std::chrono::steady_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch());
return static_cast<unsigned long>(ms.count());
}
class ControlCoreSuite : public ::testing::Test {
protected:
// SetUp and TearDown can be used to set up and clean up before/after each
// test
void SetUp() override {
// Initialize test data here
}
void TearDown() override {
// Clean up test data here
}
};
void send_udp_message(const char *message, const char *ip, int port) {
WSADATA wsaData;
SOCKET sock;
struct sockaddr_in server_addr;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "WSAStartup failed" << std::endl;
return;
}
// Create a UDP socket
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET) {
std::cerr << "Socket creation failed" << std::endl;
WSACleanup();
return;
}
// Set up the sockaddr_in structure for the destination
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons((u_short)port);
// Convert the IP address
if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) {
std::cerr << "Invalid address" << std::endl;
closesocket(sock);
WSACleanup();
return;
}
// Send the UDP message
int sent_bytes = sendto(sock, message, strlen(message), 0,
(struct sockaddr *)&server_addr, sizeof(server_addr));
if (sent_bytes == SOCKET_ERROR) {
std::cerr << "sendto failed with error: " << WSAGetLastError() << std::endl;
} else {
std::cout << "Message sent successfully!" << std::endl;
}
// Close the socket and clean up Winsock
closesocket(sock);
WSACleanup();
}
TEST_F(ControlCoreSuite, Dummytest) {
send_udp_message("Hello, UDP!", "127.0.0.1", 8080); // Send to localhost
ASSERT_EQ(1, 1);
}
TEST_F(ControlCoreSuite, Participant) {
Participant participant = Participant("127.0.0.1", 7681);
unsigned long milliseconds = get_time_ms();
unsigned long startTime = milliseconds;
while (milliseconds < startTime + 1000) {
participant.Update(milliseconds);
milliseconds = get_time_ms();
}
ASSERT_EQ(1, 1);
}
} // namespace Passer
#endif