Add IO utilities. (#5091)
* Add fixed size stream for reading model stream. * Add file extension.
This commit is contained in:
43
tests/cpp/common/test_io.cc
Normal file
43
tests/cpp/common/test_io.cc
Normal file
@@ -0,0 +1,43 @@
|
||||
/*!
|
||||
* Copyright (c) by XGBoost Contributors 2019
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include "../../../src/common/io.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
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);
|
||||
}
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
Reference in New Issue
Block a user