diff --git a/tests/cpp/data/test_metainfo.cc b/tests/cpp/data/test_metainfo.cc index c4be0e0eb..bd944dab4 100644 --- a/tests/cpp/data/test_metainfo.cc +++ b/tests/cpp/data/test_metainfo.cc @@ -1,6 +1,7 @@ // Copyright by Contributors #include -#include + +#include "../helpers.h" TEST(MetaInfo, GetSet) { xgboost::MetaInfo info; @@ -31,4 +32,32 @@ TEST(MetaInfo, GetSet) { info.SetInfo("group", uint64_t2, xgboost::kUInt64, 2); ASSERT_EQ(info.group_ptr.size(), 3); EXPECT_EQ(info.group_ptr[2], 3); + + info.Clear(); + ASSERT_EQ(info.group_ptr.size(), 0); +} + +TEST(MetaInfo, SaveLoadBinary) { + xgboost::MetaInfo info; + double vals[2] = {1.0, 2.0}; + info.SetInfo("label", vals, xgboost::kDouble, 2); + info.num_row = 2; + info.num_col = 1; + + std::string tmp_file = TempFileName(); + dmlc::Stream * fs = dmlc::Stream::Create(tmp_file.c_str(), "w"); + info.SaveBinary(fs); + delete fs; + + ASSERT_EQ(GetFileSize(tmp_file), 76) + << "Expected saved binary file size to be same as object size"; + + fs = dmlc::Stream::Create(tmp_file.c_str(), "r"); + xgboost::MetaInfo inforead; + inforead.LoadBinary(fs); + EXPECT_EQ(inforead.labels, info.labels); + EXPECT_EQ(inforead.num_col, info.num_col); + EXPECT_EQ(inforead.num_row, info.num_row); + + std::remove(tmp_file.c_str()); } diff --git a/tests/cpp/helpers.cc b/tests/cpp/helpers.cc new file mode 100644 index 000000000..86f4d729e --- /dev/null +++ b/tests/cpp/helpers.cc @@ -0,0 +1,11 @@ +#include "./helpers.h" + +std::string TempFileName() { + return std::tmpnam(nullptr); +} + +long GetFileSize(const std::string filename) { + struct stat st; + stat(filename.c_str(), &st); + return st.st_size; +} diff --git a/tests/cpp/helpers.h b/tests/cpp/helpers.h new file mode 100644 index 000000000..d60df1199 --- /dev/null +++ b/tests/cpp/helpers.h @@ -0,0 +1,17 @@ +#ifndef XGBOOST_TESTS_CPP_HELPERS_H_ +#define XGBOOST_TESTS_CPP_HELPERS_H_ + +#include +#include +#include +#include +#include +#include + +#include + +std::string TempFileName(); + +long GetFileSize(const std::string filename); + +#endif