From: Thomas Jarosch Date: Wed, 28 Jan 2009 09:13:56 +0000 (+0100) Subject: fix off-by-one / half-open-interval on end of interval, add unit-tests X-Git-Tag: v2.6~136^2~20 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=d2936cf9e418ac7f6699ab6a3226e9653ddb94ac;p=libi2ncommon fix off-by-one / half-open-interval on end of interval, add unit-tests --- diff --git a/src/cron.cpp b/src/cron.cpp index bae1c9c..6a4118a 100644 --- a/src/cron.cpp +++ b/src/cron.cpp @@ -23,8 +23,8 @@ bool WeekCron::is_sane() if (every != -1) { - if (end < 0 || end > 86399 || - every < 1 || every > 86399 || + if (end < 0 || end > 86400 || + every < 1 || every > 86400 || begin > end) return false; } @@ -59,7 +59,7 @@ time_t WeekCron::get_next_run(time_t calc_from) else { // interval - if (get_next_point(calc_from,begin,true) > get_next_point(calc_from,end,true)) + if (get_next_point(calc_from,begin,true) > get_next_point(calc_from,end-1,true)) { // next begin > next end means we are at the begin or within the interval diff --git a/test/test_cron.cpp b/test/test_cron.cpp index 532b1b3..dacba9b 100644 --- a/test/test_cron.cpp +++ b/test/test_cron.cpp @@ -46,6 +46,12 @@ class TestCronFunc : public TestFixture CPPUNIT_TEST(IntervalLaststepMatch); CPPUNIT_TEST(IntervalEnd); CPPUNIT_TEST(IntervalBigstep); + CPPUNIT_TEST(IntervalWholeDayStart); + CPPUNIT_TEST(IntervalWholeDayStartNow); + CPPUNIT_TEST(IntervalWholeDayEnd); + CPPUNIT_TEST(IntervalWholeDayWithin1); + CPPUNIT_TEST(IntervalWholeDayWithin2); + CPPUNIT_TEST(IntervalWholeDayWithinBoundary); CPPUNIT_TEST_SUITE_END(); @@ -182,6 +188,43 @@ public: CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233100800))); } + void IntervalWholeDayStart() + { + WeekCron cron("345",0,86400,10); + CPPUNIT_ASSERT_EQUAL( 1233097200, static_cast(cron.get_next_run(1233097199))); + } + + void IntervalWholeDayStartNow() + { + WeekCron cron("345",0,86400,10); + CPPUNIT_ASSERT_EQUAL( 1233097210, static_cast(cron.get_next_run(1233097200))); + } + + void IntervalWholeDayEnd() + { + WeekCron cron("2",0,86400,10); + CPPUNIT_ASSERT_EQUAL( 1233615600, static_cast(cron.get_next_run(1233097199))); + } + + void IntervalWholeDayWithin1() + { + WeekCron cron("2345",0,86400,10); + CPPUNIT_ASSERT_EQUAL( 1233097230, static_cast(cron.get_next_run(1233097220))); + } + + void IntervalWholeDayWithin2() + { + WeekCron cron("2345",0,86400,10); + CPPUNIT_ASSERT_EQUAL( 1233097230, static_cast(cron.get_next_run(1233097229))); + } + + void IntervalWholeDayWithinBoundary() + { + WeekCron cron("2345",0,86400,10); + CPPUNIT_ASSERT_EQUAL( 1233097200, static_cast(cron.get_next_run(1233097199))); + } + + // TODO: Tom, add some evil tests here, im done here for yesterday ;) // ideas: // - daylightsaving on