Skip to content

Commit

Permalink
Added serialisation for std::string.
Browse files Browse the repository at this point in the history
Added unit tests for std::string and float.
  • Loading branch information
MStachowicz committed Apr 3, 2024
1 parent d85db72 commit 733912a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
51 changes: 50 additions & 1 deletion source/Test/Tests/ComponentSerialiseTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Component/Lights.hpp"
#include "Component/Transform.hpp"
#include "Utility/Logger.hpp"
#include "Utility/Serialise.hpp"

#include <fstream>

Expand All @@ -27,7 +28,39 @@ namespace Test
p_deserialised = ComponentType::Deserialise(in, 0);
in.close();
}
catch (std::ofstream::failure& e)
catch (std::exception& e)
{
CHECK_TRUE(false, e.what());
std::remove("test.bin");
return false;
}

std::remove("test.bin");
return true;
}

// Helper function to test the serialisation and deserialisation of a value.
//@param p_to_serialise The value to write to a binary file.
//@param test_name The name of the test.
//@return True if the serialisation and deserialisation was successful, false otherwise.
template <typename T>
bool ComponentSerialiseTester::test_serialisation_utility(const T& p_to_serialise, T& p_deserialised)
{
try
{
// Write the value to a binary file.
std::ofstream out("test.bin", std::ios::binary);
out.exceptions(std::ofstream::failbit | std::ofstream::badbit);
Utility::write_binary(out, p_to_serialise);
out.close();

// Read the value back from the binary file.
std::ifstream in("test.bin", std::ios::binary);
in.exceptions(std::ifstream::failbit | std::ifstream::badbit);
Utility::read_binary(in, p_deserialised);
in.close();
}
catch (std::exception& e)
{
CHECK_TRUE(false, e.what());
std::remove("test.bin");
Expand All @@ -40,6 +73,22 @@ namespace Test

void ComponentSerialiseTester::run_unit_tests()
{
{SCOPE_SECTION("Utility::Serialise")

{SCOPE_SECTION("Float");
float out_float = 3.14f;
float in_float = 0.f;
if (test_serialisation_utility(out_float, in_float))
CHECK_EQUAL(out_float, in_float, "Float equality");
}
{SCOPE_SECTION("String");
std::string out_string = "Hello, world!";
std::string in_string = "";
if (test_serialisation_utility(out_string, in_string))
CHECK_EQUAL(out_string, in_string, "String equality");
}
}

SCOPE_SECTION("Component serialise");

{SCOPE_SECTION("Directional light");
Expand Down
2 changes: 2 additions & 0 deletions source/Test/Tests/ComponentSerialiseTester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ namespace Test
// Used only inside ComponentSerialiseTester.cpp, no need to expose it to the public.
template<typename ComponentType>
bool test_serialisation(const ComponentType& p_to_serialise, ComponentType& p_deserialised);
template <typename T>
bool test_serialisation_utility(const T& p_to_serialise, T& p_deserialised);
};
} // namespace Test
17 changes: 17 additions & 0 deletions source/Utility/Serialise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,21 @@ namespace Utility

p_in.read(reinterpret_cast<char*>(&p_value), sizeof(T));
}
template <>
void write_binary<std::string>(std::ofstream& p_out, const std::string& p_value)
{
// Write the size of the string first then write the string data
std::size_t size = p_value.size();
p_out.write(reinterpret_cast<const char*>(&size), sizeof(size));
p_out.write(p_value.data(), p_value.size());
}
template <>
void read_binary<std::string>(std::ifstream& p_in, std::string& p_value)
{
// Read the size of the string first then read the string data
std::size_t size;
p_in.read(reinterpret_cast<char*>(&size), sizeof(size));
p_value.resize(size);
p_in.read(p_value.data(), size);
}
} // namespace Utility

0 comments on commit 733912a

Please sign in to comment.