BLT/tests/iterator_tests.cpp

210 lines
6.5 KiB
C++
Raw Permalink Normal View History

2024-09-28 18:31:49 -04:00
/*
* <Short Description>
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <blt/std/logging.h>
#include <blt/std/types.h>
#include <blt/std/assert.h>
#include <blt/math/vectors.h>
#include <blt/math/log_util.h>
#include <blt/iterator/zip.h>
2024-10-01 13:27:39 -04:00
#include <blt/iterator/enumerate.h>
2024-09-28 18:31:49 -04:00
#include <blt/iterator/iterator.h>
2024-10-01 16:46:15 -04:00
#include <blt/std/ranges.h>
2024-09-28 18:31:49 -04:00
#include <blt/format/boxing.h>
#include <array>
2024-09-29 18:26:34 -04:00
#include <forward_list>
2024-09-28 18:31:49 -04:00
2024-09-29 18:26:34 -04:00
constexpr auto increasing_reverse_pairs =
[](blt::size_t i, blt::size_t index, blt::size_t size) { return i == 0 ? index : (size - 1) - index; };
constexpr auto increasing_pairs =
[](blt::size_t, blt::size_t index, blt::size_t) { return index; };
constexpr auto decreasing_pairs =
[](blt::size_t, blt::size_t index, blt::size_t size) { return size - index; };
2024-09-28 18:31:49 -04:00
template<blt::size_t n, typename Func>
2024-09-29 18:26:34 -04:00
std::array<blt::vec2, n> make_array(Func func)
2024-09-28 18:31:49 -04:00
{
std::array<blt::vec2, n> array;
for (auto&& [index, value] : blt::enumerate(array))
value = blt::vec2(func(0, index, n), func(1, index, n));
return array;
}
2024-09-29 18:26:34 -04:00
template<blt::size_t n, typename Func>
std::forward_list<blt::vec2> make_list(Func func)
{
std::forward_list<blt::vec2> array;
for (auto index : blt::range(0ul, n))
array.push_front(blt::vec2(func(0, index, n), func(1, index, n)));
return array;
}
2024-09-28 18:31:49 -04:00
constexpr blt::size_t array_size = 10;
auto array_1 = make_array<array_size>(increasing_reverse_pairs);
auto array_2 = make_array<array_size>(increasing_pairs);
auto array_3 = make_array<array_size>(decreasing_pairs);
2024-09-29 18:26:34 -04:00
auto list_1 = make_list<array_size>(increasing_reverse_pairs);
2024-09-28 18:31:49 -04:00
void test_enumerate()
{
blt::log_box_t box(std::cout, "Enumerate Tests", 25);
for (const auto& [index, item] : blt::enumerate(array_1))
BLT_TRACE_STREAM << index << " : " << item << "\n";
BLT_TRACE("");
for (const auto& [index, item] : blt::enumerate(array_1).rev())
BLT_TRACE_STREAM << index << " : " << item << "\n";
BLT_TRACE("");
for (const auto& [index, item] : blt::enumerate(array_1).take(3))
{
BLT_TRACE_STREAM << index << " : " << item << "\n";
BLT_ASSERT(index < 3);
}
BLT_TRACE("");
for (const auto& [index, item] : blt::enumerate(array_1).take(3).rev())
{
BLT_TRACE_STREAM << index << " : " << item << "\n";
BLT_ASSERT(index < 3);
}
BLT_TRACE("");
for (const auto& [index, item] : blt::enumerate(array_1).skip(3))
{
BLT_TRACE_STREAM << index << " : " << item << "\n";
BLT_ASSERT(index >= 3);
}
BLT_TRACE("");
for (const auto& [index, item] : blt::enumerate(array_1).skip(3).rev())
{
BLT_TRACE_STREAM << index << " : " << item << "\n";
BLT_ASSERT(index >= 3);
}
BLT_TRACE("");
for (const auto& [index, item] : blt::enumerate(array_1).skip(3).take(5))
{
BLT_TRACE_STREAM << index << " : " << item << "\n";
BLT_ASSERT(index >= 3 && index < (array_1.size() - 5) + 3);
}
BLT_TRACE("");
for (const auto& [index, item] : blt::enumerate(array_1).skip(3).rev().take(5))
{
BLT_TRACE_STREAM << index << " : " << item << "\n";
BLT_ASSERT(index >= 5);
}
}
void test_pairs()
{
blt::log_box_t box(std::cout, "Pairs Tests", 25);
2024-10-01 16:46:15 -04:00
for (auto [a1, a2] : blt::in_pairs(array_1, array_2))
{
BLT_TRACE_STREAM << a1 << " : " << a2 << "\n";
}
2024-09-28 18:31:49 -04:00
}
void test_zip()
{
blt::log_box_t box(std::cout, "Zip Tests", 25);
2024-09-30 02:38:36 -04:00
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, list_1))
2024-09-29 18:26:34 -04:00
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
BLT_TRACE("================================");
2024-09-30 02:38:36 -04:00
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, list_1).take(3))
2024-09-29 18:26:34 -04:00
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
BLT_TRACE("================================");
2024-09-30 18:03:44 -04:00
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, array_3).take(3).rev())
2024-09-29 18:26:34 -04:00
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
BLT_TRACE("================================");
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, array_3).take_or(13))
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
BLT_TRACE("================================");
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, array_3).rev().take(3))
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
2024-09-30 18:03:44 -04:00
BLT_TRACE("================================");
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, array_3).skip(2).rev())
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
2024-09-28 18:31:49 -04:00
}
2024-10-01 16:46:15 -04:00
void test_iterate()
{
blt::log_box_t box(std::cout, "Iterate Tests", 25);
for (auto v : blt::iterate(array_1))
{
BLT_TRACE_STREAM << "Element: " << v << "\n";
}
BLT_TRACE("================================");
for (auto v : blt::iterate(array_1).skip(5))
{
BLT_TRACE_STREAM << "Element: " << v << "\n";
}
BLT_TRACE("================================");
for (auto v : blt::iterate(array_1).take(5))
{
BLT_TRACE_STREAM << "Element: " << v << "\n";
}
BLT_TRACE("================================");
for (auto v : blt::iterate(array_1).rev())
{
BLT_TRACE_STREAM << "Element: " << v << "\n";
}
BLT_TRACE("================================");
for (auto [a, b] : blt::iterate(array_1).zip(list_1))
{
BLT_TRACE_STREAM << "Zip: " << a << " " << b << "\n";
}
BLT_TRACE("================================");
for (auto [i, data] : blt::iterate(array_1).zip(list_1).skip(3).take(4).enumerate())
{
auto [a, b] = data;
BLT_TRACE_STREAM << "Zip (" << i << "): " << a << " " << b << "\n";
}
}
2024-09-28 18:31:49 -04:00
int main()
{
test_enumerate();
std::cout << std::endl;
test_pairs();
std::cout << std::endl;
test_zip();
2024-10-01 16:46:15 -04:00
std::cout << std::endl;
test_iterate();
2024-09-28 18:31:49 -04:00
}