Skip to content

Commit

Permalink
Merge branch 'testing_iterbase' into testiterbase14
Browse files Browse the repository at this point in the history
Conflicts:
	internal/iterbase.hpp
	powerset.hpp
	unique_everseen.hpp
	zip_longest.hpp
  • Loading branch information
ryanhaining committed Sep 9, 2015
2 parents 8aa11a7 + b285ed2 commit 2281de8
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 19 deletions.
2 changes: 1 addition & 1 deletion accumulate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class iter::impl::Accumulator {
acc_val{other.acc_val ? new AccumVal(*other.acc_val) : nullptr} {}

Iterator& operator=(const Iterator& other) {
if (this == &other) return *this;
if (this == &other) { return *this; }
this->sub_iter = other.sub_iter;
this->sub_end = other.sub_end;
this->accumulate_func = other.accumulate_func;
Expand Down
3 changes: 2 additions & 1 deletion chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class iter::impl::ChainedFromIterable {
: container(std::forward<Container>(in_container)) {}

public:
ChainedFromIterable(ChainedFromIterable&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag,
iterator_traits_deref<iterator_deref<Container>>> {
private:
Expand Down Expand Up @@ -224,7 +225,7 @@ class iter::impl::ChainedFromIterable {
sub_end_p{clone_sub_pointer(other.sub_end_p.get())} {}

Iterator& operator=(const Iterator& other) {
if (this == &other) return *this;
if (this == &other) { return *this; }

this->top_level_iter = other.top_level_iter;
this->top_level_end = other.top_level_end;
Expand Down
2 changes: 1 addition & 1 deletion groupby.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class iter::impl::GroupProducer {
key_func{other.key_func} {}

Iterator& operator=(const Iterator& other) {
if (this == &other) return *this;
if (this == &other) { return *this; }
this->sub_iter = other.sub_iter;
this->sub_end = other.sub_end;
this->item = other.item;
Expand Down
18 changes: 8 additions & 10 deletions internal/iterbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace iter {
template <typename T, typename = void>
struct ArrowHelper {
using type = void;
void operator()(T&) const noexcept { }
void operator()(T&) const noexcept {}
};

template <typename T>
Expand Down Expand Up @@ -239,8 +239,7 @@ namespace iter {
// get() returns a reference to the held item
// get_ptr() returns a pointer to the held item
// reset() replaces the currently held item

template <typename T, typename = void>
template <typename T>
class DerefHolder {
private:
static_assert(!std::is_lvalue_reference<T>::value,
Expand Down Expand Up @@ -283,17 +282,16 @@ namespace iter {
}

explicit operator bool() const {
return this->item_p;
return static_cast<bool>(this->item_p);
}
};

// Specialization for when T is an lvalue ref. Keep this in mind
// wherever a T appears.
// Specialization for when T is an lvalue ref
template <typename T>
class DerefHolder<T, std::enable_if_t<std::is_lvalue_reference<T>::value>> {
class DerefHolder<T&> {
public:
using reference = T;
using pointer = std::remove_reference_t<T>*;
using reference = T&;
using pointer = T*;

private:
pointer item_p{};
Expand All @@ -309,7 +307,7 @@ namespace iter {
return this->item_p;
}

void reset(T item) {
void reset(reference item) {
this->item_p = &item;
}

Expand Down
5 changes: 2 additions & 3 deletions range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace iter {
T start_{};
T value_{};
T step_{};
unsigned long steps_taken{};
std::size_t steps_taken{};

public:
constexpr RangeIterData() noexcept = default;
Expand Down Expand Up @@ -162,9 +162,8 @@ class iter::impl::Range {
const Iterator& lhs, const Iterator& rhs) noexcept {
if (rhs.is_end) {
return not_equal_to_impl(lhs, rhs, std::is_unsigned<T>{});
} else {
return not_equal_to_impl(rhs, lhs, std::is_unsigned<T>{});
}
return not_equal_to_impl(rhs, lhs, std::is_unsigned<T>{});
}

public:
Expand Down
2 changes: 1 addition & 1 deletion sliding_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class iter::impl::WindowSlider {
while (i < window_sz && this->sub_iter != in_end) {
this->window.get().push_back(this->sub_iter);
++i;
if (i != window_sz) ++this->sub_iter;
if (i != window_sz) { ++this->sub_iter; }
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import os

env = Environment(
ENV = os.environ,
CXX='c++',
CXXFLAGS= ['-g', '-Wall', '-Wextra',
'-pedantic', '-std=c++14',
'-I/usr/local/include', '-I.'],
CPPPATH='..',
LINKFLAGS='-L/usr/local/lib')
LINKFLAGS=['-L/usr/local/lib'])

# allows highighting to print to terminal from compiler output
env['ENV']['TERM'] = os.environ['TERM']
Expand Down Expand Up @@ -44,6 +43,7 @@ progs = Split(
zip
iteratoriterator
iterbase
mixed
helpers
'''
Expand Down
91 changes: 91 additions & 0 deletions test/test_iterbase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// AGAIN the contents of iterbase are completely subject to change, do not rely
// on any of this. Users of the library must consider all of this undocumented
//

#include <internal/iterbase.hpp>
#include <type_traits>
#include <vector>
#include <iterator>
#include <string>
#include <list>
#include <enumerate.hpp>

#include "catch.hpp"
#include "helpers.hpp"

namespace it = iter::impl;

using IVec = std::vector<int>;

template <typename T>
using hrai = it::has_random_access_iter<T>;
TEST_CASE("Detects random access iterators correctly", "[iterbase]") {
REQUIRE(hrai<std::vector<int>>::value);
REQUIRE(hrai<std::string>::value);
REQUIRE(hrai<int[10]>::value);

REQUIRE_FALSE(hrai<std::list<int>>::value);
REQUIRE_FALSE(hrai<decltype(iter::enumerate(std::list<int>{}))>::value);
REQUIRE_FALSE(hrai<itertest::BasicIterable<int>>::value);
}

TEST_CASE("Detects correct iterator types", "[iterbase]") {
REQUIRE((std::is_same<it::iterator_type<IVec>, IVec::iterator>::value));
REQUIRE((std::is_same<it::iterator_type<IVec>, IVec::iterator>::value));
REQUIRE((std::is_same<it::iterator_deref<IVec>,
IVec::iterator::reference>::value));
REQUIRE((std::is_same<it::const_iterator_deref<IVec>,
IVec::iterator::reference>::value));
REQUIRE((std::is_same<it::iterator_traits_deref<IVec>,
IVec::iterator::value_type>::value));

REQUIRE(
(std::is_same<it::iterator_arrow<IVec>, IVec::iterator::pointer>::value));
REQUIRE((std::is_same<it::iterator_arrow<int[10]>, int*>::value));
}

TEST_CASE("advance, next, size", "[iterbase]") {
IVec v = {2, 4, 6, 8, 10, 12, 14, 16, 18};
auto itr = std::begin(v);
REQUIRE(it::apply_arrow(itr) == &v[0]);

it::dumb_advance(itr, 3);
REQUIRE(itr == (std::begin(v) + 3));
REQUIRE(it::dumb_next(std::begin(v), 3) == std::begin(v) + 3);
REQUIRE(it::dumb_size(v) == v.size());
}

TEST_CASE("are_same", "[iterbase]") {
REQUIRE((it::are_same<int, int, int, int>::value));
REQUIRE_FALSE((it::are_same<double, int, int, int>::value));
REQUIRE_FALSE((it::are_same<int, int, int, double>::value));
REQUIRE_FALSE((it::are_same<int, double, int, int>::value));
}

TEST_CASE("DerefHolder lvalue reference", "[iterbase]") {
it::DerefHolder<int&> dh;
int a = 2;
int b = 5;
REQUIRE_FALSE(dh);
dh.reset(a);
REQUIRE(dh);

REQUIRE(dh.get_ptr() == &a);
REQUIRE(&dh.get() == &a);
dh.reset(b);
REQUIRE(dh.get_ptr() == &b);
REQUIRE(&dh.get() == &b);
}

TEST_CASE("DerefHolder non-reference", "[iterbase]") {
it::DerefHolder<int> dh;
int a = 2;
int b = 5;
REQUIRE_FALSE(dh);
dh.reset(std::move(a));
REQUIRE(dh.get() == 2);
REQUIRE(&dh.get() != &a);

dh.reset(std::move(b));
REQUIRE(dh.get() == 5);
}

0 comments on commit 2281de8

Please sign in to comment.