| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include "stk_util/stk_config.h" |
| 36 |
|
| 37 |
#include "gtest/gtest.h" |
| 38 |
#include "stk_util/util/ParameterList.hpp" // for ParameterList, Type, etc |
| 39 |
#include <stddef.h> // for size_t |
| 40 |
#include <sys/types.h> // for int64_t |
| 41 |
#include <complex> // for complex, operator<<, etc |
| 42 |
#include <exception> // for exception |
| 43 |
#include <iostream> // for basic_ostream::operator<<, etc |
| 44 |
#include <map> // for operator==, etc |
| 45 |
#include <string> // for string, basic_string |
| 46 |
#include <vector> // for vector |
| 47 |
|
| 48 |
namespace |
| 49 |
{ |
| 50 |
TEST(StkUtilTestForDocumentation, Parameters) |
| 51 |
{ |
| 52 |
//BEGINParametersInit |
| 53 |
//+ INITIALIZATION |
| 54 |
std::vector<std::string> expected_name; |
| 55 |
std::vector<stk::util::ParameterType::Type> expected_type; |
| 56 |
|
| 57 |
//+ Scalar values of type double, float, int, int64_t, and string |
| 58 |
double pi = 3.14159; |
| 59 |
float e = 2.71828; |
| 60 |
int answer = 42; |
| 61 |
int64_t big_answer = 42000000000001; |
| 62 |
std::string team_name = "STK Transition Team"; |
| 63 |
|
| 64 |
expected_name.push_back("PI"); |
| 65 |
expected_type.push_back(stk::util::ParameterType::DOUBLE); |
| 66 |
expected_name.push_back("E"); |
| 67 |
expected_type.push_back(stk::util::ParameterType::FLOAT); |
| 68 |
expected_name.push_back("Answer"); |
| 69 |
expected_type.push_back(stk::util::ParameterType::INTEGER); |
| 70 |
expected_name.push_back("Answer_64"); |
| 71 |
expected_type.push_back(stk::util::ParameterType::INT64); |
| 72 |
expected_name.push_back("TeamName"); |
| 73 |
expected_type.push_back(stk::util::ParameterType::STRING); |
| 74 |
|
| 75 |
//+ vector of doubles |
| 76 |
std::vector<double> my_double_vector; |
| 77 |
my_double_vector.push_back(2.78); my_double_vector.push_back(5.30); |
| 78 |
my_double_vector.push_back(6.21); |
| 79 |
expected_name.push_back("some_doubles"); |
| 80 |
expected_type.push_back(stk::util::ParameterType::DOUBLEVECTOR); |
| 81 |
|
| 82 |
//+ vector of floats |
| 83 |
std::vector<float> my_float_vector; |
| 84 |
my_float_vector.push_back(194.0); my_float_vector.push_back(-194.0); |
| 85 |
my_float_vector.push_back(47.0); my_float_vector.push_back(92.0); |
| 86 |
expected_name.push_back("some_floats"); |
| 87 |
expected_type.push_back(stk::util::ParameterType::FLOATVECTOR); |
| 88 |
|
| 89 |
//+ vector of ints |
| 90 |
std::vector<int> ages; |
| 91 |
ages.push_back(55); ages.push_back(49); ages.push_back(21); ages.push_back(19); |
| 92 |
expected_name.push_back("Ages"); |
| 93 |
expected_type.push_back(stk::util::ParameterType::INTEGERVECTOR); |
| 94 |
|
| 95 |
//+ vector of int64_ts |
| 96 |
std::vector<int64_t> ages_64; |
| 97 |
ages_64.push_back(55); ages_64.push_back(49); ages_64.push_back(21); ages_64.push_back(19); |
| 98 |
expected_name.push_back("Ages_64"); |
| 99 |
expected_type.push_back(stk::util::ParameterType::INT64VECTOR); |
| 100 |
|
| 101 |
//+ vector of strings |
| 102 |
std::vector<std::string> names; |
| 103 |
names.push_back("greg"); names.push_back("chloe"); names.push_back("tuffy"); |
| 104 |
names.push_back("liberty"); names.push_back("I have spaces"); |
| 105 |
expected_name.push_back("Names"); |
| 106 |
expected_type.push_back(stk::util::ParameterType::STRINGVECTOR); |
| 107 |
//ENDParametersInit |
| 108 |
|
| 109 |
//BEGINParametersDefine |
| 110 |
//+ Define parameters... |
| 111 |
stk::util::ParameterList params; |
| 112 |
params.set_param("PI", pi); |
| 113 |
params.set_param("E", e); |
| 114 |
params.set_param("Answer", answer); |
| 115 |
params.set_param("Answer_64", big_answer); |
| 116 |
params.set_param("TeamName", team_name); |
| 117 |
params.set_param("some_doubles", my_double_vector); |
| 118 |
params.set_param("some_floats", my_float_vector); |
| 119 |
params.set_param("Ages", ages); |
| 120 |
params.set_param("Ages_64", ages_64); |
| 121 |
params.set_param("Names", names); |
| 122 |
//ENDParametersDefine |
| 123 |
|
| 124 |
//BEGINParametersAccessingValues |
| 125 |
//+ Write parameters to stdout... |
| 126 |
params.write_parameter_list(std::cout); |
| 127 |
|
| 128 |
//+ Access parameters by name... |
| 129 |
size_t num_param = expected_name.size(); |
| 130 |
for (size_t i=0; i < num_param; i++) { |
| 131 |
stk::util::Parameter ¶m = params.get_param(expected_name[i]); |
| 132 |
EXPECT_EQ(param.type, expected_type[i]); |
| 133 |
} |
| 134 |
|
| 135 |
//+ Extract some parameter values if know type: |
| 136 |
std::vector<int> pages = params.get_value<std::vector<int> >("Ages"); |
| 137 |
for (size_t i=0; i < pages.size(); i++) { |
| 138 |
EXPECT_EQ(pages[i], ages[i]); |
| 139 |
} |
| 140 |
|
| 141 |
double my_pi = params.get_value<double>("PI"); |
| 142 |
EXPECT_EQ(my_pi, pi); |
| 143 |
|
| 144 |
//+ Change value of an existing parameter |
| 145 |
params.set_value("Answer", 21); |
| 146 |
|
| 147 |
int new_answer = params.get_value<int>("Answer"); |
| 148 |
EXPECT_EQ(new_answer, 21); |
| 149 |
|
| 150 |
{ |
| 151 |
//+ Access a variable of unknown type... |
| 152 |
stk::util::Parameter ¶m = params.get_param("Answer"); |
| 153 |
double value_as_double = 0.0; |
| 154 |
switch (param.type) { |
| 155 |
case stk::util::ParameterType::DOUBLE: |
| 156 |
value_as_double = param.get_value<double>(); |
| 157 |
break; |
| 158 |
case stk::util::ParameterType::FLOAT: |
| 159 |
value_as_double = static_cast<double>(param.get_value<float>()); |
| 160 |
break; |
| 161 |
case stk::util::ParameterType::INTEGER: |
| 162 |
value_as_double = static_cast<double>(param.get_value<int>()); |
| 163 |
break; |
| 164 |
case stk::util::ParameterType::INT64: |
| 165 |
value_as_double = static_cast<double>(param.get_value<int64_t>()); |
| 166 |
break; |
| 167 |
default: |
| 168 |
std::cerr << "ERROR: I can not convert 'Answers' value to a double\n"; |
| 169 |
break; |
| 170 |
} |
| 171 |
EXPECT_EQ(static_cast<double>(new_answer), value_as_double); |
| 172 |
} |
| 173 |
|
| 174 |
//ENDParametersAccessingValues |
| 175 |
|
| 176 |
//BEGINParametersErrors |
| 177 |
//+ If the requested parameter does not exist, |
| 178 |
//+ an error message is printed to stderr and an invalid |
| 179 |
//+ parameter object is returned |
| 180 |
stk::util::Parameter no_exist = params.get_param("DoesNotExist"); |
| 181 |
EXPECT_EQ(stk::util::ParameterType::INVALID, no_exist.type); |
| 182 |
|
| 183 |
//+ In this method of requesting a parameter, no error |
| 184 |
//+ message is printed if the parameter doesn't exist and |
| 185 |
//+ instead the returned iterator is equal to the end of the |
| 186 |
//+ parameter list. |
| 187 |
stk::util::ParameterMapType::iterator it = params.find("DoesNotExist"); |
| 188 |
EXPECT_TRUE(it == params.end()); |
| 189 |
|
| 190 |
//+ If the value of a non-existant parameter is requested, |
| 191 |
//+ an error message is printed and the value 0 is returned. |
| 192 |
double invalid_value = params.get_value<double>("DoesNotExist"); |
| 193 |
EXPECT_EQ(0.0, invalid_value); |
| 194 |
|
| 195 |
//+ If the parameter types do not match, an error message is |
| 196 |
//+ printed and the value 0 of the requested type is returned. |
| 197 |
int invalid = params.get_value<int>("PI"); |
| 198 |
EXPECT_EQ(0, invalid); |
| 199 |
|
| 200 |
//+ If the parameter types do not match, an error message is |
| 201 |
//+ printed and an empty vector of the requested type is returned. |
| 202 |
std::vector<double> pies = params.get_value<std::vector<double> >("PI"); |
| 203 |
EXPECT_EQ(0u, pies.size()); |
| 204 |
//ENDParametersErrors |
| 205 |
|
| 206 |
//+ The parameter query by name is case-sensitive |
| 207 |
double not_found = params.get_value<double>("pi"); |
| 208 |
EXPECT_EQ(0.0, not_found); |
| 209 |
|
| 210 |
//BEGINUnsupportedTypes |
| 211 |
//+ Adding a parameter of "unsupported" type... |
| 212 |
stk::util::ParameterList more_params; |
| 213 |
std::complex<double> phase(3.14,2.718); |
| 214 |
more_params.set_param("phase", phase); |
| 215 |
|
| 216 |
//+ The print system doesn't know about this type, so will print |
| 217 |
//+ a warning message about unrecognized type. |
| 218 |
more_params.write_parameter_list(std::cout); |
| 219 |
|
| 220 |
//+ However, you can still retrieve the value of the parameter |
| 221 |
//+ if you know what type it is. |
| 222 |
std::complex<double> my_phase = more_params.get_value<std::complex<double> >("phase"); |
| 223 |
EXPECT_EQ(my_phase, phase); |
| 224 |
|
| 225 |
//+ The Parameter class won't help you on determining the type, |
| 226 |
//+ You must know what it is. |
| 227 |
EXPECT_EQ(more_params.get_param("phase").type, stk::util::ParameterType::INVALID); |
| 228 |
|
| 229 |
//+ If the wrong type is specified, an exception will be thrown... |
| 230 |
EXPECT_ANY_THROW(more_params.get_value<std::complex<int> >("phase")); |
| 231 |
//ENDUnsupportedTypes |
| 232 |
} |
| 233 |
} |
| 234 |
|