test_utils.cpp 15.2 KB
Newer Older
1
// Copyright 2017-2021, Schlumberger
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Based on Salmon/Shared/TestUtils/TempFileAutoDelete.cpp
// And wrapper/test_utils.py

#include "test_utils.h"
Paal Kvamme's avatar
Paal Kvamme committed
19
#include "test_all.h"
20
21
22
23
24
25
26
27
28
29
30
#include "../impl/environment.h"

#include <sstream>
#include <chrono>
#include <algorithm>
#include <random>
#include <functional>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
31
#include <thread>
32
#include <iostream>
Paal Kvamme's avatar
Paal Kvamme committed
33
#include <numeric>
34
35
36
37
38
39
40
41
42

#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#include <io.h>
#else
#include <unistd.h>
#endif

Paal Kvamme's avatar
Paal Kvamme committed
43
#include "../api.h"
44
45
46
47
48
#include "../iocontext.h"
#include "../exception.h"
#include "../impl/file.h"
#include "../impl/file_sd.h"

49
50
51
52
53
54
55
using InternalZGY::Environment;

namespace Test_Utils {
#if 0
}
#endif

56
TempFileAutoDelete::TempFileAutoDelete(const std::string& name, const OpenZGY::IOContext* ctx)
57
58
  : armed_(true)
  , name_(name)
59
  , context_(ctx ? ctx->clone() : std::shared_ptr<OpenZGY::IOContext>())
60
61
62
63
64
65
{
}

TempFileAutoDelete::~TempFileAutoDelete()
{
  if (armed_ && !name_.empty()) {
66
67
68
69
70
71
72
73
74
75
76
77
78
    try {
      // Code smell, the reason is that I don't want virtuals here.
      if (name_.substr(0,5) == "sd://") {
        CloudFileAutoDelete::remove(name_, dynamic_cast<OpenZGY::SeismicStoreIOContext*>(savedcontext().get()));
      }
      else {
        LocalFileAutoDelete::remove(name_);
      }
      armed_ = false;
    }
    catch (const std::exception& ex) {
      std::cerr << "WARNING: Could not remove temporary file: " << ex.what() << "\n";
    }
79
80
81
  }
}

Paal Kvamme's avatar
Paal Kvamme committed
82
83
84
85
86
/**
 * Return an integer between 0 and 32767 inclusive.
 * Don't use this for anything important.
 * The number generator has very little entropy.
 */
87
88
89
90
91
92
93
94
std::uint32_t
TempFileAutoDelete::myrand()
{
  static std::uint32_t seed = static_cast<std::uint32_t>(time(0));
  seed = seed * 1103515245 + 12345;
  return((unsigned)(seed / 65536) % 32768);
}

Paal Kvamme's avatar
Paal Kvamme committed
95
96
97
98
99
/**
 * Return an integer between 0 and max inclusive.
 * Don't use this for anything important.
 * The number generator has very little entropy.
 */
100
101
102
103
104
105
std::uint32_t
TempFileAutoDelete::myrand(std::uint32_t max)
{
  return myrand() / (32767 / max);
}

106
bool LocalFileAutoDelete::exists(const std::string &filename)
107
108
109
110
111
112
113
114
{
#ifdef WIN32
  return ::GetFileAttributesA(filename.c_str()) != INVALID_FILE_ATTRIBUTES;
#else
  return ::access(filename.c_str(), F_OK) >= 0;
#endif
}

115
void LocalFileAutoDelete::remove(const std::string &filename)
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
{
  //std::cerr << "@ AUTO_DELETE \"" << filename << "\"\n";
  // TODO-Low, print warning on failure. Maybe support throw_on_error.
#ifdef WIN32
  ::DeleteFileA(filename.c_str());
#else
  ::remove(filename.c_str());
#endif
}

std::string
TempFileAutoDelete::join(const std::string& base, const std::string& file)
{
  if (file.empty())
    return file;
  else if (base.empty() || file.front() == '/' || file.front() == '\\')
    return file;
  else if (base.back() == '/' || base.back() == '\\')
    return base + file;
  else {
#ifdef WIN32
    return base + '\\' + file;
#else
    return base + '/' + file;
#endif
  }
}

std::string
TempFileAutoDelete::randomname()
{
Paal Kvamme's avatar
Paal Kvamme committed
147
148
149
150
  // The random number generator is low quality, but a collision
  // in temp file name clash isn't actually an earth shattering
  // bug. At worst some unit test might fail.
  std::uint32_t rnd = myrand();
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  std::stringstream ss;
  ss << std::hex << std::setw(8) << std::setfill('0') << rnd << "-";
  return ss.str();
}

