diff --git a/coding/coding_tests/file_container_test.cpp b/coding/coding_tests/file_container_test.cpp index 4e340c55b2..fdb1923171 100644 --- a/coding/coding_tests/file_container_test.cpp +++ b/coding/coding_tests/file_container_test.cpp @@ -6,7 +6,7 @@ #include "../../base/string_utils.hpp" -UNIT_TEST(FileContainer_Smoke) +UNIT_TEST(FilesContainer_Smoke) { string const fName = "file_container.tmp"; FileWriter::DeleteFile(fName); @@ -70,3 +70,73 @@ UNIT_TEST(FileContainer_Smoke) } FileWriter::DeleteFile(fName); } + +namespace +{ + void CheckInvariant(FilesContainerR & reader, string const & tag, int64_t test) + { + FileReader r = reader.GetReader(tag); + TEST_EQUAL(test, ReadPrimitiveFromPos(r, 0), ()); + } +} + +UNIT_TEST(FilesContainer_Shared) +{ + string const fName = "file_container.tmp"; + FileWriter::DeleteFile(fName); + + uint32_t const count = 10; + int64_t const test64 = 908175281437210836; + + { + // shared container fill + + FilesContainerW writer(fName); + + FileWriter w1 = writer.GetWriter("1"); + WriteToSink(w1, uint32_t(0)); + + for (uint32_t i = 0; i < count; ++i) + WriteVarUint(w1, i); + w1.Flush(); + + FileWriter w2 = writer.GetWriter("2"); + WriteToSink(w2, test64); + w2.Flush(); + + writer.Finish(); + } + + { + // shared container read and fill + + FilesContainerR reader(fName); + FileReader r1 = reader.GetReader("1"); + uint64_t const offset = sizeof(uint32_t); + r1 = r1.SubReader(offset, r1.Size() - offset); + + CheckInvariant(reader, "2", test64); + + FilesContainerW writer(fName, FileWriter::OP_APPEND); + FileWriter w = writer.GetWriter("3"); + + ReaderSource src(r1); + for (uint32_t i = 0; i < count; ++i) + { + uint32_t test = ReadVarUint(src); + TEST_EQUAL(test, i, ()); + WriteVarUint(w, i); + } + + w.Flush(); + writer.Finish(); + } + + { + // check invariant + FilesContainerR reader(fName); + CheckInvariant(reader, "2", test64); + } + + FileWriter::DeleteFile(fName); +}