OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
CmdArgumentsTest.cpp
Go to the documentation of this file.
1 //
2 // Test CmdArgumentsTest
3 //
4 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
5 // All rights reserved
6 //
7 // Implemented as part of the PhD thesis
8 // "Toward massively parallel multi-objective optimization with application to
9 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
21 #include "Util/CmdArguments.h"
22 #include "gtest/gtest.h"
23 #include "boost/smart_ptr.hpp"
24 
25 namespace {
26 
27  // The fixture for testing class Foo.
28  class CmdArgumentsTest : public ::testing::Test {
29  protected:
30 
31  CmdArgumentsTest() {
32  // You can do set-up work for each test here.
33  int argc = 3;
34  char exe_name[] = "test";
35  char a1[] = "--arg1=val1";
36  char a2[] = "--arg2=2.2";
37  char *argv[] = { exe_name, a1, a2 };
38 
39  args_.reset(new CmdArguments(argc, argv));
40  }
41 
42  virtual ~CmdArgumentsTest() {
43  // You can do clean-up work that doesn't throw exceptions here.
44  }
45 
46  // If the constructor and destructor are not enough for setting up
47  // and cleaning up each test, you can define the following methods:
48 
49  virtual void SetUp() {
50  // Code here will be called immediately after the constructor (right
51  // before each test).
52  }
53 
54  virtual void TearDown() {
55  // Code here will be called immediately after each test (right
56  // before the destructor).
57  }
58 
59  // Objects declared here can be used by all tests in the test case
60  boost::scoped_ptr<CmdArguments> args_;
61  };
62 
63  TEST_F(CmdArgumentsTest, RetrieveCorrectFatal) {
64 
65  std::string arg1 = args_->getArg<std::string>("arg1", true);
66  double arg2 = args_->getArg<double>("arg2", true);
67 
68  EXPECT_EQ("val1", arg1) << "first argument string value wrong";
69  EXPECT_EQ(2.2, arg2) << "second argument double value wrong";
70  }
71 
72  TEST_F(CmdArgumentsTest, ThrowOnNotPresentFatal) {
73 
74  EXPECT_ANY_THROW(
75  args_->getArg<std::string>("arg11", true)
76  );
77  }
78 
79  TEST_F(CmdArgumentsTest, CorrectDefaultIfNotPresent) {
80 
81  double arg = args_->getArg<double>("arg22", 10.0, false);
82  EXPECT_EQ(10.0, arg) << "second argument double value wrong";
83  }
84 
85 }
86 
87 int main(int argc, char **argv) {
88  ::testing::InitGoogleTest(&argc, argv);
89  return RUN_ALL_TESTS();
90 }
arg(a))
int main(int argc, char **argv)