test_iocontext.cpp 4.14 KB
Newer Older
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
// Copyright 2017-2020, Schlumberger
//
// 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.

#include "test_all.h"
#include "../iocontext.h"

#include <iostream>
#include <sstream>
#include <memory>

using namespace OpenZGY;

namespace Test {
  // Friend of class SeismicStoreIOContext.
  class TestIOContext {
  public:
    static void must_throw(const char *expect, const std::function<void()>& fn);
    static void test_defaults();
    static void test_setters();
    static void test_errors();
  };
}

void Test::TestIOContext::must_throw(const char *expect, const std::function<void()>& fn)
{
  try {
    fn();
    if (verbose()) {
      std::cout << "Did not get expected exception \"" << expect << "\"\n";
    }
    TEST_CHECK(false && "Did not get expected exception");
  }
  catch (const OpenZGY::Errors::ZgyError& ex) {
    if (verbose()) {
      if (strstr(ex.what(), expect) != nullptr)
        std::cout << "Got expected exception: " << ex.what() << std::endl;
      else
        std::cout << "Got exception, expected \"" << expect << "\""
                  << ", got \"" << ex.what()<< "\"\n";
    }
    TEST_CHECK(strstr(ex.what(), expect) != nullptr);
  }
}

void Test::TestIOContext::test_defaults()
{
  SeismicStoreIOContext ctx;
  if (verbose())
    std::cout << ctx.toString();
  // Not true if OPENZGY_ variables set.
  //TEST_CHECK(ctx._sdurl == "");
  //TEST_CHECK(ctx._sdapikey == "");
  // Not true if OPENZGY_TOKEN is set.
  //TEST_CHECK(ctx._sdtoken == "");
  TEST_CHECK(ctx._maxsize ==   64 * 1024*1024);
  TEST_CHECK(ctx._maxhole ==    2 * 1024*1024);
  TEST_CHECK(ctx._aligned ==    0 * 1024*1024);
  TEST_CHECK(ctx._segsize == 1024 * 1024*1024);
70
  TEST_CHECK(ctx._segsplit == 8);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  TEST_CHECK(ctx._threads == 1);
  TEST_CHECK(ctx._legaltag == "");
  TEST_CHECK(ctx._writeid == "");
  TEST_CHECK(ctx._seismicmeta == "");
}

void Test::TestIOContext::test_setters()
{
  SeismicStoreIOContext ctx;
  ctx.sdurl("https://seismicstore.com")
    .sdapikey("secret-api-key")
    .sdtoken("algo.payload.signature", "mocktoken")
    .maxsize(42)
    .maxhole(7)
    .aligned(1)
    .segsize(15)
87
    .segsplit(3)
88
89
90
91
92
93
94
95
96
97
98
99
100
    .threads(8)
    .legaltag("illegal")
    .writeid("WID")
    .seismicmeta("{\"foo\": 42}");
  if (verbose())
    std::cout << ctx.toString();
  TEST_CHECK(ctx._sdurl == "https://seismicstore.com");
  TEST_CHECK(ctx._sdapikey.substr(0,3) == "sec");
  TEST_CHECK(ctx._sdtoken.substr(0,3) == "alg");
  TEST_CHECK(ctx._maxsize ==   42 * 1024*1024);
  TEST_CHECK(ctx._maxhole ==    7 * 1024*1024);
  TEST_CHECK(ctx._aligned ==    1 * 1024*1024);
  TEST_CHECK(ctx._segsize ==   15 * 1024*1024);
101
  TEST_CHECK(ctx._segsplit ==  3);
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  TEST_CHECK(ctx._threads == 8);
  TEST_CHECK(ctx._legaltag == "illegal");
  TEST_CHECK(ctx._writeid == "WID");
  TEST_CHECK(ctx._seismicmeta == "{\"foo\": 42}");
  std::string str = ctx.toString();
  TEST_CHECK(str.find("maxsize:  42 MB") != std::string::npos);
}

void Test::TestIOContext::test_errors()
{
  SeismicStoreIOContext ctx;
  must_throw("must be between", [&](){ctx.maxsize(2000);});
  must_throw("must be between", [&](){ctx.maxhole(2001);});
  must_throw("must be between", [&](){ctx.aligned(2000);});
  must_throw("must be between", [&](){ctx.segsize(200000);});
117
  must_throw("must be between", [&](){ctx.segsplit(0);});
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  must_throw("must be between", [&](){ctx.threads(0);});
}

namespace {
  class Register
  {
  public:
    Register()
    {
      register_test("iocontext.defaults",            Test::TestIOContext::test_defaults);
      register_test("iocontext.setters",             Test::TestIOContext::test_setters);
      register_test("iocontext.errors",              Test::TestIOContext::test_errors);
    }
  } dummy;
} // namespace for registration