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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/*
* (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";
const char buf2[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ56789";
class Tester {
public:
Tester() {
std::string base = Resource<std::string>("$TMPDIR", "/tmp");
path1_ = PathName::unique(base + "/path1");
path1_ += ".dat";
path2_ = PathName::unique(base + "/path2");
path2_ += ".dat";
path3_ = PathName::unique(base + "/path3");
path3_ += ".dat";
{
FileHandle f1(path1_);
f1.openForWrite(0);
f1.write(buf1, sizeof(buf1) - 1);
f1.close();
std::cout << "created: " << path1_ << std::endl;
}
{
FileHandle f2(path2_);
f2.openForWrite(0);
f2.write(buf2, sizeof(buf2) - 1);
f2.write(buf2, sizeof(buf2) - 1);
f2.close();
std::cout << "created: " << path2_ << std::endl;
}
{
FileHandle f3(path3_);
f3.openForWrite(0);
f3.write(buf1, sizeof(buf1) - 1);
f3.write(buf1, sizeof(buf1) - 1);
f3.write(buf1, sizeof(buf1) - 1);
f3.close();
std::cout << "created: " << path3_ << std::endl;
}
}
~Tester() {
bool verbose = false;
path1_.unlink(verbose);
path2_.unlink(verbose);
path3_.unlink(verbose);
}
static Buffer makeBuffer() {
Buffer buffer(128);
buffer.zero();
return buffer;
}
PathName path1_;
PathName path2_;
PathName path3_;
};
//----------------------------------------------------------------------------------------------------------------------
CASE("Multihandle") {
format(std::cout, Log::fullFormat);
Tester test;
OffsetList ol0 = {};
LengthList ll0 = {};
OffsetList ol1 = {0, 2, 6, 13, 23};
LengthList ll1 = {1, 2, 4, 6, 8};
SECTION("PartFileHandle compress") {
char expect[] = "aAbcBCdefgDEFGhijklmnoHIJKLMNOpqrstuvwxyz01234PQRSTUVWXYZ56789";
MultiHandle mh1;
{
for (int i = 0; i < 5; i++) {
mh1 += new PartFileHandle(test.path1_, (1 << i) - 1, 1 << i);
mh1 += new PartFileHandle(test.path2_, (1 << i) - 1, 1 << i);
}
// std::cout << mh1 << std::endl;
EXPECT(mh1.size() == Length(62));
mh1.compress();
// std::cout << mh1 << std::endl;
EXPECT(mh1.size() == Length(62));
}
MemoryHandle result(128);
EXPECT_NO_THROW(mh1.saveInto(result));
EXPECT(::memcmp(expect, result.data(), strlen(expect)) == 0);
}
SECTION("Seek/skip/position in empty MultiHandle") {
MultiHandle mh0;
Length size = mh0.size();
EXPECT_EQUAL(size, Length(0));
mh0.openForRead();
EXPECT_NO_THROW(mh0.seek(0));
{
Buffer buff = Tester::makeBuffer();
long r = mh0.read(buff, 10);
EXPECT(r == 0);
}
EXPECT_THROWS_AS(mh0.seek(1), AssertionFailed); // seeking beoynd EOF asserts
{
Buffer buff = Tester::makeBuffer();
long r = mh0.read(buff, 10);
EXPECT(r == 0);
}
EXPECT(mh0.position() == Offset(0));
EXPECT_THROWS_AS(mh0.skip(4), AssertionFailed); // skipping beoynd EOF asserts
EXPECT(mh0.position() == Offset(0));
}
SECTION("Clinical case of empty PartFileHandles making a MultiHandle") {
MultiHandle mh1;
mh1 += new PartFileHandle(test.path1_, ol0, ll0);
mh1 += new MemoryHandle(0);
mh1 += new PartFileHandle(test.path3_, ol0, ll0);
mh1.openForRead();
EXPECT_NO_THROW(mh1.seek(0));
{
Buffer buff = Tester::makeBuffer();
long r = mh1.read(buff, 10);
EXPECT(r == 0);
}
EXPECT_THROWS_AS(mh1.seek(1), AssertionFailed); // seeking beoynd EOF asserts
{
Buffer buff = Tester::makeBuffer();
long r = mh1.read(buff, 10);
EXPECT(r == 0);
}
EXPECT(mh1.position() == Offset(0));
EXPECT_THROWS_AS(mh1.skip(4), AssertionFailed); // skipping beoynd EOF asserts
EXPECT(mh1.position() == Offset(0));
}
SECTION("MultiHandle of PartFileHandles") {
MultiHandle mh2;
mh2 += new PartFileHandle(test.path1_, ol1, ll1);
mh2 += new MemoryHandle(0);
mh2 += new PartFileHandle(test.path3_, ol0, ll0);
mh2.openForRead();
EXPECT_NO_THROW(mh2.seek(21));
{
Buffer buff = Tester::makeBuffer();
long r = mh2.read(buff, 10);
EXPECT(r == 0);
}
}
SECTION("MultiHandle of MemoryHandle") {
MultiHandle mh3;
mh3 += new MemoryHandle(100);
mh3 += new MemoryHandle(100);
EXPECT_THROWS(mh3.seek(0)); // MultiHandle is not open in read mode
EXPECT_THROWS(mh3.openForWrite(10)); // MultiHandle cannot be opened in write mode
MultiHandle mh;
mh += new PartFileHandle(test.path1_, ol1, ll1);
// 0 1 2 3
// 0123456789012345678901234567890
// abcdefghijklmnopqrstuvwxyz01234
// = == ==== ====== ========
// a cd ghij nopqrs xyz01234
OffsetList ol2 = {2, 4, 8, 16, 32};
LengthList ll2 = {1, 2, 4, 8, 16};
mh += new PartFileHandle(test.path2_, ol2, ll2);
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901234567890123456789012345678901
// ABCDEFGHIJKLMNOPQRSTUVWXYZ56789ABCDEFGHIJKLMNOPQRSTUVWXYZ56789
// = == ==== ======== ================
// C EF IJKL QRSTUVWX BCDEFGHIJKLMNOPQ
OffsetList ol3 = {2, 4, 8, 16, 32, 64};
LengthList ll3 = {1, 2, 4, 8, 16, 29};
mh += new PartFileHandle(test.path3_, ol3, ll3);
// 0 1 2 3 4 5 6 7 8 9
// 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
// abcdefghijklmnopqrstuvwxyz01234abcdefghijklmnopqrstuvwxyz01234abcdefghijklmnopqrstuvwxyz01234
// = == ==== ======== ================ =============================
// c ef ijkl qrstuvwx bcdefghijklmnopq cdefghijklmnopqrstuvwxyz01234
// 1 1
// 0 1 2 3 4 5 6 7 8 9 0 1
// 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
// Unused variable expect[]
// char expect[] =
// "acdghijnopqrsxyz01234CEFIJKLQRSTUVWXBCDEFGHIJKLMNOPQcefijklqrstuvwxbcdefghijklmnopqcdefghijklmnopqrstuvwxy"
// "z01234";
mh.openForRead();
EXPECT(mh.size() == Length(112));
EXPECT_NO_THROW(mh.seek(10));
EXPECT(mh.position() == Offset(10));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 13);
EXPECT(r == 13);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "qrsxyz01234CE");
}
EXPECT(mh.position() == Offset(23));
// move to end of first PartFileHandle
EXPECT_NO_THROW(mh.skip(-2));
EXPECT(mh.position() == Offset(21));
EXPECT_NO_THROW(mh.skip(9));
EXPECT(mh.position() == Offset(30));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 10);
EXPECT(r == 10);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "STUVWXBCDE");
}
EXPECT_NO_THROW(mh.seek(0));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 7);
EXPECT(r == 7);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "acdghij");
}
EXPECT(mh.position() == Offset(7));
EXPECT_NO_THROW(mh.seek(106));
EXPECT(mh.position() == Offset(106));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 10);
EXPECT(r == 6);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "z01234");
}
EXPECT(mh.position() == Offset(112));
EXPECT_THROWS_AS(mh.seek(120), AssertionFailed); // seek beyond EOF throws
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 10);
EXPECT(r == 0);
}
EXPECT(mh.position() == Offset(112));
EXPECT_NO_THROW(mh.skip(-20)); // go back -20 from 112 => 92
EXPECT(mh.position() == Offset(92));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 30);
EXPECT(r == 20);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "lmnopqrstuvwxyz01234");
}
}
SECTION("Multihandle seek in FileHandle") {
MultiHandle mh;
mh += new FileHandle(test.path1_);
mh += new FileHandle(test.path2_);
mh.openForRead();
EXPECT(mh.size() == Length(31 * 3));
EXPECT_NO_THROW(mh.seek(20));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 13);
EXPECT(r == 13);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "uvwxyz01234AB");
}
EXPECT_NO_THROW(mh.seek(35));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 10);
EXPECT(r == 10);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "EFGHIJKLMN");
}
EXPECT_NO_THROW(mh.seek(0));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 7);
EXPECT(r == 7);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "abcdefg");
}
EXPECT_NO_THROW(mh.seek(93 - 6));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 10);
EXPECT(r == 6);
std::cout << std::string(buff) << std::endl;
EXPECT(std::string(buff) == "Z56789");
}
EXPECT_THROWS_AS(mh.seek(100), AssertionFailed); // Seek beyond EOF throws
EXPECT(mh.position() == Offset(31 * 3));
{
Buffer buff = Tester::makeBuffer();
long r = mh.read(buff, 10);
EXPECT(r == 0);
}
}
}
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
int main(int argc, char** argv) {
return run_tests(argc, argv);
}
|