File: test_factory.cc

package info (click to toggle)
metview 5.26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 614,288 kB
  • sloc: cpp: 560,215; ansic: 44,633; xml: 19,933; f90: 17,905; sh: 7,269; python: 5,565; yacc: 2,318; lex: 1,372; perl: 701; makefile: 87
file content (245 lines) | stat: -rw-r--r-- 6,429 bytes parent folder | download | duplicates (2)
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
/*
 * (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 <string>

#include "eckit/memory/Builder.h"
#include "eckit/memory/Factory.h"
#include "eckit/memory/Owned.h"
#include "eckit/testing/Test.h"
#include "eckit/value/Properties.h"


namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

class Base0 {
public:

    using builder_t = BuilderT0<Base0>;

    static std::string className() { return "eckit_test.Base0"; }

    virtual ~Base0() = default;

    virtual std::string foo() const = 0;
};

class A0 : public Base0 {
public:

    static std::string className() { return "eckit_test.A0"; }

    A0() : s1_("A.0") {}

    std::string foo() const override { return className() + "." + s1_; }

private:

    std::string s1_;
};

class B0 : public Base0 {
public:

    static std::string className() { return "eckit_test.B0"; }

    B0() : s2_("B.0") {}

    std::string foo() const override { return className() + "." + s2_; }

private:

    std::string s2_;
};

static const ConcreteBuilderT0<Base0, A0> builder_A0;
static const ConcreteBuilderT0<Base0, B0> builder_B0;

//----------------------------------------------------------------------------------------------------------------------

class Base1 : public Owned {
public:

    using builder_t = BuilderT1<Base1>;
    using ARG1      = const Properties&;

    static std::string className() { return "eckit_test.Base1"; }

    virtual ~Base1() = default;

    virtual std::string foo() const = 0;
};

class A1 : public Base1 {
public:

    static std::string className() { return "eckit_test.A1"; }

    explicit A1(const Properties& p) : s1_(p["mystr"].as<std::string>() + ".1") {}

    std::string foo() const override { return className() + "." + s1_; }

private:

    std::string s1_;
};

class B1 : public Base1 {
public:

    static std::string className() { return "eckit_test.B1"; }

    explicit B1(const Properties& p) : s2_(p["mystr"].as<std::string>() + ".2") {}

    std::string foo() const override { return className() + "." + s2_; }

private:

    std::string s2_;
};

static const ConcreteBuilderT1<Base1, A1> builder_A1;
static const ConcreteBuilderT1<Base1, B1> builder_B1;

//----------------------------------------------------------------------------------------------------------------------

class Base2 : public Owned {
public:

    using builder_t = BuilderT2<Base2>;
    using ARG1      = std::string;
    using ARG2      = int;

    static std::string className() { return "eckit_test.Base2"; }

    virtual ~Base2() = default;

    virtual std::string foo() const = 0;
};

class A2 : public Base2 {
public:

    static std::string className() { return "eckit_test.A2"; }

    A2(std::string s, int i) : s1_(s + "." + std::to_string(i)) {}

    std::string foo() const override { return className() + "." + s1_; }

private:

    std::string s1_;
};

class B2 : public Base2 {
public:

    static std::string className() { return "eckit_test.B2"; }

    B2(std::string s, int i) : s2_(s + "." + std::to_string(i)) {}

    std::string foo() const override { return className() + "." + s2_; }

private:

    std::string s2_;
};

static const ConcreteBuilderT2<Base2, A2> builder_A2;
static const ConcreteBuilderT2<Base2, B2> builder_B2_1;
static const ConcreteBuilderT2<Base2, B2> builder_B2_2("eckit_test.B2.x");

//----------------------------------------------------------------------------------------------------------------------

CASE("test_eckit_memory_factory_0") {
    EXPECT(Factory<Base0>::build_type() == "eckit_test.Base0");
    const auto& factory = Factory<Base0>::instance();

    EXPECT(factory.size() == 2);

    EXPECT(factory.exists("eckit_test.A0"));
    EXPECT(factory.exists("eckit_test.B0"));

    EXPECT(factory.get("eckit_test.A0").name() == "eckit_test.A0");
    EXPECT(factory.get("eckit_test.B0").name() == "eckit_test.B0");

    std::unique_ptr<Base0> p1(factory.get("eckit_test.A0").create());
    std::unique_ptr<Base0> p2(factory.get("eckit_test.B0").create());

    EXPECT(p1);
    EXPECT(p2);

    EXPECT(p1->foo() == "eckit_test.A0.A.0");
    EXPECT(p2->foo() == "eckit_test.B0.B.0");
}

CASE("test_eckit_memory_factory_1") {
    EXPECT(Factory<Base1>::build_type() == "eckit_test.Base1");
    const auto& factory = Factory<Base1>::instance();

    EXPECT(factory.size() == 2);

    EXPECT(factory.exists("eckit_test.A1"));
    EXPECT(factory.exists("eckit_test.B1"));

    EXPECT(factory.get("eckit_test.A1").name() == "eckit_test.A1");
    EXPECT(factory.get("eckit_test.B1").name() == "eckit_test.B1");

    Properties p;
    p.set("mystr", "lolo");

    std::unique_ptr<Base1> p1(factory.get("eckit_test.A1").create(p));
    std::unique_ptr<Base1> p2(factory.get("eckit_test.B1").create(p));

    EXPECT(p1);
    EXPECT(p2);

    EXPECT(p1->foo() == "eckit_test.A1.lolo.1");
    EXPECT(p2->foo() == "eckit_test.B1.lolo.2");
}

CASE("test_eckit_memory_factory_2") {
    EXPECT(Factory<Base2>::build_type() == "eckit_test.Base2");
    const auto& factory = Factory<Base2>::instance();

    EXPECT(factory.size() == 3);

    EXPECT(factory.exists("eckit_test.A2"));
    EXPECT(factory.exists("eckit_test.B2"));

    EXPECT(factory.get("eckit_test.A2").name() == "eckit_test.A2");
    EXPECT(factory.get("eckit_test.B2").name() == "eckit_test.B2");

    std::string s("lolo");

    std::unique_ptr<Base2> p1(factory.get("eckit_test.A2").create(s, 42));
    std::unique_ptr<Base2> p2(factory.get("eckit_test.B2").create(s, 42));
    std::unique_ptr<Base2> p3(factory.get("eckit_test.B2.x").create(s, -42));

    EXPECT(p1);
    EXPECT(p2);
    EXPECT(p3);

    EXPECT(p1->foo() == "eckit_test.A2.lolo.42");
    EXPECT(p2->foo() == "eckit_test.B2.lolo.42");
    EXPECT(p3->foo() == "eckit_test.B2.lolo.-42");
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

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