diff --git a/visualdl/logic/sdk_test.cc b/visualdl/logic/sdk_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..5f204095320e8a7377d325faeaf033a01b4df339 --- /dev/null +++ b/visualdl/logic/sdk_test.cc @@ -0,0 +1,78 @@ +#include "visualdl/logic/sdk.h" + +#include + +namespace visualdl { + +struct ScalarTestHelper { + ImHelper rim; + ImHelper wim; + const std::string dir = "./tmp/1.test"; + + void operator()(std::function read, std::function write) { + wim.StartWriteSerice(dir, 200); + write(); + + rim.StartReadService(dir, 200); + std::this_thread::sleep_for(std::chrono::milliseconds(600)); + read(); + } +}; + +TEST(Scalar, set_caption) { + ScalarTestHelper helper; + + const std::vector captions({"train", "test"}); + + auto write = [&] { + auto tablet = helper.wim.AddTablet("tag0", -1); + components::ScalarHelper scalar(tablet, &helper.wim.handler()); + + scalar.SetCaptions(captions); + }; + + auto read = [&] { + auto mytablet = helper.rim.tablet("tag0"); + components::ScalarHelper myscalar(mytablet, &helper.rim.handler()); + auto mycaptions = myscalar.GetCaptions(); + + ASSERT_EQ(captions, mycaptions); + }; + + helper(read, write); +} + +TEST(Scalar, add_records) { + ScalarTestHelper helper; + + const std::vector captions({"train", "test"}); + + const size_t nsteps = 100; + + auto write = [&] { + auto tablet = helper.wim.AddTablet("tag0", -1); + components::ScalarHelper scalar(tablet, &helper.wim.handler()); + + scalar.SetCaptions(captions); + + for (int i = 0; i < nsteps; i++) { + scalar.AddRecord(i * 10, std::vector({(float)i, (float)i + 1})); + } + }; + + auto read = [&] { + auto mytablet = helper.rim.tablet("tag0"); + components::ScalarHelper myscalar(mytablet, &helper.rim.handler()); + + auto records = myscalar.GetRecords(); + ASSERT_EQ(records.size(), nsteps); + + for (int i = 0; i < nsteps; i++) { + ASSERT_EQ(records[i], std::vector({(float)i, (float)i + 1})); + } + }; + + helper(read, write); +} + +} // namespace visualdl