Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Condition_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
16 
17 namespace Teuchos{
18 
22 TEUCHOS_UNIT_TEST(Teuchos_Conditions, testConditions){
23  //Settin up initial list
24  RCP<ParameterList> testingList = rcp(new ParameterList("Condition Testing List"));
25 
26  /*
27  * Testing for string condition
28  */
29  Array<std::string> validValues(tuple<std::string>("mountain dew", "pepsi", "coke", "fanta"));
30  RCP<StringValidator> stringVali1 = rcp(new StringValidator(validValues));
31 
32  testingList->set("string param", "fanta", "parameter for testing string conditions", stringVali1);
33 
34  StringCondition::ValueList conValues1(tuple<std::string>("pepsi", "coke"));
35  RCP<StringCondition> stringCon1 = rcp( new StringCondition(testingList->getEntryRCP("string param"), conValues1));
36  TEST_ASSERT(!stringCon1->isConditionTrue());
37  testingList->set("string param", "coke");
38  TEST_ASSERT(stringCon1->isConditionTrue());
39 
40  /*
41  * Testing for number condition
42  */
43  testingList->set("double param", 5.0, "parameter for testing number conditions");
44 
45  RCP<NumberCondition<double> > numberCon1 =
46  rcp( new NumberCondition<double>(testingList->getEntryRCP("double param")));
47  TEST_ASSERT(numberCon1->isConditionTrue());
48  testingList->set("double param", -1.0);
49  TEST_ASSERT(!numberCon1->isConditionTrue());
51  RCP<NumberCondition<double> > numberCon2 =
52  rcp( new NumberCondition<double>(testingList->getEntryRCP("double param"), doubleTesterFunc));
53  TEST_ASSERT(!numberCon2->isConditionTrue());
54  testingList->set("double param", 101.0);
55  TEST_ASSERT(numberCon2->isConditionTrue());
56 
57  /*
58  * Testing bool conditions
59  */
60  testingList->set("bool param", true, "parameter for testing bool conditions");
61 
62  RCP<BoolCondition> boolCon1 = rcp( new BoolCondition(testingList->getEntryRCP("bool param")));
63  TEST_ASSERT(boolCon1->isConditionTrue());
64  testingList->set("bool param", false);
65  TEST_ASSERT(!boolCon1->isConditionTrue());
66 
67  /*
68  * Test Not condition
69  */
70  RCP<NotCondition> notCon1 = rcp(new NotCondition(numberCon1));
71  TEST_ASSERT(!notCon1->isConditionTrue());
72  testingList->set("double param", -1.0);
73  TEST_ASSERT(notCon1->isConditionTrue());
74 
75  /*
76  * Test And condition
77  */
78  Condition::ConstConditionList conList1(tuple<RCP<const Condition> >(stringCon1, boolCon1));
79  RCP<AndCondition> andCon1 = rcp(new AndCondition(conList1));
80  TEST_ASSERT(!andCon1->isConditionTrue());
81  testingList->set("bool param", true);
82  TEST_ASSERT(andCon1->isConditionTrue());
83 
84  /*
85  * Testing or condition
86  */
87  testingList->set("bool param", false);
88  RCP<OrCondition> orCon1 = rcp(new OrCondition(conList1));
89  TEST_ASSERT(orCon1->isConditionTrue());
90  testingList->set("string param", "fanta");
91 
92  /*
93  * Testing equal condition
94  */
95  RCP<EqualsCondition> equalsCon1 = rcp(new EqualsCondition(conList1));
96  TEST_ASSERT(equalsCon1->isConditionTrue());
97  testingList->set("bool param", true);
98  TEST_ASSERT(!equalsCon1->isConditionTrue());
99 }
100 
101 //Test getters and setters
102 TEUCHOS_UNIT_TEST(Teuchos_Conditions, testConditionGetterAndSetters){
103  //Settin up initial list
104  RCP<ParameterList> testingList = rcp(new ParameterList("Condition Testing List"));
105 
106  Array<std::string> validValues(tuple<std::string>("mountain dew", "pepsi", "coke", "fanta"));
107  RCP<StringValidator> stringVali1 = rcp(new StringValidator(validValues));
108 
109  testingList->set("string param", "fanta", "parameter for testing string conditions", stringVali1);
110 
111  StringCondition::ValueList conValues1(tuple<std::string>("pepsi", "coke"));
112  RCP<StringCondition> stringCon1 = rcp( new StringCondition(testingList->getEntryRCP("string param"), conValues1));
113  Dependency::ConstParameterEntryList stringParameters = stringCon1->getAllParameters();
114  TEST_ASSERT(stringParameters.size() == 1);
115  TEST_ASSERT(stringParameters.find(testingList->getEntryRCP("string param")) != stringParameters.end());
116 
117  /*
118  * Testing for number condition
119  */
120  testingList->set("double param", 5.0, "parameter for testing number conditions");
121 
122  RCP<NumberCondition<double> > numberCon1 = rcp( new NumberCondition<double>(testingList->getEntryRCP("double param")));
123  Dependency::ConstParameterEntryList numberParameters = numberCon1->getAllParameters();
124  TEST_ASSERT(numberParameters.size() == 1);
125  TEST_ASSERT(numberParameters.find(testingList->getEntryRCP("double param")) != numberParameters.end());
126 
127  /*
128  * Testing bool conditions
129  */
130  testingList->set("bool param", true, "parameter for testing bool conditions");
131 
132  RCP<BoolCondition> boolCon1 = rcp( new BoolCondition(testingList->getEntryRCP("bool param")));
133  Dependency::ConstParameterEntryList boolParameters = boolCon1->getAllParameters();
134  TEST_ASSERT(boolParameters.size() == 1);
135  TEST_ASSERT(boolParameters.find(testingList->getEntryRCP("bool param")) != boolParameters.end());
136 
137  /*
138  * Test Not condition
139  */
140  RCP<NotCondition> notCon1 = rcp(new NotCondition(numberCon1));
141  Dependency::ConstParameterEntryList notParameters = notCon1->getAllParameters();
142  TEST_ASSERT(notParameters.size() == 1);
143  TEST_ASSERT(notParameters.find(testingList->getEntryRCP("double param")) != notParameters.end());
144 
145  /*
146  * Test And condition
147  */
148  Condition::ConstConditionList conList1(tuple<RCP<const Condition> >(stringCon1, boolCon1));
149  RCP<AndCondition> andCon1 = rcp(new AndCondition(conList1));
150  Dependency::ConstParameterEntryList andParameters = andCon1->getAllParameters();
151  TEST_ASSERT(andParameters.size() == 2);
152  TEST_ASSERT(andParameters.find(testingList->getEntryRCP("string param")) != andParameters.end());
153  TEST_ASSERT(andParameters.find(testingList->getEntryRCP("bool param")) != andParameters.end());
154 
155  /*
156  * Testing or condition
157  */
158  RCP<OrCondition> orCon1 = rcp(new OrCondition(conList1));
159  Dependency::ConstParameterEntryList orParameters = orCon1->getAllParameters();
160  TEST_ASSERT(orParameters.size() == 2);
161  TEST_ASSERT(orParameters.find(testingList->getEntryRCP("string param")) != orParameters.end());
162  TEST_ASSERT(orParameters.find(testingList->getEntryRCP("bool param")) != orParameters.end());
163 
164  /*
165  * Testing Equsl condition
166  */
167  Condition::ConstConditionList conList2(tuple<RCP<const Condition> >(numberCon1, boolCon1));
168  RCP<EqualsCondition> equalsCon1 = rcp(new EqualsCondition(conList2));
169  Dependency::ConstParameterEntryList equalsParameters = equalsCon1->getAllParameters();
170  TEST_ASSERT(equalsParameters.size() == 2);
171  TEST_ASSERT(equalsParameters.find(testingList->getEntryRCP("double param")) != equalsParameters.end());
172  TEST_ASSERT(equalsParameters.find(testingList->getEntryRCP("bool param")) != equalsParameters.end());
173 
174  /*
175  * Testing BoolLogicCondition add
176  */
177  equalsCon1->addCondition(orCon1);
178  Dependency::ConstParameterEntryList equalsParameters2 = equalsCon1->getAllParameters();
179  TEST_ASSERT(equalsParameters2.size() == 3);
180  TEST_ASSERT(equalsParameters2.find(testingList->getEntryRCP("string param")) != equalsParameters2.end());
181  TEST_ASSERT(equalsParameters2.find(testingList->getEntryRCP("double param")) != equalsParameters2.end());
182  TEST_ASSERT(equalsParameters2.find(testingList->getEntryRCP("bool param")) != equalsParameters2.end());
183 
184 }
185 
186 //Test that exceptions get thrown when they should.
187 TEUCHOS_UNIT_TEST(Teuchos_Conditions, testConditionException){
188  //Settin up initial list
189  RCP<ParameterList> testingList = rcp(new ParameterList("Condition Testing List"));
190  testingList->set("double param",1.0);
191  testingList->set("string param", "awesome");
192  RCP<ParameterList> testingList2 = rcp(new ParameterList("Condition Testing List"));
193  testingList2->set("bool param", true);
194 
195  TEST_THROW(BoolCondition boolCon1(testingList->getEntryRCP("bool param")), InvalidConditionException);
196  TEST_THROW(StringCondition stringCon1(testingList->getEntryRCP("double param"), "coke"), InvalidConditionException);
197  TEST_THROW(BoolCondition boolCon1(testingList->getEntryRCP("double param")), InvalidConditionException);
202 }
203 
204 } //namespace Teuchos
A Bool Logic Condition that returns the result or perfroming a logical OR on the conditions.
#define TEST_ASSERT(v1)
Assert the given statement is true.
std::set< RCP< const ParameterEntry >, RCPConstComp > ConstParameterEntryList
A list of dependents.
RCP< ParameterEntry > getEntryRCP(const std::string &name)
Retrieves the RCP for an entry with the name name if it exists.
A Bool Logic Condition that returns the result or perfroming a logical AND on the conditions...
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
A Not condition returns the result of performing a logical NOT on a given condition.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method.
A Number Condition is a Parameter Condition that evaluates whether or not a number parameter is great...
A simple function object that subtracts a specififed value from the given arguement in the runFunctio...
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
A String Condition is a Parameter Condition that evaluates whether or not a string parameter has take...
Unit testing support.
A Bool Logic Condition that returns the result or perfroming a logical EQUALS on the conditions...
Dependency::ConstParameterEntryList getAllParameters() const
A list of parameters of arbitrary type.
A simple validator that only allows certain string values to be choosen or simply enforces that a par...
A Bool Condition is a Parameter Condition that evaluates whether or not a Boolean parameter is ture...
bool isConditionTrue() const
Determins whether or not a condition is true.
Smart reference counting pointer class for automatic garbage collection.
Dependency::ConstParameterEntryList getAllParameters() const
Gets all of the parameters that are evaluated in this condition.
void addCondition(RCP< const Condition > toAdd)
Adds a Condition to the list of conditions that will be evaluated by this Bool Logic Condition...
Standard Conditions to be used.