mirror of
https://github.com/fraillt/bitsery.git
synced 2026-06-08 08:13:56 +00:00
improved configuration to follow modern cmake practices
This commit is contained in:
@@ -20,25 +20,14 @@
|
||||
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
#SOFTWARE.
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
set(TestProjectName bitsery_tests)
|
||||
project(${TestProjectName} C CXX)
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(bitsery_tests CXX)
|
||||
|
||||
find_package(GTest 1.8 REQUIRED)
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
#add googletest external project
|
||||
#USE_GMOCK enable gmock
|
||||
#exports variables GTEST_INCLUDE_DIRS, GTEST_LIBS_DIR, GTEST_LIBNAME, GTEST_MAIN_LIBNAME
|
||||
set(ExtCMakeFilesDir ${CMAKE_SOURCE_DIR}/ext)
|
||||
set(UseGMock ON)
|
||||
add_subdirectory(${ExtCMakeFilesDir}/gtest ${CMAKE_BINARY_DIR}/gtest)
|
||||
|
||||
#this helps idea to know which files are actually used
|
||||
file(GLOB_RECURSE IncludeHeaders ${CMAKE_SOURCE_DIR}/include/bitsery/*.h)
|
||||
# set common include folder for module
|
||||
include_directories(SYSTEM ${GTestIncludeDirs})
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
if (NOT TARGET Bitsery::bitsery)
|
||||
message(FATAL_ERROR "Bitsery::bitsery alias not set. Please generate CMake from bitsery root directory.")
|
||||
endif()
|
||||
|
||||
file(GLOB TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||
|
||||
@@ -47,28 +36,27 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
|
||||
list(REMOVE_ITEM TestSourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/serialization_ext_std_optional.cpp)
|
||||
endif()
|
||||
|
||||
include(${ExtCMakeFilesDir}/LinkTestLib.cmake)
|
||||
enable_testing()
|
||||
|
||||
FOREACH(TestFile ${TestSourceFiles})
|
||||
foreach(TestFile ${TestSourceFiles})
|
||||
get_filename_component(TestName ${TestFile} NAME_WE)
|
||||
set(TestName TEST_${TestName})
|
||||
add_executable(${TestName} ${TestFile} ${IncludeHeaders} serialization_test_utils.h)
|
||||
LinkTestLib(${TestName})
|
||||
|
||||
set(TestName bitsery.test.${TestName})
|
||||
add_executable(${TestName} ${TestFile})
|
||||
target_link_libraries(${TestName} PRIVATE GTest::Main Bitsery::bitsery)
|
||||
add_test(NAME ${TestName} COMMAND $<TARGET_FILE:${TestName}>)
|
||||
endforeach()
|
||||
|
||||
ENDFOREACH()
|
||||
#======================= setup development environment ====================
|
||||
|
||||
#all in one tests for code coverage
|
||||
add_executable(${TestProjectName} ${TestSourceFiles})
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
include(${ExtCMakeFilesDir}/CodeCoverage.cmake)
|
||||
target_compile_options(${TestProjectName} PUBLIC -O0 -fprofile-arcs -ftest-coverage)
|
||||
target_link_libraries(${TestProjectName} -O0 -fprofile-arcs -ftest-coverage)
|
||||
setup_target_for_coverage(tests_coverage ${TestProjectName} coverage)
|
||||
# get all header files for IDE (in my case Clion) and create dummy project that consumes theses files
|
||||
get_directory_property(ParentDir PARENT_DIRECTORY)
|
||||
if (ParentDir)
|
||||
# only include when working from root project (Bitsery)
|
||||
file(GLOB_RECURSE HeadersForIDE ${ParentDir}/include/bitsery/*.h)
|
||||
# create dummy target IDE
|
||||
file(WRITE ${CMAKE_BINARY_DIR}/dummy_for_ide.cpp "//generated by CMake to create dummy target with all includes for IDE.")
|
||||
add_library(bitsery.dummy_for_ide ${CMAKE_BINARY_DIR}/dummy_for_ide.cpp )
|
||||
# add headers so IDE correctly show them
|
||||
target_sources(bitsery.dummy_for_ide PRIVATE ${HeadersForIDE} serialization_test_utils.h)
|
||||
target_link_libraries(bitsery.dummy_for_ide PRIVATE GTest::Main Bitsery::bitsery)
|
||||
endif()
|
||||
|
||||
LinkTestLib(${TestProjectName})
|
||||
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ TEST(FlexibleSyntax, FundamentalTypesAndBool) {
|
||||
int ti = 8745;
|
||||
MyEnumClass te = MyEnumClass::E4;
|
||||
float tf = 485.042f;
|
||||
double_t td = -454184.48445;
|
||||
double td = -454184.48445;
|
||||
bool tb=true;
|
||||
SerializationContext ctx{};
|
||||
ctx.createSerializer().archive(ti,te,tf,td,tb);
|
||||
@@ -53,7 +53,7 @@ TEST(FlexibleSyntax, FundamentalTypesAndBool) {
|
||||
int ri{};
|
||||
MyEnumClass re{};
|
||||
float rf{};
|
||||
double_t rd{};
|
||||
double rd{};
|
||||
bool rb{};
|
||||
ctx.createDeserializer().archive(ri,re,rf,rd,rb);
|
||||
|
||||
@@ -69,7 +69,7 @@ TEST(FlexibleSyntax, UseObjectFncInsteadOfValueN) {
|
||||
int ti = 8745;
|
||||
MyEnumClass te = MyEnumClass::E4;
|
||||
float tf = 485.042f;
|
||||
double_t td = -454184.48445;
|
||||
double td = -454184.48445;
|
||||
bool tb=true;
|
||||
SerializationContext ctx;
|
||||
auto& ser = ctx.createSerializer();
|
||||
@@ -83,7 +83,7 @@ TEST(FlexibleSyntax, UseObjectFncInsteadOfValueN) {
|
||||
int ri{};
|
||||
MyEnumClass re{};
|
||||
float rf{};
|
||||
double_t rd{};
|
||||
double rd{};
|
||||
bool rb{};
|
||||
auto& des = ctx.createDeserializer();
|
||||
des.object(ri);
|
||||
@@ -104,7 +104,7 @@ TEST(FlexibleSyntax, MixDifferentSyntax) {
|
||||
int ti = 8745;
|
||||
MyEnumClass te = MyEnumClass::E4;
|
||||
float tf = 485.042f;
|
||||
double_t td = -454184.48445;
|
||||
double td = -454184.48445;
|
||||
bool tb=true;
|
||||
SerializationContext ctx;
|
||||
auto& ser = ctx.createSerializer();
|
||||
@@ -116,7 +116,7 @@ TEST(FlexibleSyntax, MixDifferentSyntax) {
|
||||
int ri{};
|
||||
MyEnumClass re{};
|
||||
float rf{};
|
||||
double_t rd{};
|
||||
double rd{};
|
||||
bool rb{};
|
||||
auto& des = ctx.createDeserializer();
|
||||
des.archive(ri, re, rf);
|
||||
|
||||
@@ -250,4 +250,20 @@ TYPED_TEST(SerializeContainerFixedSizeCompositeTypes, CustomFunctionThatSerializ
|
||||
EXPECT_THAT(res, ContainerEq(src));
|
||||
}
|
||||
|
||||
class SerializeContainer : public ::testing::TestWithParam<size_t> {
|
||||
};
|
||||
|
||||
TEST_P(SerializeContainer, SizeHasVariableLength) {
|
||||
SerializationContext ctx{};
|
||||
auto emptyFnc = [](uint8_t &) {};
|
||||
|
||||
std::vector<uint8_t > src(GetParam());
|
||||
std::vector<uint8_t > res{};
|
||||
ctx.createSerializer().container(src, std::numeric_limits<size_t>::max(), emptyFnc);
|
||||
ctx.createDeserializer().container(res, std::numeric_limits<size_t>::max(), emptyFnc);
|
||||
|
||||
EXPECT_THAT(res.size(), Eq(src.size()));
|
||||
EXPECT_THAT(ctx.getBufferSize(), Eq(ctx.containerSizeSerializedBytesCount(src.size())));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(LargeContainerSize, SerializeContainer, ::testing::Values(0x01, 0x80, 0x4000));
|
||||
@@ -345,6 +345,7 @@ TEST(SerializeExtensionInheritance, CanSerializeAbstractClass) {
|
||||
ImplementedBase data{};
|
||||
data.x = 4;
|
||||
data.y = 2;
|
||||
data.exec();
|
||||
ImplementedBase res{};
|
||||
SerContext ctx{};
|
||||
ctx.createSerializer().object(data);
|
||||
|
||||
@@ -72,7 +72,7 @@ void serialize(S& s, Derived2& o) {
|
||||
}
|
||||
|
||||
struct MultipleVirtualInheritance: Derived1, Derived2 {
|
||||
uint8_t z{};
|
||||
int8_t z{};
|
||||
MultipleVirtualInheritance() = default;
|
||||
|
||||
MultipleVirtualInheritance(uint8_t x_, uint8_t y1_, uint8_t y2_, uint8_t z_) {
|
||||
@@ -91,10 +91,6 @@ struct MultipleVirtualInheritance: Derived1, Derived2 {
|
||||
|
||||
};
|
||||
|
||||
bool operator == (const MultipleVirtualInheritance& lhs, const MultipleVirtualInheritance& rhs) {
|
||||
return std::tie(lhs.x, lhs.y1, lhs.y2, lhs.z) == std::tie(rhs.x, rhs.y1, rhs.y2, rhs.z);
|
||||
}
|
||||
|
||||
//define PolymorphicBase relationships for runtime polymorphism
|
||||
|
||||
namespace bitsery {
|
||||
@@ -130,6 +126,10 @@ public:
|
||||
bool isPointerContextValid() {
|
||||
return std::get<0>(plctx1).isValid();
|
||||
}
|
||||
|
||||
virtual void TearDown() override {
|
||||
EXPECT_TRUE(isPointerContextValid());
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, Data0Result0) {
|
||||
@@ -170,6 +170,7 @@ TEST_F(SerializeExtensionPointerPolymorphicTypes, Data1Result0) {
|
||||
EXPECT_THAT(res, ::testing::NotNull());
|
||||
EXPECT_THAT(res->x, Eq(data->x));
|
||||
EXPECT_THAT(res->y1, Eq(data->y1));
|
||||
delete baseRes;
|
||||
}
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, Data1Result1) {
|
||||
@@ -190,3 +191,59 @@ TEST_F(SerializeExtensionPointerPolymorphicTypes, Data1Result1) {
|
||||
EXPECT_THAT(res->x, Eq(data->x));
|
||||
EXPECT_THAT(res->y1, Eq(data->y1));
|
||||
}
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, ComplexTypeData1Result0) {
|
||||
MultipleVirtualInheritance md1{};
|
||||
md1.x = 3;
|
||||
md1.y1 = 78;
|
||||
md1.y2 = 14;
|
||||
md1.z = -33;
|
||||
Base* baseData = &md1;
|
||||
createSerializer().ext(baseData, PointerOwner{});
|
||||
Base* baseRes = nullptr;
|
||||
createDeserializer().ext(baseRes, PointerOwner{});
|
||||
|
||||
auto* data = dynamic_cast<MultipleVirtualInheritance*>(baseData);
|
||||
auto* res = dynamic_cast<MultipleVirtualInheritance*>(baseRes);
|
||||
|
||||
EXPECT_THAT(baseRes, ::testing::NotNull());
|
||||
EXPECT_THAT(data, ::testing::NotNull());
|
||||
EXPECT_THAT(res, ::testing::NotNull());
|
||||
EXPECT_THAT(res->x, Eq(data->x));
|
||||
EXPECT_THAT(res->y1, Eq(data->y1));
|
||||
EXPECT_THAT(res->y2, Eq(data->y2));
|
||||
EXPECT_THAT(res->z, Eq(data->z));
|
||||
delete baseRes;
|
||||
}
|
||||
|
||||
TEST_F(SerializeExtensionPointerPolymorphicTypes, WhenResultIsDifferentTypeThenRecreate) {
|
||||
MultipleVirtualInheritance md1{};
|
||||
md1.x = 3;
|
||||
md1.y1 = 78;
|
||||
md1.y2 = 14;
|
||||
md1.z = -33;
|
||||
Base* baseData = &md1;
|
||||
createSerializer().ext(baseData, PointerOwner{});
|
||||
Base* baseRes = new Derived1{};
|
||||
EXPECT_THAT(dynamic_cast<MultipleVirtualInheritance*>(baseRes), ::testing::IsNull());
|
||||
createDeserializer().ext(baseRes, PointerOwner{});
|
||||
EXPECT_THAT(dynamic_cast<MultipleVirtualInheritance*>(baseRes), ::testing::NotNull());
|
||||
delete baseRes;
|
||||
}
|
||||
|
||||
//struct UnknownType:Base {
|
||||
//};
|
||||
//
|
||||
//template <typename S>
|
||||
//void serialize(S& s, UnknownType& o) {
|
||||
// s.ext(o, VirtualBaseClass<Base>{});
|
||||
//}
|
||||
|
||||
// todo reimplement whole polymorphism thing, because with current solution this test fails
|
||||
//TEST(SerializeExtensionPointerPolymorphicTypesErrors, WhenSerializingUnknownTypeThenAssert) {
|
||||
// TContext plctx1{};
|
||||
// SerContext sctx1{};
|
||||
// UnknownType obj{};
|
||||
// UnknownType* unknownPtr = &obj;
|
||||
// EXPECT_DEATH(sctx1.createSerializer(&plctx1).ext(unknownPtr, PointerOwner{}), "");
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user