You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

87 lines
2.0 KiB

/**
* Copyright (c) 2018 Niklas Rosenstein
* MIT licensed.
*/
#include <benchmark/benchmark.h>
#include <gtest/gtest.h>
#include <iostream>
#include <vector>
#include <NiklasRosenstein/enumerate.hpp>
#include <NiklasRosenstein/filter.hpp>
#include <NiklasRosenstein/range.hpp>
namespace nr = niklasrosenstein;
TEST(Iterator, range) {
std::size_t index1 = 2;
for (std::size_t index2 : nr::range<std::size_t>(2, 10)) {
ASSERT_EQ(index1++, index2);
}
}
TEST(Iterator, enumerator_rvalue) {
std::size_t index1 = 5;
for (auto item : nr::enumerate(nr::range<std::size_t>(5, 27))) {
ASSERT_EQ(index1 - 5, item.index);
ASSERT_EQ(index1++, item.value);
}
ASSERT_EQ(index1, 27);
}
TEST(Iterator, enumerator_lvalue) {
std::size_t index = 0;
std::vector<int> items;
items.push_back(1);
items.push_back(2);
items.push_back(3);
items.push_back(4);
for (auto item : nr::enumerate(items)) {
ASSERT_EQ(item.index, item.value - 1);
ASSERT_EQ(index++, item.index);
}
ASSERT_EQ(index, 4);
}
struct Element {
int value;
Element() : value(-1) {}
Element(int value_) : value(value_) {}
Element(Element&& other) : value(std::move(other.value)) {}
Element(Element const&) = delete;
Element& operator = (Element const&) = delete;
};
TEST(Iterator, enumerator_value_by_reference) {
size_t index = 0;
std::vector<Element> elements;
elements.push_back({1});
elements.push_back({2});
elements.push_back({3});
elements.push_back({4});
for (auto item : nr::enumerate(elements)) {
ASSERT_EQ(item.index, item.value.value - 1);
ASSERT_EQ(&item.value, &elements[item.index]);
ASSERT_EQ(index++, item.index);
}
ASSERT_EQ(index, 4);
}
TEST(Iterator, filter_range_bool) {
size_t index = 0;
for (size_t value : nr::filter(nr::range<size_t>(10))) {
ASSERT_EQ(index++, value - 1);
}
ASSERT_EQ(index, 9);
}
TEST(Iterator, filter_range_func) {
size_t index = 0;
for (size_t value : nr::filter(nr::range<size_t>(10), [](size_t v) { return v%2 == 0; })) {
ASSERT_EQ(index++, value / 2);
}
ASSERT_EQ(index, 5);
}