diff options
author | Geir Storli <geirst@verizonmedia.com> | 2019-03-01 09:52:31 +0000 |
---|---|---|
committer | Geir Storli <geirst@verizonmedia.com> | 2019-03-01 09:52:31 +0000 |
commit | 30eb3a77474cc08b7904b52537e5311ce0c54465 (patch) | |
tree | 51fd98b47cc2351a242d30988afebbae591bae9c /document | |
parent | 2aee5c3732b9b1ea5181031a01f18645c63b4b3e (diff) |
Use common code to read and write data buffer from/to file.
Diffstat (limited to 'document')
-rw-r--r-- | document/src/tests/documentupdatetestcase.cpp | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/document/src/tests/documentupdatetestcase.cpp b/document/src/tests/documentupdatetestcase.cpp index 7164ee106e5..295372a42ca 100644 --- a/document/src/tests/documentupdatetestcase.cpp +++ b/document/src/tests/documentupdatetestcase.cpp @@ -91,6 +91,28 @@ void testRoundtripSerialize(const UpdateType& update, const DataType &type) { } } +void +writeBufferToFile(const ByteBuffer &buf, const vespalib::string &fileName) +{ + auto file = std::fstream(fileName, std::ios::out | std::ios::binary); + file.write(buf.getBuffer(), buf.getPos()); + assert(file.good()); + file.close(); +} + +ByteBuffer::UP +readBufferFromFile(const vespalib::string &fileName) +{ + auto file = std::fstream(fileName, std::ios::in | std::ios::binary | std::ios::ate); + auto size = file.tellg(); + auto result = std::make_unique<ByteBuffer>(size); + file.seekg(0); + file.read(result->getBuffer(), size); + assert(file.good()); + file.close(); + return result; +} + } TEST(DocumentUpdateTest, testSimpleUsage) @@ -446,17 +468,8 @@ TEST(DocumentUpdateTest, testReadSerializedFile) const std::string file_name = "data/crossplatform-java-cpp-doctypes.cfg"; DocumentTypeRepo repo(readDocumenttypesConfig(file_name)); - int fd = open("data/serializeupdatejava.dat" , O_RDONLY); - - int len = lseek(fd,0,SEEK_END); - ByteBuffer buf(len); - lseek(fd,0,SEEK_SET); - if (read(fd, buf.getBuffer(), len) != len) { - throw vespalib::Exception("read failed"); - } - close(fd); - - nbostream is(buf.getBufferAtPos(), buf.getRemaining()); + auto buf = readBufferFromFile("data/serializeupdatejava.dat"); + nbostream is(buf->getBufferAtPos(), buf->getRemaining()); DocumentUpdate::UP updp(DocumentUpdate::create42(repo, is)); DocumentUpdate& upd(*updp); @@ -536,12 +549,7 @@ TEST(DocumentUpdateTest, testGenerateSerializedFile) .addUpdate(MapValueUpdate(StringFieldValue("foo"), ArithmeticValueUpdate(ArithmeticValueUpdate::Mul, 2)))); ByteBuffer::UP buf(serialize42(upd)); - - int fd = open("data/serializeupdatecpp.dat", O_WRONLY | O_TRUNC | O_CREAT, 0644); - if (write(fd, buf->getBuffer(), buf->getPos()) != (ssize_t)buf->getPos()) { - throw vespalib::Exception("read failed"); - } - close(fd); + writeBufferToFile(*buf, "data/serializeupdatecpp.dat"); } @@ -1097,21 +1105,13 @@ struct TensorUpdateSerializeFixture { void serializeUpdateToFile(const DocumentUpdate &update, const vespalib::string &fileName) { ByteBuffer::UP buf = serializeHEAD(update); - auto file = std::fstream(fileName, std::ios::out | std::ios::binary); - file.write(buf->getBuffer(), buf->getPos()); - file.close(); + writeBufferToFile(*buf, fileName); } DocumentUpdate::UP deserializeUpdateFromFile(const vespalib::string &fileName) { - auto file = std::fstream(fileName, std::ios::in | std::ios::binary | std::ios::ate); - auto size = file.tellg(); - ByteBuffer buf(size); - file.seekg(0); - file.read(buf.getBuffer(), size); - file.close(); - - nbostream inStream(buf.getBufferAtPos(), buf.getRemaining()); - return DocumentUpdate::createHEAD(*repo, inStream); + auto buf = readBufferFromFile(fileName); + nbostream stream(buf->getBufferAtPos(), buf->getRemaining()); + return DocumentUpdate::createHEAD(*repo, stream); } }; |