tests/cpp/test_metainfo: Add tests to save and load

This commit is contained in:
AbdealiJK 2016-12-02 12:35:11 +05:30 committed by Tianqi Chen
parent 8eb69e0677
commit ef7fe06cf8
3 changed files with 58 additions and 1 deletions

View File

@ -1,6 +1,7 @@
// Copyright by Contributors
#include <xgboost/data.h>
#include <gtest/gtest.h>
#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());
}

11
tests/cpp/helpers.cc Normal file
View File

@ -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;
}

17
tests/cpp/helpers.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef XGBOOST_TESTS_CPP_HELPERS_H_
#define XGBOOST_TESTS_CPP_HELPERS_H_
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <gtest/gtest.h>
std::string TempFileName();
long GetFileSize(const std::string filename);
#endif