28#include <gmock/gmock.h>
29#include <gtest/gtest-spi.h>
30#include <gtest/gtest.h>
62 return (stat(dir.c_str(), &info) == 0) && (info.st_mode & S_IFDIR);
70 std::unique_ptr<CheckpointIn>
cpt;
72 using SerializeFixture::SerializeFixture;
122 using SerializeFixture::SerializeFixture;
147TEST(CheckpointInTest, SetGetDirSlash)
163TEST(CheckpointInTest, SetGetDirTick)
182 std::ifstream file(getCptPath());
183 assert(!file.good());
198 std::ofstream file(getCptPath());
220 ASSERT_TRUE(cpt->sectionExists(
"General"));
221 ASSERT_TRUE(cpt->sectionExists(
"Junk"));
222 ASSERT_TRUE(cpt->sectionExists(
"Foo"));
225 ASSERT_FALSE(cpt->sectionExists(
"Junk2"));
226 ASSERT_FALSE(cpt->sectionExists(
"Test1"));
233 ASSERT_TRUE(cpt->entryExists(
"General",
"Test2"));
234 ASSERT_TRUE(cpt->entryExists(
"Junk",
"Test3"));
235 ASSERT_TRUE(cpt->entryExists(
"Junk",
"Test4"));
236 ASSERT_TRUE(cpt->entryExists(
"General",
"Test1"));
237 ASSERT_TRUE(cpt->entryExists(
"General",
"Test3"));
240 ASSERT_FALSE(cpt->entryExists(
"Junk2",
"test3"));
241 ASSERT_FALSE(cpt->entryExists(
"Junk",
"test4"));
250 ASSERT_TRUE(cpt->find(
"General",
"Test2", value));
251 ASSERT_EQ(value,
"bar");
252 ASSERT_TRUE(cpt->find(
"Junk",
"Test3", value));
253 ASSERT_EQ(value,
"yo");
254 ASSERT_TRUE(cpt->find(
"Junk",
"Test4", value));
255 ASSERT_EQ(value,
"mama mia");
256 ASSERT_TRUE(cpt->find(
"General",
"Test1", value));
257 ASSERT_EQ(value,
"BARasdf");
258 ASSERT_TRUE(cpt->find(
"General",
"Test3", value));
259 ASSERT_EQ(value,
"89");
262 ASSERT_FALSE(cpt->find(
"Junk2",
"test3", value));
263 ASSERT_FALSE(cpt->find(
"Junk",
"test4", value));
281 "Section1.Section2.Section3");
294 std::ofstream cpt(getCptPath());
305 "Section1.Section2.Section3");
320 std::ofstream
os(getCptPath());
327 std::ifstream
is(getCptPath());
329 ASSERT_EQ(
is.peek(), std::ifstream::traits_type::eof());
332 ASSERT_EQ(
is.peek(), std::ifstream::traits_type::eof());
340 auto buf =
is.rdbuf();
341 std::ostringstream oss;
360 std::ifstream
is(filename);
362 return std::string(std::istreambuf_iterator<char>(
is),
363 std::istreambuf_iterator<char>());
390 expected +=
"\n[Section1.Section2]\n";
394 expected +=
"\n[Section1.Section2.Section3]\n";
399 expected +=
"\n[Section1.Section4]\n";
416 expected +=
"\n[Section1.Section2]\n";
421 expected +=
"\n[Section1.Section2.Section3]\n";
427 expected +=
"\n[Section1.Section2.Section4]\n";
432 expected +=
"\n[Section1.Section5]\n";
437 expected +=
"\n[Section1.Section5.Section6]\n";
441 expected +=
"\n[Section1.Section5.Section6.Section7]\n";
446 expected +=
"\n[Section1.Section5.Section8]\n";
451TEST(SerializableDeathTest, GenerateCptOutFail)
454 const std::string dir_name =
459 dir_name +
"/b/a/n/a/n/a/", cpt));
466 assert(rmdir(getDirName().c_str()) == 0);
475 "## checkpoint generated: "), std::string::npos);
487 EXPECT_TRUE(remove((getCptPath()).c_str()) == 0);
549 "\n[Section1]\n\n[Section2]\n");
555 "\n[Section1]\n\n[Section2]\n\n[Section3]\n");
587 int unserialized_integer;
589 ASSERT_ANY_THROW(
paramIn(*cpt_in,
"Param1", unserialized_integer));
598 const int integer = 5;
599 const double real = 3.7;
600 const bool boolean =
true;
601 const std::string str =
"string test";
602 const char character =
'c';
607 std::string
expected =
"\n[Section1]\n";
609 paramOut(*cpt_out,
"Param1", integer);
617 paramOut(*cpt_out,
"Param3",
boolean);
625 paramOut(*cpt_out,
"Param5", character);
634 int unserialized_integer;
635 double unserialized_real;
636 bool unserialized_boolean;
637 std::string unserialized_str;
638 char unserialized_character;
642 paramIn(cpt,
"Param1", unserialized_integer);
643 ASSERT_EQ(integer, unserialized_integer);
645 paramIn(cpt,
"Param2", unserialized_real);
646 ASSERT_EQ(real, unserialized_real);
648 paramIn(cpt,
"Param3", unserialized_boolean);
649 ASSERT_EQ(
boolean, unserialized_boolean);
651 paramIn(cpt,
"Param4", unserialized_str);
652 ASSERT_EQ(str, unserialized_str);
654 paramIn(cpt,
"Param5", unserialized_character);
655 ASSERT_EQ(character, unserialized_character);
665 const int integer = 5;
666 const double real = 3.7;
667 const bool boolean =
true;
668 const std::string str =
"string test";
669 const char character =
'c';
674 std::string
expected =
"\n[Section1]\n";
676 paramOut(*cpt_out,
"Param1", integer);
686 expected +=
"\n[Section1.Section2]\n";
688 paramOut(*cpt_out,
"Param3",
boolean);
703 expected +=
"\n[Section1.Section3]\n";
706 expected +=
"\n[Section1.Section3.Section4]\n";
708 paramOut(*cpt_out,
"Param5", character);
719 int unserialized_integer;
720 double unserialized_real;
721 bool unserialized_boolean;
722 std::string unserialized_str;
723 char unserialized_character;
727 paramIn(cpt,
"Param1", unserialized_integer);
728 ASSERT_EQ(integer, unserialized_integer);
730 paramIn(cpt,
"Param2", unserialized_real);
731 ASSERT_EQ(real, unserialized_real);
736 paramIn(cpt,
"Param3", unserialized_boolean);
737 ASSERT_EQ(
boolean, unserialized_boolean);
741 paramIn(cpt,
"Param4", unserialized_str);
742 ASSERT_EQ(str, unserialized_str);
749 paramIn(cpt,
"Param5", unserialized_character);
750 ASSERT_EQ(character, unserialized_character);
758 const double real = 3.7;
759 const std::string str =
"string test";
763 std::ofstream cpt(getCptPath());
773 int unserialized_integer;
774 double unserialized_real;
775 bool unserialized_boolean;
776 std::string unserialized_str;
782 ASSERT_FALSE(
optParamIn(cpt,
"Param1", unserialized_integer,
false));
786 ASSERT_TRUE(
optParamIn(cpt,
"Param2", unserialized_real,
false));
787 ASSERT_EQ(real, unserialized_real);
792 ASSERT_FALSE(
optParamIn(cpt,
"Param3", unserialized_boolean));
794 ::testing::HasSubstr(
"warn: optional parameter Section1:Param3 "
798 ASSERT_TRUE(
optParamIn(cpt,
"Param4", unserialized_str));
799 ASSERT_EQ(str, unserialized_str);
804 ASSERT_FALSE(
optParamIn(cpt,
"Param5", unserialized_boolean,
true));
806 ::testing::HasSubstr(
"warn: optional parameter Section1:Param5 "
815 GTEST_SKIP() <<
"Skipping as assertions are "
816 "stripped out of fast builds";
819 std::ofstream
of(getCptPath());
821 int unserialized_integer;
822 ASSERT_DEATH(
paramIn(cpt,
"Param1", unserialized_integer),
"");
828 const int integer[] = {5, 10, 15};
829 std::array<double, 4> real = {0.1, 1.345, 892.72, 1
e+10};
832 std::set<uint64_t> uint64 = {12751928501, 13, 111111};
837 std::ofstream cpt(getCptPath());
839 std::string
expected =
"\n[Section1]\n";
846 expected +=
"Param2=0.1 1.345 892.72 1e+10\n";
854 expected +=
"Param4=a string test\n";
858 expected +=
"Param5=13 111111 12751928501\n";
870 int unserialized_integer[3];
871 std::array<double, 4> unserialized_real;
874 std::set<uint64_t> unserialized_uint64;
880 ASSERT_THAT(unserialized_integer, testing::ElementsAre(5, 10, 15));
883 unserialized_real.size());
884 ASSERT_EQ(real, unserialized_real);
887 ASSERT_EQ(
boolean, unserialized_boolean);
890 ASSERT_EQ(str, unserialized_str);
893 ASSERT_EQ(uint64, unserialized_uint64);
896 ASSERT_EQ(uint8, unserialized_uint8);
911 std::ofstream cpt(getCptPath());
913 std::string
expected =
"\n[Section1]\n";
916 expected +=
"Param1=string test for array param out\n";
929 ASSERT_EQ(str, unserialized_str);
941 const int integer[] = {5, 10, 15};
943 std::ofstream cpt(getCptPath());
945 std::string
expected =
"\n[Section1]\n";
956 int unserialized_integer[1];
960 ASSERT_ANY_THROW(
arrayParamIn(cpt,
"Param1", unserialized_integer, 2));
967 const int integers[] = {10, 32, 100};
968 std::array<double, 4> reals = {0.1, 1.345, 892.72, 1
e+10};
969 const char*
const names_ints[] = {
"ten",
"thirty-two",
"one hundred"};
970 const char*
const names_reals[] = {
"first",
"second",
"third",
"fourth"};
974 std::ofstream cpt(getCptPath());
976 std::string
expected =
"\n[Section1]\n";
979 expected +=
"\n[Section1.Integers]\nten=10\nthirty-two=32\n"
983 mappingParamOut(cpt,
"Reals", names_reals, reals.data(), reals.size());
984 expected +=
"\n[Section1.Reals]\nfirst=0.1\nsecond=1.345\n"
985 "third=892.72\nfourth=1e+10\n";
993 int unserialized_integers[3];
994 std::array<double, 4> unserialized_reals;
997 unserialized_integers, 3);
998 ASSERT_THAT(unserialized_integers, testing::ElementsAre(10, 32, 100));
1001 unserialized_reals.data(), unserialized_reals.size());
1002 ASSERT_EQ(unserialized_reals, reals);
1009 const int integers[] = {10, 32, 100};
1010 std::array<double, 4> reals = {0.1, 1.345, 892.72, 1
e+10};
1014 const char*
const names_ints[] = {
"ten",
"thirty-two",
"one hundred"};
1015 const char*
const names_reals[] =
1016 {
"first",
"second",
"third",
"fourth"};
1018 std::ofstream cpt(getCptPath());
1020 std::string
expected =
"\n[Section1]\n";
1023 expected +=
"\n[Section1.Integers]\nten=10\nthirty-two=32\n"
1024 "one hundred=100\n";
1027 mappingParamOut(cpt,
"Reals", names_reals, reals.data(), reals.size());
1028 expected +=
"\n[Section1.Reals]\nfirst=0.1\nsecond=1.345\n"
1029 "third=892.72\nfourth=1e+10\n";
1035 const char*
const names_ints[] = {
"one hundred"};
1036 const char*
const names_reals[] = {
"first",
"third"};
1037 std::array<double, 2> expected_reals = {0.1, 892.72};
1042 int unserialized_integers[1];
1043 std::array<double, 2> unserialized_reals;
1047 unserialized_integers, 1);
1048 ASSERT_THAT(unserialized_integers, testing::ElementsAre(100));
1050 ASSERT_THAT(
err, ::testing::HasSubstr(
"warn: unknown entry found in "
1051 "checkpoint: Section1.Integers thirty-two 32\n"));
1052 ASSERT_THAT(
err, ::testing::HasSubstr(
"warn: unknown entry found in "
1053 "checkpoint: Section1.Integers ten 10\n"));
1057 unserialized_reals.data(), unserialized_reals.size());
1058 ASSERT_EQ(unserialized_reals, expected_reals);
1060 ASSERT_THAT(
err, ::testing::HasSubstr(
"warn: unknown entry found in "
1061 "checkpoint: Section1.Reals fourth 1e+10\n"));
1062 ASSERT_THAT(
err, ::testing::HasSubstr(
"warn: unknown entry found in "
1063 "checkpoint: Section1.Reals second 1.345\n"));
1073 const int expected_integer = 5;
1074 const double expected_real = 3.7;
1075 const bool expected_boolean =
true;
1076 const std::string expected_str =
"string test";
1080 const int integer = expected_integer;
1081 const double real = expected_real;
1082 const bool boolean = expected_boolean;
1083 const std::string str = expected_str;
1085 std::ofstream cp(getCptPath());
1087 std::string
expected =
"\n[Section1]\n";
1118 ASSERT_EQ(integer, expected_integer);
1121 ASSERT_EQ(real, expected_real);
1124 ASSERT_EQ(
boolean, expected_boolean);
1127 ASSERT_EQ(str, expected_str);
1134 const double expected_real = 3.7;
1135 const std::string expected_str =
"string test";
1139 const double real = expected_real;
1140 const std::string str = expected_str;
1142 std::ofstream cp(getCptPath());
1163 ASSERT_EQ(real, expected_real);
1169 ASSERT_EQ(str, expected_str);
1185 const Number expected_val =
ZERO;
1186 const Number expected_val_2 = THIRTY_TWO;
1190 const Number zero = expected_val;
1191 const Number thirty_two = expected_val_2;
1193 std::ofstream cp(getCptPath());
1195 std::string
expected =
"\n[Section1]\n";
1216 ASSERT_EQ(zero, expected_val);
1219 ASSERT_EQ(thirty_two, expected_val_2);
1226 std::array<double, 4> expected_real = {0.1, 1.345, 892.72, 1
e+10};
1230 const int integer[] = {5, 10, 15};
1231 const double *real = expected_real.data();
1233 std::ofstream cp(getCptPath());
1235 std::string
expected =
"\n[Section1]\n";
1242 expected +=
"real=0.1 1.345 892.72 1e+10\n";
1251 std::array<double, 4> real_array;
1252 double *real = real_array.data();
1257 ASSERT_THAT(integer, testing::ElementsAre(5, 10, 15));
1260 ASSERT_EQ(real_array, expected_real);
1269 std::set<uint64_t> expected_uint64 = {12751928501, 13, 111111};
1276 const std::set<uint64_t> uint64 = expected_uint64;
1279 std::ofstream cp(getCptPath());
1281 std::string
expected =
"\n[Section1]\n";
1284 expected +=
"boolean=true false\n";
1292 expected +=
"uint64=13 111111 12751928501\n";
1306 std::set<uint64_t> uint64;
1312 ASSERT_EQ(
boolean, expected_boolean);
1315 ASSERT_EQ(str, expected_str);
1318 ASSERT_EQ(uint64, expected_uint64);
1321 ASSERT_EQ(uint8, expected_uint8);
1328 const int expected_integers[] = {10, 32, 100};
1329 std::array<double, 4> expected_reals = {0.1, 1.345, 892.72, 1
e+10};
1330 const char*
const names_ints[] = {
"ten",
"thirty-two",
"one hundred"};
1331 const char*
const names_reals[] = {
"first",
"second",
"third",
"fourth"};
1335 const int *integers = expected_integers;
1336 const double *reals = expected_reals.data();
1338 std::ofstream cp(getCptPath());
1340 std::string
expected =
"\n[Section1]\n";
1343 expected +=
"\n[Section1.integers]\nten=10\nthirty-two=32\n"
1344 "one hundred=100\n";
1348 expected +=
"\n[Section1.reals]\nfirst=0.1\nsecond=1.345\n"
1349 "third=892.72\nfourth=1e+10\n";
1363 ASSERT_THAT(integers, testing::ElementsAre(10, 32, 100));
1366 ASSERT_THAT(reals, testing::ElementsAre(0.1, 1.345, 892.72, 1
e+10));
std::unique_ptr< CheckpointIn > cpt
A fixture to handle checkpoint in and out variables, as well as the testing of the temporary director...
std::unique_ptr< CheckpointIn > cpt_in
std::unique_ptr< std::ofstream > cpt_out
bool checkAndResetUnserialized()
Checks if unserialize() has been called and then marks it as not called.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
bool checkAndResetSerialized()
Checks if serialize() has been called and then marks it as not called.
void serialize(CheckpointOut &cp) const override
Serialize an object.
Basic support for object serialization.
Fixture class that handles temporary directory creation.
std::string getDirName() const
Get the name of the directory we have created on SetUp.
std::string getCptPath() const
Get the path to the checkpoint file.
static std::string generateTempDirName()
Generate a temporary directory name.
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })
bool optParamIn(CheckpointIn &cp, const std::string &name, T ¶m, bool do_warn=true)
This function is used for restoring optional parameters from the checkpoint.
#define UNSERIALIZE_CONTAINER(member)
#define SERIALIZE_ENUM(scalar)
#define UNSERIALIZE_OPT_SCALAR(scalar)
static std::string setDir(const std::string &base_name)
Set the current directory.
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
static const std::string & currentSection()
Gets the fully-qualified name of the active section.
#define UNSERIALIZE_ARRAY(member, size)
#define SERIALIZE_ARRAY(member, size)
#define SERIALIZE_CONTAINER(member)
static std::string dir()
Get the current checkout directory name.
#define UNSERIALIZE_ENUM(scalar)
static void generateCheckpointOut(const std::string &cpt_dir, std::ofstream &outstream)
Generate a checkpoint file so that the serialization can be routed to it.
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
void mappingParamOut(CheckpointOut &os, const char *sectionName, const char *const names[], const T *param, unsigned size)
Serialize a mapping represented as two arrays: one containing names and the other containing values.
std::ostream CheckpointOut
void paramOut(CheckpointOut &cp, const std::string &name, ExtMachInst const &machInst)
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
void arrayParamOut(CheckpointOut &cp, const std::string &name, const CircleBuf< T > ¶m)
thread_local GTestLogOutput gtestLogOutput
void mappingParamIn(CheckpointIn &cp, const char *sectionName, const char *const names[], T *param, unsigned size)
Restore mappingParamOut.
void arrayParamIn(CheckpointIn &cp, const std::string &name, CircleBuf< T > ¶m)
#define UNSERIALIZE_MAPPING(member, names, size)
#define UNSERIALIZE_SCALAR(scalar)
#define SERIALIZE_SCALAR(scalar)
#define SERIALIZE_MAPPING(member, names, size)
std::string getString(std::istream &is)
SerializationFixture SerializeFixture
GTestTickHandler tickHandler
bool dirExists(std::string dir)
std::string getContents(std::ofstream &cpt, std::string filename)
Flushes the checkpoint and reads its contents.
TEST_F(SerializeFixtureDeathTest, ConstructorFailure)
Test constructor failure by requesting the creation of a checkpoint in a non-existent dir.
TEST(CheckpointInTest, SetGetDirSlash)
Tests that when setting a checkpoint dir it always ends with a slash.