sdk_test.cc 1.8 KB
Newer Older
S
Superjom 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include "visualdl/logic/sdk.h"

#include <gtest/gtest.h>

namespace visualdl {

struct ScalarTestHelper {
  ImHelper rim;
  ImHelper wim;
  const std::string dir = "./tmp/1.test";

  void operator()(std::function<void()> read, std::function<void()> 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<std::string> captions({"train", "test"});

  auto write = [&] {
    auto tablet = helper.wim.AddTablet("tag0", -1);
    components::ScalarHelper<float> scalar(tablet, &helper.wim.handler());

    scalar.SetCaptions(captions);
  };

  auto read = [&] {
    auto mytablet = helper.rim.tablet("tag0");
    components::ScalarHelper<float> 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<std::string> captions({"train", "test"});

  const size_t nsteps = 100;

  auto write = [&] {
    auto tablet = helper.wim.AddTablet("tag0", -1);
    components::ScalarHelper<float> scalar(tablet, &helper.wim.handler());

    scalar.SetCaptions(captions);

    for (int i = 0; i < nsteps; i++) {
      scalar.AddRecord(i * 10, std::vector<float>({(float)i, (float)i + 1}));
    }
  };

  auto read = [&] {
    auto mytablet = helper.rim.tablet("tag0");
    components::ScalarHelper<float> 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>({(float)i, (float)i + 1}));
    }
  };

  helper(read, write);
}

}  // namespace visualdl