From 8165fc6f99deef2f905ec70b8cc48c06e87aeed0 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 24 Aug 2024 16:48:30 +0100 Subject: [PATCH] Add randomized test This was especially helpful for my previous attempt at a solution, which did was more complicated. The original rules for this assignment are quite silly, I don't think they really optimize anything, and make understanding the actual algorithm more difficult than it should be. --- tests/unit/unit_test.cc | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/unit/unit_test.cc b/tests/unit/unit_test.cc index a5d2c5c..c429d29 100644 --- a/tests/unit/unit_test.cc +++ b/tests/unit/unit_test.cc @@ -2,6 +2,7 @@ #include +#include #include #include @@ -221,3 +222,44 @@ TEST_F(IntervalMapTest, insert_range_overlaps_many_oversize) { assign(40, 50, 5); assign(-10, 60, -1); } + +TEST_F(IntervalMapTest, fuzzing_001) { + assign(-50, 20, 1); + assign(40, 80, 2); + assign(-100, -10, 3); +} + +TEST_F(IntervalMapTest, fuzzing_002) { + assign(-100, 90, 1); + assign(0, 120, 2); + assign(-60, 60, 3); +} + +TEST_F(IntervalMapTest, fuzzing_003) { + assign(-80, 70, 1); + assign(-50, 40, 2); + assign(-40, 20, 3); + assign(-110, -10, 4); +} + +TEST_F(IntervalMapTest, randomized_test) { + auto const seed = []() { + std::random_device r; + return r(); + }(); + SCOPED_TRACE(seed); + auto random = std::mt19937_64(seed); + + auto keys = std::uniform_int_distribution( + std::numeric_limits::min(), + std::numeric_limits::max()); + auto values = std::uniform_int_distribution(0, 10); + + for (auto i = 0; i < 1000; ++i) { + auto const start = keys(random); + auto const end = keys(random); + auto const value = values(random); + assign(start, end, value); + check(); + } +}