mirror of
				https://github.com/PerMalmberg/libcron.git
				synced 2025-11-03 20:48:08 -06:00 
			
		
		
		
	#1 - Randomization tests green.
This commit is contained in:
		@@ -61,17 +61,21 @@ namespace libcron
 | 
			
		||||
                    std::to_string(left) + "-" + std::to_string(right), numbers);
 | 
			
		||||
 | 
			
		||||
            // Remove items outside limits.
 | 
			
		||||
//            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(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)
 | 
			
		||||
            {
 | 
			
		||||
 
 | 
			
		||||
@@ -49,7 +49,7 @@ namespace libcron
 | 
			
		||||
            std::set<Months> month_range{};
 | 
			
		||||
            if (selected_value == -1)
 | 
			
		||||
            {
 | 
			
		||||
                // Month is not specific, we need to get the 'max minimum' day of month to use.
 | 
			
		||||
                // 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);
 | 
			
		||||
            }
 | 
			
		||||
@@ -77,7 +77,7 @@ namespace libcron
 | 
			
		||||
 | 
			
		||||
    std::pair<int, int> CronRandomization::day_limiter(const std::set<Months>& months)
 | 
			
		||||
    {
 | 
			
		||||
        auto max = 31;
 | 
			
		||||
        int max = CronData::value_of(DayOfMonth::Last);
 | 
			
		||||
 | 
			
		||||
        for (auto month : months)
 | 
			
		||||
        {
 | 
			
		||||
@@ -90,12 +90,9 @@ namespace libcron
 | 
			
		||||
                               CronData::months_with_31.end(),
 | 
			
		||||
                               month) == CronData::months_with_31.end())
 | 
			
		||||
            {
 | 
			
		||||
                // Not among the months with 31 days
 | 
			
		||||
                max = std::min(max, 30);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                max = std::min(max, 31);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        auto res = std::pair<int, int>{ CronData::value_of(DayOfMonth::First), max };
 | 
			
		||||
 
 | 
			
		||||
@@ -8,81 +8,71 @@
 | 
			
		||||
 | 
			
		||||
using namespace libcron;
 | 
			
		||||
 | 
			
		||||
SCENARIO("Randomize all the things")
 | 
			
		||||
void test(const char* const random_schedule)
 | 
			
		||||
{
 | 
			
		||||
    const char* full_random = "R(0-59) R(0-59) R(0-23) R(1-31) R(1-12) ?";
 | 
			
		||||
    Cron<> cron;
 | 
			
		||||
 | 
			
		||||
    GIVEN(full_random)
 | 
			
		||||
    {
 | 
			
		||||
        THEN("Only valid schedules generated")
 | 
			
		||||
        {
 | 
			
		||||
    libcron::CronRandomization cr;
 | 
			
		||||
    std::unordered_map<int, std::unordered_map<int, int>> results{};
 | 
			
		||||
 | 
			
		||||
            for (int i = 0; i < 1000; ++i)
 | 
			
		||||
    for (int i = 0; i < 10000; ++i)
 | 
			
		||||
    {
 | 
			
		||||
                auto res = cr.parse(full_random);
 | 
			
		||||
        auto res = cr.parse(random_schedule);
 | 
			
		||||
        REQUIRE(std::get<0>(res));
 | 
			
		||||
        auto schedule = std::get<1>(res);
 | 
			
		||||
 | 
			
		||||
                auto start = schedule.begin();
 | 
			
		||||
                auto space = std::find(schedule.begin(), schedule.end(), ' ');
 | 
			
		||||
 | 
			
		||||
                for (int section = 0; start != schedule.end() && section < 5; ++section)
 | 
			
		||||
                {
 | 
			
		||||
                    auto& map = results[section];
 | 
			
		||||
                    auto s = std::string{start, space};
 | 
			
		||||
                    auto value = std::stoi(s);
 | 
			
		||||
                    map[value]++;
 | 
			
		||||
                    start = space + 1;
 | 
			
		||||
                    space = std::find(start, schedule.end(), ' ');
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                REQUIRE(results.size() == 5);
 | 
			
		||||
        INFO("schedule:" << schedule);
 | 
			
		||||
        Cron<> cron;
 | 
			
		||||
        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")
 | 
			
		||||
{
 | 
			
		||||
    // Only generate DayOfMonth up to 28 to prevent failing tests where the month doesn't have more days.
 | 
			
		||||
    const char* full_random = "R(45-15) R(30-0) R(18-2) R(28-15) 2 ?";
 | 
			
		||||
    Cron<> cron;
 | 
			
		||||
    const char* random_schedule = "R(45-15) R(30-0) R(18-2) R(28-15) R(8-3) ?";
 | 
			
		||||
 | 
			
		||||
    GIVEN(full_random)
 | 
			
		||||
    GIVEN(random_schedule)
 | 
			
		||||
    {
 | 
			
		||||
        THEN("Only valid schedules generated")
 | 
			
		||||
        {
 | 
			
		||||
            libcron::CronRandomization cr;
 | 
			
		||||
            std::unordered_map<int, std::unordered_map<int, int>> results{};
 | 
			
		||||
 | 
			
		||||
            for (int i = 0; i < 1000; ++i)
 | 
			
		||||
            {
 | 
			
		||||
                auto res = cr.parse(full_random);
 | 
			
		||||
                REQUIRE(std::get<0>(res));
 | 
			
		||||
                auto schedule = std::get<1>(res);
 | 
			
		||||
 | 
			
		||||
                auto start = schedule.begin();
 | 
			
		||||
                auto space = std::find(schedule.begin(), schedule.end(), ' ');
 | 
			
		||||
 | 
			
		||||
                for (int section = 0; start != schedule.end() && section < 5; ++section)
 | 
			
		||||
                {
 | 
			
		||||
                    auto& map = results[section];
 | 
			
		||||
                    auto s = std::string{start, space};
 | 
			
		||||
                    auto value = std::stoi(s);
 | 
			
		||||
                    map[value]++;
 | 
			
		||||
                    start = space + 1;
 | 
			
		||||
                    space = std::find(start, schedule.end(), ' ');
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                REQUIRE(results.size() == 5);
 | 
			
		||||
                INFO("schedule:" << schedule);
 | 
			
		||||
                REQUIRE(cron.add_schedule("validate schedule", schedule, []() {}));
 | 
			
		||||
            }
 | 
			
		||||
            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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user