Skip to content

Commit

Permalink
Update (#8)
Browse files Browse the repository at this point in the history
- Mark external includes as SYSTEM
- Match function names between classes
- Extend test and convert it to gtest
- Apply catkin_lint_cmake and fixed issues
  • Loading branch information
MatthijsBurgh authored Dec 12, 2022
2 parents a25e7ae + 644377c commit cc23112
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 84 deletions.
34 changes: 23 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ generate_messages(
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS include
LIBRARIES tue_serialization
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS message_runtime
)

Expand All @@ -43,28 +43,40 @@ catkin_package(

include_directories(
include
SYSTEM
${catkin_INCLUDE_DIRS}
)

file(GLOB_RECURSE HEADER_FILES include/*.h)

add_library(tue_serialization
add_library(${PROJECT_NAME}
src/conversions.cpp
src/filesystem.cpp
${HEADER_FILES}
)
target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES})
add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS})

add_executable(test_serialization test/test_serialization.cpp)
target_link_libraries(test_serialization tue_serialization)
install(
DIRECTORY include/
DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}
)

add_dependencies(tue_serialization ${${PROJECT_NAME}_EXPORTED_TARGETS})
add_dependencies(test_serialization ${${PROJECT_NAME}_EXPORTED_TARGETS})
install(
TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)

#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_tue_serialization.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
if (CATKIN_ENABLE_TESTING)
find_package(catkin_lint_cmake REQUIRED)
catkin_add_catkin_lint_test("-W2 --ignore HEADER_OUTSIDE_PACKAGE_INCLUDE_PATH")

catkin_add_gtest(${PROJECT_NAME}-test test/test_${PROJECT_NAME}.cpp)
target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
endif()
2 changes: 1 addition & 1 deletion include/tue/serialization/archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Archive {
inline Archive& operator>>(std::string& s) {
s.clear();
char c;
while(true) {
while(!stream_.eof()) {
stream_.read(&c, 1);
if (c == '\0') {
break;
Expand Down
6 changes: 3 additions & 3 deletions include/tue/serialization/input_archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class InputArchive {
inline InputArchive& operator>>(std::string& s) {
s.clear();
char c;
while(true) {
while(!stream_.eof()) {
stream_.read(&c, 1);
if (c == '\0') {
break;
Expand All @@ -39,9 +39,9 @@ class InputArchive {
return *this;
}

inline std::istream& getStream() { return stream_; }
inline std::istream& stream() { return stream_; }

inline int getVersion() { return version_; }
inline int version() { return version_; }

protected:

Expand Down
4 changes: 3 additions & 1 deletion package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package format="3">
<name>tue_serialization</name>
<version>0.0.0</version>
<description>The tue_serialization package</description>
<description>Simple library for serialization of datastreams</description>

<author email="[email protected]">Sjoerd van den Dries</author>

Expand All @@ -20,6 +20,8 @@
<build_export_depend>message_runtime</build_export_depend>
<exec_depend>message_runtime</exec_depend>

<test_depend>catkin_lint_cmake</test_depend>

<doc_depend>doxygen</doc_depend>

<export>
Expand Down
68 changes: 0 additions & 68 deletions test/test_serialization.cpp

This file was deleted.

109 changes: 109 additions & 0 deletions test/test_tue_serialization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <gtest/gtest.h>

#include <tue/serialization/archive.h>
#include <tue/serialization/input_archive.h>
#include <tue/serialization/output_archive.h>

#include <tue/serialization/filesystem.h>
#include <tue/serialization/version.h>

#include <fstream>
#include <sstream>

// example


class tue_serialization : public testing::Test
{
protected:
const double d_c = 3.15;
const float f_c = 5.0;
const int i_c = 123;
const std::string s_c = "Hello, this is just a simple test";
};

TEST_F(tue_serialization, OutputArchive_InputArchive)
{
std::stringstream ss;
tue::serialization::OutputArchive a_out(ss);
a_out << d_c << f_c << i_c << s_c;

// read
double d;
float f;
int i;
std::string s;

tue::serialization::InputArchive a_in(ss);
a_in >> d >> f >> i >> s;
EXPECT_DOUBLE_EQ(d, d_c);
EXPECT_FLOAT_EQ(f, f_c);
EXPECT_EQ(i, i_c);
EXPECT_EQ(s, s_c);
std::cout << "OutputArchive -> InputArchive:" << std::endl;
std::cout << d << ", " << f << ", " << i << ", \"" << s << "\"" << std::endl;
std::cout << "version: " << a_in.version() << std::endl;
}

TEST_F(tue_serialization, toFile_fromFile)
{
std::string test_filename = "/tmp/tue_test_serialization";

{
tue::serialization::Archive a_out;
a_out << d_c << f_c << i_c << s_c;

tue::serialization::toFile(a_out, test_filename);
}

{
tue::serialization::Archive a_in;
tue::serialization::fromFile(test_filename, a_in);

// read
double d;
float f;
int i;
std::string s;

a_in >> d >> f >> i >> s;

EXPECT_DOUBLE_EQ(d, d_c);
EXPECT_FLOAT_EQ(f, f_c);
EXPECT_EQ(i, i_c);
EXPECT_EQ(s, s_c);
std::cout << "toFile -> fromFile:" << std::endl;
std::cout << d << ", " << f << ", " << i << ", \"" << s << "\"" << std::endl;
std::cout << "version: " << a_in.version() << std::endl;
}
}

TEST_F(tue_serialization, Archive)
{
tue::serialization::Archive a;

a << d_c << f_c << i_c << s_c;

// read
double d;
float f;
int i;
std::string s;

a >> d >> f >> i >> s;

EXPECT_DOUBLE_EQ(d, d_c);
EXPECT_FLOAT_EQ(f, f_c);
EXPECT_EQ(i, i_c);
EXPECT_EQ(s, s_c);
std::cout << "Archive:" << std::endl;
std::cout << d << ", " << f << ", " << i << ", \"" << s << "\"" << std::endl;
std::cout << "version: " << a.version() << std::endl;
}

// Run all the tests that were declared with TEST()
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit cc23112

Please sign in to comment.