diff --git a/README.md b/README.md index 24f4f1b..8efbbde 100644 --- a/README.md +++ b/README.md @@ -56,18 +56,33 @@ Each part is separated by one or more whitespaces. It is thus important to keep * 0, 3, 40-50 * * * * ? `Day of month` and `day of week` are mutually exclusive so one of them must at always be ignored using -the '?'-character unless one field already is something other than '*'. +the '?'-character to ensure that it is not possible to specify a statement which results in an impossible mix of these fields. -# Examples +## Examples |Expression | Meaning | --- | --- | | * * * * * ? | Every second -|0 0 12 * * MON-FRI | Every Weekday at noon -|0 0 12 1/2 * ? | Every 2 days, starting on the 1st at noon +| 0 0 12 * * MON-FRI | Every Weekday at noon +| 0 0 12 1/2 * ? | Every 2 days, starting on the 1st at noon | 0 0 */12 ? * * | Every twelve hours -# Third party libraries +# Randomization + +The standard cron format does not allow for randomization, but with the use of `CronRandomization` you can generate random +schedules using the following format: `R(range_start-range_end)`, where `range_start` and `range_end` follow the same rules +as for a regular cron range with the addition that only numbers are allowed. All the rules for a regular cron expression +still applies when using randomization, i.e. mutual exclusiveness and not extra spaces. + +## Examples +|Expression | Meaning +| --- | --- | +| 0 0 R(13-20) * * ? | On the hour, on a random hour 13-20, inclusive. +| 0 0 0 ? * R(0-6) | A random weekday, every week, at midnight. +| 0 R(45-15) */12 ? * * | A random minute between 45-15, inclusive, every 12 hours. + + +# Used Third party libraries Howard Hinnant's [date libraries](https://github.com/HowardHinnant/date/) diff --git a/test/CronRandomizationTest.cpp b/test/CronRandomizationTest.cpp index 6fc469f..7f9b8d1 100644 --- a/test/CronRandomizationTest.cpp +++ b/test/CronRandomizationTest.cpp @@ -13,7 +13,7 @@ void test(const char* const random_schedule) libcron::CronRandomization cr; std::unordered_map> results{}; - for (int i = 0; i < 50000; ++i) + for (int i = 0; i < 5000; ++i) { auto res = cr.parse(random_schedule); REQUIRE(std::get<0>(res)); @@ -76,3 +76,30 @@ SCENARIO("Randomize all the things with reverse ranges - day of week") } } } + +SCENARIO("Test readme examples") +{ + GIVEN("0 0 R(13-20) * * ?") + { + THEN("Valid schedule generated") + { + test("0 0 R(13-20) * * ?"); + } + } + + GIVEN("0 0 0 ? * R(0-6)") + { + THEN("Valid schedule generated") + { + test("0 0 0 ? * R(0-6)"); + } + } + + GIVEN("0 R(45-15) */12 ? * *") + { + THEN("Valid schedule generated") + { + test("0 R(45-15) */12 ? * *"); + } + } +} diff --git a/test/out/cron_test b/test/out/cron_test index 3d03f55..4ef17cd 100755 Binary files a/test/out/cron_test and b/test/out/cron_test differ