fix off-by-one / half-open-interval on end of interval, add unit-tests
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 28 Jan 2009 09:13:56 +0000 (10:13 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 28 Jan 2009 09:13:56 +0000 (10:13 +0100)
src/cron.cpp
test/test_cron.cpp

index bae1c9c..6a4118a 100644 (file)
@@ -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
 
index 532b1b3..dacba9b 100644 (file)
@@ -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<int>(cron.get_next_run(1233100800)));
     }
 
+    void IntervalWholeDayStart()
+    {
+        WeekCron cron("345",0,86400,10);
+        CPPUNIT_ASSERT_EQUAL( 1233097200, static_cast<int>(cron.get_next_run(1233097199)));
+    }
+
+    void IntervalWholeDayStartNow()
+    {
+        WeekCron cron("345",0,86400,10);
+        CPPUNIT_ASSERT_EQUAL( 1233097210, static_cast<int>(cron.get_next_run(1233097200)));
+    }
+
+    void IntervalWholeDayEnd()
+    {
+        WeekCron cron("2",0,86400,10);
+        CPPUNIT_ASSERT_EQUAL( 1233615600, static_cast<int>(cron.get_next_run(1233097199)));
+    }
+
+    void IntervalWholeDayWithin1()
+    {
+        WeekCron cron("2345",0,86400,10);
+        CPPUNIT_ASSERT_EQUAL( 1233097230, static_cast<int>(cron.get_next_run(1233097220)));
+    }
+
+    void IntervalWholeDayWithin2()
+    {
+        WeekCron cron("2345",0,86400,10);
+        CPPUNIT_ASSERT_EQUAL( 1233097230, static_cast<int>(cron.get_next_run(1233097229)));
+    }
+
+    void IntervalWholeDayWithinBoundary()
+    {
+        WeekCron cron("2345",0,86400,10);
+        CPPUNIT_ASSERT_EQUAL( 1233097200, static_cast<int>(cron.get_next_run(1233097199)));
+    }
+
+
     // TODO: Tom, add some evil tests here, im done here for yesterday ;)
     // ideas:
     // - daylightsaving on