Implemented support for using textual names in randomization.

This commit is contained in:
Per Malmberg
2019-05-17 13:39:32 +02:00
parent a918f3d93f
commit d61086f69e
9 changed files with 180 additions and 23 deletions

View File

@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.6)
project(cron_test)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
if( MSVC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")

View File

@@ -224,4 +224,17 @@ SCENARIO("Dates that does not exist")
SCENARIO("Date that exist in one of the months")
{
REQUIRE(CronData::create("0 0 * 31 APR,MAY ?").is_valid());
}
SCENARIO("Replacing text with numbers")
{
{
std::string s = "SUN-TUE";
REQUIRE(CronData::replace_string_name_with_numeric<libcron::DayOfWeek>(s) == "0-2");
}
{
std::string s = "JAN-DEC";
REQUIRE(CronData::replace_string_name_with_numeric<libcron::Months>(s) == "1-12");
}
}

View File

@@ -7,21 +7,31 @@
#include <iostream>
using namespace libcron;
const auto EXPECT_FAILURE = true;
void test(const char* const random_schedule)
void test(const char* const random_schedule, bool expect_failure = false)
{
libcron::CronRandomization cr;
std::unordered_map<int, std::unordered_map<int, int>> results{};
for (int i = 0; i < 5000; ++i)
{
auto res = cr.parse(random_schedule);
REQUIRE(std::get<0>(res));
auto schedule = std::get<1>(res);
INFO("schedule:" << schedule);
Cron<> cron;
REQUIRE(cron.add_schedule("validate schedule", schedule, []() {}));
if(expect_failure)
{
// Parsing of random might succeed, but it yields an invalid schedule.
auto r = std::get<0>(res) && cron.add_schedule("validate schedule", schedule, []() {});
REQUIRE_FALSE(r);
}
else
{
REQUIRE(std::get<0>(res));
REQUIRE(cron.add_schedule("validate schedule", schedule, []() {}));
}
}
}
@@ -103,3 +113,68 @@ SCENARIO("Test readme examples")
}
}
}
SCENARIO("Randomization using text versions of days and months")
{
GIVEN("0 0 0 ? * R(TUE-FRI)")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? * R(TUE-FRI)");
}
}
GIVEN("Valid schedule")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? R(JAN-DEC) R(MON-FRI)");
}
AND_WHEN("Given 0 0 0 ? R(DEC-MAR) R(SAT-SUN)")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? R(DEC-MAR) R(SAT-SUN)");
}
}
AND_THEN("Given 0 0 0 ? R(JAN-FEB) *")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? R(JAN-FEB) *");
}
}
AND_THEN("Given 0 0 0 ? R(OCT-OCT) *")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? R(OCT-OCT) *");
}
}
}
GIVEN("Invalid schedule")
{
THEN("No schedule generated")
{
// Day of month specified - not allowed with day of week
test("0 0 0 1 R(JAN-DEC) R(MON-SUN)", EXPECT_FAILURE);
}
AND_THEN("No schedule generated")
{
// Invalid range
test("0 0 0 ? R(JAN) *", EXPECT_FAILURE);
}
AND_THEN("No schedule generated")
{
// Days in month field
test("0 0 0 ? R(MON-TUE) *", EXPECT_FAILURE);
}
AND_THEN("No schedule generated")
{
// Month in day field
test("0 0 0 ? * R(JAN-JUN)", EXPECT_FAILURE);
}
}
}