Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Condition_UnitTests.cpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Teuchos: Common Tools Package
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
48
49namespace Teuchos{
50
54TEUCHOS_UNIT_TEST(Teuchos_Conditions, testConditions){
55 //Settin up initial list
56 RCP<ParameterList> testingList = rcp(new ParameterList("Condition Testing List"));
57
58 /*
59 * Testing for string condition
60 */
61 Array<std::string> validValues(tuple<std::string>("mountain dew", "pepsi", "coke", "fanta"));
62 RCP<StringValidator> stringVali1 = rcp(new StringValidator(validValues));
63
64 testingList->set("string param", "fanta", "parameter for testing string conditions", stringVali1);
65
66 StringCondition::ValueList conValues1(tuple<std::string>("pepsi", "coke"));
67 RCP<StringCondition> stringCon1 = rcp( new StringCondition(testingList->getEntryRCP("string param"), conValues1));
68 TEST_ASSERT(!stringCon1->isConditionTrue());
69 testingList->set("string param", "coke");
70 TEST_ASSERT(stringCon1->isConditionTrue());
71
72 /*
73 * Testing for number condition
74 */
75 testingList->set("double param", 5.0, "parameter for testing number conditions");
76
77 RCP<NumberCondition<double> > numberCon1 =
78 rcp( new NumberCondition<double>(testingList->getEntryRCP("double param")));
79 TEST_ASSERT(numberCon1->isConditionTrue());
80 testingList->set("double param", -1.0);
81 TEST_ASSERT(!numberCon1->isConditionTrue());
83 RCP<NumberCondition<double> > numberCon2 =
84 rcp( new NumberCondition<double>(testingList->getEntryRCP("double param"), doubleTesterFunc));
85 TEST_ASSERT(!numberCon2->isConditionTrue());
86 testingList->set("double param", 101.0);
87 TEST_ASSERT(numberCon2->isConditionTrue());
88
89 /*
90 * Testing bool conditions
91 */
92 testingList->set("bool param", true, "parameter for testing bool conditions");
93
94 RCP<BoolCondition> boolCon1 = rcp( new BoolCondition(testingList->getEntryRCP("bool param")));
95 TEST_ASSERT(boolCon1->isConditionTrue());
96 testingList->set("bool param", false);
97 TEST_ASSERT(!boolCon1->isConditionTrue());
98
99 /*
100 * Test Not condition
101 */
102 RCP<NotCondition> notCon1 = rcp(new NotCondition(numberCon1));
103 TEST_ASSERT(!notCon1->isConditionTrue());
104 testingList->set("double param", -1.0);
105 TEST_ASSERT(notCon1->isConditionTrue());
106
107 /*
108 * Test And condition
109 */
110 Condition::ConstConditionList conList1(tuple<RCP<const Condition> >(stringCon1, boolCon1));
111 RCP<AndCondition> andCon1 = rcp(new AndCondition(conList1));
112 TEST_ASSERT(!andCon1->isConditionTrue());
113 testingList->set("bool param", true);
114 TEST_ASSERT(andCon1->isConditionTrue());
115
116 /*
117 * Testing or condition
118 */
119 testingList->set("bool param", false);
120 RCP<OrCondition> orCon1 = rcp(new OrCondition(conList1));
121 TEST_ASSERT(orCon1->isConditionTrue());
122 testingList->set("string param", "fanta");
123
124 /*
125 * Testing equal condition
126 */
127 RCP<EqualsCondition> equalsCon1 = rcp(new EqualsCondition(conList1));
128 TEST_ASSERT(equalsCon1->isConditionTrue());
129 testingList->set("bool param", true);
130 TEST_ASSERT(!equalsCon1->isConditionTrue());
131}
132
133//Test getters and setters
134TEUCHOS_UNIT_TEST(Teuchos_Conditions, testConditionGetterAndSetters){
135 //Settin up initial list
136 RCP<ParameterList> testingList = rcp(new ParameterList("Condition Testing List"));
137
138 Array<std::string> validValues(tuple<std::string>("mountain dew", "pepsi", "coke", "fanta"));
139 RCP<StringValidator> stringVali1 = rcp(new StringValidator(validValues));
140
141 testingList->set("string param", "fanta", "parameter for testing string conditions", stringVali1);
142
143 StringCondition::ValueList conValues1(tuple<std::string>("pepsi", "coke"));
144 RCP<StringCondition> stringCon1 = rcp( new StringCondition(testingList->getEntryRCP("string param"), conValues1));
145 Dependency::ConstParameterEntryList stringParameters = stringCon1->getAllParameters();
146 TEST_ASSERT(stringParameters.size() == 1);
147 TEST_ASSERT(stringParameters.find(testingList->getEntryRCP("string param")) != stringParameters.end());
148
149 /*
150 * Testing for number condition
151 */
152 testingList->set("double param", 5.0, "parameter for testing number conditions");
153
154 RCP<NumberCondition<double> > numberCon1 = rcp( new NumberCondition<double>(testingList->getEntryRCP("double param")));
155 Dependency::ConstParameterEntryList numberParameters = numberCon1->getAllParameters();
156 TEST_ASSERT(numberParameters.size() == 1);
157 TEST_ASSERT(numberParameters.find(testingList->getEntryRCP("double param")) != numberParameters.end());
158
159 /*
160 * Testing bool conditions
161 */
162 testingList->set("bool param", true, "parameter for testing bool conditions");
163
164 RCP<BoolCondition> boolCon1 = rcp( new BoolCondition(testingList->getEntryRCP("bool param")));
165 Dependency::ConstParameterEntryList boolParameters = boolCon1->getAllParameters();
166 TEST_ASSERT(boolParameters.size() == 1);
167 TEST_ASSERT(boolParameters.find(testingList->getEntryRCP("bool param")) != boolParameters.end());
168
169 /*
170 * Test Not condition
171 */
172 RCP<NotCondition> notCon1 = rcp(new NotCondition(numberCon1));
173 Dependency::ConstParameterEntryList notParameters = notCon1->getAllParameters();
174 TEST_ASSERT(notParameters.size() == 1);
175 TEST_ASSERT(notParameters.find(testingList->getEntryRCP("double param")) != notParameters.end());
176
177 /*
178 * Test And condition
179 */
180 Condition::ConstConditionList conList1(tuple<RCP<const Condition> >(stringCon1, boolCon1));
181 RCP<AndCondition> andCon1 = rcp(new AndCondition(conList1));
182 Dependency::ConstParameterEntryList andParameters = andCon1->getAllParameters();
183 TEST_ASSERT(andParameters.size() == 2);
184 TEST_ASSERT(andParameters.find(testingList->getEntryRCP("string param")) != andParameters.end());
185 TEST_ASSERT(andParameters.find(testingList->getEntryRCP("bool param")) != andParameters.end());
186
187 /*
188 * Testing or condition
189 */
190 RCP<OrCondition> orCon1 = rcp(new OrCondition(conList1));
191 Dependency::ConstParameterEntryList orParameters = orCon1->getAllParameters();
192 TEST_ASSERT(orParameters.size() == 2);
193 TEST_ASSERT(orParameters.find(testingList->getEntryRCP("string param")) != orParameters.end());
194 TEST_ASSERT(orParameters.find(testingList->getEntryRCP("bool param")) != orParameters.end());
195
196 /*
197 * Testing Equsl condition
198 */
199 Condition::ConstConditionList conList2(tuple<RCP<const Condition> >(numberCon1, boolCon1));
200 RCP<EqualsCondition> equalsCon1 = rcp(new EqualsCondition(conList2));
201 Dependency::ConstParameterEntryList equalsParameters = equalsCon1->getAllParameters();
202 TEST_ASSERT(equalsParameters.size() == 2);
203 TEST_ASSERT(equalsParameters.find(testingList->getEntryRCP("double param")) != equalsParameters.end());
204 TEST_ASSERT(equalsParameters.find(testingList->getEntryRCP("bool param")) != equalsParameters.end());
205
206 /*
207 * Testing BoolLogicCondition add
208 */
209 equalsCon1->addCondition(orCon1);
210 Dependency::ConstParameterEntryList equalsParameters2 = equalsCon1->getAllParameters();
211 TEST_ASSERT(equalsParameters2.size() == 3);
212 TEST_ASSERT(equalsParameters2.find(testingList->getEntryRCP("string param")) != equalsParameters2.end());
213 TEST_ASSERT(equalsParameters2.find(testingList->getEntryRCP("double param")) != equalsParameters2.end());
214 TEST_ASSERT(equalsParameters2.find(testingList->getEntryRCP("bool param")) != equalsParameters2.end());
215
216}
217
218//Test that exceptions get thrown when they should.
219TEUCHOS_UNIT_TEST(Teuchos_Conditions, testConditionException){
220 //Settin up initial list
221 RCP<ParameterList> testingList = rcp(new ParameterList("Condition Testing List"));
222 testingList->set("double param",1.0);
223 testingList->set("string param", "awesome");
224 RCP<ParameterList> testingList2 = rcp(new ParameterList("Condition Testing List"));
225 testingList2->set("bool param", true);
226
227 TEST_THROW(BoolCondition boolCon1(testingList->getEntryRCP("bool param")), InvalidConditionException);
228 TEST_THROW(StringCondition stringCon1(testingList->getEntryRCP("double param"), "coke"), InvalidConditionException);
229 TEST_THROW(BoolCondition boolCon1(testingList->getEntryRCP("double param")), InvalidConditionException);
234}
235
236} //namespace Teuchos
#define TEST_ASSERT(v1)
Assert the given statement is true.
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails).
Templated Parameter List class.
Standard Conditions to be used.
Unit testing support.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
A Bool Logic Condition that returns the result or perfroming a logical AND on the conditions.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
A Bool Condition is a Parameter Condition that evaluates whether or not a Boolean parameter is ture.
std::set< RCP< const ParameterEntry >, RCPConstComp > ConstParameterEntryList
A list of dependents.
A Bool Logic Condition that returns the result or perfroming a logical EQUALS on the conditions.
A Not condition returns the result of performing a logical NOT on a given condition.
A Number Condition is a Parameter Condition that evaluates whether or not a number parameter is great...
A Bool Logic Condition that returns the result or perfroming a logical OR on the conditions.
A list of parameters of arbitrary type.
Smart reference counting pointer class for automatic garbage collection.
A String Condition is a Parameter Condition that evaluates whether or not a string parameter has take...
A simple validator that only allows certain string values to be choosen or simply enforces that a par...
A simple function object that subtracts a specififed value from the given arguement in the runFunctio...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.