summaryrefslogtreecommitdiff
path: root/src/compat/iter_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/compat/iter_test.cpp')
-rw-r--r--src/compat/iter_test.cpp65
1 files changed, 62 insertions, 3 deletions
diff --git a/src/compat/iter_test.cpp b/src/compat/iter_test.cpp
index a07cb0f..6732c47 100644
--- a/src/compat/iter_test.cpp
+++ b/src/compat/iter_test.cpp
@@ -20,10 +20,15 @@
#include <gtest/gtest.h>
-#include "../strings/xstring.hpp"
+#include <algorithm>
+
+#include "../ints/udl.hpp"
#include "../poison.hpp"
+
+namespace tmwa
+{
TEST(iterpair, strings)
{
IteratorPair<ValueIterator<char>> pair = value_range('0', ':');
@@ -33,7 +38,7 @@ TEST(iterpair, strings)
TEST(iterpair, signed8)
{
- IteratorPair<ValueIterator<int8_t>> pair = value_range(int8_t(-128), int8_t(127));
+ IteratorPair<ValueIterator<int8_t>> pair = value_range(-128_n8, +127_p8);
int8_t arr[255] =
{
-128, -127, -126, -125, -124, -123, -122, -121, -120,
@@ -68,7 +73,7 @@ TEST(iterpair, signed8)
TEST(iterpair, unsigned8)
{
- IteratorPair<ValueIterator<uint8_t>> pair = value_range(uint8_t(0), uint8_t(255));
+ IteratorPair<ValueIterator<uint8_t>> pair = value_range(0_u8, 255_u8);
uint8_t arr[255] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
@@ -100,3 +105,57 @@ TEST(iterpair, unsigned8)
};
EXPECT_TRUE(std::equal(pair.begin(), pair.end(), arr));
}
+
+static
+bool is_odd_ref(int& i)
+{
+ return i % 2;
+}
+
+TEST(iterpair, filter1)
+{
+ int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
+
+ int expected_arr[] = {1, 3, 5, 7};
+ int *expected_it = expected_arr;
+ int *expected_end = expected_arr + 4;
+
+ for (int& i : filter_iterator<int&>(&arr, is_odd_ref))
+ {
+ EXPECT_EQ(i, *expected_it);
+ ++expected_it;
+ }
+ EXPECT_EQ(expected_it, expected_end);
+}
+
+TEST(iterpair, filter2)
+{
+ std::vector<int> vals = {0, 1, 0, 2, 0, 3, 0};
+
+ int sum = 0, count = 0;
+ for (int i : filter_iterator<int>(&vals))
+ {
+ sum += i;
+ count++;
+ }
+ EXPECT_EQ(sum, 6);
+ EXPECT_EQ(count, 3);
+}
+
+TEST(iterpair, filter3)
+{
+ int one = 1;
+ int two = 2;
+ int three = 3;
+ std::vector<int *> vals = {nullptr, &one, nullptr, &two, nullptr, &three, nullptr};
+
+ int sum = 0, count = 0;
+ for (int *i : filter_iterator<int *>(&vals))
+ {
+ sum += *i;
+ count++;
+ }
+ EXPECT_EQ(sum, 6);
+ EXPECT_EQ(count, 3);
+}
+} // namespace tmwa