OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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
24namespace {
25
26 // The fixture for testing class Foo.
27 class CmdArgumentsTest : public ::testing::Test {
28 protected:
29
30 CmdArgumentsTest() {
31 // You can do set-up work for each test here.
32 int argc = 3;
33 char exe_name[] = "test";
34 char a1[] = "--arg1=val1";
35 char a2[] = "--arg2=2.2";
36 char *argv[] = { exe_name, a1, a2 };
37
38 args_.reset(new CmdArguments(argc, argv));
39 }
40
41 virtual ~CmdArgumentsTest() {
42 // You can do clean-up work that doesn't throw exceptions here.
43 }
44
45 // If the constructor and destructor are not enough for setting up
46 // and cleaning up each test, you can define the following methods:
47
48 virtual void SetUp() {
49 // Code here will be called immediately after the constructor (right
50 // before each test).
51 }
52
53 virtual void TearDown() {
54 // Code here will be called immediately after each test (right
55 // before the destructor).
56 }
57
58 // Objects declared here can be used by all tests in the test case
59 std::unique_ptr<CmdArguments> args_;
60 };
61
62 TEST_F(CmdArgumentsTest, RetrieveCorrectFatal) {
63
64 std::string arg1 = args_->getArg<std::string>("arg1", true);
65 double arg2 = args_->getArg<double>("arg2", true);
66
67 EXPECT_EQ("val1", arg1) << "first argument string value wrong";
68 EXPECT_EQ(2.2, arg2) << "second argument double value wrong";
69 }
70
71 TEST_F(CmdArgumentsTest, ThrowOnNotPresentFatal) {
72
73 EXPECT_ANY_THROW(
74 args_->getArg<std::string>("arg11", true)
75 );
76 }
77
78 TEST_F(CmdArgumentsTest, CorrectDefaultIfNotPresent) {
79
80 double arg = args_->getArg<double>("arg22", 10.0, false);
81 EXPECT_EQ(10.0, arg) << "second argument double value wrong";
82 }
83
84}
85
86int main(int argc, char **argv) {
87 ::testing::InitGoogleTest(&argc, argv);
88 return RUN_ALL_TESTS();
89}
arg(a))
int main(int argc, char **argv)