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 
42 #include "Teuchos_VerboseObject.hpp"
43 #include "Teuchos_ParameterList.hpp"
44 #include "Teuchos_StandardDependencies.hpp"
45 #include "Teuchos_DependencySheet.hpp"
46 #include "Teuchos_StandardConditions.hpp"
47 #include "Teuchos_UnitTestHarness.hpp"
48 
49 
50 namespace Teuchos{
51 
52 /**
53  * Test all the validator dependencies.
54  */
TEUCHOS_UNIT_TEST(Teuchos_Dependencies,testValiDeps)55 TEUCHOS_UNIT_TEST(Teuchos_Dependencies, testValiDeps){
56 	RCP<ParameterList> My_deplist = rcp(new ParameterList);
57 	RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
58 
59 	/*
60 	 * Testing StringValidatorDependency
61 	 */
62  	RCP<StringToIntegralParameterEntryValidator<int> >
63    	stringFoodTypeValidator = rcp(
64 		new StringToIntegralParameterEntryValidator<int>(
65 		tuple<std::string>( "Cheese", "Soda", "Chips" )
66 		,"Food Type"
67 		)
68 	);
69 
70 	RCP<StringToIntegralParameterEntryValidator<int> >
71     cheeseValidator = rcp(
72 		new StringToIntegralParameterEntryValidator<int>(
73    			tuple<std::string>( "Swiss", "American", "Super Awesome Cheese" )
74 			,"Food Selector"
75 			)
76 	);
77 
78 	RCP<StringToIntegralParameterEntryValidator<int> >
79 	sodaValidator = rcp(
80 		new StringToIntegralParameterEntryValidator<int>(
81 			tuple<std::string>( "Pepsi", "Coke", "Kurtis Cola", "Bad Cola" )
82 			,"Food Selector"
83 			)
84 		);
85 
86 	RCP<StringToIntegralParameterEntryValidator<int> >
87 	chipsValidator = rcp(
88 		new StringToIntegralParameterEntryValidator<int>(
89 			tuple<std::string>( "Lays", "Doritos", "Kurtis Super Awesome Brand" )
90 			,"Food Selector"
91 		)
92 	);
93 
94 	StringValidatorDependency::ValueToValidatorMap testValidatorMap1;
95 	testValidatorMap1["Cheese"] = cheeseValidator;
96 	testValidatorMap1["Soda"] = sodaValidator;
97 	testValidatorMap1["Chips"] = chipsValidator;
98 
99 	ParameterList stringValiDepList = My_deplist->sublist(
100     "String Validator Dependency", false,
101     "String Validator Dependency testing list.");
102 	stringValiDepList.set(
103     "Food Selector", "Swiss", "select the food you want", cheeseValidator);
104 	stringValiDepList.set(
105     "Food Type",
106     "Cheese",
107     "String Validator Dependency Tester",
108     stringFoodTypeValidator);
109 
110 	RCP<StringValidatorDependency>
111 	stringValiDep = rcp(
112 		new StringValidatorDependency(
113 			stringValiDepList.getEntryRCP("Food Type"),
114 			stringValiDepList.getEntryRCP("Food Selector"),
115 			testValidatorMap1,
116 			cheeseValidator
117 		)
118 	);
119 
120 	depSheet1->addDependency(stringValiDep);
121 
122 	TEST_NOTHROW(stringValiDepList.validateParameters(stringValiDepList));
123 	TEST_ASSERT(depSheet1->hasDependents(
124     stringValiDepList.getEntryRCP("Food Type")));
125 	RCP<const DependencySheet::DepSet> stringValiDepSet =
126     depSheet1->getDependenciesForParameter(
127       stringValiDepList.getEntryRCP("Food Type"));
128 	TEST_ASSERT(stringValiDepSet->size() == 1);
129 	stringValiDepList.set("Food Type","Soda");
130 	stringValiDep->evaluate();
131 	TEST_ASSERT(stringValiDepList.getEntry("Food Selector").validator()
132     ==
133     sodaValidator);
134 	TEST_THROW(stringValiDepList.validateParameters(stringValiDepList),
135     Exceptions::InvalidParameterValue);
136 	stringValiDepList.set("Food Selector", "Pepsi");
137 	TEST_NOTHROW(stringValiDepList.validateParameters(stringValiDepList));
138 
139 
140 	/*
141 	 * Tesing some different aspects of the StringValidatorDependency
142 	 */
143 	ParameterList
144 	stringValiDepList2 = My_deplist->sublist(
145 		"String Validator Dependency (other validators)",
146 		false,
147 		"String validator testing"
148 	);
149 
150 	RCP<StringToIntegralParameterEntryValidator<int> >
151 	stringRangeValidator = rcp(
152 		new StringToIntegralParameterEntryValidator<int>(
153 		tuple<std::string>( "1-10", "10-33", "50-60" ),
154 		"Range selector"
155 		)
156 	);
157 
158 	RCP<EnhancedNumberValidator<int> > range110Vali =
159 	rcp(new EnhancedNumberValidator<int>(1,10));
160 	RCP<EnhancedNumberValidator<int> > range1033Vali =
161 	rcp(new EnhancedNumberValidator<int>(10,33));
162 	RCP<EnhancedNumberValidator<int> > range5060Vali =
163 	rcp(new EnhancedNumberValidator<int>(50,60));
164 
165 	stringValiDepList2.set("Range selector", "1-10",
166     "selects the range to validate", stringRangeValidator);
167 
168 	StringValidatorDependency::ValueToValidatorMap rangeValidatorMap1;
169 	rangeValidatorMap1["1-10"] = range110Vali;
170 	rangeValidatorMap1["10-33"] = range1033Vali;
171 	rangeValidatorMap1["50-60"] = range5060Vali;
172 	stringValiDepList2.set(
173     "RangeValue", 3, "the value of the range", range110Vali);
174 
175 	RCP<StringValidatorDependency>
176 	stringValiDep2 = RCP<StringValidatorDependency>(
177 		new StringValidatorDependency(
178 			stringValiDepList2.getEntryRCP("Range selector"),
179 			stringValiDepList2.getEntryRCP("RangeValue"),
180 			rangeValidatorMap1,
181       range110Vali
182 		)
183 	);
184 
185 	depSheet1->addDependency(stringValiDep2);
186 
187 	TEST_NOTHROW(stringValiDepList2.validateParameters(stringValiDepList2));
188 	TEST_ASSERT(depSheet1->hasDependents(
189     stringValiDepList2.getEntryRCP("Range selector")));
190 	RCP<const DependencySheet::DepSet> stringValiDepSet2 =
191     depSheet1->getDependenciesForParameter(
192       stringValiDepList2.getEntryRCP("Range selector"));
193 	TEST_ASSERT(stringValiDepSet2->size() == 1);
194 	stringValiDepList2.set("Range selector","50-60");
195 	stringValiDep2->evaluate();
196 	TEST_ASSERT(stringValiDepList2.getEntry("RangeValue").validator()
197     ==
198     range5060Vali);
199 	TEST_THROW(stringValiDepList2.validateParameters(stringValiDepList2),
200     Exceptions::InvalidParameterValue);
201 	stringValiDepList2.set("RangeValue", 55);
202 	TEST_NOTHROW(stringValiDepList2.validateParameters(stringValiDepList2));
203 
204 	/*
205 	 * Testing the BoolValidatorDependency.
206 	 */
207 	ParameterList
208 	boolValidatorDepList = My_deplist->sublist(
209 		"Bool Validator Dependency List",
210 		false,
211 		"Bool Validator Dependency testing list."
212 	);
213 
214 	boolValidatorDepList.set("Use Validator?",
215     true, "truns the validator on and off");
216 	RCP<EnhancedNumberValidator<int> > basicVali =
217     rcp(new EnhancedNumberValidator<int>(1,10));
218 	RCP<EnhancedNumberValidator<int> > basicVali2 =
219     rcp(new EnhancedNumberValidator<int>());
220 	boolValidatorDepList.set("do I have a validator?",
221     4, "does it have a validator?", basicVali);
222 
223 	RCP<BoolValidatorDependency>
224 	boolValiDep = RCP<BoolValidatorDependency>(
225 		new BoolValidatorDependency(
226 			boolValidatorDepList.getEntryRCP("Use Validator?"),
227 			boolValidatorDepList.getEntryRCP("do I have a validator?"),
228 			basicVali,
229 			basicVali2
230 		)
231 	);
232 
233 	depSheet1->addDependency(boolValiDep);
234 
235 	TEST_ASSERT(depSheet1->hasDependents(
236     boolValidatorDepList.getEntryRCP("Use Validator?")));
237 	TEST_ASSERT(
238     boolValidatorDepList.getEntry("do I have a validator?").validator()
239     ==
240     basicVali);
241 	TEST_NOTHROW(
242     boolValidatorDepList.validateParameters(boolValidatorDepList));
243 	RCP<const DependencySheet::DepSet> boolValiDepSet =
244     depSheet1->getDependenciesForParameter(boolValidatorDepList.getEntryRCP(
245       "Use Validator?"));
246 	TEST_ASSERT(boolValiDepSet->size() == 1);
247 	boolValidatorDepList.set("Use Validator?",false);
248 	boolValiDep->evaluate();
249 	TEST_ASSERT(
250     boolValidatorDepList.getEntry("do I have a validator?").validator()
251     ==
252     basicVali2);
253 
254 
255 	/*
256 	 * Testing the RangeValidatorDependency
257 	 */
258 	RCP<StringToIntegralParameterEntryValidator<int> >
259 	lowTempCheeseValidator = rcp(
260 		new StringToIntegralParameterEntryValidator<int>(
261 			tuple<std::string>( "PepperJack", "Swiss", "American" ),
262 			"Cheese to Fondue"
263 		)
264 	);
265 
266 	RCP<StringToIntegralParameterEntryValidator<int> >
267 	highTempCheeseValidator = rcp(
268 		new StringToIntegralParameterEntryValidator<int>(
269 			tuple<std::string>(
270         "Munster", "Provalone", "Kurtis Super Awesome Cheese"),
271 			"Cheese to Fondue"
272 		)
273 	);
274 
275 	RCP<StringToIntegralParameterEntryValidator<int> >
276 	defaultCheeseValidator = rcp(
277 		new StringToIntegralParameterEntryValidator<int>(
278 			tuple<std::string>(
279         "Other cheese", "other cheese 1", "other cheese 3"),
280 			"Cheese to Fondue"
281 		)
282 	);
283 
284 	ParameterList&
285 	rangeValidatorDepList = My_deplist->sublist(
286 		"Range Validator Dependency List",
287 		false,
288 		"Range Validator Dependency testing list.\nWorking June 27th 2009"
289 	);
290 	rangeValidatorDepList.set(
291     "Temperature",101.0, "The temperature of the fondue");
292 	rangeValidatorDepList.set(
293     "Cheese to Fondue", "Swiss",
294     "The cheese we'll be using in our fondue pot.", lowTempCheeseValidator);
295 	RangeValidatorDependency<double>::RangeToValidatorMap tempranges;
296 	tempranges[std::pair<double,double>(100,200)] = lowTempCheeseValidator;
297 	tempranges[std::pair<double,double>(200,300)] = highTempCheeseValidator;
298 	RCP<RangeValidatorDependency<double> >
299 	cheeseTempDep = RCP<RangeValidatorDependency<double> >(
300 		new RangeValidatorDependency<double>(
301 			rangeValidatorDepList.getEntryRCP("Temperature"),
302 			rangeValidatorDepList.getEntryRCP("Cheese to Fondue"),
303 			tempranges,
304       defaultCheeseValidator
305 		)
306 	);
307 	depSheet1->addDependency(cheeseTempDep);
308 
309 	TEST_ASSERT(depSheet1->hasDependents(
310     rangeValidatorDepList.getEntryRCP("Temperature")));
311 	RCP<const DependencySheet::DepSet> rangeValiDepSet =
312     depSheet1->getDependenciesForParameter(
313       rangeValidatorDepList.getEntryRCP("Temperature"));
314 	TEST_ASSERT(rangeValiDepSet->size() == 1);
315 	rangeValidatorDepList.set("Temperature",250.0);
316 	cheeseTempDep->evaluate();
317 	TEST_ASSERT(
318     rangeValidatorDepList.getEntry("Cheese to Fondue").validator()
319     ==
320     highTempCheeseValidator);
321 	TEST_THROW(
322     rangeValidatorDepList.validateParameters(rangeValidatorDepList),
323     Exceptions::InvalidParameterValue);
324 	rangeValidatorDepList.set("Cheese to Fondue", "Provalone");
325 	TEST_NOTHROW(
326     rangeValidatorDepList.validateParameters(rangeValidatorDepList));
327   rangeValidatorDepList.set("Temperature", 50.0);
328   cheeseTempDep->evaluate();
329   TEST_ASSERT(
330     rangeValidatorDepList.getEntry("Cheese to Fondue").validator()
331     ==
332     defaultCheeseValidator
333   );
334 
335 }
336 
337 /**
338  * Testing all the visual dependencies
339  */
TEUCHOS_UNIT_TEST(Teuchos_Dependencies,testVisualDeps)340 TEUCHOS_UNIT_TEST(Teuchos_Dependencies, testVisualDeps){
341 	RCP<ParameterList> My_deplist = RCP<ParameterList>(new ParameterList);
342 	RCP<DependencySheet> depSheet1 =
343     RCP<DependencySheet>(new DependencySheet);
344   /*
345    * Two Simple NumberVisualDependency test
346    */
347 
348 	ParameterList
349 	simpleNumDepTestList = My_deplist->sublist(
350 		"NumberVisual Dependency List (double)",
351 		false,
352 		"Number visual Dependency testing list"
353 	);
354 
355 	simpleNumDepTestList.set("Temperature",101.0);
356 	simpleNumDepTestList.set("Cheese to Fondue", "Swiss", "The cheese to fondue");
357 	simpleNumDepTestList.set("reverse param", "hello");
358 
359 	RCP<NumberVisualDependency<double> > simpleNumDep =
360 	RCP<NumberVisualDependency<double> >(
361 		new NumberVisualDependency<double>(
362 			simpleNumDepTestList.getEntryRCP("Temperature"),
363 			simpleNumDepTestList.getEntryRCP("Cheese to Fondue"),
364       true
365 		)
366 	);
367 	RCP<NumberVisualDependency<double> > reverseNumDep =
368 	RCP<NumberVisualDependency<double> >(
369 		new NumberVisualDependency<double>(
370 			simpleNumDepTestList.getEntryRCP("Temperature"),
371 			simpleNumDepTestList.getEntryRCP("reverse param"),
372       false
373 		)
374 	);
375   depSheet1->addDependency(simpleNumDep);
376   depSheet1->addDependency(reverseNumDep);
377 	simpleNumDep->evaluate();
378 	reverseNumDep->evaluate();
379 	TEST_ASSERT(simpleNumDep->isDependentVisible());
380 	TEST_ASSERT(!reverseNumDep->isDependentVisible());
381 	simpleNumDepTestList.set("Temperature",-1.0);
382 	simpleNumDep->evaluate();
383 	reverseNumDep->evaluate();
384 	TEST_ASSERT(!simpleNumDep->isDependentVisible());
385 	TEST_ASSERT(reverseNumDep->isDependentVisible());
386 
387 
388 	/*
389 	 * complex Testing the NumberVisualDependency
390 	 */
391 	ParameterList
392 	doubleVisualDepList = My_deplist->sublist(
393 		"NumberVisual Dependency List (double)",
394 		false,
395 		"Number visual Dependency testing list"
396 	);
397 
398 	doubleVisualDepList.set(
399     "Temperature",101.0, "The temperature of the fondue");
400 	doubleVisualDepList.set(
401     "Cheese to Fondue", "Swiss", "The cheese to fondue");
402   doubleVisualDepList.set("reverse param", "hello");
403   RCP<SubtractionFunction<double> > fondueFunc = rcp(new
404     SubtractionFunction<double>(100));
405 
406 	RCP<NumberVisualDependency<double> > fondueDep =
407 	RCP<NumberVisualDependency<double> >(
408 		new NumberVisualDependency<double>(
409 			doubleVisualDepList.getEntryRCP("Temperature"),
410 			doubleVisualDepList.getEntryRCP("Cheese to Fondue"),
411       true,
412 			fondueFunc
413 		)
414 	);
415 	RCP<NumberVisualDependency<double> > reverseFondueDep =
416 	RCP<NumberVisualDependency<double> >(
417 		new NumberVisualDependency<double>(
418 			doubleVisualDepList.getEntryRCP("Temperature"),
419 			doubleVisualDepList.getEntryRCP("reverse param"),
420       false,
421 			fondueFunc
422 		)
423 	);
424 	depSheet1->addDependency(fondueDep);
425 	depSheet1->addDependency(reverseFondueDep);
426 	fondueDep->evaluate();
427   reverseFondueDep->evaluate();
428 	TEST_ASSERT(fondueDep->isDependentVisible());
429 	TEST_ASSERT(!reverseFondueDep->isDependentVisible());
430 	doubleVisualDepList.set("Temperature",99.0);
431 	fondueDep->evaluate();
432   reverseFondueDep->evaluate();
433 	TEST_ASSERT(!fondueDep->isDependentVisible());
434 	TEST_ASSERT(reverseFondueDep->isDependentVisible());
435 
436 	/*
437 	 * Testing the BoolVisualDependency
438 	 */
439 	ParameterList&
440 	boolVisDepList = My_deplist->sublist(
441 		"Bool Visual Dependency List",
442 		false,
443 		"Bool Visual Dependency testing list."
444 	);
445 	boolVisDepList.set(
446     "ShowPrecs", true, "Whether or not to should the Preciondtioner list");
447 	ParameterList
448 	Prec_List0 = boolVisDepList.sublist(
449     "Preconditioner",false,"Sublist that defines the preconditioner.");
450 	Prec_List0.set("Type", "ILU", "The tpye of preconditioner to use");
451 	RCP<EnhancedNumberValidator<double> > droptolValidator =
452     rcp(new EnhancedNumberValidator<double>(0,10,1e-3));
453 	Prec_List0.set(
454     "Drop Tolerance", 1e-3,
455     "The tolerance below which entries from the "
456     "factorization are left out of the factors.", droptolValidator);
457 	RCP<BoolVisualDependency>
458 	precDep1 = RCP<BoolVisualDependency>(
459 		new BoolVisualDependency(
460 			boolVisDepList.getEntryRCP("ShowPrecs"),
461 			boolVisDepList.getEntryRCP("Preconditioner"),
462 			true
463 		)
464 	);
465 	depSheet1->addDependency(precDep1);
466 	precDep1->evaluate();
467 	TEST_ASSERT(precDep1->isDependentVisible());
468 	boolVisDepList.set("ShowPrecs", false);
469 	precDep1->evaluate();
470 	TEST_ASSERT(!precDep1->isDependentVisible());
471 
472 
473 
474 	/*
475 	 * Testing the StringVisualDepenency
476 	 */
477 	ParameterList&
478     stringVisDepList = My_deplist->sublist(
479 		"String Visual Dependency List",
480 		false,
481 		"String Visual Dependency testing list."
482 	);
483 	RCP<StringToIntegralParameterEntryValidator<int> >
484 	favCheeseValidator = rcp(
485 		new StringToIntegralParameterEntryValidator<int>(
486 			tuple<std::string>( "Swiss", "American", "Cheder" ),
487 			"Favorite Cheese"
488 		)
489 	);
490 
491 	stringVisDepList.set("Favorite Cheese",
492     "American", "Your favorite type of cheese", favCheeseValidator);
493 	RCP<EnhancedNumberValidator<int> >
494 	swissValidator = rcp(new EnhancedNumberValidator<int>(0,10));
495 	stringVisDepList.set("Swiss rating", 0,
496     "How you rate swiss on a scale of 1 to 10", swissValidator);
497 	RCP<StringVisualDependency>
498 	swissDep1 = RCP<StringVisualDependency>(
499 		new StringVisualDependency(
500 			stringVisDepList.getEntryRCP("Favorite Cheese"),
501 			stringVisDepList.getEntryRCP("Swiss rating"),
502 			"Swiss",
503 			true
504 		)
505 	);
506 	depSheet1->addDependency(swissDep1);
507 	swissDep1->evaluate();
508 	TEST_ASSERT(!swissDep1->isDependentVisible());
509 	stringVisDepList.set("Favorite Cheese", "Swiss");
510 	swissDep1->evaluate();
511 	TEST_ASSERT(swissDep1->isDependentVisible());
512 
513 	/*
514 	 * String Visual Tester with multiple values
515 	 */
516 	ParameterList multiStringVisDepList = My_deplist->sublist(
517 		"Multi String Visual Dependency List",
518 		false
519 	);
520 	RCP<StringToIntegralParameterEntryValidator<int> >
521 	favCheeseValidator2 = rcp(
522 		new StringToIntegralParameterEntryValidator<int>(
523 			tuple<std::string>( "Provalone", "Swiss", "American", "Cheder" ),
524 			"Favorite Cheese"
525 		)
526 	);
527 
528 	multiStringVisDepList.set(
529     "Favorite Cheese", "American",
530     "Your favorite type of cheese", favCheeseValidator2);
531 	multiStringVisDepList.set("Swiss rating", 0,
532     "How you rate swiss on a scale of 1 to 10", swissValidator);
533 	RCP<StringVisualDependency>
534 	swissDep2 = RCP<StringVisualDependency>(
535 		new StringVisualDependency(
536 			multiStringVisDepList.getEntryRCP("Favorite Cheese"),
537 			multiStringVisDepList.getEntryRCP("Swiss rating"),
538 			tuple<std::string>("Swiss", "Cheder"),
539 			true
540 		)
541 	);
542 	depSheet1->addDependency(swissDep2);
543 	swissDep2->evaluate();
544 	TEST_ASSERT(!swissDep2->isDependentVisible());
545 	multiStringVisDepList.set("Favorite Cheese", "Cheder");
546 	swissDep2->evaluate();
547 	TEST_ASSERT(swissDep2->isDependentVisible());
548 
549 	/*
550 	 * Another test of the NumberVisualDependency.
551 	 */
552 	ParameterList
553     numberVisDepList = My_deplist->sublist(
554 		"Number Visual Dependency List",
555 		false,
556 		"Number Visual Dependency testing list."
557 	);
558 	numberVisDepList.set("Ice", 50, "Ice stuff");
559 	numberVisDepList.set("Room Temp", 10, "Room temperature");
560   RCP<SubtractionFunction<int> > visFunc = rcp(new
561     SubtractionFunction<int>(32));
562 	RCP<NumberVisualDependency<int> >
563 	iceDep = RCP<NumberVisualDependency<int> >(
564 		new NumberVisualDependency<int>(
565 			numberVisDepList.getEntryRCP("Room Temp"),
566 			numberVisDepList.getEntryRCP("Ice"),
567       true,
568 			visFunc
569 		)
570 	);
571 	depSheet1->addDependency(iceDep);
572 	iceDep->evaluate();
573 	TEST_ASSERT(!iceDep->isDependentVisible());
574 	numberVisDepList.set("Room Temp", 33);
575 	iceDep->evaluate();
576 	TEST_ASSERT(iceDep->isDependentVisible());
577 
578 	/*
579 	 * Test condition visual dependency
580 	 */
581 	RCP<ParameterList> conVisDepList = sublist(
582     My_deplist,"Condition Visual Dependency List", false);
583 	conVisDepList->set("double param", 4.0, "double parameter");
584 	conVisDepList->set("bool param", true, "bool parameter");
585 	conVisDepList->set("string param", "blah", "a string parameter");
586 	RCP<NumberCondition<double> > numberCon =
587     rcp( new NumberCondition<double>(
588       conVisDepList->getEntryRCP("double param")));
589 	RCP<BoolCondition> boolCon =
590     rcp(new BoolCondition(conVisDepList->getEntryRCP("bool param")));
591 	Condition::ConstConditionList conList =
592     tuple<RCP<const Condition> >(numberCon, boolCon);
593 	RCP<AndCondition> andCon = rcp(new AndCondition(conList));
594 	RCP<ConditionVisualDependency> conVisDep =
595     rcp(new ConditionVisualDependency(
596       andCon, conVisDepList->getEntryRCP("string param"), true));
597 	depSheet1->addDependency(conVisDep);
598 	conVisDep->evaluate();
599 	TEST_ASSERT(conVisDep->isDependentVisible());
600 	conVisDepList->set("bool param", false);
601 	conVisDep->evaluate();
602 	TEST_ASSERT(!conVisDep->isDependentVisible());
603 }
604 
605 /**
606  * Test the TwoDRowDependency.
607  */
TEUCHOS_UNIT_TEST(Teuchos_Dependencies,testTwoDRowDependency)608 TEUCHOS_UNIT_TEST(Teuchos_Dependencies, testTwoDRowDependency){
609 	RCP<ParameterList> My_deplist = RCP<ParameterList>(new ParameterList);
610 	RCP<DependencySheet> depSheet1 =
611     RCP<DependencySheet>(new DependencySheet);
612 
613 	ParameterList
614 	rowNumDepList = My_deplist->sublist(
615     "2D Row Depdency List", false,
616     "2D Row Dependecy testing list.");
617 	rowNumDepList.set("Num rows", 10, "num rows setter");
618   TwoDArray<double> variableRowsArray(11,2,16.5);
619 	RCP<EnhancedNumberValidator<double> >
620 	varRowArrayVali = RCP<EnhancedNumberValidator<double> >(
621   		new EnhancedNumberValidator<double>(10,50,4)
622 	);
623 	rowNumDepList.set(
624     "Variable Row Array", variableRowsArray, "variable row array",
625 	  RCP<TwoDArrayNumberValidator<double> >(
626       new TwoDArrayNumberValidator<double>(varRowArrayVali)));
627 
628 	RCP<TwoDRowDependency<int, double> >
629 	  arrayRowDep = rcp(
630   		new TwoDRowDependency<int, double>(
631 		  rowNumDepList.getEntryRCP("Num rows"),
632 			rowNumDepList.getEntryRCP("Variable Row Array") ,
633       rcp(new AdditionFunction<int>(1))
634 		)
635 	);
636 	depSheet1->addDependency(arrayRowDep);
637   TwoDArray<double> curArray =
638     rowNumDepList.get<TwoDArray<double> >("Variable Row Array");
639 	TEST_EQUALITY_CONST(curArray.getNumRows(),11);
640 	rowNumDepList.set("Num rows", 12);
641 	arrayRowDep()->evaluate();
642   curArray =
643     rowNumDepList.get<TwoDArray<double> >("Variable Row Array");
644 	TEST_EQUALITY_CONST(curArray.getNumRows(),13);
645 	rowNumDepList.set("Num rows", -2);
646 	TEST_THROW(arrayRowDep()->evaluate(),
647     Exceptions::InvalidParameterValue);
648 }
649 
650 /**
651  * Test the TwoDColDependency.
652  */
TEUCHOS_UNIT_TEST(Teuchos_Dependencies,testTwoDColDependency)653 TEUCHOS_UNIT_TEST(Teuchos_Dependencies, testTwoDColDependency){
654 	RCP<ParameterList> My_deplist = RCP<ParameterList>(new ParameterList);
655 	RCP<DependencySheet> depSheet1 =
656     RCP<DependencySheet>(new DependencySheet);
657 
658 	ParameterList
659 	colNumDepList = My_deplist->sublist(
660     "2D Col Depdency List", false,
661     "2D Col Dependecy testing list.");
662 	colNumDepList.set("Num cols", 2, "num cols setter");
663   TwoDArray<double> variableColsArray(11,3,16.5);
664 	RCP<EnhancedNumberValidator<double> >
665 	varColArrayVali = RCP<EnhancedNumberValidator<double> >(
666   		new EnhancedNumberValidator<double>(10,50,4)
667 	);
668 	colNumDepList.set(
669     "Variable Col Array", variableColsArray, "variable col array",
670 	  RCP<TwoDArrayNumberValidator<double> >(
671       new TwoDArrayNumberValidator<double>(varColArrayVali)));
672 
673 	RCP<TwoDColDependency<int, double> >
674 	  arrayColDep = rcp(
675   		new TwoDColDependency<int, double>(
676 		  colNumDepList.getEntryRCP("Num cols"),
677 			colNumDepList.getEntryRCP("Variable Col Array") ,
678       rcp(new AdditionFunction<int>(1))
679 		)
680 	);
681 	depSheet1->addDependency(arrayColDep);
682   TwoDArray<double> curArray =
683     colNumDepList.get<TwoDArray<double> >("Variable Col Array");
684 	TEST_EQUALITY_CONST(curArray.getNumCols(),3);
685 	colNumDepList.set("Num cols", 4);
686 	arrayColDep()->evaluate();
687   curArray =
688     colNumDepList.get<TwoDArray<double> >("Variable Col Array");
689 	TEST_EQUALITY_CONST(curArray.getNumCols(),5);
690 	colNumDepList.set("Num cols", -2);
691 	TEST_THROW(arrayColDep()->evaluate(),
692     Exceptions::InvalidParameterValue);
693 }
694 
695 
696 /**
697  * Test the ArrayLengthDependency.
698  */
TEUCHOS_UNIT_TEST(Teuchos_Dependencies,testArrayLengthDep)699 TEUCHOS_UNIT_TEST(Teuchos_Dependencies, testArrayLengthDep){
700 	RCP<ParameterList> My_deplist = RCP<ParameterList>(new ParameterList);
701 	RCP<DependencySheet> depSheet1 =
702     RCP<DependencySheet>(new DependencySheet);
703 
704 	ParameterList
705 	numberArrayLengthDepList = My_deplist->sublist(
706     "Number Array Length Dependency List", false,
707     "Number Array Length Dependecy testing list.");
708 	numberArrayLengthDepList.set("Array Length", 10, "array length setter");
709 	Array<double> variableLengthArray(11,23.0);
710 	RCP<EnhancedNumberValidator<double> >
711 	varLengthArrayVali = RCP<EnhancedNumberValidator<double> >(
712   		new EnhancedNumberValidator<double>(10,50,4)
713 	);
714 	numberArrayLengthDepList.set(
715     "Variable Length Array", variableLengthArray, "variable length array",
716 	  RCP<ArrayNumberValidator<double> >(
717       new ArrayNumberValidator<double>(varLengthArrayVali)));
718 
719 	RCP<NumberArrayLengthDependency<int, double> >
720 	  arrayLengthDep(
721   		new NumberArrayLengthDependency<int, double>(
722 			numberArrayLengthDepList.getEntryRCP("Array Length"),
723 			numberArrayLengthDepList.getEntryRCP("Variable Length Array"),
724       rcp(new AdditionFunction<int>(1))
725 		)
726 	);
727 	depSheet1->addDependency(arrayLengthDep);
728   Array<double> curArray =
729     numberArrayLengthDepList.get<Array<double> >("Variable Length Array");
730 	TEST_ASSERT(curArray.length() ==11);
731 	numberArrayLengthDepList.set("Array Length", 12);
732 	arrayLengthDep()->evaluate();
733   curArray =
734     numberArrayLengthDepList.get<Array<double> >("Variable Length Array");
735   out << curArray.length() << std::endl;
736 	TEST_ASSERT(curArray.length() ==13);
737 	numberArrayLengthDepList.set("Array Length", -2);
738 	TEST_THROW(arrayLengthDep()->evaluate(),
739     Exceptions::InvalidParameterValue);
740 }
741 
742 /**
743  * Tests the excpetions associated with Dependencies
744  */
TEUCHOS_UNIT_TEST(Teuchos_Dependencies,testDepExceptions)745 TEUCHOS_UNIT_TEST(Teuchos_Dependencies, testDepExceptions){
746 	RCP<ParameterList> list1 = RCP<ParameterList>(new ParameterList());
747 	RCP<ParameterList> list2 = RCP<ParameterList>(new ParameterList());
748 
749 	list1->set("int parameter", 4, "int parameter");
750 	list1->set("double parameter", 6.0, "double parameter");
751 	list1->set("string parameter", "hahahaha", "string parameter");
752 	Array<double> doubleArray(10,23.0);
753 	list1->set("array parameter", doubleArray, "array parameter");
754 	list1->set("bool parameter", true, "bool parameter");
755 
756   RCP<AdditionFunction<int> > intFuncTester = rcp(new
757     AdditionFunction<int>(10));
758 	TEST_THROW(RCP<NumberVisualDependency<int> > numValiDep =
759     rcp(
760       new NumberVisualDependency<int>(
761         list1->getEntryRCP("bool parameter"),
762         list1->getEntryRCP("double parameter"),
763         true,
764         intFuncTester)),
765     InvalidDependencyException);
766 
767 	/*
768 	 * Testing StringVisualDepenendcy exceptions.
769 	 */
770 	RCP<StringVisualDependency> stringVisDep;
771 	TEST_THROW(stringVisDep = RCP<StringVisualDependency>(
772     new StringVisualDependency(
773       list1->getEntryRCP("double parameter"),
774       list1->getEntryRCP("int parameter"),
775       "cheese", true)),
776     InvalidDependencyException);
777 
778 	/*
779 	 * Testing BoolVisualDependency exceptions.
780 	 */
781 	TEST_THROW(RCP<BoolVisualDependency> boolVisDep =
782     RCP<BoolVisualDependency>(new BoolVisualDependency(
783       list1->getEntryRCP("int parameter"),
784       list1->getEntryRCP("double parameter"), false)),
785       InvalidDependencyException);
786 
787   /**
788    * Tesint NumberArrayLengthDependency excpetions */
789   RCP<NumberArrayLengthDependency<int, double> > numArrayLengthDep;
790 	TEST_THROW(numArrayLengthDep =
791       rcp(new NumberArrayLengthDependency<int, double>(
792         list1->getEntryRCP("double parameter"),
793         list1->getEntryRCP("array parameter"))),
794       InvalidDependencyException);
795 
796 	TEST_THROW(numArrayLengthDep =
797       rcp(new NumberArrayLengthDependency<int, double>(
798         list1->getEntryRCP("int parameter"),
799         list1->getEntryRCP("double parameter"))),
800       InvalidDependencyException);
801 
802 	/*
803 	 * Testing StringValidatorDependency exceptions.
804 	 */
805 	RCP<StringToIntegralParameterEntryValidator<int> >
806     cheeseValidator = rcp(
807 		new StringToIntegralParameterEntryValidator<int>(
808    	  tuple<std::string>( "Swiss", "American", "Super Awesome Cheese"),
809 			"Food Selector"
810 		)
811 	);
812 
813 	RCP<StringToIntegralParameterEntryValidator<int> >
814 	sodaValidator = rcp(
815 		new StringToIntegralParameterEntryValidator<int>(
816 			tuple<std::string>( "Pepsi", "Coke", "Kurtis Cola", "Bad Cola" ),
817 			"Food Selector"
818 		)
819 	);
820 
821 	RCP<StringToIntegralParameterEntryValidator<int> >
822 	chipsValidator = rcp(
823 		new StringToIntegralParameterEntryValidator<int>(
824 			tuple<std::string>( "Lays", "Doritos", "Kurtis Super Awesome Brand"),
825 			"Food Selector"
826 		)
827 	);
828 
829 
830 	list1->set(
831     "string 2 parameter", "Swiss",
832     "second string parameter", cheeseValidator);
833 	StringValidatorDependency::ValueToValidatorMap testValidatorMap1;
834 	testValidatorMap1["Cheese"] = cheeseValidator;
835 	testValidatorMap1["Soda"] = sodaValidator;
836 	testValidatorMap1["Chips"] = chipsValidator;
837 	TEST_THROW(RCP<StringValidatorDependency> stringValiDep =
838     RCP<StringValidatorDependency>(
839       new StringValidatorDependency(
840         list1->getEntryRCP("int parameter"),
841         list1->getEntryRCP("string 2 parameter"),
842         testValidatorMap1)),
843     InvalidDependencyException);
844 	RCP<EnhancedNumberValidator<int> > intVali =
845     rcp(new EnhancedNumberValidator<int>(0,20));
846 	testValidatorMap1["Candy"] = intVali;
847 	TEST_THROW(RCP<StringValidatorDependency> stringValiDep =
848     RCP<StringValidatorDependency>(
849       new StringValidatorDependency(
850         list1->getEntryRCP("string parameter"),
851         list1->getEntryRCP("string 2 parameter"),
852         testValidatorMap1)),
853     InvalidDependencyException);
854 
855   StringValidatorDependency::ValueToValidatorMap emptyMap;
856 	TEST_THROW(RCP<StringValidatorDependency> stringValiDep =
857     RCP<StringValidatorDependency>(
858       new StringValidatorDependency(
859         list1->getEntryRCP("string parameter"),
860         list1->getEntryRCP("string 2 parameter"),
861         emptyMap)),
862     InvalidDependencyException);
863 
864 	/*
865 	 * Testing BoolValidatorDependency exceptions.
866 	 */
867 	RCP<EnhancedNumberValidator<double> > doubleVali1 =
868     rcp(new EnhancedNumberValidator<double>(0.0,20.0));
869 	RCP<EnhancedNumberValidator<double> > doubleVali2 =
870     rcp(new EnhancedNumberValidator<double>(5.0,20.0));
871 	list1->set("double parameter", 6.0, "double parameter", doubleVali1);
872 
873 	TEST_THROW(RCP<BoolValidatorDependency> boolValiDep =
874     RCP<BoolValidatorDependency>(
875       new BoolValidatorDependency(
876         list1->getEntryRCP("int parameter"),
877         list1->getEntryRCP("double parameter"),
878         doubleVali1,
879         doubleVali2)),
880     InvalidDependencyException);
881 
882 	TEST_THROW(RCP<BoolValidatorDependency> boolValiDep =
883     RCP<BoolValidatorDependency>(
884       new BoolValidatorDependency(
885       list1->getEntryRCP("bool parameter"),
886       list1->getEntryRCP("double parameter"),
887       intVali,
888       doubleVali2)),
889     InvalidDependencyException);
890 
891 	TEST_THROW(RCP<BoolValidatorDependency> boolValiDep =
892     RCP<BoolValidatorDependency>(
893       new BoolValidatorDependency(
894         list1->getEntryRCP("bool parameter"),
895         list1->getEntryRCP("double parameter"),
896         doubleVali1,
897         intVali)),
898     InvalidDependencyException);
899 
900 	/*
901 	 * Testing RangeValidatorDependency exceptions.
902 	 */
903 	list1->set("Cheese to Fondue", "Swiss", "the cheese to fondue");
904 	RCP<StringToIntegralParameterEntryValidator<int> >
905 	lowTempCheeseValidator = rcp(
906 		new StringToIntegralParameterEntryValidator<int>(
907 			tuple<std::string>( "PepperJack", "Swiss", "American" ),
908 			"Cheese to Fondue"
909 		)
910 	);
911 	RCP<StringToIntegralParameterEntryValidator<int> >
912 	highTempCheeseValidator = rcp(
913 		new StringToIntegralParameterEntryValidator<int>(
914 			tuple<std::string>("Munster", "Provalone",
915         "Kurtis Super Awesome Cheese"),
916 			"Cheese to Fondue"
917 		)
918 	);
919 
920 	list1->set(
921     "Cheese to Fondue", "Swiss", "the cheese to fondue",
922     lowTempCheeseValidator);
923 
924 	RangeValidatorDependency<double>::RangeToValidatorMap tempranges;
925 	tempranges[std::pair<double,double>(100,200)] = lowTempCheeseValidator;
926 	tempranges[std::pair<double,double>(200,300)] = highTempCheeseValidator;
927 	TEST_THROW(
928 		RCP<RangeValidatorDependency<double> >
929 		cheeseTempDep = RCP<RangeValidatorDependency<double> >(
930 			new RangeValidatorDependency<double>(
931 			  list1->getEntryRCP("string parameter"),
932 				list1->getEntryRCP("Cheese to Fondue"),
933 				tempranges
934 			)
935 		),
936 		InvalidDependencyException
937 	);
938 
939 	tempranges[std::pair<double,double>(400,800)] = intVali;
940 	TEST_THROW(
941 		RCP<RangeValidatorDependency<double> >
942 		cheeseTempDep = RCP<RangeValidatorDependency<double> >(
943 			new RangeValidatorDependency<double>(
944 			  list1->getEntryRCP("int parameter"),
945 				list1->getEntryRCP("Cheese to Fondue"),
946 				tempranges
947 			)
948 		),
949 		InvalidDependencyException
950 	);
951 
952   RangeValidatorDependency<double>::RangeToValidatorMap emptyMap2;
953 	TEST_THROW(
954 		RCP<RangeValidatorDependency<double> >
955 		emptyMapDep = RCP<RangeValidatorDependency<double> >(
956 			new RangeValidatorDependency<double>(
957 			  list1->getEntryRCP("double parameter"),
958 				list1->getEntryRCP("Cheese to Fondue"),
959 				emptyMap2
960 			)
961 		),
962 		InvalidDependencyException
963 	);
964 
965 	RangeValidatorDependency<int>::RangeToValidatorMap badRanges;
966 	tempranges[std::pair<int,int>(200,100)] = lowTempCheeseValidator;
967 	TEST_THROW(
968 		RCP<RangeValidatorDependency<int> >
969 		cheeseTempDep = RCP<RangeValidatorDependency<int> >(
970 			new RangeValidatorDependency<int>(
971 			  list1->getEntryRCP("string parameter"),
972 				list1->getEntryRCP("Cheese to Fondue"),
973 				badRanges
974 			)
975 		),
976 		InvalidDependencyException
977 	);
978 }
979 
980 /**
981  * Tests various DependencySheet functions.
982  */
TEUCHOS_UNIT_TEST(Teuchos_Dependencies,DepSheetTest)983 TEUCHOS_UNIT_TEST(Teuchos_Dependencies, DepSheetTest){
984 	RCP<DependencySheet> depSheet1 = rcp(new DependencySheet);
985   RCP<BoolVisualDependency> boolDep1 =
986     DummyObjectGetter<BoolVisualDependency>::getDummyObject();
987   TEST_ASSERT(depSheet1->empty());
988   depSheet1->addDependency(boolDep1);
989   TEST_ASSERT(!depSheet1->empty());
990 
991 
992   RCP<DependencySheet> depSheet2 = rcp(new DependencySheet);
993   RCP<StringVisualDependency> stringDep1 =
994     DummyObjectGetter<StringVisualDependency>::getDummyObject();
995   depSheet1->addDependency(stringDep1);
996   RCP<StringValidatorDependency> stringValiDep1 =
997     DummyObjectGetter<StringValidatorDependency>::getDummyObject();
998   depSheet1->addDependency(stringValiDep1);
999 
1000   depSheet1->addDependencies(depSheet2);
1001   TEST_EQUALITY_CONST(depSheet1->size(), 3);
1002   bool found1 = false;
1003   bool found2 = false;
1004   bool found3 = false;
1005   for(
1006     DependencySheet::DepSet::iterator it = depSheet1->depBegin();
1007     it != depSheet1->depEnd();
1008     ++it
1009   )
1010   {
1011     if(*it == boolDep1){
1012       found1 = true;
1013     }
1014     else if(*it == stringDep1){
1015       found2 = true;
1016     }
1017     else if(*it == stringValiDep1){
1018       found3 = true;
1019     }
1020   }
1021   TEST_ASSERT(found1);
1022   TEST_ASSERT(found2);
1023   TEST_ASSERT(found3);
1024 }
1025 
1026 
1027 } //namespace Teuchos
1028 
1029