-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
test_io.cc
93 lines (77 loc) · 2.44 KB
/
test_io.cc
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*!
* Copyright (c) by XGBoost Contributors 2019
*/
#include <gtest/gtest.h>
#include <fstream>
#include "../../../src/common/io.h"
#include "../helpers.h"
#include "../filesystem.h" // dmlc::TemporaryDirectory
namespace xgboost {
namespace common {
TEST(MemoryFixSizeBuffer, Seek) {
size_t constexpr kSize { 64 };
std::vector<int32_t> memory( kSize );
rabit::utils::MemoryFixSizeBuffer buf(memory.data(), memory.size());
buf.Seek(rabit::utils::MemoryFixSizeBuffer::kSeekEnd);
size_t end = buf.Tell();
ASSERT_EQ(end, kSize);
}
TEST(IO, FileExtension) {
std::string filename {u8"model.json"};
auto ext = FileExtension(filename);
ASSERT_EQ(ext, u8"json");
}
TEST(IO, FixedSizeStream) {
std::string buffer {"This is the content of stream"};
{
MemoryFixSizeBuffer stream(static_cast<void *>(&buffer[0]), buffer.size());
PeekableInStream peekable(&stream);
FixedSizeStream fixed(&peekable);
std::string out_buffer;
fixed.Take(&out_buffer);
ASSERT_EQ(buffer, out_buffer);
}
{
std::string huge_buffer;
for (size_t i = 0; i < 512; i++) {
huge_buffer += buffer;
}
MemoryFixSizeBuffer stream(static_cast<void*>(&huge_buffer[0]), huge_buffer.size());
PeekableInStream peekable(&stream);
FixedSizeStream fixed(&peekable);
std::string out_buffer;
fixed.Take(&out_buffer);
ASSERT_EQ(huge_buffer, out_buffer);
}
}
TEST(IO, LoadSequentialFile) {
EXPECT_THROW(LoadSequentialFile("non-exist"), dmlc::Error);
dmlc::TemporaryDirectory tempdir;
std::ofstream fout(tempdir.path + "test_file");
std::string content;
// Generate a JSON file.
size_t constexpr kRows = 1000, kCols = 100;
std::shared_ptr<DMatrix> p_dmat{
RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true)};
std::unique_ptr<Learner> learner { Learner::Create({p_dmat}) };
learner->SetParam("tree_method", "hist");
learner->Configure();
for (int32_t iter = 0; iter < 10; ++iter) {
learner->UpdateOneIter(iter, p_dmat);
}
Json out { Object() };
learner->SaveModel(&out);
std::string str;
Json::Dump(out, &str);
std::string tmpfile = tempdir.path + "/model.json";
{
std::unique_ptr<dmlc::Stream> fo(
dmlc::Stream::Create(tmpfile.c_str(), "w"));
fo->Write(str.c_str(), str.size());
}
auto loaded = LoadSequentialFile(tmpfile, true);
ASSERT_EQ(loaded, str);
ASSERT_THROW(LoadSequentialFile("non-exist", true), dmlc::Error);
}
} // namespace common
} // namespace xgboost