std::string
LocalFileAutoDelete::makePrefix()
{
  std::stringstream ss;
  ss << "tmp-"
     << std::hex << std::setw(8) << std::setfill('0')
     << std::uint32_t(time(nullptr))
     << "-";
  return join(Environment::getStringEnv("TESTRUNDIR", "."), ss.str());
}

std::string
LocalFileAutoDelete::getPrefix()
{
  // Every call will return the same value.
  static const std::string prefix = makePrefix();
  return prefix;
}

175
176
177
178
179
CloudFileAutoDelete::CloudFileAutoDelete(const std::string& suffix, const OpenZGY::SeismicStoreIOContext* ctx)
  : TempFileAutoDelete(getPrefix() + randomname() + suffix, ctx)
{
}

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
std::string
CloudFileAutoDelete::makePrefix()
{
  std::stringstream ss;
  ss << "/tmp-"
     << std::hex << std::setw(8) << std::setfill('0')
     << std::uint32_t(time(nullptr))
     << "-";
  return Environment::getStringEnv("OPENZGY_SDTESTSINK", "sd://sntc/testsink/d") + ss.str();
}

std::string
CloudFileAutoDelete::getPrefix()
{
  // Every call will return the same value.
  static const std::string prefix = makePrefix();
  return prefix;
}

199
200
201
void
CloudFileAutoDelete::remove(const std::string& name, const OpenZGY::SeismicStoreIOContext* ctx)
{
202
#ifdef HAVE_SD
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  // std::cerr << "DELETE FROM CLOUD \"" << name << "\"\n";
  if (!name.empty()) {
    if (ctx) {
      // Using the low level delete functionality;
      // I could also have used OpenZGY::IZgyUtils.
      std::shared_ptr<InternalZGY::FileADT> fd = InternalZGY::FileFactory::instance().create
        (name, InternalZGY::OpenMode::Closed, ctx);
      auto fd_sd = dynamic_cast<InternalZGY::FileUtilsSeismicStore*>(fd.get());
      if (fd_sd) {
        fd_sd->deleteFile(name, /*missing_ok=*/true);
      }
      else {
        throw OpenZGY::Errors::ZgyInternalError("CloudFileAutoDelete::remove founbd no FileUtils");
      }
    }
    else {
      throw OpenZGY::Errors::ZgyInternalError("CloudFileAutoDelete::remove requires a context");
    }
  }
222
#endif
223
224
}

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
 * \brief Create a vecor of random values.
 *
 * Is it just me, or is this horribly convoluted compared to the
 * equivalent function in Python?
 */
std::vector<float>
random_vector(std::size_t size)
{
  static auto seed = std::chrono::system_clock::now().time_since_epoch().count();
  static std::default_random_engine generator(static_cast<unsigned>(seed));
  static std::normal_distribution<float> distribution(0.0, 1.0);
  static std::function<float()> rnd = [&](){return distribution(generator);};
  std::vector<float> result(size);
  std::generate(result.begin(), result.end(), rnd);
  return result;
}

243
244
245
246
247
248
249
250
251
252
253
254
std::vector<double>
random_double_vector(std::size_t size)
{
  static auto seed = std::chrono::system_clock::now().time_since_epoch().count();
  static std::default_random_engine generator(static_cast<unsigned>(seed));
  static std::normal_distribution<double> distribution(0.0, 1.0);
  static std::function<double()> rnd = [&](){return distribution(generator);};
  std::vector<double> result(size);
  std::generate(result.begin(), result.end(), rnd);
  return result;
}

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
 * Delay execution of the current thread by approximately the provided
 * number of milliseconds but varying between half and double the
 * specified time.
 */
void
random_delay(int ms)
{
  static auto seed = std::chrono::system_clock::now().time_since_epoch().count();
  static std::default_random_engine generator(static_cast<unsigned>(seed));
  std::uniform_int_distribution<int> distribution(ms/2, ms*2);
  int sleeptime = distribution(generator);
  std::this_thread::sleep_for(std::chrono::milliseconds(sleeptime));
}

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
#ifdef HAVE_SD
/**
 * Convenience to hard code credentials for testing.
 */
const OpenZGY::SeismicStoreIOContext*
default_sd_context()
{
  // Picking up sdurl/sdapikey from the environment is redundant since
  // the library already does this as a fallback.
  using InternalZGY::Environment;
  static OpenZGY::SeismicStoreIOContext instance =
    OpenZGY::SeismicStoreIOContext()
    .sdurl(Environment::getStringEnv("OPENZGY_SDURL"))
    .sdapikey(Environment::getStringEnv("OPENZGY_SDAPIKEY"))
    .sdtoken(Environment::getStringEnv("OPENZGY_TOKEN"), "");
  return &instance;
}
#endif

