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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
#include <cstring>
#include "eckit/config/Resource.h"
#include "eckit/filesystem/PathName.h"
#include "eckit/io/Buffer.h"
#include "eckit/io/FileHandle.h"
#include "eckit/io/MemoryHandle.h"
#include "eckit/io/MultiHandle.h"
#include "eckit/io/PartFileHandle.h"
#include "eckit/log/Log.h"
#include "eckit/memory/Zero.h"
#include "eckit/runtime/Tool.h"
#include "eckit/testing/Test.h"
#include "eckit/types/Types.h"
using namespace std;
using namespace eckit;
using namespace eckit::testing;
namespace eckit::test {
//----------------------------------------------------------------------------------------------------------------------
const char buf1[] = "abcdefghijklmnopqrstuvwxyz01234";
class Tester {
public:
Tester() {
format(std::cout, Log::fullFormat);
std::string base = Resource<std::string>("$TMPDIR", "/tmp");
path1_ = PathName::unique(base + "/path1");
path1_ += ".dat";
{
FileHandle f1(path1_);
f1.openForWrite(0);
f1.write(buf1, sizeof(buf1) - 1);
f1.close();
std::cout << "created: " << path1_ << std::endl;
}
}
~Tester() {
bool verbose = false;
path1_.unlink(verbose);
}
static Buffer makeBuffer() {
Buffer buffer(64);
buffer.zero();
return buffer;
}
PathName path1_;
};
//----------------------------------------------------------------------------------------------------------------------
CASE("PartFileHandle with no parts (empty)") {
Tester test;
OffsetList ol0 = {};
LengthList ll0 = {};
PartFileHandle ph(test.path1_, ol0, ll0);
ph.openForRead();
SECTION("Read @ start") {
EXPECT_NO_THROW(ph.seek(0));
{
Buffer buff = Tester::makeBuffer();
long r = ph.read(buff, 10);
EXPECT(r == 0);
}
EXPECT(ph.position() == Offset(0));
EXPECT_NO_THROW(ph.skip(0));
}
SECTION("Seek beyond EOF throws, but we can still read without error") {
EXPECT_THROWS_AS(ph.seek(1), AssertionFailed);
EXPECT_NO_THROW(ph.seek(0));
{
Buffer buff = Tester::makeBuffer();
long r = ph.read(buff, 10);
EXPECT(r == 0);
}
EXPECT(ph.position() == Offset(0));
EXPECT_NO_THROW(ph.skip(0));
}
ph.close();
}
CASE("PartFileHandle with multiple parts") {
Tester test;
OffsetList ol1 = {0, 2, 6, 13, 23};
LengthList ll1 = {1, 2, 4, 6, 8};
PartFileHandle ph(test.path1_, ol1, ll1);
// 0 1 2 3
// 0123456789012345678901234567890
// abcdefghijklmnopqrstuvwxyz01234
// = == ==== ====== ========
// a cd ghij nopqrs xyz01234
// size = 21
ph.openForRead();
SECTION("Seek to EOF") {
EXPECT(ph.size() == Length(21));
EXPECT_NO_THROW(ph.seek(21));
{
Buffer buff = Tester::makeBuffer();
long r = ph.read(buff, 10);
EXPECT(r == 0);
}
EXPECT_NO_THROW(ph.seek(10));
EXPECT(ph.position() == Offset(10));
{
Buffer buff = Tester::makeBuffer();
long r = ph.read(buff, 13);
EXPECT(r == 11);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "qrsxyz01234");
}
EXPECT(ph.position() == Offset(21));
EXPECT_NO_THROW(ph.skip(-2));
EXPECT(ph.position() == Offset(19));
// skipping past the EOF asserts, and leaves position @ EOF
{
EXPECT_THROWS_AS(ph.skip(9), AssertionFailed);
Offset position = ph.position();
EXPECT_EQUAL(position, Offset(21));
Buffer buff = Tester::makeBuffer();
long r = ph.read(buff, 10);
EXPECT(r == 0);
}
// complete read
{
EXPECT_NO_THROW(ph.rewind());
EXPECT(ph.position() == Offset(0));
Buffer buff = Tester::makeBuffer();
long r = ph.read(buff, 40);
EXPECT(r == 21);
std::string read(buff);
std::cout << read << std::endl;
EXPECT_EQUAL(read, "acdghijnopqrsxyz01234");
}
// partial read in the middle and overlapping parts
{
EXPECT_NO_THROW(ph.seek(5));
EXPECT(ph.position() == Offset(5));
Buffer buff = Tester::makeBuffer();
long r = ph.read(buff, 12);
EXPECT(r == 12);
std::string read(buff);
std::cout << read << std::endl;
EXPECT_EQUAL(read, "ijnopqrsxyz0");
}
}
ph.close();
}
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
int main(int argc, char** argv) {
return run_tests(argc, argv);
}
|