1 #include "catch.hpp" 2 3 // Tests of generator implementation details 4 TEST_CASE("Generators internals", "[generators][internals]") { 5 using namespace Catch::Generators; 6 7 SECTION("Single value") { 8 auto gen = value(123); 9 REQUIRE(gen.get() == 123); 10 REQUIRE_FALSE(gen.next()); 11 } 12 SECTION("Preset values") { 13 auto gen = values({ 1, 3, 5 }); 14 REQUIRE(gen.get() == 1); 15 REQUIRE(gen.next()); 16 REQUIRE(gen.get() == 3); 17 REQUIRE(gen.next()); 18 REQUIRE(gen.get() == 5); 19 REQUIRE_FALSE(gen.next()); 20 } 21 SECTION("Generator combinator") { 22 auto gen = makeGenerators(1, 5, values({ 2, 4 }), 0); 23 REQUIRE(gen.get() == 1); 24 REQUIRE(gen.next()); 25 REQUIRE(gen.get() == 5); 26 REQUIRE(gen.next()); 27 REQUIRE(gen.get() == 2); 28 REQUIRE(gen.next()); 29 REQUIRE(gen.get() == 4); 30 REQUIRE(gen.next()); 31 REQUIRE(gen.get() == 0); 32 REQUIRE_FALSE(gen.next()); 33 } 34 SECTION("Explicitly typed generator sequence") { 35 auto gen = makeGenerators(as<std::string>{}, "aa", "bb", "cc"); 36 // This just checks that the type is std::string: 37 REQUIRE(gen.get().size() == 2); 38 // Iterate over the generator 39 REQUIRE(gen.get() == "aa"); 40 REQUIRE(gen.next()); 41 REQUIRE(gen.get() == "bb"); 42 REQUIRE(gen.next()); 43 REQUIRE(gen.get() == "cc"); 44 REQUIRE_FALSE(gen.next()); 45 } 46 SECTION("Filter generator") { 47 // Normal usage __anon2edf02960102(int i) 48 auto gen = filter([] (int i) { return i != 2; }, values({ 2, 1, 2, 3, 2, 2 })); 49 REQUIRE(gen.get() == 1); 50 REQUIRE(gen.next()); 51 REQUIRE(gen.get() == 3); 52 REQUIRE_FALSE(gen.next()); 53 54 // Completely filtered-out generator should throw on construction __anon2edf02960202(int) 55 REQUIRE_THROWS_AS(filter([] (int) { return false; }, value(1)), Catch::GeneratorException); 56 } 57 SECTION("Take generator") { 58 SECTION("Take less") { 59 auto gen = take(2, values({ 1, 2, 3 })); 60 REQUIRE(gen.get() == 1); 61 REQUIRE(gen.next()); 62 REQUIRE(gen.get() == 2); 63 REQUIRE_FALSE(gen.next()); 64 } 65 SECTION("Take more") { 66 auto gen = take(2, value(1)); 67 REQUIRE(gen.get() == 1); 68 REQUIRE_FALSE(gen.next()); 69 } 70 } 71 SECTION("Map") { __anon2edf02960302(int i) 72 auto gen = map<double>([] (int i) {return 2.0 * i; }, values({ 1, 2, 3 })); 73 REQUIRE(gen.get() == 2.0); 74 REQUIRE(gen.next()); 75 REQUIRE(gen.get() == 4.0); 76 REQUIRE(gen.next()); 77 REQUIRE(gen.get() == 6.0); 78 REQUIRE_FALSE(gen.next()); 79 } 80 SECTION("Repeat") { 81 SECTION("Singular repeat") { 82 auto gen = repeat(1, value(3)); 83 REQUIRE(gen.get() == 3); 84 REQUIRE_FALSE(gen.next()); 85 } 86 SECTION("Actual repeat") { 87 auto gen = repeat(2, values({ 1, 2, 3 })); 88 REQUIRE(gen.get() == 1); 89 REQUIRE(gen.next()); 90 REQUIRE(gen.get() == 2); 91 REQUIRE(gen.next()); 92 REQUIRE(gen.get() == 3); 93 REQUIRE(gen.next()); 94 REQUIRE(gen.get() == 1); 95 REQUIRE(gen.next()); 96 REQUIRE(gen.get() == 2); 97 REQUIRE(gen.next()); 98 REQUIRE(gen.get() == 3); 99 REQUIRE_FALSE(gen.next()); 100 } 101 } 102 SECTION("Range") { 103 SECTION("Positive auto step") { 104 SECTION("Integer") { 105 auto gen = range(-2, 2); 106 REQUIRE(gen.get() == -2); 107 REQUIRE(gen.next()); 108 REQUIRE(gen.get() == -1); 109 REQUIRE(gen.next()); 110 REQUIRE(gen.get() == 0); 111 REQUIRE(gen.next()); 112 REQUIRE(gen.get() == 1); 113 REQUIRE_FALSE(gen.next()); 114 } 115 } 116 SECTION("Negative auto step") { 117 SECTION("Integer") { 118 auto gen = range(2, -2); 119 REQUIRE(gen.get() == 2); 120 REQUIRE(gen.next()); 121 REQUIRE(gen.get() == 1); 122 REQUIRE(gen.next()); 123 REQUIRE(gen.get() == 0); 124 REQUIRE(gen.next()); 125 REQUIRE(gen.get() == -1); 126 REQUIRE_FALSE(gen.next()); 127 } 128 } 129 SECTION("Positive manual step") { 130 SECTION("Integer") { 131 SECTION("Exact") { 132 auto gen = range(-7, 5, 3); 133 REQUIRE(gen.get() == -7); 134 REQUIRE(gen.next()); 135 REQUIRE(gen.get() == -4); 136 REQUIRE(gen.next()); 137 REQUIRE(gen.get() == -1); 138 REQUIRE(gen.next()); 139 REQUIRE(gen.get() == 2); 140 REQUIRE_FALSE(gen.next()); 141 } 142 SECTION("Slightly over end") { 143 auto gen = range(-7, 4, 3); 144 REQUIRE(gen.get() == -7); 145 REQUIRE(gen.next()); 146 REQUIRE(gen.get() == -4); 147 REQUIRE(gen.next()); 148 REQUIRE(gen.get() == -1); 149 REQUIRE(gen.next()); 150 REQUIRE(gen.get() == 2); 151 REQUIRE_FALSE(gen.next()); 152 } 153 SECTION("Slightly under end") { 154 auto gen = range(-7, 6, 3); 155 REQUIRE(gen.get() == -7); 156 REQUIRE(gen.next()); 157 REQUIRE(gen.get() == -4); 158 REQUIRE(gen.next()); 159 REQUIRE(gen.get() == -1); 160 REQUIRE(gen.next()); 161 REQUIRE(gen.get() == 2); 162 REQUIRE(gen.next()); 163 REQUIRE(gen.get() == 5); 164 REQUIRE_FALSE(gen.next()); 165 } 166 } 167 } 168 SECTION("Negative manual step") { 169 SECTION("Integer") { 170 SECTION("Exact") { 171 auto gen = range(5, -7, -3); 172 REQUIRE(gen.get() == 5); 173 REQUIRE(gen.next()); 174 REQUIRE(gen.get() == 2); 175 REQUIRE(gen.next()); 176 REQUIRE(gen.get() == -1); 177 REQUIRE(gen.next()); 178 REQUIRE(gen.get() == -4); 179 REQUIRE_FALSE(gen.next()); 180 } 181 SECTION("Slightly over end") { 182 auto gen = range(5, -6, -3); 183 REQUIRE(gen.get() == 5); 184 REQUIRE(gen.next()); 185 REQUIRE(gen.get() == 2); 186 REQUIRE(gen.next()); 187 REQUIRE(gen.get() == -1); 188 REQUIRE(gen.next()); 189 REQUIRE(gen.get() == -4); 190 REQUIRE_FALSE(gen.next()); 191 } 192 SECTION("Slightly under end") { 193 auto gen = range(5, -8, -3); 194 REQUIRE(gen.get() == 5); 195 REQUIRE(gen.next()); 196 REQUIRE(gen.get() == 2); 197 REQUIRE(gen.next()); 198 REQUIRE(gen.get() == -1); 199 REQUIRE(gen.next()); 200 REQUIRE(gen.get() == -4); 201 REQUIRE(gen.next()); 202 REQUIRE(gen.get() == -7); 203 REQUIRE_FALSE(gen.next()); 204 } 205 } 206 } 207 } 208 209 } 210