mirror of
https://github.com/PerMalmberg/libcron.git
synced 2025-07-03 09:22:57 -05:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
b82267acca | |||
d61086f69e | |||
a918f3d93f | |||
bdc5054354 | |||
e725abf87f | |||
802d8e724e | |||
18dc065f00 | |||
2a3b8914e5 | |||
70f55b8ce6 | |||
6ed4bc3b2e | |||
c20a146980 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -34,3 +34,4 @@
|
|||||||
cmake-build-*
|
cmake-build-*
|
||||||
.idea/workspace.xml
|
.idea/workspace.xml
|
||||||
out/*
|
out/*
|
||||||
|
test/out*
|
||||||
|
29
.idea/codeStyles/Project.xml
generated
29
.idea/codeStyles/Project.xml
generated
@ -1,29 +0,0 @@
|
|||||||
<component name="ProjectCodeStyleConfiguration">
|
|
||||||
<code_scheme name="Project" version="173">
|
|
||||||
<Objective-C-extensions>
|
|
||||||
<file>
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
|
|
||||||
</file>
|
|
||||||
<class>
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
|
|
||||||
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
|
|
||||||
</class>
|
|
||||||
<extensions>
|
|
||||||
<pair source="cpp" header="h" fileNamingConvention="NONE" />
|
|
||||||
<pair source="c" header="h" fileNamingConvention="NONE" />
|
|
||||||
</extensions>
|
|
||||||
</Objective-C-extensions>
|
|
||||||
</code_scheme>
|
|
||||||
</component>
|
|
1
.idea/vcs.xml
generated
1
.idea/vcs.xml
generated
@ -2,5 +2,6 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
<mapping directory="$PROJECT_DIR$/libcron/externals/date" vcs="Git" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -1,7 +1,5 @@
|
|||||||
cmake_minimum_required(VERSION 3.6)
|
cmake_minimum_required(VERSION 3.6)
|
||||||
|
|
||||||
set(OUTPUT_LOCATION ${CMAKE_CURRENT_LIST_DIR}/out/)
|
|
||||||
|
|
||||||
add_subdirectory(libcron)
|
add_subdirectory(libcron)
|
||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
|
|
||||||
|
22
README.md
22
README.md
@ -56,9 +56,9 @@ Each part is separated by one or more whitespaces. It is thus important to keep
|
|||||||
* 0, 3, 40-50 * * * * ?
|
* 0, 3, 40-50 * * * * ?
|
||||||
|
|
||||||
`Day of month` and `day of week` are mutually exclusive so one of them must at always be ignored using
|
`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
|
|Expression | Meaning
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
@ -67,7 +67,23 @@ the '?'-character unless one field already is something other than '*'.
|
|||||||
| 0 0 12 1/2 * ? | Every 2 days, starting on the 1st at noon
|
| 0 0 12 1/2 * ? | Every 2 days, starting on the 1st at noon
|
||||||
| 0 0 */12 ? * * | Every twelve hours
|
| 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 (step-syntax is not supported). All the rules for a regular cron expression still applies
|
||||||
|
when using randomization, i.e. mutual exclusiveness and no 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.
|
||||||
|
|0 0 0 ? R(DEC-MAR) R(SAT-SUN)| On the hour, on a random month december to march, on a random weekday saturday to sunday.
|
||||||
|
|
||||||
|
|
||||||
|
# Used Third party libraries
|
||||||
|
|
||||||
Howard Hinnant's [date libraries](https://github.com/HowardHinnant/date/)
|
Howard Hinnant's [date libraries](https://github.com/HowardHinnant/date/)
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.6)
|
cmake_minimum_required(VERSION 3.6)
|
||||||
project(libcron)
|
project(libcron)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
if( MSVC )
|
if( MSVC )
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
||||||
@ -9,22 +9,26 @@ else()
|
|||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_LIST_DIR}/externals/date/include)
|
|
||||||
|
|
||||||
add_library(${PROJECT_NAME}
|
add_library(${PROJECT_NAME}
|
||||||
Cron.h
|
include/libcron/Cron.h
|
||||||
Task.h
|
include/libcron/CronClock.h
|
||||||
CronData.h
|
include/libcron/CronData.h
|
||||||
TimeTypes.h
|
include/libcron/CronRandomization.h
|
||||||
CronData.cpp
|
include/libcron/CronSchedule.h
|
||||||
CronSchedule.cpp
|
include/libcron/DateTime.h
|
||||||
CronSchedule.h
|
include/libcron/Task.h
|
||||||
DateTime.h
|
include/libcron/TimeTypes.h
|
||||||
Task.cpp
|
src/CronClock.cpp
|
||||||
CronClock.h
|
src/CronData.cpp
|
||||||
CronClock.cpp)
|
src/CronRandomization.cpp
|
||||||
|
src/CronSchedule.cpp
|
||||||
|
src/Task.cpp)
|
||||||
|
|
||||||
|
target_include_directories(${PROJECT_NAME}
|
||||||
|
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/externals/date/include
|
||||||
|
PUBLIC include)
|
||||||
|
|
||||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||||
ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_LOCATION}"
|
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_LOCATION}"
|
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_LOCATION}")
|
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}")
|
@ -1,43 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
namespace libcron
|
|
||||||
{
|
|
||||||
enum class Seconds : int8_t
|
|
||||||
{
|
|
||||||
First = 0,
|
|
||||||
Last = 59
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class Minutes : int8_t
|
|
||||||
{
|
|
||||||
First = 0,
|
|
||||||
Last = 59
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class Hours : int8_t
|
|
||||||
{
|
|
||||||
First = 0,
|
|
||||||
Last = 23
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class DayOfMonth : uint8_t
|
|
||||||
{
|
|
||||||
First = 1,
|
|
||||||
Last = 31
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class Months : uint8_t
|
|
||||||
{
|
|
||||||
First = 1,
|
|
||||||
Last = 12
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class DayOfWeek : uint8_t
|
|
||||||
{
|
|
||||||
// Sunday = 0 ... Saturday = 6
|
|
||||||
First = 0,
|
|
||||||
Last = 6,
|
|
||||||
};
|
|
||||||
}
|
|
@ -4,16 +4,20 @@
|
|||||||
#include <regex>
|
#include <regex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "TimeTypes.h"
|
#include <libcron/TimeTypes.h>
|
||||||
|
|
||||||
namespace libcron
|
namespace libcron
|
||||||
{
|
{
|
||||||
class CronData
|
class CronData
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static const int NUMBER_OF_LONG_MONTHS = 7;
|
||||||
|
static const libcron::Months months_with_31[NUMBER_OF_LONG_MONTHS];
|
||||||
|
|
||||||
static CronData create(const std::string& cron_expression);
|
static CronData create(const std::string& cron_expression);
|
||||||
|
|
||||||
CronData();
|
CronData() = default;
|
||||||
|
|
||||||
CronData(const CronData&) = default;
|
CronData(const CronData&) = default;
|
||||||
|
|
||||||
bool is_valid() const
|
bool is_valid() const
|
||||||
@ -61,6 +65,7 @@ namespace libcron
|
|||||||
static bool has_any_in_range(const std::set<T>& set, uint8_t low, uint8_t high)
|
static bool has_any_in_range(const std::set<T>& set, uint8_t low, uint8_t high)
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
|
||||||
for (auto i = low; !found && i <= high; ++i)
|
for (auto i = low; !found && i <= high; ++i)
|
||||||
{
|
{
|
||||||
found |= set.find(static_cast<T>(i)) != set.end();
|
found |= set.find(static_cast<T>(i)) != set.end();
|
||||||
@ -69,8 +74,13 @@ namespace libcron
|
|||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
template<typename T>
|
||||||
|
bool convert_from_string_range_to_number_range(const std::string& range, std::set<T>& numbers);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static std::string& replace_string_name_with_numeric(std::string& s);
|
||||||
|
|
||||||
|
private:
|
||||||
void parse(const std::string& cron_expression);
|
void parse(const std::string& cron_expression);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -114,8 +124,8 @@ namespace libcron
|
|||||||
std::set<DayOfWeek> day_of_week{};
|
std::set<DayOfWeek> day_of_week{};
|
||||||
bool valid = false;
|
bool valid = false;
|
||||||
|
|
||||||
std::vector<std::string> month_names;
|
static const std::vector<std::string> month_names;
|
||||||
std::vector<std::string> day_names;
|
static const std::vector<std::string> day_names;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void add_full_range(std::set<T>& set);
|
void add_full_range(std::set<T>& set);
|
||||||
@ -142,20 +152,20 @@ namespace libcron
|
|||||||
for (const auto& name : names)
|
for (const auto& name : names)
|
||||||
{
|
{
|
||||||
std::regex m(name, std::regex_constants::ECMAScript | std::regex_constants::icase);
|
std::regex m(name, std::regex_constants::ECMAScript | std::regex_constants::icase);
|
||||||
for (size_t i = 0; i < parts.size(); ++i)
|
|
||||||
|
for (auto& part : parts)
|
||||||
{
|
{
|
||||||
std::string replaced;
|
std::string replaced;
|
||||||
std::regex_replace(std::back_inserter(replaced), parts[i].begin(), parts[i].end(), m,
|
std::regex_replace(std::back_inserter(replaced), part.begin(), part.end(), m,
|
||||||
std::to_string(value_of_first_name));
|
std::to_string(value_of_first_name));
|
||||||
|
|
||||||
parts[i] = replaced;
|
part = replaced;
|
||||||
}
|
}
|
||||||
|
|
||||||
value_of_first_name++;
|
value_of_first_name++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return process_parts(parts, numbers);
|
return process_parts(parts, numbers);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -163,60 +173,9 @@ namespace libcron
|
|||||||
{
|
{
|
||||||
bool res = true;
|
bool res = true;
|
||||||
|
|
||||||
T left;
|
|
||||||
T right;
|
|
||||||
uint8_t step_start;
|
|
||||||
uint8_t step;
|
|
||||||
|
|
||||||
for (const auto& p : parts)
|
for (const auto& p : parts)
|
||||||
{
|
{
|
||||||
if (p == "*" || p == "?")
|
res &= convert_from_string_range_to_number_range(p, numbers);
|
||||||
{
|
|
||||||
// We treat the ignore-character '?' the same as the full range being allowed.
|
|
||||||
add_full_range<T>(numbers);
|
|
||||||
}
|
|
||||||
else if (is_number(p))
|
|
||||||
{
|
|
||||||
res &= add_number<T>(numbers, std::stoi(p));
|
|
||||||
}
|
|
||||||
else if (get_range<T>(p, left, right))
|
|
||||||
{
|
|
||||||
// A range can be written as both 1-22 or 22-1, meaning totally different ranges.
|
|
||||||
// First case is 1...22 while 22-1 is only four hours: 22, 23, 0, 1.
|
|
||||||
if (left <= right)
|
|
||||||
{
|
|
||||||
for (auto v = value_of(left); v <= value_of(right); ++v)
|
|
||||||
{
|
|
||||||
res &= add_number(numbers, v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// 'left' and 'right' are not in value order. First, get values between 'left' and T::Last, inclusive
|
|
||||||
for (auto v = value_of(left); v <= value_of(T::Last); ++v)
|
|
||||||
{
|
|
||||||
res &= add_number(numbers, v);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next, get values between T::First and 'right', inclusive.
|
|
||||||
for (auto v = value_of(T::First); v <= value_of(right); ++v)
|
|
||||||
{
|
|
||||||
res &= add_number(numbers, v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (get_step<T>(p, step_start, step))
|
|
||||||
{
|
|
||||||
// Add from step_start to T::Last with a step of 'step'
|
|
||||||
for (auto v = step_start; v <= value_of(T::Last); v += step)
|
|
||||||
{
|
|
||||||
res &= add_number(numbers, v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
res = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
@ -235,8 +194,8 @@ namespace libcron
|
|||||||
|
|
||||||
if (std::regex_match(s.begin(), s.end(), match, range))
|
if (std::regex_match(s.begin(), s.end(), match, range))
|
||||||
{
|
{
|
||||||
auto left = std::stoi(match[1].str().c_str());
|
auto left = std::stoi(match[1].str());
|
||||||
auto right = std::stoi(match[2].str().c_str());
|
auto right = std::stoi(match[2].str());
|
||||||
|
|
||||||
if (is_within_limits<T>(left, right))
|
if (is_within_limits<T>(left, right))
|
||||||
{
|
{
|
||||||
@ -263,16 +222,17 @@ namespace libcron
|
|||||||
if (std::regex_match(s.begin(), s.end(), match, range))
|
if (std::regex_match(s.begin(), s.end(), match, range))
|
||||||
{
|
{
|
||||||
int raw_start;
|
int raw_start;
|
||||||
|
|
||||||
if (match[1].str() == "*")
|
if (match[1].str() == "*")
|
||||||
{
|
{
|
||||||
raw_start = value_of(T::First);
|
raw_start = value_of(T::First);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
raw_start = std::stoi(match[1].str().c_str());
|
raw_start = std::stoi(match[1].str());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto raw_step = std::stoi(match[2].str().c_str());
|
auto raw_step = std::stoi(match[2].str());
|
||||||
|
|
||||||
if (is_within_limits<T>(raw_start, raw_start) && raw_step > 0)
|
if (is_within_limits<T>(raw_start, raw_start) && raw_step > 0)
|
||||||
{
|
{
|
||||||
@ -326,5 +286,100 @@ namespace libcron
|
|||||||
&& is_between(high, value_of(T::First), value_of(T::Last));
|
&& is_between(high, value_of(T::First), value_of(T::Last));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
bool CronData::convert_from_string_range_to_number_range(const std::string& range, std::set<T>& numbers)
|
||||||
|
{
|
||||||
|
T left;
|
||||||
|
T right;
|
||||||
|
uint8_t step_start;
|
||||||
|
uint8_t step;
|
||||||
|
|
||||||
|
bool res = true;
|
||||||
|
|
||||||
|
if (range == "*" || range == "?")
|
||||||
|
{
|
||||||
|
// We treat the ignore-character '?' the same as the full range being allowed.
|
||||||
|
add_full_range<T>(numbers);
|
||||||
|
}
|
||||||
|
else if (is_number(range))
|
||||||
|
{
|
||||||
|
res = add_number<T>(numbers, std::stoi(range));
|
||||||
|
}
|
||||||
|
else if (get_range<T>(range, left, right))
|
||||||
|
{
|
||||||
|
// A range can be written as both 1-22 or 22-1, meaning totally different ranges.
|
||||||
|
// First case is 1...22 while 22-1 is only four hours: 22, 23, 0, 1.
|
||||||
|
if (left <= right)
|
||||||
|
{
|
||||||
|
for (auto v = value_of(left); v <= value_of(right); ++v)
|
||||||
|
{
|
||||||
|
res &= add_number(numbers, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 'left' and 'right' are not in value order. First, get values between 'left' and T::Last, inclusive
|
||||||
|
for (auto v = value_of(left); v <= value_of(T::Last); ++v)
|
||||||
|
{
|
||||||
|
res = add_number(numbers, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next, get values between T::First and 'right', inclusive.
|
||||||
|
for (auto v = value_of(T::First); v <= value_of(right); ++v)
|
||||||
|
{
|
||||||
|
res = add_number(numbers, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (get_step<T>(range, step_start, step))
|
||||||
|
{
|
||||||
|
// Add from step_start to T::Last with a step of 'step'
|
||||||
|
for (auto v = step_start; v <= value_of(T::Last); v += step)
|
||||||
|
{
|
||||||
|
res = add_number(numbers, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::string & CronData::replace_string_name_with_numeric(std::string& s)
|
||||||
|
{
|
||||||
|
auto value = static_cast<int>(T::First);
|
||||||
|
|
||||||
|
const std::vector<std::string>* name_source{};
|
||||||
|
|
||||||
|
static_assert(std::is_same<T, libcron::Months>()
|
||||||
|
|| std::is_same<T, libcron::DayOfWeek>(),
|
||||||
|
"T must be either Months or DayOfWeek");
|
||||||
|
|
||||||
|
if constexpr (std::is_same<T, libcron::Months>())
|
||||||
|
{
|
||||||
|
name_source = &month_names;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
name_source = &day_names;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& name : *name_source)
|
||||||
|
{
|
||||||
|
std::regex m(name, std::regex_constants::ECMAScript | std::regex_constants::icase);
|
||||||
|
|
||||||
|
std::string replaced;
|
||||||
|
|
||||||
|
std::regex_replace(std::back_inserter(replaced), s.begin(), s.end(), m, std::to_string(value));
|
||||||
|
|
||||||
|
s = replaced;
|
||||||
|
|
||||||
|
++value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
}
|
}
|
100
libcron/include/libcron/CronRandomization.h
Normal file
100
libcron/include/libcron/CronRandomization.h
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <random>
|
||||||
|
#include <regex>
|
||||||
|
#include <functional>
|
||||||
|
#include "CronData.h"
|
||||||
|
|
||||||
|
namespace libcron
|
||||||
|
{
|
||||||
|
class CronRandomization
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::tuple<bool, std::string> parse(const std::string& cron_schedule);
|
||||||
|
|
||||||
|
CronRandomization();
|
||||||
|
|
||||||
|
CronRandomization(const CronRandomization&) = delete;
|
||||||
|
|
||||||
|
CronRandomization & operator=(const CronRandomization &) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<typename T>
|
||||||
|
std::pair<bool, std::string> get_random_in_range(const std::string& section,
|
||||||
|
int& selected_value,
|
||||||
|
std::pair<int, int> limit = std::make_pair(-1, -1));
|
||||||
|
|
||||||
|
std::pair<int, int> day_limiter(const std::set<Months>& month);
|
||||||
|
|
||||||
|
int cap(int value, int lower, int upper);
|
||||||
|
|
||||||
|
std::regex const rand_expression{ R"#([rR]\((\d+)\-(\d+)\))#", std::regex_constants::ECMAScript };
|
||||||
|
std::random_device rd{};
|
||||||
|
std::mt19937 twister;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::pair<bool, std::string> CronRandomization::get_random_in_range(const std::string& section,
|
||||||
|
int& selected_value,
|
||||||
|
std::pair<int, int> limit)
|
||||||
|
{
|
||||||
|
auto res = std::make_pair(true, std::string{});
|
||||||
|
selected_value = -1;
|
||||||
|
|
||||||
|
std::smatch random_match;
|
||||||
|
|
||||||
|
if (std::regex_match(section.cbegin(), section.cend(), random_match, rand_expression))
|
||||||
|
{
|
||||||
|
// Random range, get left and right numbers.
|
||||||
|
auto left = std::stoi(random_match[1].str());
|
||||||
|
auto right = std::stoi(random_match[2].str());
|
||||||
|
|
||||||
|
if (limit.first != -1 && limit.second != -1)
|
||||||
|
{
|
||||||
|
left = cap(left, limit.first, limit.second);
|
||||||
|
right = cap(right, limit.first, limit.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
libcron::CronData cd;
|
||||||
|
std::set<T> numbers;
|
||||||
|
res.first = cd.convert_from_string_range_to_number_range<T>(
|
||||||
|
std::to_string(left) + "-" + std::to_string(right), numbers);
|
||||||
|
|
||||||
|
// Remove items outside limits.
|
||||||
|
if (limit.first != -1 && limit.second != -1)
|
||||||
|
{
|
||||||
|
for (auto it = numbers.begin(); it != numbers.end(); )
|
||||||
|
{
|
||||||
|
if (CronData::value_of(*it) < limit.first || CronData::value_of(*it) > limit.second)
|
||||||
|
{
|
||||||
|
it = numbers.erase(it);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.first)
|
||||||
|
{
|
||||||
|
// Generate random indexes to select one of the numbers in the range.
|
||||||
|
std::uniform_int_distribution<> dis(0, static_cast<int>(numbers.size() - 1));
|
||||||
|
|
||||||
|
// Select the random number to use as the schedule
|
||||||
|
auto it = numbers.begin();
|
||||||
|
std::advance(it, dis(twister));
|
||||||
|
selected_value = CronData::value_of(*it);
|
||||||
|
res.second = std::to_string(selected_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Not random, just append input to output.
|
||||||
|
res.second = section;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "CronData.h"
|
#include "libcron/CronData.h"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#pragma warning(push)
|
#pragma warning(push)
|
||||||
@ -11,7 +11,7 @@
|
|||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "DateTime.h"
|
#include "libcron/DateTime.h"
|
||||||
|
|
||||||
namespace libcron
|
namespace libcron
|
||||||
{
|
{
|
55
libcron/include/libcron/TimeTypes.h
Normal file
55
libcron/include/libcron/TimeTypes.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace libcron
|
||||||
|
{
|
||||||
|
enum class Seconds : int8_t
|
||||||
|
{
|
||||||
|
First = 0,
|
||||||
|
Last = 59
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class Minutes : int8_t
|
||||||
|
{
|
||||||
|
First = 0,
|
||||||
|
Last = 59
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class Hours : int8_t
|
||||||
|
{
|
||||||
|
First = 0,
|
||||||
|
Last = 23
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class DayOfMonth : uint8_t
|
||||||
|
{
|
||||||
|
First = 1,
|
||||||
|
Last = 31
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class Months : uint8_t
|
||||||
|
{
|
||||||
|
First = 1,
|
||||||
|
January = First,
|
||||||
|
February,
|
||||||
|
March,
|
||||||
|
April,
|
||||||
|
May,
|
||||||
|
June,
|
||||||
|
July,
|
||||||
|
August,
|
||||||
|
September,
|
||||||
|
October,
|
||||||
|
November,
|
||||||
|
December = 12,
|
||||||
|
Last = December
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class DayOfWeek : uint8_t
|
||||||
|
{
|
||||||
|
// Sunday = 0 ... Saturday = 6
|
||||||
|
First = 0,
|
||||||
|
Last = 6,
|
||||||
|
};
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
#include "CronClock.h"
|
#include "libcron/CronClock.h"
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#ifndef NOMINMAX
|
#ifndef NOMINMAX
|
@ -1,10 +1,20 @@
|
|||||||
#include <date/date.h>
|
#include <date/date.h>
|
||||||
#include "CronData.h"
|
#include "libcron/CronData.h"
|
||||||
|
|
||||||
using namespace date;
|
using namespace date;
|
||||||
|
|
||||||
namespace libcron
|
namespace libcron
|
||||||
{
|
{
|
||||||
|
const constexpr Months CronData::months_with_31[NUMBER_OF_LONG_MONTHS] = { Months::January,
|
||||||
|
Months::March,
|
||||||
|
Months::May,
|
||||||
|
Months::July,
|
||||||
|
Months::August,
|
||||||
|
Months::October,
|
||||||
|
Months::December };
|
||||||
|
|
||||||
|
const std::vector<std::string> CronData::month_names{ "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
|
||||||
|
const std::vector<std::string> CronData::day_names{ "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
|
||||||
|
|
||||||
CronData CronData::create(const std::string& cron_expression)
|
CronData CronData::create(const std::string& cron_expression)
|
||||||
{
|
{
|
||||||
@ -14,12 +24,6 @@ namespace libcron
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
CronData::CronData()
|
|
||||||
: month_names({"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}),
|
|
||||||
day_names({"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"})
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void CronData::parse(const std::string& cron_expression)
|
void CronData::parse(const std::string& cron_expression)
|
||||||
{
|
{
|
||||||
// First, split on white-space. We expect six parts.
|
// First, split on white-space. We expect six parts.
|
||||||
@ -54,7 +58,6 @@ namespace libcron
|
|||||||
std::sregex_token_iterator(),
|
std::sregex_token_iterator(),
|
||||||
std::back_inserter(res));
|
std::back_inserter(res));
|
||||||
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,24 +93,14 @@ namespace libcron
|
|||||||
// Make sure that if the days contains only 31, at least one month allows that date.
|
// Make sure that if the days contains only 31, at least one month allows that date.
|
||||||
if (day_of_month.size() == 1 && day_of_month.find(DayOfMonth::Last) != day_of_month.end())
|
if (day_of_month.size() == 1 && day_of_month.find(DayOfMonth::Last) != day_of_month.end())
|
||||||
{
|
{
|
||||||
std::vector<int32_t> months_with_31;
|
|
||||||
for (int32_t i = 1; i <= 12; ++i)
|
|
||||||
{
|
|
||||||
auto ymd = 2018_y / i / date::last;
|
|
||||||
if (unsigned(ymd.day()) == 31)
|
|
||||||
{
|
|
||||||
months_with_31.push_back(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res = false;
|
res = false;
|
||||||
for (size_t i = 0; !res && i < months_with_31.size(); ++i)
|
|
||||||
{
|
|
||||||
res = months.find(static_cast<Months>(months_with_31[i])) != months.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for (size_t i = 0; !res && i < NUMBER_OF_LONG_MONTHS; ++i)
|
||||||
|
{
|
||||||
|
res = months.find(months_with_31[i]) != months.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
140
libcron/src/CronRandomization.cpp
Normal file
140
libcron/src/CronRandomization.cpp
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
#include <libcron/CronRandomization.h>
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
|
#include <map>
|
||||||
|
#include <array>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
|
#include <libcron/TimeTypes.h>
|
||||||
|
#include <libcron/CronData.h>
|
||||||
|
|
||||||
|
namespace libcron
|
||||||
|
{
|
||||||
|
CronRandomization::CronRandomization()
|
||||||
|
: twister(rd())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tuple<bool, std::string> CronRandomization::parse(const std::string& cron_schedule)
|
||||||
|
{
|
||||||
|
// Split on space to get each separate part, six parts expected
|
||||||
|
const std::regex split{ R"#(^\s*(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$)#",
|
||||||
|
std::regex_constants::ECMAScript };
|
||||||
|
|
||||||
|
std::smatch all_sections;
|
||||||
|
auto res = std::regex_match(cron_schedule.cbegin(), cron_schedule.cend(), all_sections, split);
|
||||||
|
|
||||||
|
// Replace text with numbers
|
||||||
|
std::string working_copy{};
|
||||||
|
|
||||||
|
if (res)
|
||||||
|
{
|
||||||
|
// Merge seconds, minutes, hours and day of month back together
|
||||||
|
working_copy += all_sections[1].str();
|
||||||
|
working_copy += " ";
|
||||||
|
working_copy += all_sections[2].str();
|
||||||
|
working_copy += " ";
|
||||||
|
working_copy += all_sections[3].str();
|
||||||
|
working_copy += " ";
|
||||||
|
working_copy += all_sections[4].str();
|
||||||
|
working_copy += " ";
|
||||||
|
|
||||||
|
// Replace month names
|
||||||
|
auto month = all_sections[5].str();
|
||||||
|
CronData::replace_string_name_with_numeric<libcron::Months>(month);
|
||||||
|
|
||||||
|
working_copy += " ";
|
||||||
|
working_copy += month;
|
||||||
|
|
||||||
|
// Replace day names
|
||||||
|
auto dow = all_sections[6].str();
|
||||||
|
CronData::replace_string_name_with_numeric<libcron::DayOfWeek>(dow);
|
||||||
|
|
||||||
|
working_copy += " ";
|
||||||
|
working_copy += dow;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string final_cron_schedule{};
|
||||||
|
|
||||||
|
// Split again on space
|
||||||
|
res = res && std::regex_match(working_copy.cbegin(), working_copy.cend(), all_sections, split);
|
||||||
|
|
||||||
|
if (res)
|
||||||
|
{
|
||||||
|
int selected_value = -1;
|
||||||
|
auto second = get_random_in_range<Seconds>(all_sections[1].str(), selected_value);
|
||||||
|
res = second.first;
|
||||||
|
final_cron_schedule = second.second;
|
||||||
|
|
||||||
|
auto minute = get_random_in_range<Minutes>(all_sections[2].str(), selected_value);
|
||||||
|
res &= minute.first;
|
||||||
|
final_cron_schedule += " " + minute.second;
|
||||||
|
|
||||||
|
auto hour = get_random_in_range<Hours>(all_sections[3].str(), selected_value);
|
||||||
|
res &= hour.first;
|
||||||
|
final_cron_schedule += " " + hour.second;
|
||||||
|
|
||||||
|
// Do Month before DayOfMonth to allow capping the allowed range.
|
||||||
|
auto month = get_random_in_range<Months>(all_sections[5].str(), selected_value);
|
||||||
|
res &= month.first;
|
||||||
|
|
||||||
|
std::set<Months> month_range{};
|
||||||
|
|
||||||
|
if (selected_value == -1)
|
||||||
|
{
|
||||||
|
// Month is not specific, get the range.
|
||||||
|
CronData cr;
|
||||||
|
res &= cr.convert_from_string_range_to_number_range<Months>(all_sections[5].str(), month_range);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
month_range.emplace(static_cast<Months>(selected_value));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto limits = day_limiter(month_range);
|
||||||
|
|
||||||
|
auto day_of_month = get_random_in_range<DayOfMonth>(all_sections[4].str(),
|
||||||
|
selected_value,
|
||||||
|
limits);
|
||||||
|
|
||||||
|
res &= day_of_month.first;
|
||||||
|
final_cron_schedule += " " + day_of_month.second + " " + month.second;
|
||||||
|
|
||||||
|
auto day_of_week = get_random_in_range<DayOfWeek>(all_sections[6].str(), selected_value);
|
||||||
|
res &= day_of_week.first;
|
||||||
|
final_cron_schedule += " " + day_of_week.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { res, final_cron_schedule };
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<int, int> CronRandomization::day_limiter(const std::set<Months>& months)
|
||||||
|
{
|
||||||
|
int max = CronData::value_of(DayOfMonth::Last);
|
||||||
|
|
||||||
|
for (auto month : months)
|
||||||
|
{
|
||||||
|
if (month == Months::February)
|
||||||
|
{
|
||||||
|
// Limit to 29 days, possibly causing delaying schedule until next leap year.
|
||||||
|
max = std::min(max, 29);
|
||||||
|
}
|
||||||
|
else if (std::find(std::begin(CronData::months_with_31),
|
||||||
|
std::end(CronData::months_with_31),
|
||||||
|
month) == std::end(CronData::months_with_31))
|
||||||
|
{
|
||||||
|
// Not among the months with 31 days
|
||||||
|
max = std::min(max, 30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto res = std::pair<int, int>{ CronData::value_of(DayOfMonth::First), max };
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CronRandomization::cap(int value, int lower, int upper)
|
||||||
|
{
|
||||||
|
return std::max(std::min(value, upper), lower);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
#include "CronSchedule.h"
|
#include "libcron/CronSchedule.h"
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
@ -1,5 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Task.h"
|
#include "libcron/Task.h"
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.6)
|
cmake_minimum_required(VERSION 3.6)
|
||||||
project(cron_test)
|
project(cron_test)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
if( MSVC )
|
if( MSVC )
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
||||||
@ -18,11 +18,13 @@ include_directories(
|
|||||||
add_executable(
|
add_executable(
|
||||||
${PROJECT_NAME}
|
${PROJECT_NAME}
|
||||||
CronDataTest.cpp
|
CronDataTest.cpp
|
||||||
CronScheduleTest.cpp CronTest.cpp)
|
CronRandomizationTest.cpp
|
||||||
|
CronScheduleTest.cpp
|
||||||
|
CronTest.cpp)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME} libcron)
|
target_link_libraries(${PROJECT_NAME} libcron)
|
||||||
|
|
||||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||||
ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_LOCATION}"
|
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out"
|
||||||
LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_LOCATION}"
|
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out"
|
||||||
RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_LOCATION}")
|
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out")
|
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
#include <date/date.h>
|
#include <date/date.h>
|
||||||
#include <libcron/Cron.h>
|
#include <libcron/include/libcron/Cron.h>
|
||||||
#include <libcron/CronData.h>
|
#include <libcron/include/libcron/CronData.h>
|
||||||
|
|
||||||
using namespace libcron;
|
using namespace libcron;
|
||||||
using namespace date;
|
using namespace date;
|
||||||
@ -225,3 +225,16 @@ SCENARIO("Date that exist in one of the months")
|
|||||||
{
|
{
|
||||||
REQUIRE(CronData::create("0 0 * 31 APR,MAY ?").is_valid());
|
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");
|
||||||
|
}
|
||||||
|
}
|
180
test/CronRandomizationTest.cpp
Normal file
180
test/CronRandomizationTest.cpp
Normal file
@ -0,0 +1,180 @@
|
|||||||
|
#include <catch.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <libcron/CronRandomization.h>
|
||||||
|
#include <libcron/Cron.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace libcron;
|
||||||
|
const auto EXPECT_FAILURE = true;
|
||||||
|
|
||||||
|
void test(const char* const random_schedule, bool expect_failure = false)
|
||||||
|
{
|
||||||
|
libcron::CronRandomization cr;
|
||||||
|
|
||||||
|
for (int i = 0; i < 5000; ++i)
|
||||||
|
{
|
||||||
|
auto res = cr.parse(random_schedule);
|
||||||
|
auto schedule = std::get<1>(res);
|
||||||
|
|
||||||
|
Cron<> cron;
|
||||||
|
|
||||||
|
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, []() {}));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SCENARIO("Randomize all the things")
|
||||||
|
{
|
||||||
|
const char* random_schedule = "R(0-59) R(0-59) R(0-23) R(1-31) R(1-12) ?";
|
||||||
|
|
||||||
|
GIVEN(random_schedule)
|
||||||
|
{
|
||||||
|
THEN("Only valid schedules generated")
|
||||||
|
{
|
||||||
|
test(random_schedule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SCENARIO("Randomize all the things with reverse ranges")
|
||||||
|
{
|
||||||
|
const char* random_schedule = "R(45-15) R(30-0) R(18-2) R(28-15) R(8-3) ?";
|
||||||
|
|
||||||
|
GIVEN(random_schedule)
|
||||||
|
{
|
||||||
|
THEN("Only valid schedules generated")
|
||||||
|
{
|
||||||
|
test(random_schedule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SCENARIO("Randomize all the things - day of week")
|
||||||
|
{
|
||||||
|
const char* random_schedule = "R(0-59) R(0-59) R(0-23) ? R(1-12) R(0-6)";
|
||||||
|
|
||||||
|
GIVEN(random_schedule)
|
||||||
|
{
|
||||||
|
THEN("Only valid schedules generated")
|
||||||
|
{
|
||||||
|
test(random_schedule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SCENARIO("Randomize all the things with reverse ranges - day of week")
|
||||||
|
{
|
||||||
|
const char* random_schedule = "R(45-15) R(30-0) R(18-2) ? R(8-3) R(4-1)";
|
||||||
|
|
||||||
|
GIVEN(random_schedule)
|
||||||
|
{
|
||||||
|
THEN("Only valid schedules generated")
|
||||||
|
{
|
||||||
|
test(random_schedule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 ? * *");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <date/date.h>
|
#include <date/date.h>
|
||||||
#include <libcron/Cron.h>
|
#include <libcron/include/libcron/Cron.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
using namespace libcron;
|
using namespace libcron;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
#include <libcron/Cron.h>
|
#include <libcron/include/libcron/Cron.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
214
uncrustify.cfg
Normal file
214
uncrustify.cfg
Normal file
@ -0,0 +1,214 @@
|
|||||||
|
# Uncrustify-0.67-87-d75a44aa9
|
||||||
|
newlines = lf
|
||||||
|
input_tab_size = 4
|
||||||
|
output_tab_size = 4
|
||||||
|
string_replace_tab_chars = true
|
||||||
|
utf8_bom = remove
|
||||||
|
sp_arith = force
|
||||||
|
sp_arith_additive = force
|
||||||
|
sp_assign = force
|
||||||
|
sp_cpp_lambda_assign = remove
|
||||||
|
sp_cpp_lambda_paren = remove
|
||||||
|
sp_assign_default = force
|
||||||
|
sp_after_assign = force
|
||||||
|
sp_enum_paren = force
|
||||||
|
sp_enum_assign = force
|
||||||
|
sp_enum_before_assign = force
|
||||||
|
sp_enum_after_assign = force
|
||||||
|
sp_pp_stringify = remove
|
||||||
|
sp_before_pp_stringify = remove
|
||||||
|
sp_bool = force
|
||||||
|
sp_compare = force
|
||||||
|
sp_inside_paren = remove
|
||||||
|
sp_paren_paren = remove
|
||||||
|
sp_before_ptr_star = remove
|
||||||
|
sp_before_unnamed_ptr_star = remove
|
||||||
|
sp_between_ptr_star = remove
|
||||||
|
sp_after_ptr_star = force
|
||||||
|
sp_after_ptr_block_caret = remove
|
||||||
|
sp_after_ptr_star_qualifier = force
|
||||||
|
sp_after_ptr_star_func = force
|
||||||
|
sp_ptr_star_paren = force
|
||||||
|
sp_before_ptr_star_func = force
|
||||||
|
sp_before_byref = remove
|
||||||
|
sp_before_unnamed_byref = remove
|
||||||
|
sp_after_byref = force
|
||||||
|
sp_after_byref_func = force
|
||||||
|
sp_before_byref_func = force
|
||||||
|
sp_before_angle = remove
|
||||||
|
sp_inside_angle = remove
|
||||||
|
sp_angle_colon = force
|
||||||
|
sp_after_angle = force
|
||||||
|
sp_angle_paren_empty = remove
|
||||||
|
sp_angle_word = force
|
||||||
|
sp_angle_shift = remove
|
||||||
|
sp_permit_cpp11_shift = true
|
||||||
|
sp_before_sparen = force
|
||||||
|
sp_inside_sparen = remove
|
||||||
|
sp_inside_sparen_close = remove
|
||||||
|
sp_inside_sparen_open = remove
|
||||||
|
sp_after_sparen = force
|
||||||
|
sp_sparen_brace = force
|
||||||
|
sp_special_semi = remove
|
||||||
|
sp_before_semi_for = remove
|
||||||
|
sp_before_semi_for_empty = remove
|
||||||
|
sp_after_semi = remove
|
||||||
|
sp_after_semi_for_empty = force
|
||||||
|
sp_after_comma = force
|
||||||
|
sp_before_ellipsis = remove
|
||||||
|
sp_after_class_colon = force
|
||||||
|
sp_before_class_colon = force
|
||||||
|
sp_after_constr_colon = force
|
||||||
|
sp_before_constr_colon = force
|
||||||
|
sp_after_operator = remove
|
||||||
|
sp_after_operator_sym = remove
|
||||||
|
sp_after_cast = remove
|
||||||
|
sp_inside_paren_cast = remove
|
||||||
|
sp_cpp_cast_paren = remove
|
||||||
|
sp_sizeof_paren = remove
|
||||||
|
sp_inside_braces_enum = force
|
||||||
|
sp_inside_braces_struct = force
|
||||||
|
sp_after_type_brace_init_lst_open = force
|
||||||
|
sp_before_type_brace_init_lst_close = force
|
||||||
|
sp_inside_type_brace_init_lst = force
|
||||||
|
sp_inside_braces_empty = remove
|
||||||
|
sp_type_func = force
|
||||||
|
sp_type_brace_init_lst = remove
|
||||||
|
sp_func_proto_paren = remove
|
||||||
|
sp_func_proto_paren_empty = remove
|
||||||
|
sp_func_def_paren = remove
|
||||||
|
sp_inside_tparen = remove
|
||||||
|
sp_after_tparen_close = remove
|
||||||
|
sp_square_fparen = remove
|
||||||
|
sp_fparen_brace = force
|
||||||
|
sp_fparen_dbrace = force
|
||||||
|
sp_func_call_paren = remove
|
||||||
|
sp_func_class_paren_empty = remove
|
||||||
|
sp_return_paren = remove
|
||||||
|
sp_attribute_paren = remove
|
||||||
|
sp_defined_paren = remove
|
||||||
|
sp_throw_paren = remove
|
||||||
|
sp_after_throw = force
|
||||||
|
sp_catch_paren = force
|
||||||
|
sp_oc_catch_paren = force
|
||||||
|
sp_else_brace = force
|
||||||
|
sp_brace_else = force
|
||||||
|
sp_before_dc = remove
|
||||||
|
sp_after_dc = remove
|
||||||
|
sp_before_nl_cont = force
|
||||||
|
sp_cond_question = force
|
||||||
|
sp_after_new = force
|
||||||
|
sp_between_new_paren = remove
|
||||||
|
sp_inside_newop_paren = force
|
||||||
|
sp_inside_newop_paren_open = remove
|
||||||
|
sp_inside_newop_paren_close = remove
|
||||||
|
indent_columns = 4
|
||||||
|
indent_with_tabs = 0
|
||||||
|
indent_align_string = true
|
||||||
|
indent_namespace = true
|
||||||
|
indent_namespace_level = 4
|
||||||
|
indent_class = true
|
||||||
|
indent_constr_colon = true
|
||||||
|
indent_ctor_init = 4
|
||||||
|
indent_access_spec = 0
|
||||||
|
indent_access_spec_body = true
|
||||||
|
indent_cpp_lambda_body = true
|
||||||
|
indent_cpp_lambda_only_once = true
|
||||||
|
nl_assign_leave_one_liners = true
|
||||||
|
nl_class_leave_one_liners = true
|
||||||
|
nl_enum_leave_one_liners = true
|
||||||
|
nl_getset_leave_one_liners = true
|
||||||
|
nl_func_leave_one_liners = true
|
||||||
|
nl_cpp_lambda_leave_one_liners = true
|
||||||
|
nl_start_of_file = remove
|
||||||
|
nl_end_of_file = force
|
||||||
|
nl_end_of_file_min = 1
|
||||||
|
nl_enum_brace = force
|
||||||
|
nl_enum_class = remove
|
||||||
|
nl_enum_class_identifier = remove
|
||||||
|
nl_if_brace = force
|
||||||
|
nl_brace_else = force
|
||||||
|
nl_elseif_brace = force
|
||||||
|
nl_else_brace = force
|
||||||
|
nl_else_if = remove
|
||||||
|
nl_before_if_closing_paren = remove
|
||||||
|
nl_brace_finally = force
|
||||||
|
nl_finally_brace = force
|
||||||
|
nl_try_brace = force
|
||||||
|
nl_for_brace = force
|
||||||
|
nl_catch_brace = force
|
||||||
|
nl_while_brace = force
|
||||||
|
nl_do_brace = force
|
||||||
|
nl_brace_while = force
|
||||||
|
nl_enum_own_lines = force
|
||||||
|
nl_func_type_name = remove
|
||||||
|
nl_func_decl_empty = remove
|
||||||
|
nl_func_def_empty = remove
|
||||||
|
nl_func_call_empty = remove
|
||||||
|
nl_return_expr = remove
|
||||||
|
nl_after_semicolon = true
|
||||||
|
nl_after_brace_close = true
|
||||||
|
nl_before_if = force
|
||||||
|
nl_after_if = force
|
||||||
|
nl_before_for = force
|
||||||
|
nl_after_for = force
|
||||||
|
nl_before_while = force
|
||||||
|
nl_after_while = force
|
||||||
|
nl_before_switch = force
|
||||||
|
nl_after_switch = force
|
||||||
|
nl_before_synchronized = force
|
||||||
|
nl_after_synchronized = force
|
||||||
|
nl_before_do = force
|
||||||
|
nl_after_do = force
|
||||||
|
nl_max = 2
|
||||||
|
nl_after_func_proto = 2
|
||||||
|
nl_after_func_proto_group = 2
|
||||||
|
nl_after_func_class_proto = 2
|
||||||
|
nl_after_func_class_proto_group = 2
|
||||||
|
nl_before_func_body_def = 2
|
||||||
|
nl_before_func_body_proto = 2
|
||||||
|
nl_after_func_body = 2
|
||||||
|
nl_after_func_body_class = 2
|
||||||
|
nl_after_func_body_one_liner = 1
|
||||||
|
nl_before_block_comment = 2
|
||||||
|
nl_before_c_comment = 2
|
||||||
|
nl_before_cpp_comment = 2
|
||||||
|
nl_after_multiline_comment = true
|
||||||
|
nl_after_label_colon = true
|
||||||
|
nl_after_struct = 2
|
||||||
|
nl_before_class = 2
|
||||||
|
nl_after_class = 2
|
||||||
|
nl_before_access_spec = 1
|
||||||
|
nl_after_access_spec = 1
|
||||||
|
nl_comment_func_def = 1
|
||||||
|
nl_after_try_catch_finally = 2
|
||||||
|
nl_around_cs_property = 2
|
||||||
|
nl_between_get_set = 2
|
||||||
|
eat_blanks_after_open_brace = true
|
||||||
|
eat_blanks_before_close_brace = true
|
||||||
|
nl_before_return = true
|
||||||
|
pos_arith = lead
|
||||||
|
code_width = 120
|
||||||
|
ls_for_split_full = true
|
||||||
|
ls_func_split_full = true
|
||||||
|
cmt_width = 120
|
||||||
|
cmt_reflow_mode = 2
|
||||||
|
cmt_indent_multi = false
|
||||||
|
cmt_c_group = true
|
||||||
|
cmt_sp_after_star_cont = 1
|
||||||
|
mod_full_brace_do = force
|
||||||
|
mod_full_brace_for = force
|
||||||
|
mod_full_brace_function = force
|
||||||
|
mod_full_brace_if = force
|
||||||
|
mod_full_brace_nl_block_rem_mlcond = true
|
||||||
|
mod_full_brace_while = force
|
||||||
|
mod_full_brace_using = force
|
||||||
|
mod_paren_on_return = remove
|
||||||
|
mod_remove_extra_semicolon = true
|
||||||
|
mod_sort_using = true
|
||||||
|
mod_case_brace = force
|
||||||
|
mod_remove_empty_return = true
|
||||||
|
pp_ignore_define_body = true
|
||||||
|
use_indent_func_call_param = false
|
||||||
|
# option(s) with 'not default' value: 209
|
||||||
|
#
|
Reference in New Issue
Block a user