const OpenZGY::IOContext*
default_context()
{
#ifdef HAVE_SD
  return default_sd_context();
#else
  return nullptr;
#endif
}

Paal Kvamme's avatar
Paal Kvamme committed
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
namespace {
  template<typename T, std::size_t  N>
  std::ostream& operator<<(std::ostream& os, const std::array<T,N>& a)
  {
    os << "[";
    for (std::size_t ii=0; ii<N; ++ii)
      os << a[ii] << (ii == N-1 ? "" : ", ");
    os << "]";
    return os;
  }

  std::ostream& operator<<(std::ostream& os, const OpenZGY::SampleStatistics& in)
  {
    os << "cnt: " << in.cnt
       << " sum: " << in.sum
       << " ssq: " << in.ssq
       << " min: " << in.min
       << " max: " << in.max;
    return os;
  }

  std::ostream& operator<<(std::ostream& os, const OpenZGY::SampleHistogram& in)
  {
    os << "cnt: " << in.samplecount
       << " min: " << in.minvalue
       << " max: " << in.maxvalue
       << " bincount: " << in.bins.size();
    return os;
  }
}

static bool
similar(double a, double b, double eps)
{
  return std::abs(a - b)  <= eps * 0.5 * (std::abs(a) + std::abs(b));
}

static void
compare_stats(const OpenZGY::SampleStatistics& a, const OpenZGY::SampleStatistics& b, double eps)
{
  TEST_CHECK(a.cnt == b.cnt);
  TEST_CHECK(similar(a.sum, b.sum, eps));
  TEST_CHECK(similar(a.ssq, b.ssq, eps));
  TEST_CHECK(similar(a.min, b.min, eps));
  TEST_CHECK(similar(a.max, b.max, eps));
}

static void
compare_histo(const OpenZGY::SampleHistogram& a, const OpenZGY::SampleHistogram& b, double eps)
{
  // Consistency checks done on both histograms in turn.
  // The histogram on file stores sample_count. If there are no NaN, Inf,
  // or outside-range this should match the sum of all bins.
  const std::int64_t acount =
    std::accumulate(a.bins.begin(), a.bins.end(), std::int64_t(0));
  const std::int64_t bcount =
    std::accumulate(b.bins.begin(), b.bins.end(), std::int64_t(0));
  TEST_CHECK(a.samplecount == acount);
  TEST_CHECK(b.samplecount == bcount);
  // Now compare the two histograms.
  TEST_CHECK(a.bins.size() == b.bins.size());
  TEST_CHECK(a.samplecount == b.samplecount);
  TEST_CHECK(similar(a.minvalue, b.minvalue, eps));
  TEST_CHECK(similar(a.maxvalue, b.maxvalue, eps));
  for (std::size_t ii = 0; ii < std::min(a.bins.size(), b.bins.size()); ++ii) {
    TEST_CHECK(a.bins[ii] == b.bins[ii]);
    if (verbose())
      std::cout << "[" << ii << "] "
                << std::setw(8) << a.bins[ii] << " "
                << std::setw(8) << b.bins[ii] << " "
                << std::setw(8) << (a.bins[ii] - b.bins[ii]) << "\n";
  }
}

/**
 * Compare two files, possibly with differing value types and
 * bricksize. Bulk is compared after conversion to float. Meant to be
 * used for small files only. The entire file will be read into
 * memory. Any sample that is NaN will cause the compare to fail.
 */
