Prechádzať zdrojové kódy

refactor/test: begin migrating tests to own file(s)

Sam Jaffe 2 rokov pred
rodič
commit
0869fb94f0

+ 15 - 0
include/stream/algorithm.h

@@ -0,0 +1,15 @@
+//
+//  algorithm.h
+//  stream
+//
+//  Created by Sam Jaffe on 4/2/23.
+//
+
+#pragma once
+
+#include <stream/algorithm/count.h>
+#include <stream/algorithm/equal.h>
+#include <stream/algorithm/fold.h>
+#include <stream/algorithm/for_each.h>
+#include <stream/algorithm/minmax.h>
+#include <stream/algorithm/size.h>

+ 28 - 0
include/stream/algorithm/equal.h

@@ -0,0 +1,28 @@
+//
+//  equal.h
+//  stream
+//
+//  Created by Sam Jaffe on 4/2/23.
+//
+
+#pragma once
+
+#include <algorithm>
+
+namespace stream::range {
+
+template <typename It1, typename S1, typename It2, typename S2,
+          typename Cmp = std::equal_to<>>
+bool equal(It1 it1, S1 end1, It2 it2, S2 end2, Cmp cmp = {}) {
+  for (; it1 != end1 && it2 != end2; ++it1, ++it2) {
+    if (!std::invoke(cmp, *it1, *it2)) { return false; }
+  }
+  return (it1 != end1) == (it2 != end2);
+}
+
+template <typename S1, typename S2, typename Cmp = std::equal_to<>>
+bool equal(S1 const s1, S2 const & s2, Cmp cmp = {}) {
+  return equal(s1.begin(), s1.end(), s2.begin(), s2.end(), cmp);
+}
+
+}

+ 11 - 0
include/stream/iterator.h

@@ -0,0 +1,11 @@
+//
+//  iterator.h
+//  stream
+//
+//  Created by Sam Jaffe on 4/2/23.
+//
+
+#pragma once
+
+#include <stream/iterator/common_iterator.h>
+#include <stream/iterator/transform_iterator.h>

+ 3 - 21
include/stream/streams.hpp

@@ -2,26 +2,8 @@
 
 #include <stream/forward.h>
 
-#include <stream/algorithm/count.h>
-#include <stream/algorithm/fold.h>
-#include <stream/algorithm/for_each.h>
-#include <stream/algorithm/minmax.h>
-#include <stream/algorithm/size.h>
-
-#include <stream/iterator/common_iterator.h>
-#include <stream/iterator/transform_iterator.h>
-
-#include <stream/view/all.h>
-#include <stream/view/any.h>
-#include <stream/view/common.h>
-#include <stream/view/empty.h>
-#include <stream/view/filter.h>
-#include <stream/view/iota.h>
-#include <stream/view/join.h>
-#include <stream/view/map.h>
-#include <stream/view/ref.h>
-#include <stream/view/single.h>
-#include <stream/view/subrange.h>
-#include <stream/view/transform.h>
+#include <stream/algorithm.h>
+#include <stream/iterator.h>
+#include <stream/view.h>
 
 #include <stream/to_container.h>

+ 21 - 0
include/stream/view.h

@@ -0,0 +1,21 @@
+//
+//  view.h
+//  stream
+//
+//  Created by Sam Jaffe on 4/2/23.
+//
+
+#pragma once
+
+#include <stream/view/all.h>
+#include <stream/view/any.h>
+#include <stream/view/common.h>
+#include <stream/view/empty.h>
+#include <stream/view/filter.h>
+#include <stream/view/iota.h>
+#include <stream/view/join.h>
+#include <stream/view/map.h>
+#include <stream/view/ref.h>
+#include <stream/view/single.h>
+#include <stream/view/subrange.h>
+#include <stream/view/transform.h>

+ 4 - 0
include/stream/view/all.h

@@ -7,6 +7,8 @@
 
 #pragma once
 
+#include <stream/forward.h>
+
 #include <iterator/detail/macro.h>
 
 namespace stream::ranges {
@@ -17,6 +19,8 @@ private:
 public:
   all_view(C && container) : container_(FWD(container)) {}
 
+  auto & base() const { return container_; }
+
   auto begin() const { return container_.begin(); }
   auto end() const { return container_.end(); }
   bool empty() const { return container_.empty(); }

+ 14 - 0
stream.xcodeproj/project.pbxproj

@@ -8,6 +8,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 */; };
 		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 */
@@ -96,6 +97,12 @@
 		CDA37D3A29D9BDF2000A1F97 /* undef.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = undef.h; sourceTree = "<group>"; };
 		CDA37D3B29D9C30B000A1F97 /* common_iterator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = common_iterator.h; sourceTree = "<group>"; };
 		CDA37D3C29D9C311000A1F97 /* transform_iterator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = transform_iterator.h; sourceTree = "<group>"; };
+		CDA37D3D29D9D129000A1F97 /* equal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = equal.h; sourceTree = "<group>"; };
+		CDA37D3E29D9D38E000A1F97 /* all_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = all_test.cxx; sourceTree = "<group>"; };
+		CDA37D4129D9D3F5000A1F97 /* algorithm.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = algorithm.h; sourceTree = "<group>"; };
+		CDA37D4229D9D417000A1F97 /* iterator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = iterator.h; sourceTree = "<group>"; };
+		CDA37D4329D9D434000A1F97 /* view.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = view.h; sourceTree = "<group>"; };
+		CDA37D4429D9D55D000A1F97 /* stream_test.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = stream_test.h; 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>"; };
@@ -156,6 +163,9 @@
 				CDA37D3729D9BB9C000A1F97 /* numeric */,
 				CD52827C29D4FB11001A84DE /* to_container.h */,
 				CDA37D3829D9BBA5000A1F97 /* view */,
