OOPS
test/base/Variables.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #ifndef TEST_BASE_VARIABLES_H_
12 #define TEST_BASE_VARIABLES_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
19 
20 #include "eckit/config/LocalConfiguration.h"
21 #include "eckit/testing/Test.h"
22 #include "oops/base/Variables.h"
23 #include "oops/runs/Test.h"
24 #include "test/base/Fortran.h"
25 #include "test/TestEnvironment.h"
26 
27 namespace test {
28 
29 // -----------------------------------------------------------------------------
30 
31 void testConstructor() {
32  std::unique_ptr<oops::Variables> vars(new oops::Variables());
33  EXPECT(vars.get());
34 
35  const std::vector<std::string> varnames{"bt", "emiss"};
36  const std::vector<int> channels{1, 2, 3, 4};
37  std::unique_ptr<oops::Variables> other(new oops::Variables(varnames, channels));
38  EXPECT(other.get());
39 
40  vars.reset();
41  EXPECT(!vars.get());
42 
43  // Test bad construction from eckit::Configuration
44  EXPECT_THROWS_AS(auto bad_vars = oops::Variables(TestEnvironment::config(), "FOO"),
45  eckit::BadParameter);
46 }
47 
48 // -----------------------------------------------------------------------------
49 
51  eckit::LocalConfiguration conf(TestEnvironment::config());
52  std::unique_ptr<oops::Variables> vars(new oops::Variables(conf, "test variables"));
53  EXPECT(vars.get());
54 
55  std::unique_ptr<oops::Variables> other(new oops::Variables(*vars));
56  EXPECT(other.get());
57 
58  other.reset();
59  EXPECT(!other.get());
60 
61  EXPECT(vars.get());
62 }
63 
64 // -----------------------------------------------------------------------------
65 
67  oops::Variables vars;
68 
70 
71  // test content of variable list
72  std::vector<std::string> vars_check = TestEnvironment::config().getStringVector("test variables");
73 
74  // The fortran routine tests the push_back_vector method, with the variables
75  // from the config file, as well as the push of a single variable name.
76  // So, if both were successful, vars should contain one extra item in
77  // the variable list
78  EXPECT(vars.size() == vars_check.size()+1);
79 
80  for (std::size_t jvar = 0; jvar < vars_check.size(); ++jvar) {
81  EXPECT(vars[jvar] == vars_check[jvar]);
82  }
83 }
84 
85 // -----------------------------------------------------------------------------
86 
87 class Variables : public oops::Test {
88  public:
89  Variables() {}
90  virtual ~Variables() {}
91  private:
92  std::string testid() const override {return "test::Variables";}
93 
94  void register_tests() const override {
95  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
96 
97  ts.emplace_back(CASE("Variables/testConstructor")
98  { testConstructor(); });
99  ts.emplace_back(CASE("Variables/testCopyConstructor")
100  { testCopyConstructor(); });
101  ts.emplace_back(CASE("Variables/testFortranInterface")
102  { testFortranInterface(); });
103  }
104 
105  void clear() const override {}
106 };
107 
108 } // namespace test
109 
110 #endif // TEST_BASE_VARIABLES_H_
test::Variables::~Variables
virtual ~Variables()
Definition: test/base/Variables.h:90
test::testFortranInterface
void testFortranInterface()
Definition: test/base/Variables.h:66
test::testCopyConstructor
void testCopyConstructor()
Definition: test/base/Variables.h:50
test::Variables::testid
std::string testid() const override
Definition: test/base/Variables.h:92
test::Variables::register_tests
void register_tests() const override
Definition: test/base/Variables.h:94
Fortran.h
test::Variables::Variables
Variables()
Definition: test/base/Variables.h:89
test::CASE
CASE("test_linearmodelparameterswrapper_valid_name")
Definition: LinearModelFactory.cc:22
test
Definition: LinearModelFactory.cc:20
oops::Variables::size
size_t size() const
Definition: oops/base/Variables.h:37
Test.h
test::TestEnvironment::config
static const eckit::Configuration & config()
Definition: TestEnvironment.h:40
test::Variables::clear
void clear() const override
Definition: test/base/Variables.h:105
TestEnvironment.h
test::testConstructor
void testConstructor()
Tests creation and destruction of ObsErrorCovariances.
Definition: test/base/ObsErrorCovariance.h:32
test::Variables
Definition: test/base/Variables.h:87
oops::Test
Definition: Test.h:39
oops::Variables
Definition: oops/base/Variables.h:23
test::test_vars_interface_f
void test_vars_interface_f(const eckit::Configuration &, oops::Variables &)
This is intended as a general interface for testing objects in the util namespace from Fortran.
Variables.h