From 3d73a872a73bd6fc3def5b8dbb4c65be5e644ab3 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Wed, 28 Jan 2009 12:03:00 +0100 Subject: [PATCH] fix casts in unit-test, return StNimmerleinsDay for never, take care of very short intervals --- src/cron.cpp | 11 +++++-- src/cron.hpp | 5 ++- test/test_cron.cpp | 86 +++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/cron.cpp b/src/cron.cpp index 463bbdb..16a2bb3 100644 --- a/src/cron.cpp +++ b/src/cron.cpp @@ -13,6 +13,8 @@ #include +const time_t WeekCron::StNimmerleinsDay = static_cast(-1); + /** * Constructor * @param daystring String representing the active weekdays as numbers. 0 is Sunday. @@ -63,10 +65,12 @@ bool WeekCron::is_sane() const } /** - * Returns the next point in time the item is scheduled for. Also handles intervals. + * Returns the next point in time the item is scheduled for. + * Handles the full possibilities of WeekCron. * @note if it is scheduled for calc_from we return the next schedule, not now! * @param calc_from unix-time to start calculating from (0 means now) * @return Next point in time the item is scheduled for + * returns constant #StNimmerleinsDay if it is scheduled never */ time_t WeekCron::get_next_run(time_t calc_from) { @@ -80,7 +84,7 @@ time_t WeekCron::get_next_run(time_t calc_from) throw std::runtime_error("WeekCron doesn't work for times near 0"); if (Week.none_set()) - return 0; + return StNimmerleinsDay; if (Every == -1) { @@ -115,7 +119,7 @@ time_t WeekCron::get_next_run(time_t calc_from) } /** - * Returns the next point in time the item is scheduled for. + * Returns the next point in time the item is scheduled for. Does not care about intervals. * Returns next point if scheduled for #calc_from. * @param calc_from Point of time to start calculations from * @param daysec Start point of time in seconds since the start of the day @@ -152,6 +156,7 @@ time_t WeekCron::get_next_point(time_t calc_from, int daysec, bool todaycheck) /** * Returns the previous or current point in time the item was scheduled for. + * Does not care about intervals. * Returns #calc_from if scheduled for #calc_from. * @param calc_from Point of time to start calculations from * @param daysec Start point of time in seconds since the start of the day diff --git a/src/cron.hpp b/src/cron.hpp index f3d98a8..d976e78 100644 --- a/src/cron.hpp +++ b/src/cron.hpp @@ -14,13 +14,16 @@ #include -/// Time points and intervals repeating each week /** + @brief Time points and intervals repeating each week This class represents recurring time points and intervals which can be repeated on configurable days of the week. */ class WeekCron { + public: + static const time_t StNimmerleinsDay; + private: /// Start point of time in seconds since the start of the day int Begin; diff --git a/test/test_cron.cpp b/test/test_cron.cpp index dacba9b..f3ad0c1 100644 --- a/test/test_cron.cpp +++ b/test/test_cron.cpp @@ -52,6 +52,11 @@ class TestCronFunc : public TestFixture CPPUNIT_TEST(IntervalWholeDayWithin1); CPPUNIT_TEST(IntervalWholeDayWithin2); CPPUNIT_TEST(IntervalWholeDayWithinBoundary); + CPPUNIT_TEST(IntervalBeforeOnce); + CPPUNIT_TEST(IntervalBeforeOnceBig); + CPPUNIT_TEST(IntervalAfterOnce); + CPPUNIT_TEST(IntervalOnceShort); + CPPUNIT_TEST(IntervalTooShort); CPPUNIT_TEST_SUITE_END(); @@ -73,61 +78,61 @@ public: void NotYetToday() { WeekCron cron("2345",3600); - CPPUNIT_ASSERT_EQUAL( 1233100800, static_cast(cron.get_next_run(1233099657))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233100800), cron.get_next_run(1233099657)); } void Now() { WeekCron cron("2345",3600); - CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233100800))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233187200), cron.get_next_run(1233100800)); } void LaterToday() { WeekCron cron("2345",3600); - CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233100801))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233187200), cron.get_next_run(1233100801)); } void Tomorrow() { WeekCron cron("45",3600); - CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233099657))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233187200), cron.get_next_run(1233099657)); } void NextWeek() { WeekCron cron("1",3600); - CPPUNIT_ASSERT_EQUAL( 1233532800, static_cast(cron.get_next_run(1233099657))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233532800), cron.get_next_run(1233099657)); } void NextWeekFromToday() { WeekCron cron("13",3600); - CPPUNIT_ASSERT_EQUAL( 1233532800, static_cast(cron.get_next_run(1233100801))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233532800), cron.get_next_run(1233100801)); } void StartMidnight() { WeekCron cron("2345",0); - CPPUNIT_ASSERT_EQUAL( 1233097200, static_cast(cron.get_next_run(1233097199))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233097200), cron.get_next_run(1233097199)); } void StartMidnightTomorrow() { WeekCron cron("2345",0); - CPPUNIT_ASSERT_EQUAL( 1233183600, static_cast(cron.get_next_run(1233097200))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233183600), cron.get_next_run(1233097200)); } void StartLastDaysec() { WeekCron cron("2345",86399); - CPPUNIT_ASSERT_EQUAL( 1233183599, static_cast(cron.get_next_run(1233097200))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233183599), cron.get_next_run(1233097200)); } void HalfOpenInterval() { WeekCron cron("2345",86400); - CPPUNIT_ASSERT_THROW(static_cast(cron.get_next_run(1233097200)),std::runtime_error); + CPPUNIT_ASSERT_THROW(cron.get_next_run(1233097200),std::runtime_error); } ///////////////////////////////////////////////////// @@ -137,93 +142,124 @@ public: void IntervalBeginToday() { WeekCron cron("2345",3600,7200,10); - CPPUNIT_ASSERT_EQUAL( 1233100800, static_cast(cron.get_next_run(1233099657))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233100800), cron.get_next_run(1233099657)); } void IntervalDoneToday() { WeekCron cron("2345",3600); - CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233150000))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233187200), cron.get_next_run(1233150000)); } void IntervalBeginNow() { WeekCron cron("2345",3600,7200,10); - CPPUNIT_ASSERT_EQUAL( 1233100810, static_cast(cron.get_next_run(1233100800))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233100810), cron.get_next_run(1233100800)); } void IntervalBeginStep() { WeekCron cron("2345",3600,7200,10); - CPPUNIT_ASSERT_EQUAL( 1233100820, static_cast(cron.get_next_run(1233100810))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233100820), cron.get_next_run(1233100810)); } void IntervalWithin() { WeekCron cron("2345",3600,7200,10); - CPPUNIT_ASSERT_EQUAL( 1233100830, static_cast(cron.get_next_run(1233100822))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233100830), cron.get_next_run(1233100822)); } void IntervalLaststep() { WeekCron cron("2345",3600,7200,11); - CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233104399))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233187200), cron.get_next_run(1233104399)); } void IntervalLaststepMatch() { WeekCron cron("2345",3600,7200,10); - CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233104399))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233187200), cron.get_next_run(1233104399)); } void IntervalEnd() { WeekCron cron("2345",3600,7200,10); - CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233104400))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233187200), cron.get_next_run(1233104400)); } void IntervalBigstep() { WeekCron cron("2345",3600,7200,10000); - CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233100800))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233187200), cron.get_next_run(1233100800)); } void IntervalWholeDayStart() { WeekCron cron("345",0,86400,10); - CPPUNIT_ASSERT_EQUAL( 1233097200, static_cast(cron.get_next_run(1233097199))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233097200), cron.get_next_run(1233097199)); } void IntervalWholeDayStartNow() { WeekCron cron("345",0,86400,10); - CPPUNIT_ASSERT_EQUAL( 1233097210, static_cast(cron.get_next_run(1233097200))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233097210), cron.get_next_run(1233097200)); } void IntervalWholeDayEnd() { WeekCron cron("2",0,86400,10); - CPPUNIT_ASSERT_EQUAL( 1233615600, static_cast(cron.get_next_run(1233097199))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233615600), cron.get_next_run(1233097199)); } void IntervalWholeDayWithin1() { WeekCron cron("2345",0,86400,10); - CPPUNIT_ASSERT_EQUAL( 1233097230, static_cast(cron.get_next_run(1233097220))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233097230), cron.get_next_run(1233097220)); } void IntervalWholeDayWithin2() { WeekCron cron("2345",0,86400,10); - CPPUNIT_ASSERT_EQUAL( 1233097230, static_cast(cron.get_next_run(1233097229))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233097230), cron.get_next_run(1233097229)); } void IntervalWholeDayWithinBoundary() { WeekCron cron("2345",0,86400,10); - CPPUNIT_ASSERT_EQUAL( 1233097200, static_cast(cron.get_next_run(1233097199))); + CPPUNIT_ASSERT_EQUAL( static_cast(1233097200), cron.get_next_run(1233097199)); } + void IntervalBeforeOnce() + { + WeekCron cron("2345",0,3600,3600); + CPPUNIT_ASSERT_EQUAL( static_cast(1233183600), cron.get_next_run(1233100799)); + } + + void IntervalBeforeOnceBig() + { + WeekCron cron("2345",0,3600,86400); + CPPUNIT_ASSERT_EQUAL( static_cast(1233183600), cron.get_next_run(1233100799)); + } + + void IntervalAfterOnce() + { + WeekCron cron("2345",0,3600,86400); + CPPUNIT_ASSERT_EQUAL( static_cast(1233183600), cron.get_next_run(1233097200)); + } + + void IntervalOnceShort() + { + WeekCron cron("2345",0,1,86400); + CPPUNIT_ASSERT_EQUAL( static_cast(1233097200), cron.get_next_run(1233097199)); + } + + void IntervalTooShort() + { + WeekCron cron("2345",0,0,86400); + CPPUNIT_ASSERT_EQUAL( static_cast(1233183600), cron.get_next_run(1233097200)); + } + + // TODO: Tom, add some evil tests here, im done here for yesterday ;) // ideas: -- 1.7.1