Skip to content

Commit

Permalink
[CPP] add cereal test
Browse files Browse the repository at this point in the history
  • Loading branch information
keosu committed Mar 1, 2024
1 parent cc9c11f commit b01884a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 24 deletions.
83 changes: 83 additions & 0 deletions code/c++/tests/test_cereal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

#include <fstream>
#include <iostream>
#include <string>

#include "cereal/archives/json.hpp"
#include "cereal/types/memory.hpp"
#include "cereal/types/string.hpp"
#include "cereal/types/unordered_map.hpp"

using namespace std;

struct MyRecord {
int x, y;
float z;

template <class Archive>
void serialize(Archive& ar) {
ar(CEREAL_NVP(x), CEREAL_NVP(y), CEREAL_NVP(z));
}

friend std::ostream& operator<<(std::ostream& os, const MyRecord& mr);
};

std::ostream& operator<<(std::ostream& os, const MyRecord& mr) {
os << "MyRecord(" << mr.x << ", " << mr.y << "," << mr.z << ")\n";
return os;
}

struct SomeData {
int32_t id;
std::shared_ptr<std::unordered_map<uint32_t, MyRecord>> data;

SomeData(int32_t id_ = 0) : id(id_), data(new std::unordered_map<uint32_t, MyRecord>) {}

template <class Archive>
void save(Archive& ar) const {
ar(CEREAL_NVP(id), CEREAL_NVP(data));
}

template <class Archive>
void load(Archive& ar) {
ar(id, data);
}

void push(uint32_t, const MyRecord& mr) { data->insert(std::make_pair(100, mr)); }

void print() {
std::cout << "ID : " << id << "\n";
if (data->empty()) return;
for (auto& item : *data) {
std::cout << item.first << "\t" << item.second << "\n";
}
}
};

void test() {
{
std::ofstream os("obj.json");
cereal::JSONOutputArchive archive(os);

MyRecord mr = {1, 2, 3.0};

SomeData myData(1111);
myData.push(100, mr);

archive(myData);
}
{
std::ifstream is("obj.json");
cereal::JSONInputArchive archive(is);

SomeData myData;
archive(myData);
myData.print();
}
}

int main() {
test();

return 0;
}
27 changes: 27 additions & 0 deletions code/c++/tests/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
add_requires("raylib")
add_requires("catch2")
add_requires("spdlog")
add_requires("cereal")

target("test_raylib")
set_kind("binary")
add_files("test_raylib.cpp")
add_packages("raylib")


target("test_catch2")
set_kind("binary")
add_files("test_catch2.cpp")
add_packages("catch2")


target("test_spdlog")
set_kind("binary")
add_files("test_spdlog.cpp")
add_packages("spdlog")

target("test_cereal")
set_kind("binary")
add_files("test_cereal.cpp")
add_packages("cereal")
set_languages("c++17")
30 changes: 6 additions & 24 deletions code/c++/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@


add_requires("raylib")
add_requires("catch2")
add_requires("spdlog")


add_repositories("my-repo myrepo")
add_requires("raylibx")

add_rules("mode.debug", "mode.release")

target("test_raylib")
set_kind("binary")
add_files("tests/test_raylib.cpp")
add_packages("raylib")


target("test_catch2")
set_kind("binary")
add_files("tests/test_catch2.cpp")
add_packages("catch2")


target("test_spdlog")
set_kind("binary")
add_files("tests/test_spdlog.cpp")
add_packages("spdlog")

includes("tests/")

includes("src/")


target("test_jsoncpp")
set_kind("binary")
add_files("tmp.cpp")
add_packages("raylibx")
-- target("test_jsoncpp")
-- set_kind("binary")
-- add_files("tmp.cpp")
-- add_packages("raylibx")

0 comments on commit b01884a

Please sign in to comment.