File: input.cc

package info (click to toggle)
metview 5.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 614,356 kB
  • sloc: cpp: 560,586; ansic: 44,641; xml: 19,933; f90: 17,984; sh: 7,454; python: 5,565; yacc: 2,318; lex: 1,372; perl: 701; makefile: 87
file content (177 lines) | stat: -rw-r--r-- 5,238 bytes parent folder | download
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
/*
 * (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 <memory>
#include <vector>

#include "eckit/testing/Test.h"

#include "mir/data/MIRField.h"
#include "mir/input/MultiDimensionalInput.h"
#include "mir/util/Exceptions.h"
#include "mir/util/Log.h"


namespace mir::tests::unit {


struct TestingInput : input::MIRInput {
    const param::MIRParametrisation& parametrisation(size_t /*which*/) const override { NOTIMP; }
    bool sameAs(const MIRInput& /*unused*/) const override { return false; }
    data::MIRField field() const override { NOTIMP; }
    void print(std::ostream& /*unused*/) const override { NOTIMP; }
};


struct TestingMultiDimensionalInput final : TestingInput {
    TestingMultiDimensionalInput(size_t Nfields, size_t Ndim) {
        for (size_t n = Nfields; n > 0; n -= Ndim) {
            if (n < Ndim) {
                dims_.insert(dims_.begin(), n);
                break;
            }
            dims_.push_back(Ndim);
        }
        dims_.push_back(0);
    }

private:
    bool next() override {
        dims_.pop_back();
        return !dims_.empty();
    }

    size_t dimensions() const override {
        ASSERT(!dims_.empty());
        return dims_.back();
    }

    std::vector<size_t> dims_;
};


struct TestingSingleDimensionalInput final : TestingInput {
    TestingSingleDimensionalInput(size_t Nfields) : Nfields_(Nfields + 1) {}

private:
    bool next() override { return --Nfields_ != 0; }
    size_t dimensions() const override { return 1; }
    size_t Nfields_;
};


CASE("MultiDimensionalInput") {


    SECTION("MultiDimensionalInput::next(), dimensions()") {
        const std::vector<size_t> _numberOfFields{1, 2, 3, 4, 5};
        const std::vector<size_t> _numberOfDimensions{1, 2, 3};

        for (size_t numberOfFields : _numberOfFields) {
            for (size_t numberOfDimensions : _numberOfDimensions) {
                auto numberOfNext = (numberOfFields - 1) / numberOfDimensions + 1;
                ASSERT(numberOfNext >= 1);

                Log::info() << "Test numberOfFields/numberOfDimensions = numberOfNext => " << numberOfFields << " / "
                            << numberOfDimensions << " = " << numberOfNext << std::endl;

                std::unique_ptr<input::MIRInput> input(
                    new TestingMultiDimensionalInput(numberOfFields, numberOfDimensions));

                size_t Nfields = 0;
                size_t Nnext   = 0;
                for (; input->next(); Nnext++) {
                    auto Ndims = input->dimensions();
                    Log::info() << "\tnext() => dimensions() = " << Ndims << std::endl;

                    Nfields += Ndims;

                    auto numberOfDims =
                        Nfields == numberOfFields ? (numberOfFields - 1) % numberOfDimensions + 1 : numberOfDimensions;

                    EXPECT(numberOfFields >= Nfields);
                    EXPECT(numberOfDims == Ndims);
                }

                EXPECT(numberOfFields == Nfields);
                EXPECT(numberOfNext == Nnext);
            }
        }
    }


    SECTION("MultiDimensionalInput empty") {
        std::unique_ptr<input::MIRInput> input(new input::MultiDimensionalInput);
        EXPECT(!input->next());
    }


    SECTION("MultiDimensionalInput::append() different-sized fields") {
        std::unique_ptr<input::MIRInput> input([]() {
            auto* multi = new input::MultiDimensionalInput;
            ASSERT(multi != nullptr);

            multi->append(new TestingSingleDimensionalInput(4));
            multi->append(new TestingSingleDimensionalInput(2));
            multi->append(new TestingSingleDimensionalInput(4));

            return multi;
        }());

        EXPECT(input->next());
        EXPECT(input->dimensions() == 3);

        EXPECT(input->next());
        EXPECT(input->dimensions() == 3);

        EXPECT(input->next());
        EXPECT(input->dimensions() == 2);

        EXPECT(input->next());
        EXPECT(input->dimensions() == 2);

        EXPECT(!input->next());
    }


    SECTION("MultiDimensionalInput::append() include empty-sized fields") {
        std::unique_ptr<input::MIRInput> input([]() {
            auto* multi = new input::MultiDimensionalInput;
            ASSERT(multi != nullptr);

            multi->append(new TestingSingleDimensionalInput(2));
            multi->append(new TestingSingleDimensionalInput(0));
            multi->append(new TestingSingleDimensionalInput(3));

            return multi;
        }());

        EXPECT(input->next());
        EXPECT(input->dimensions() == 2);

        EXPECT(input->next());
        EXPECT(input->dimensions() == 2);

        EXPECT(input->next());
        EXPECT(input->dimensions() == 1);

        EXPECT(!input->next());
    }
}


}  // namespace mir::tests::unit


int main(int argc, char** argv) {
    return eckit::testing::run_tests(argc, argv);
}