Bläddra i källkod

test: add tests for common_view

Sam Jaffe 2 år sedan
förälder
incheckning
f9f138baaf
4 ändrade filer med 98 tillägg och 3 borttagningar
  1. 4 0
      stream.xcodeproj/project.pbxproj
  2. 0 2
      test/all_test.cxx
  3. 93 0
      test/common_test.cxx
  4. 1 1
      test/stream_test.h

+ 4 - 0
stream.xcodeproj/project.pbxproj

@@ -9,6 +9,7 @@
 /* Begin PBXBuildFile section */
 		CD52820429D3B1AB001A84DE /* stream in Headers */ = {isa = PBXBuildFile; fileRef = CDAA170121A3A738007BBA11 /* stream */; settings = {ATTRIBUTES = (Public, ); }; };
 		CDA37D4029D9D38E000A1F97 /* all_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDA37D3E29D9D38E000A1F97 /* all_test.cxx */; };
+		CDA54F9E29DA59C4006C0FAA /* common_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDA54F9D29DA59C4006C0FAA /* common_test.cxx */; };
 		CDEC1D7623514BEB0091D9F2 /* stream_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD9337281E3CD78B00699FF5 /* stream_test.cxx */; };
 		CDEC1D7923514BF80091D9F2 /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CDEC1D6323514BB50091D9F2 /* GoogleMock.framework */; };
 /* End PBXBuildFile section */
@@ -110,6 +111,7 @@
 		CDA37D5029DA22A7000A1F97 /* min.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = min.h; sourceTree = "<group>"; };
 		CDA37D5129DA22AA000A1F97 /* max.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = max.h; sourceTree = "<group>"; };
 		CDA54F9729DA4BFC006C0FAA /* invoke.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = invoke.h; sourceTree = "<group>"; };
+		CDA54F9D29DA59C4006C0FAA /* common_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = common_test.cxx; sourceTree = "<group>"; };
 		CDAA170121A3A738007BBA11 /* stream */ = {isa = PBXFileReference; lastKnownFileType = folder; name = stream; path = include/stream; sourceTree = "<group>"; };
 		CDE8545E24DEBEBF006FE7C7 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
 		CDEC1D5B23514BB50091D9F2 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
@@ -216,6 +218,7 @@
 				CDA37D4429D9D55D000A1F97 /* stream_test.h */,
 				CD9337281E3CD78B00699FF5 /* stream_test.cxx */,
 				CDA37D3E29D9D38E000A1F97 /* all_test.cxx */,
+				CDA54F9D29DA59C4006C0FAA /* common_test.cxx */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -470,6 +473,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDA54F9E29DA59C4006C0FAA /* common_test.cxx in Sources */,
 				CDEC1D7623514BEB0091D9F2 /* stream_test.cxx in Sources */,
 				CDA37D4029D9D38E000A1F97 /* all_test.cxx in Sources */,
 			);

+ 0 - 2
test/all_test.cxx

@@ -7,8 +7,6 @@
 
 #include "stream/view/all.h"
 
-#include "stream/algorithm.h"
-
 #include "stream_test.h"
 
 using testing::Eq;

+ 93 - 0
test/common_test.cxx

@@ -0,0 +1,93 @@
+//
+//  common_test.cxx
+//  stream-test
+//
+//  Created by Sam Jaffe on 4/2/23.
+//
+
+#include "stream/view/common.h"
+
+#include <iterator/facade.h>
+#include <iterator/sentinel.h>
+
+#include "stream_test.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);
+
+  auto range = input | views::common();
+
+  StaticAssertTypeEq<decltype(range.begin()), decltype(range.end())>();
+}
+
+TEST(CommonView, IteratesThroughSameValues) {
+  SentinelRange input(10);
+
+  auto range = input | views::common();
+
+  EXPECT_THAT(range, RangesEq(input));
+}
+
+TEST(CommonView, PropagatesSize) {
+  SentinelRange input(10);
+
+  auto range = input | views::common();
+
+  static_assert(stream::detail::has_size_v<decltype(range)>);
+  EXPECT_THAT(range.size(), input.size());
+}
+
+TEST(CommonView, PropagatesEmpty) {
+  SentinelRange input(10);
+
+  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)>();
+}

+ 1 - 1
test/stream_test.h

@@ -28,7 +28,7 @@ bool equal(It1 it1, S1 end1, It2 it2, S2 end2,
     }
   }
   *result_listener << "whose sizes differ";
-  return (it1 != end1) == (it2 != end2);
+  return (it1 == end1) && (it2 == end2);
 }
 }