void
compare_files(const std::string& a_name, const std::string& b_name, double epsilon, double lodepsilon)
{
  std::shared_ptr<OpenZGY::IZgyReader> a_reader =
    OpenZGY::IZgyReader::open(a_name, Test_Utils::default_context());
  std::shared_ptr<OpenZGY::IZgyReader> b_reader =
    OpenZGY::IZgyReader::open(b_name, Test_Utils::default_context());
  const OpenZGY::IZgyReader& a = *a_reader;
  const OpenZGY::IZgyReader& b = *b_reader;
  std::shared_ptr<const OpenZGY::FileStatistics>a_filestats = a.filestats();
  std::shared_ptr<const OpenZGY::FileStatistics>b_filestats = b.filestats();
  const OpenZGY::IZgyMeta::corners_t a_corners = a.corners();
  const OpenZGY::IZgyMeta::corners_t b_corners = b.corners();

  if (verbose()) {
    std::cout << "Statistics\n"
              << "a: " << a.statistics() << "\nb: " << b.statistics() << "\n";
    std::cout << "Histogram\n"
              << "a: " << a.histogram() << "\nb: " << b.histogram() << "\n";
  }

  TEST_CHECK(a.size() == b.size());
  TEST_CHECK(a.nlods() == b.nlods());
  TEST_CHECK(similar(a.datarange()[0], b.datarange()[0], epsilon));
  TEST_CHECK(similar(a.datarange()[1], b.datarange()[1], epsilon));
  TEST_CHECK(similar(a.annotstart()[0], b.annotstart()[0], 0.01));
  TEST_CHECK(similar(a.annotstart()[1], b.annotstart()[1], 0.01));
  TEST_CHECK(similar(a.annotinc()[0],   b.annotinc()[0], 0.01));
  TEST_CHECK(similar(a.annotinc()[1],b.annotinc()[1], 0.01));
  TEST_CHECK(similar(a.zstart(), b.zstart(), 0.01));
  TEST_CHECK(similar(a.zinc(), b.zinc(),  0.01));
  TEST_CHECK(a.hunitdim()    == b.hunitdim());
  TEST_CHECK(a.hunitname()   == b.hunitname());
  TEST_CHECK(a.hunitfactor() == b.hunitfactor());
  TEST_CHECK(a.zunitdim()    == b.zunitdim());
  TEST_CHECK(a.zunitname()   == b.zunitname());
  TEST_CHECK(a.zunitfactor() == b.zunitfactor());
  for (int corner = 0; corner < 4; ++corner) {
    for (int dim = 0; dim < 2; ++dim) {
      TEST_CHECK(a_corners[corner][dim] - b_corners[corner][dim] <= 3.0);
    }
  }
  if (a_filestats->fileVersion() >= 3 && a_filestats->fileVersion() >= 3) {
    compare_stats(a.statistics(), b.statistics(), epsilon);
    compare_histo(a.histogram(),  b.histogram(), epsilon);
    TEST_CHECK(a_name == b_name || a.verid() != b.verid());
    // Files with an integral type normally have histogram range set to
    // codingrange. I.e. we assume both the extreme minimum and the
    // extreme maximum are in use instead of checking. This might change
    // in the future for int16 files, so a failure here might not mean
    // there is anything wrong.
    if (a.datatype() != OpenZGY::SampleDataType::float32) {
      TEST_CHECK(similar(a.datarange()[0], a.histogram().minvalue, 1.0e-5));
      TEST_CHECK(similar(a.datarange()[1], a.histogram().maxvalue, 1.0e-5));
    }
    if (b.datatype() != OpenZGY::SampleDataType::float32) {
      TEST_CHECK(similar(b.datarange()[0], b.histogram().minvalue, 1.0e-5));
      TEST_CHECK(similar(b.datarange()[1], b.histogram().maxvalue, 1.0e-5));
    }
  }
  if (a.size() == b.size()) {
    const std::int64_t count = a.size()[0] * a.size()[1] * a.size()[2];
    const std::int32_t nlods = std::min(a.nlods(), b.nlods());
    const float nan = std::numeric_limits<float>::quiet_NaN();
    const std::array<std::int64_t,3> orig{0,0,0};
Paal Kvamme's avatar
Paal Kvamme committed
444
445
    std::unique_ptr<float[]> a_data(new float[count]);
    std::unique_ptr<float[]> b_data(new float[count]);
Paal Kvamme's avatar
Paal Kvamme committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
    std::fill(a_data.get(), a_data.get() + count, nan);
    std::fill(b_data.get(), b_data.get() + count, nan);
    float *aptr = a_data.get();
    float *bptr = b_data.get();
    std::array<std::int64_t,3> size{
      std::min(a.size()[0], b.size()[0]),
      std::min(a.size()[1], b.size()[1]),
      std::min(a.size()[2], b.size()[2]),
    };
    for (int lod=0; lod<nlods; ++lod) {
      a.read(orig, size, aptr, lod);
      b.read(orig, size, bptr, lod);
      float worst = -1;
      bool reported_error = false;
      for (int ii=0; ii<count; ++ii) {
        worst = std::max(worst, std::abs(aptr[ii] - bptr[ii]));
        if (!reported_error) {
          TEST_CHECK(similar(aptr[ii], bptr[ii], (lod?lodepsilon:epsilon)));
          if (!similar(aptr[ii], bptr[ii], (lod?lodepsilon:epsilon))) {
            std::cout << "MISMATCH at offset " << ii
                      << " lod: " << lod
                      << ", a=" << aptr[ii]
                      << " != b=" << bptr[ii]
                      << "\n";
            // avoid flooding the log with failures.
            reported_error = true;
          }
        }
      }
      if (verbose()) {
            std::cout << "Most noise in lod " << lod
                      << " is " << worst
                      << "\n";
      }
      for (int ii=0; ii<3; ++ii)
        size[ii] = (size[ii]+1)/2;
    }
  }
}

486
}