Files
nbnet/tests/serialization.c
Volodymyr FialkoandBIAGINI Nathan 15d823dab6 Enable build flags and fix compilation warnings (gcc 14.1.0) (#66)
* Fix rpc build with other examples

CPP_COMPILE is an option, a cache variable, thus regular unset() is not
working for it since it tries to unset the scope variable.

This causing cmake to skip compilation of rpc.
Unset CPP_COMPILE from CACHE to make it work as intended.

* Fix compiler flags with build targets

Currently tests, soak and examples are build without extra flags,
because add_compile_options is specified after defining build targets.

From add_compile_options() docs:
Adds options to the compiler command line for targets in the current
directory and below that are added *after* this command is invoked.

* Pass debug callbacks as struct

Fixes:
warning: ISO C forbids conversion of function pointer to object pointer
type

* Move global variables under NBNET_IMPL

Nb net could be included multiple times, to prevent creation of global
variables in each file move them under NBNET_IMPL.

Also use designated initializer to initiate a driver structure to
remove warnings about partial initialization. Designated initializer
would initialize other members to zero.

* Resolve unused variables warnings.

Cast unused function arguments to (void) to suspend warnings.

* Resolve signed vs unsigned warnings

Remove unnecessary lt 0 checks and sync types to remove signed vs
unsigned warnings.

* Resolve string related warnings

Fixes:
nbnet/tests/string_tests.c:18:9: error: ‘strncpy’
output truncated before terminating nul copying 5 bytes from a string of
the same length
[-Werror=stringop-truncation]
   18 |         strncpy(res + pos, b, len_b);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

nbnet/tests/string_tests.c:24:9: error: ‘strncpy’
specified bound depends on the length of the source argument
[-Werror=stringop-truncation]

* Resolve function defined but not used warnings

Mark GameServer_GetClientCount() as inline.

* Attempt to fix warning in ASSERT_VALUE_IN_RANGE

In case when value in unsigned type and min is 0, this commit just
obfuscates types, maybe there's better solution.

Fixes:
warning: comparison of unsigned expression in ‘>= 0’ is always true
[-Wtype-limits]

* Ensure that read len is less than word size

In release mode(-DCMAKE_BUILD_TYPE=Release) tests returning following
error:
nbnet/tests/../nbnet.h:2272:9: error: ‘memcpy’ forming offset [4, 7] is
out of the bound s [0, 4] of object ‘word’ with type ‘Word’ {aka
‘unsigned int’} [-Werror=array-bounds=]
 2272 |         memcpy(bytes, &word, length); |
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Add check to verify that length is less than Word size.

* add -Wno-unknown-pragmas flag

* fix examples compilation on windows

* fix examples compilation on windows

* fix examples compilation on windows

* fix unit tests compilation on windows

* fix soak test compilation on windows

* fix unit tests compilation on windows

* fix unit tests compilation on windows

---------

Co-authored-by: BIAGINI Nathan <nath.biagini@gmail.com>
2024-08-10 12:02:42 +02:00

122 lines
2.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "CuTest.h"
#define NBNET_IMPL
#define NBN_LogInfo printf
#define NBN_LogTrace printf
#define NBN_LogDebug printf
#define NBN_LogError printf
#define NBN_LogWarning printf
#define NBN_Allocator malloc
#define NBN_Deallocator free
#include "../nbnet.h"
typedef struct
{
float v1;
float v2;
float v3;
} BogusMessage;
int BogusMessage_Serialize(BogusMessage *msg, NBN_Stream *stream)
{
NBN_SerializeFloat(stream, msg->v1, -100, 100, 1);
NBN_SerializeFloat(stream, msg->v2, -100, 100, 2);
NBN_SerializeFloat(stream, msg->v3, -100, 100, 3);
return 0;
}
typedef struct
{
uint64_t v1;
uint64_t v2;
uint64_t v3;
uint64_t v4;
} BogusMessage2;
int BogusMessage2_Serialize(BogusMessage2 *msg, NBN_Stream *stream)
{
NBN_SerializeUInt64(stream, msg->v1);
NBN_SerializeUInt64(stream, msg->v2);
NBN_SerializeUInt64(stream, msg->v3);
NBN_SerializeUInt64(stream, msg->v4);
return 0;
}
void Test_SerializeFloat(CuTest *tc)
{
BogusMessage msg = { .v1 = 42.5, .v2 = -12.42, .v3 = -89.123 };
NBN_WriteStream w_stream;
uint8_t buffer[32];
NBN_WriteStream_Init(&w_stream, buffer, sizeof(buffer));
CuAssertIntEquals(tc, 0, BogusMessage_Serialize(&msg, (NBN_Stream *)&w_stream));
NBN_WriteStream_Flush(&w_stream);
BogusMessage r_msg;
NBN_ReadStream r_stream;
NBN_ReadStream_Init(&r_stream, buffer, sizeof(buffer));
CuAssertIntEquals(tc, 0, BogusMessage_Serialize(&r_msg, (NBN_Stream *)&r_stream));
CuAssertTrue(tc, r_msg.v1 == 42.5);
CuAssertIntEquals(tc, -1242, r_msg.v2 * 100);
CuAssertIntEquals(tc, -89123, r_msg.v3 * 1000);
}
void Test_SerializeUInt64(CuTest *tc)
{
BogusMessage2 msg = {
.v1 = 0xFFFFFFFFFFFFFFFF,
.v2 = 9223372036854775807,
.v3 = 4611686018427387903,
.v4 = 42000
};
NBN_WriteStream w_stream;
uint8_t buffer[32];
NBN_WriteStream_Init(&w_stream, buffer, sizeof(buffer));
CuAssertIntEquals(tc, 0, BogusMessage2_Serialize(&msg, (NBN_Stream *)&w_stream));
NBN_WriteStream_Flush(&w_stream);
BogusMessage2 r_msg;
NBN_ReadStream r_stream;
NBN_ReadStream_Init(&r_stream, buffer, sizeof(buffer));
CuAssertIntEquals(tc, 0, BogusMessage2_Serialize(&r_msg, (NBN_Stream *)&r_stream));
CuAssertTrue(tc, r_msg.v1 == 0xFFFFFFFFFFFFFFFF);
CuAssertTrue(tc, r_msg.v2 == 9223372036854775807);
CuAssertTrue(tc, r_msg.v3 == 4611686018427387903);
CuAssertTrue(tc, r_msg.v4 == 42000);
}
int main(int argc, char *argv[])
{
(void) argc;
(void) argv;
CuString *output = CuStringNew();
CuSuite* suite = CuSuiteNew();
SUITE_ADD_TEST(suite, Test_SerializeFloat);
SUITE_ADD_TEST(suite, Test_SerializeUInt64);
CuSuiteRun(suite);
CuSuiteSummary(suite, output);
CuSuiteDetails(suite, output);
printf("%s\n", output->buffer);
return suite->failCount;
}