|
|
@@ -7,67 +7,46 @@
|
|
|
|
|
|
#include "stream/view/common.h"
|
|
|
|
|
|
-#include <iterator/facade.h>
|
|
|
-#include <iterator/sentinel.h>
|
|
|
-
|
|
|
-#include "stream_test.h"
|
|
|
+#include "stream_helpers.h"
|
|
|
+#include "stream_matchers.h"
|
|
|
|
|
|
using testing::StaticAssertTypeEq;
|
|
|
|
|
|
-struct SizedIterator : iterator::facade<SizedIterator> {
|
|
|
-private:
|
|
|
- // Default constructed SizedIterator should not be a vaguely valid state.
|
|
|
- size_t i_{~0ul};
|
|
|
- size_t n_{~0ul};
|
|
|
-
|
|
|
-public:
|
|
|
- SizedIterator() = default;
|
|
|
- SizedIterator(size_t i, size_t n) : i_(i), n_(n) {}
|
|
|
-
|
|
|
- size_t dereference() const { return i_; }
|
|
|
- void advance(std::ptrdiff_t off) { i_ += off; }
|
|
|
- // Bad detection requires us to provide this...
|
|
|
- // TODO: See if I can use one of these tests to why...
|
|
|
- bool equal_to(SizedIterator const & other) const { return other.i_ == i_; }
|
|
|
- std::ptrdiff_t distance_to(SizedIterator const & other) const {
|
|
|
- return other.i_ - i_;
|
|
|
- }
|
|
|
- bool at_end() const { return i_ >= n_; }
|
|
|
-};
|
|
|
-
|
|
|
-MAKE_ITERATOR_FACADE_TYPEDEFS(SizedIterator);
|
|
|
-
|
|
|
-struct SentinelRange {
|
|
|
-private:
|
|
|
- size_t size_;
|
|
|
-
|
|
|
-public:
|
|
|
- SentinelRange(size_t size = 0) : size_(size) {}
|
|
|
-
|
|
|
- SizedIterator begin() const { return {0, size_}; }
|
|
|
- auto end() const { return iterator::sentinel; }
|
|
|
- bool empty() const { return size_ == 0; }
|
|
|
- size_t size() const { return size_; }
|
|
|
-};
|
|
|
-
|
|
|
TEST(CommonView, CoercesSentinelIntoCommon) {
|
|
|
- SentinelRange input(10);
|
|
|
+ Range<std::vector<int>, Sentinel> input;
|
|
|
|
|
|
auto range = input | views::common();
|
|
|
|
|
|
StaticAssertTypeEq<decltype(range.begin()), decltype(range.end())>();
|
|
|
}
|
|
|
|
|
|
+TEST(CommonView, DoesNotWrapNonSentinelTypes) {
|
|
|
+ Range<std::vector<int>, Common> input{0, 1, 2, 3, 4};
|
|
|
+
|
|
|
+ auto range = input | views::common();
|
|
|
+
|
|
|
+ StaticAssertTypeEq<decltype(input), decltype(range)>();
|
|
|
+}
|
|
|
+
|
|
|
TEST(CommonView, IteratesThroughSameValues) {
|
|
|
- SentinelRange input(10);
|
|
|
+ Range<std::vector<int>, Sentinel> input{0, 1, 2, 3, 4};
|
|
|
|
|
|
auto range = input | views::common();
|
|
|
|
|
|
EXPECT_THAT(range, RangesEq(input));
|
|
|
}
|
|
|
|
|
|
+TEST(CommonView, NotRequiredToProvideSizeOrEmpty) {
|
|
|
+ Range<std::vector<int>, Sentinel> input{0, 1, 2, 3, 4};
|
|
|
+
|
|
|
+ auto range = input | views::common();
|
|
|
+
|
|
|
+ static_assert(!stream::detail::has_size_v<decltype(range)>);
|
|
|
+ static_assert(!stream::detail::has_empty_v<decltype(range)>);
|
|
|
+}
|
|
|
+
|
|
|
TEST(CommonView, PropagatesSize) {
|
|
|
- SentinelRange input(10);
|
|
|
+ Range<std::vector<int>, Sentinel, Sized> input{0, 1, 2, 3, 4};
|
|
|
|
|
|
auto range = input | views::common();
|
|
|
|
|
|
@@ -76,18 +55,10 @@ TEST(CommonView, PropagatesSize) {
|
|
|
}
|
|
|
|
|
|
TEST(CommonView, PropagatesEmpty) {
|
|
|
- SentinelRange input(10);
|
|
|
+ Range<std::vector<int>, Sentinel, Sized> input{0, 1, 2, 3, 4};
|
|
|
|
|
|
auto range = input | views::common();
|
|
|
|
|
|
static_assert(stream::detail::has_empty_v<decltype(range)>);
|
|
|
EXPECT_THAT(range.empty(), input.empty());
|
|
|
}
|
|
|
-
|
|
|
-TEST(CommonView, PassesNonSentinelTypes) {
|
|
|
- std::vector<size_t> input(10);
|
|
|
-
|
|
|
- auto range = input | views::common();
|
|
|
-
|
|
|
- StaticAssertTypeEq<decltype(input), decltype(range)>();
|
|
|
-}
|