| 1 |
|
| 2 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 3 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 4 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 5 |
// in this software. |
| 6 |
// |
| 7 |
// Redistribution and use in source and binary forms, with or without |
| 8 |
// modification, are permitted provided that the following conditions are |
| 9 |
// met: |
| 10 |
// |
| 11 |
// * Redistributions of source code must retain the above copyright |
| 12 |
// notice, this list of conditions and the following disclaimer. |
| 13 |
// |
| 14 |
// * Redistributions in binary form must reproduce the above |
| 15 |
// copyright notice, this list of conditions and the following |
| 16 |
// disclaimer in the documentation and/or other materials provided |
| 17 |
// with the distribution. |
| 18 |
// |
| 19 |
// * Neither the name of NTESS nor the names of its contributors |
| 20 |
// may be used to endorse or promote products derived from this |
| 21 |
// software without specific prior written permission. |
| 22 |
// |
| 23 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 26 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 27 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 28 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 29 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 31 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 33 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 |
// |
| 35 |
|
| 36 |
//BEGINSchedulerExample |
| 37 |
#include "gtest/gtest.h" |
| 38 |
#include "stk_util/environment/Scheduler.hpp" // for Scheduler, Time, Step |
| 39 |
|
| 40 |
namespace |
| 41 |
{ |
| 42 |
TEST(StkUtilTestForDocumentation, TimeBasedScheduling) |
| 43 |
{ |
| 44 |
stk::util::Scheduler scheduler; |
| 45 |
|
| 46 |
const stk::util::Time startTime = 0.0; |
| 47 |
const stk::util::Time timeInterval = 1.0; |
| 48 |
scheduler.add_interval(startTime, timeInterval); |
| 49 |
|
| 50 |
stk::util::Step timeStep = 0; |
| 51 |
EXPECT_TRUE(scheduler.is_it_time(0.0, timeStep++)); |
| 52 |
EXPECT_FALSE(scheduler.is_it_time(0.5, timeStep++)); |
| 53 |
EXPECT_TRUE(scheduler.is_it_time(1.0, timeStep++)); |
| 54 |
} |
| 55 |
|
| 56 |
TEST(StkUtilTestForDocumentation, TimeBasedSchedulingWithTerminationTime) |
| 57 |
{ |
| 58 |
stk::util::Scheduler scheduler; |
| 59 |
|
| 60 |
const stk::util::Time startTime = 2.0; |
| 61 |
const stk::util::Time timeInterval = 10.0; |
| 62 |
scheduler.add_interval(startTime, timeInterval); |
| 63 |
|
| 64 |
const stk::util::Time terminationTime = 8.2; |
| 65 |
scheduler.set_termination_time(terminationTime); |
| 66 |
|
| 67 |
stk::util::Step timeStep = 0; |
| 68 |
EXPECT_FALSE(scheduler.is_it_time(startTime - 1.0, timeStep++)); |
| 69 |
const stk::util::Time firstTimeAfterStartTime = terminationTime-0.1; |
| 70 |
EXPECT_TRUE(scheduler.is_it_time(firstTimeAfterStartTime, timeStep++)); |
| 71 |
const stk::util::Time firstAfterTermination = terminationTime+0.1; |
| 72 |
EXPECT_TRUE(scheduler.is_it_time(firstAfterTermination, timeStep++)); |
| 73 |
EXPECT_FALSE(scheduler.is_it_time(terminationTime+0.2, timeStep++)); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(StkUtilTestForDocumentation, StepBasedScheduler) |
| 77 |
{ |
| 78 |
stk::util::Scheduler scheduler; |
| 79 |
|
| 80 |
const stk::util::Step startStep = 0; |
| 81 |
const stk::util::Step stepInterval = 4; |
| 82 |
scheduler.add_interval(startStep, stepInterval); |
| 83 |
|
| 84 |
const stk::util::Time dt = 0.1; |
| 85 |
for (stk::util::Step timeStep=0;timeStep<100;timeStep+=3) |
| 86 |
{ |
| 87 |
stk::util::Time time = timeStep*dt; |
| 88 |
bool check = scheduler.is_it_time(time, timeStep); |
| 89 |
if ( timeStep % stepInterval == 0 ) |
| 90 |
{ |
| 91 |
EXPECT_TRUE(check); |
| 92 |
} |
| 93 |
else |
| 94 |
{ |
| 95 |
EXPECT_FALSE(check); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
TEST(StkUtilTestForDocumentation, TimeBasedSchedulerWithTwoTimeIntervals) |
| 101 |
{ |
| 102 |
stk::util::Scheduler scheduler; |
| 103 |
const stk::util::Time startTime1 = 0.0; |
| 104 |
const stk::util::Time delta1 = 0.1; |
| 105 |
scheduler.add_interval(startTime1, delta1); |
| 106 |
const stk::util::Time startTime2 = 0.9; |
| 107 |
const stk::util::Time delta2 = 0.3; |
| 108 |
scheduler.add_interval(startTime2, delta2); |
| 109 |
|
| 110 |
stk::util::Step timeStep = 0; |
| 111 |
EXPECT_TRUE(scheduler.is_it_time(0.0, timeStep++)); |
| 112 |
EXPECT_FALSE(scheduler.is_it_time(0.07, timeStep++)); |
| 113 |
EXPECT_TRUE(scheduler.is_it_time(0.14, timeStep++)); |
| 114 |
EXPECT_TRUE(scheduler.is_it_time(0.62, timeStep++)); |
| 115 |
EXPECT_TRUE(scheduler.is_it_time(0.6999999, timeStep++)); |
| 116 |
EXPECT_FALSE(scheduler.is_it_time(0.77, timeStep++)); |
| 117 |
EXPECT_TRUE(scheduler.is_it_time(0.9, timeStep++)); |
| 118 |
EXPECT_FALSE(scheduler.is_it_time(0.97, timeStep++)); |
| 119 |
EXPECT_FALSE(scheduler.is_it_time(1.04, timeStep++)); |
| 120 |
EXPECT_FALSE(scheduler.is_it_time(1.11, timeStep++)); |
| 121 |
EXPECT_TRUE(scheduler.is_it_time(1.27, timeStep++)); |
| 122 |
} |
| 123 |
} |
| 124 |
//ENDSchedulerExample |