+				CDA37D4129D9D3F5000A1F97 /* algorithm.h */,
+				CDA37D4229D9D417000A1F97 /* iterator.h */,
+				CDA37D4329D9D434000A1F97 /* view.h */,
 				CD5281F729D3B173001A84DE /* streams.hpp */,
 			);
 			path = stream;
@@ -195,7 +205,9 @@
 			isa = PBXGroup;
 			children = (
 				CD64CCB926232D6900770A30 /* xcode_gtest_helper.h */,
+				CDA37D4429D9D55D000A1F97 /* stream_test.h */,
 				CD9337281E3CD78B00699FF5 /* stream_test.cxx */,
+				CDA37D3E29D9D38E000A1F97 /* all_test.cxx */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -204,6 +216,7 @@
 			isa = PBXGroup;
 			children = (
 				CD5A8D3529D63C05008C2A4F /* count.h */,
+				CDA37D3D29D9D129000A1F97 /* equal.h */,
 				CD52827F29D50E24001A84DE /* fold.h */,
 				CD52827D29D50081001A84DE /* for_each.h */,
 				CD6EBE2629D63B9E00F387C1 /* minmax.h */,
@@ -444,6 +457,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				CDEC1D7623514BEB0091D9F2 /* stream_test.cxx in Sources */,
+				CDA37D4029D9D38E000A1F97 /* all_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 50 - 0
test/all_test.cxx

@@ -0,0 +1,50 @@
+//
+//  all_test.cxx
+//  stream-test
+//
+//  Created by Sam Jaffe on 4/2/23.
+//
+
+#include "stream/view/all.h"
+
+#include "stream/algorithm.h"
+
+#include "stream_test.h"
+
+using testing::Eq;
+using testing::Ne;
+
+TEST(AllView, IteratesOverNonConst) {
+  std::vector<int> input{1, 2, 3, 4, 5};
+  auto view = views::all(input);
+
+  EXPECT_FALSE(view.empty());
+  EXPECT_THAT(view.size(), input.size());
+  EXPECT_THAT(view, RangesEq(input));
+}
+
+TEST(AllView, IteratesOverConst) {
+  std::vector<int> const input{1, 2, 3, 4, 5};
+  auto view = views::all(input);
+
+  EXPECT_FALSE(view.empty());
+  EXPECT_THAT(view.size(), input.size());
+  EXPECT_THAT(view, RangesEq(input));
+}
+
+TEST(AllView, TakesOwnershipOfRvalue) {
+  std::vector<int> input{1, 2, 3, 4, 5};
+  auto * address = &input;
+  auto view = views::all(static_cast<std::vector<int> &&>(input));
+
+  EXPECT_TRUE(input.empty());
+  EXPECT_THAT(&view.base(), Ne(address));
+}
+
+TEST(AllView, IteratesOverRvalue) {
+  auto view = views::all(std::vector{1, 2, 3, 4, 5});
+
+  EXPECT_FALSE(view.empty());
+  EXPECT_THAT(view.size(), 5);
+  EXPECT_THAT(view, RangesEq(std::vector{1, 2, 3, 4, 5}));
+}

+ 39 - 0
test/stream_test.h

@@ -0,0 +1,39 @@
+//
+//  stream_test.h
+//  stream
+//
+//  Created by Sam Jaffe on 4/2/23.
+//
+
+#pragma once
+
+#include "xcode_gtest_helper.h"
+
+namespace ranges = stream::ranges;
+namespace views = stream::ranges::views;
+
+namespace stream::ranges::testing {
+template <typename It1, typename S1, typename It2, typename S2,
+          typename Cmp = std::equal_to<>>
+bool equal(It1 it1, S1 end1, It2 it2, S2 end2,
+           ::testing::MatchResultListener * result_listener, Cmp cmp = {}) {
+  size_t n = 0;
+  for (; it1 != end1 && it2 != end2; ++it1, ++it2, ++n) {
+    auto const & left = *it1;
+    auto const & right = *it2;
+    if (!std::invoke(cmp, left, right)) {
+      *result_listener << "at index " << n << " where values were: [" << left
+                       << "," << right << "] differ";
+      return false;
+    }
+  }
+  *result_listener << "whose sizes differ";
+  return (it1 != end1) == (it2 != end2);
+}
+}
+
+MATCHER_P(RangesEq, to_range, "") {
+  return stream::ranges::testing::equal(arg.begin(), arg.end(),
+                                        to_range.begin(), to_range.end(),
+                                        result_listener);
+}