summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/attribute/attributefilewriter
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /searchlib/src/tests/attribute/attributefilewriter
Publish
Diffstat (limited to 'searchlib/src/tests/attribute/attributefilewriter')
-rw-r--r--searchlib/src/tests/attribute/attributefilewriter/.gitignore1
-rw-r--r--searchlib/src/tests/attribute/attributefilewriter/CMakeLists.txt8
-rw-r--r--searchlib/src/tests/attribute/attributefilewriter/attributefilewriter_test.cpp116
3 files changed, 125 insertions, 0 deletions
diff --git a/searchlib/src/tests/attribute/attributefilewriter/.gitignore b/searchlib/src/tests/attribute/attributefilewriter/.gitignore
new file mode 100644
index 00000000000..ea6a0e03bf2
--- /dev/null
+++ b/searchlib/src/tests/attribute/attributefilewriter/.gitignore
@@ -0,0 +1 @@
+searchlib_attributefilewriter_test_app
diff --git a/searchlib/src/tests/attribute/attributefilewriter/CMakeLists.txt b/searchlib/src/tests/attribute/attributefilewriter/CMakeLists.txt
new file mode 100644
index 00000000000..a1d859bbfb9
--- /dev/null
+++ b/searchlib/src/tests/attribute/attributefilewriter/CMakeLists.txt
@@ -0,0 +1,8 @@
+# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(searchlib_attributefilewriter_test_app
+ SOURCES
+ attributefilewriter_test.cpp
+ DEPENDS
+ searchlib
+)
+vespa_add_test(NAME searchlib_attributefilewriter_test_app COMMAND searchlib_attributefilewriter_test_app)
diff --git a/searchlib/src/tests/attribute/attributefilewriter/attributefilewriter_test.cpp b/searchlib/src/tests/attribute/attributefilewriter/attributefilewriter_test.cpp
new file mode 100644
index 00000000000..acf61cd58bb
--- /dev/null
+++ b/searchlib/src/tests/attribute/attributefilewriter/attributefilewriter_test.cpp
@@ -0,0 +1,116 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/fastos/fastos.h>
+#include <vespa/log/log.h>
+LOG_SETUP("attributefilewriter_test");
+#include <vespa/vespalib/testkit/testapp.h>
+#include <vespa/vespalib/stllike/string.h>
+#include <vespa/searchlib/attribute/attributefilewriter.h>
+#include <vespa/searchlib/attribute/attributefilebufferwriter.h>
+#include <vespa/searchlib/util/fileutil.h>
+#include <vespa/searchlib/util/rand48.h>
+#include <vespa/searchlib/common/tunefileinfo.h>
+#include <vespa/searchlib/common/fileheadercontext.h>
+#include <vespa/searchlib/index/dummyfileheadercontext.h>
+
+using search::index::DummyFileHeaderContext;
+
+namespace search
+{
+
+namespace
+{
+
+vespalib::string testFileName("test.dat");
+vespalib::string hello("Hello world");
+
+void removeTestFile() { FastOS_File::Delete(testFileName.c_str()); }
+
+struct Fixture {
+ TuneFileAttributes _tuneFileAttributes;
+ DummyFileHeaderContext _fileHeaderContext;
+ IAttributeSaveTarget::Config _cfg;
+ const vespalib::string _desc;
+ AttributeFileWriter _writer;
+
+ Fixture()
+ : _tuneFileAttributes(),
+ _fileHeaderContext(),
+ _cfg(),
+ _desc("Attribute file sample description"),
+ _writer(_tuneFileAttributes,
+ _fileHeaderContext,
+ _cfg,
+ _desc)
+ {
+ removeTestFile();
+ }
+
+ ~Fixture() {
+ removeTestFile();
+ }
+
+};
+
+}
+
+
+TEST_F("Test that we can write empty attribute file", Fixture)
+{
+ EXPECT_TRUE(f._writer.open(testFileName));
+ f._writer.close();
+ FileUtil::LoadedBuffer::UP loaded(FileUtil::loadFile(testFileName));
+ EXPECT_EQUAL(0u, loaded->size());
+}
+
+
+TEST_F("Test that we destroy writer without calling close", Fixture)
+{
+ EXPECT_TRUE(f._writer.open(testFileName));
+}
+
+
+TEST_F("Test that buffer writer passes on written data", Fixture)
+{
+ std::vector<int> a;
+ const size_t mysize = 3000000;
+ const size_t writerBufferSize = AttributeFileBufferWriter::BUFFER_SIZE;
+ EXPECT_GREATER(mysize * sizeof(int), writerBufferSize);
+ a.reserve(mysize);
+ search::Rand48 rnd;
+ for (uint32_t i = 0; i < mysize; ++i) {
+ a.emplace_back(rnd.lrand48());
+ }
+ EXPECT_TRUE(f._writer.open(testFileName));
+ std::unique_ptr<BufferWriter> writer(f._writer.allocBufferWriter());
+ writer->write(&a[0], a.size() * sizeof(int));
+ writer->flush();
+ writer.reset();
+ f._writer.close();
+ FileUtil::LoadedBuffer::UP loaded(FileUtil::loadFile(testFileName));
+ EXPECT_EQUAL(a.size() * sizeof(int), loaded->size());
+ EXPECT_TRUE(memcmp(&a[0], loaded->buffer(), loaded->size()) == 0);
+}
+
+
+TEST_F("Test that we can pass buffer directly", Fixture)
+{
+ using Buffer = IAttributeFileWriter::Buffer;
+ Buffer buf = f._writer.allocBuf(hello.size());
+ buf->writeBytes(hello.c_str(), hello.size());
+ EXPECT_TRUE(f._writer.open(testFileName));
+ f._writer.writeBuf(std::move(buf));
+ f._writer.close();
+ FileUtil::LoadedBuffer::UP loaded(FileUtil::loadFile(testFileName));
+ EXPECT_EQUAL(hello.size(), loaded->size());
+ EXPECT_TRUE(memcmp(hello.c_str(), loaded->buffer(), loaded->size()) == 0);
+}
+
+
+}
+
+
+TEST_MAIN()
+{
+ TEST_RUN_ALL();
+}