Skip to content

Commit

Permalink
style: clean up test codes
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Feb 23, 2024
1 parent 3f645ce commit 41d3759
Show file tree
Hide file tree
Showing 57 changed files with 6,599 additions and 6,621 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ CTestTestfile.cmake
_deps
cmake-build-debug
cmake-build-release
.idea
33 changes: 10 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,9 @@ cmake_minimum_required(VERSION 3.5)
project(raknet)


# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)


option(RAKNET_CODE_COVERAGE "Enable code coverage reporting" OFF)
option(RAKNET_BUILD_TESTING "Enable the building of tests" OFF)
if (RAKNET_BUILD_TESTING)
enable_testing()

if (RAKNET_CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g --coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g --coverage")
endif ()
endif ()


IF (WIN32 AND NOT UNIX)
list(APPEND RAKNET_LINK_LIBRARIES ws2_32.lib)
Expand All @@ -52,8 +30,17 @@ add_library(raknet ${RAKNET_SOURCE_FILES})
target_include_directories(raknet PUBLIC include)
target_link_libraries(raknet ${RAKNET_LINK_LIBRARIES})

option(BUILD_TESTING "Enable the building of tests" OFF)
if (NOT BUILD_TESTING STREQUAL OFF)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

if (RAKNET_BUILD_TESTING)
file(GLOB RAKNET_TEST_FILES CONFIGURE_DEPENDS "tests/*.cpp")
add_executable(raknet_test ${RAKNET_TEST_FILES})
target_link_libraries(raknet_test raknet GTest::gtest_main)
Expand Down
244 changes: 120 additions & 124 deletions tests/CommonFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,138 +10,134 @@

#include "CommonFunctions.h"

CommonFunctions::CommonFunctions(void)
{
CommonFunctions::CommonFunctions(void) {}

CommonFunctions::~CommonFunctions(void) {}

bool CommonFunctions::ConnectionStateMatchesOptions(
RakPeerInterface* peer,
SystemAddress currentSystem,
bool isConnected,
bool isConnecting,
bool isPending,
bool isDisconnecting,
bool isNotConnected,
bool isLoopBack,
bool isSilentlyDisconnecting) {
ConnectionState connectionState = peer->GetConnectionState(currentSystem);
switch (connectionState) {
case IS_CONNECTED:
return isConnected;
break;

case IS_CONNECTING:
return isConnecting;
break;

case IS_PENDING:
return isPending;
break;

case IS_DISCONNECTING:
return isDisconnecting;
break;

case IS_NOT_CONNECTED:
return isNotConnected;
break;

case IS_SILENTLY_DISCONNECTING:
return isSilentlyDisconnecting;
break;

default:
return false;
break;
}
}

CommonFunctions::~CommonFunctions(void)
{
bool CommonFunctions::WaitAndConnect(
RakPeerInterface* peer,
char* ip,
unsigned short int port,
int millisecondsToWait) {
SystemAddress connectToAddress;

connectToAddress.SetBinaryAddress(ip);
connectToAddress.SetPortHostOrder(port);
TimeMS entryTime = GetTimeMS();

while (!CommonFunctions::ConnectionStateMatchesOptions(
peer, connectToAddress, true) &&
GetTimeMS() - entryTime < millisecondsToWait) {
if (!CommonFunctions::ConnectionStateMatchesOptions(
peer, connectToAddress, true, true, true, true)) {
peer->Connect(ip, port, 0, 0);
}

RakSleep(100);
}

if (ConnectionStateMatchesOptions(peer, connectToAddress, true)) {
return 1;
}

return 0;
}

bool CommonFunctions::ConnectionStateMatchesOptions(RakPeerInterface *peer,SystemAddress currentSystem,bool isConnected,bool isConnecting,bool isPending,bool isDisconnecting,bool isNotConnected,bool isLoopBack , bool isSilentlyDisconnecting)
{
ConnectionState connectionState=peer->GetConnectionState(currentSystem);
switch(connectionState)
{
case IS_CONNECTED:
return isConnected;
break;

case IS_CONNECTING:
return isConnecting;
break;

case IS_PENDING:
return isPending;
break;

case IS_DISCONNECTING:
return isDisconnecting;
break;

case IS_NOT_CONNECTED:
return isNotConnected;
break;

case IS_SILENTLY_DISCONNECTING:
return isSilentlyDisconnecting;
break;

default:
return false;
break;
}
}

bool CommonFunctions::WaitAndConnect(RakPeerInterface *peer,char* ip,unsigned short int port,int millisecondsToWait)
{

SystemAddress connectToAddress;

connectToAddress.SetBinaryAddress(ip);
connectToAddress.SetPortHostOrder(port);
TimeMS entryTime=GetTimeMS();

while(!CommonFunctions::ConnectionStateMatchesOptions (peer,connectToAddress,true)&&GetTimeMS()-entryTime<millisecondsToWait)
{

if(!CommonFunctions::ConnectionStateMatchesOptions (peer,connectToAddress,true,true,true,true))
{
peer->Connect(ip,port,0,0);
}

RakSleep(100);

}

if (ConnectionStateMatchesOptions (peer,connectToAddress,true))
{
return 1;
}

return 0;
}

void CommonFunctions::DisconnectAndWait(RakPeerInterface *peer,char* ip,unsigned short int port)
{
SystemAddress targetAddress;

targetAddress.SetBinaryAddress(ip);
targetAddress.SetPortHostOrder(port);
void CommonFunctions::DisconnectAndWait(
RakPeerInterface* peer,
char* ip,
unsigned short int port) {
SystemAddress targetAddress;

while(CommonFunctions::ConnectionStateMatchesOptions (peer,targetAddress,true,true,true,true))//disconnect client
{

peer->CloseConnection (targetAddress,true,0,LOW_PRIORITY);
}
targetAddress.SetBinaryAddress(ip);
targetAddress.SetPortHostOrder(port);

while (CommonFunctions::ConnectionStateMatchesOptions(
peer, targetAddress, true, true, true, true)) //disconnect client
{
peer->CloseConnection(targetAddress, true, 0, LOW_PRIORITY);
}
}

bool CommonFunctions::WaitForMessageWithID(RakPeerInterface *reciever,int id,int millisecondsToWait)
{

RakTimer timer(millisecondsToWait);

Packet *packet;
while(!timer.IsExpired())
{
for (packet=reciever->Receive(); packet;reciever->DeallocatePacket(packet), packet=reciever->Receive())
{

//printf("Packet %i\n",packet->data[0]);
if (packet->data[0]==id)
{
reciever->DeallocatePacket(packet);
return true;
}

}

}

return false;
bool CommonFunctions::WaitForMessageWithID(
RakPeerInterface* reciever,
int id,
int millisecondsToWait) {
RakTimer timer(millisecondsToWait);

Packet* packet;
while (!timer.IsExpired()) {
for (packet = reciever->Receive(); packet;
reciever->DeallocatePacket(packet), packet = reciever->Receive()) {
//printf("Packet %i\n",packet->data[0]);
if (packet->data[0] == id) {
reciever->DeallocatePacket(packet);
return true;
}
}
}

return false;
}

Packet *CommonFunctions::WaitAndReturnMessageWithID(RakPeerInterface *reciever,int id,int millisecondsToWait)
{

RakTimer timer(millisecondsToWait);

Packet *packet;
while(!timer.IsExpired())
{
for (packet=reciever->Receive(); packet;reciever->DeallocatePacket(packet), packet=reciever->Receive())
{

// printf("Packet %i\n",packet->data[0]);
if (packet->data[0]==id)
{
return packet;
}

}

}

return 0;
Packet* CommonFunctions::WaitAndReturnMessageWithID(
RakPeerInterface* reciever,
int id,
int millisecondsToWait) {
RakTimer timer(millisecondsToWait);

Packet* packet;
while (!timer.IsExpired()) {
for (packet = reciever->Receive(); packet;
reciever->DeallocatePacket(packet), packet = reciever->Receive()) {
// printf("Packet %i\n",packet->data[0]);
if (packet->data[0] == id) {
return packet;
}
}
}

return 0;
}
52 changes: 35 additions & 17 deletions tests/CommonFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,47 @@

#pragma once


#include "RakString.h"

#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "DebugTools.h"
#include "GetTime.h"
#include "MessageIdentifiers.h"
#include "RakNetTime.h"
#include "RakPeer.h"
#include "RakPeerInterface.h"
#include "RakSleep.h"
#include "RakNetTime.h"
#include "GetTime.h"
#include "DebugTools.h"
#include "RakTimer.h"
#include "RakTimer.h"

using namespace RakNet;
class CommonFunctions
{
public:
CommonFunctions(void);
~CommonFunctions(void);
class CommonFunctions {
public:
CommonFunctions(void);
~CommonFunctions(void);

static bool WaitAndConnect(RakPeerInterface *peer,char* ip,unsigned short int port,int millisecondsToWait);
static bool WaitForMessageWithID(RakPeerInterface *reciever,int id,int millisecondsToWait);
static Packet * WaitAndReturnMessageWithID(RakPeerInterface *reciever,int id,int millisecondsToWait);
static void DisconnectAndWait(RakPeerInterface *peer,char* ip,unsigned short int port);
static bool ConnectionStateMatchesOptions(RakPeerInterface *peer, SystemAddress currentSystem, bool isConnected, bool isConnecting=false, bool isPending=false, bool isDisconnecting=false, bool isNotConnected=false, bool isLoopBack=false, bool isSilentlyDisconnecting=false);
static bool WaitAndConnect(
RakPeerInterface* peer,
char* ip,
unsigned short int port,
int millisecondsToWait);
static bool WaitForMessageWithID(
RakPeerInterface* reciever,
int id,
int millisecondsToWait);
static Packet* WaitAndReturnMessageWithID(
RakPeerInterface* reciever,
int id,
int millisecondsToWait);
static void
DisconnectAndWait(RakPeerInterface* peer, char* ip, unsigned short int port);
static bool ConnectionStateMatchesOptions(
RakPeerInterface* peer,
SystemAddress currentSystem,
bool isConnected,
bool isConnecting = false,
bool isPending = false,
bool isDisconnecting = false,
bool isNotConnected = false,
bool isLoopBack = false,
bool isSilentlyDisconnecting = false);
};
Loading

0 comments on commit 41d3759

Please sign in to comment.