From 7c690fd5adbec0a06f0b8c03c8f8a715b3dbd3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Tue, 12 Dec 2023 11:56:13 +0100 Subject: [PATCH] Implement tests --- 2023/test/test.cpp | 2 + 2023/xmaslib/iota/iota_test.hpp | 66 +++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 2023/xmaslib/iota/iota_test.hpp diff --git a/2023/test/test.cpp b/2023/test/test.cpp index 4b32cf3..f23f974 100644 --- a/2023/test/test.cpp +++ b/2023/test/test.cpp @@ -6,3 +6,5 @@ #include "solvelib/03/day03_test.hpp" #include "solvelib/04/day04_test.hpp" #include "solvelib/05/day05_test.hpp" + +#include "xmaslib/iota/iota_test.hpp" diff --git a/2023/xmaslib/iota/iota_test.hpp b/2023/xmaslib/iota/iota_test.hpp new file mode 100644 index 0000000..1f9c40a --- /dev/null +++ b/2023/xmaslib/iota/iota_test.hpp @@ -0,0 +1,66 @@ +#include "iota.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include + +TEST_CASE("iota iterator") { + + SUBCASE("Access") { + xmas::views::iota iota(3, 10); + REQUIRE_EQ(iota.size(), 7); + + CHECK_EQ(iota.at(0), 3); + CHECK_EQ(iota.at(1), 4); + CHECK_EQ(iota.at(2), 5); + CHECK_EQ(iota.at(3), 6); + CHECK_EQ(iota.at(4), 7); + CHECK_EQ(iota.at(5), 8); + CHECK_EQ(iota.at(6), 9); + + REQUIRE_THROWS(iota.at(7)); + + CHECK_EQ(iota[0], 3); + CHECK_EQ(iota[1], 4); + CHECK_EQ(iota[2], 5); + CHECK_EQ(iota[3], 6); + CHECK_EQ(iota[4], 7); + CHECK_EQ(iota[5], 8); + CHECK_EQ(iota[6], 9); + } + + SUBCASE("Copy") { + xmas::views::iota iota(11, 17); + std::vector out; + + std::copy(iota.cbegin(), iota.cend(), std::back_inserter(out)); + + REQUIRE_EQ(iota.size(), 6); + REQUIRE_EQ(out.size(), 6); + + CHECK_EQ(out[0], 11); + CHECK_EQ(out[1], 12); + CHECK_EQ(out[2], 13); + CHECK_EQ(out[3], 14); + CHECK_EQ(out[4], 15); + CHECK_EQ(out[5], 16); + } + + SUBCASE("Reduce") { + xmas::views::iota iota(0, 10); + REQUIRE_EQ(iota.size(), 10u); + + auto got = std::reduce(iota.begin(), iota.end(), 0, std::plus{}); + REQUIRE_EQ(got, 45); + } + + SUBCASE("Views") { + xmas::views::iota() | std::ranges::views::take(5); + } +} \ No newline at end of file