Ver código fonte

Convert tests to use GoogleTest.

Sam Jaffe 5 anos atrás
pai
commit
067fa97956

+ 22 - 0
pointer-test/Info.plist

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>CFBundleDevelopmentRegion</key>
+	<string>$(DEVELOPMENT_LANGUAGE)</string>
+	<key>CFBundleExecutable</key>
+	<string>$(EXECUTABLE_NAME)</string>
+	<key>CFBundleIdentifier</key>
+	<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
+	<key>CFBundleInfoDictionaryVersion</key>
+	<string>6.0</string>
+	<key>CFBundleName</key>
+	<string>$(PRODUCT_NAME)</string>
+	<key>CFBundlePackageType</key>
+	<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
+	<key>CFBundleShortVersionString</key>
+	<string>1.0</string>
+	<key>CFBundleVersion</key>
+	<string>1</string>
+</dict>
+</plist>

+ 108 - 56
pointers.xcodeproj/project.pbxproj

@@ -6,7 +6,23 @@
 	objectVersion = 46;
 	objects = {
 
+/* Begin PBXBuildFile section */
+		CDCB3C4224E4569C0029B771 /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CDEC1DCD2351677C0091D9F2 /* GoogleMock.framework */; };
+		CDCB3C4524E456A60029B771 /* const_propogating_ptr_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD0364691E1F3F0B003B7B84 /* const_propogating_ptr_test.cxx */; };
+		CDCB3C4624E456A60029B771 /* const_ptr_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD03646A1E1F41DE003B7B84 /* const_ptr_test.cxx */; };
+		CDCB3C4724E456A60029B771 /* value_ptr_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD03646B1E1F42A1003B7B84 /* value_ptr_test.cxx */; };
+		CDCB3C4824E456A60029B771 /* not_null_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDC3B0E31DF261D7002A415B /* not_null_test.cxx */; };
+		CDCB3C4924E456A60029B771 /* maybe_null_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDC3B0E71DF263B2002A415B /* maybe_null_test.cxx */; };
+/* End PBXBuildFile section */
+
 /* Begin PBXContainerItemProxy section */
+		CDCB3C4324E456A00029B771 /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = CDEC1DC52351677C0091D9F2 /* GoogleMock.xcodeproj */;
+			proxyType = 1;
+			remoteGlobalIDString = 05818F851A685AEA0072A469;
+			remoteInfo = GoogleMock;
+		};
 		CDEC1DCC2351677C0091D9F2 /* PBXContainerItemProxy */ = {
 			isa = PBXContainerItemProxy;
 			containerPortal = CDEC1DC52351677C0091D9F2 /* GoogleMock.xcodeproj */;
@@ -37,34 +53,25 @@
 		};
 /* End PBXContainerItemProxy section */
 
-/* Begin PBXCopyFilesBuildPhase section */
-		CD3DA3F01D9B42BC001B53A0 /* CopyFiles */ = {
-			isa = PBXCopyFilesBuildPhase;
-			buildActionMask = 2147483647;
-			dstPath = /usr/share/man/man1/;
-			dstSubfolderSpec = 0;
-			files = (
-			);
-			runOnlyForDeploymentPostprocessing = 1;
-		};
-/* End PBXCopyFilesBuildPhase section */
-
 /* Begin PBXFileReference section */
 		CD0364691E1F3F0B003B7B84 /* const_propogating_ptr_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = const_propogating_ptr_test.cxx; sourceTree = "<group>"; };
 		CD03646A1E1F41DE003B7B84 /* const_ptr_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = const_ptr_test.cxx; sourceTree = "<group>"; };
 		CD03646B1E1F42A1003B7B84 /* value_ptr_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = value_ptr_test.cxx; sourceTree = "<group>"; };
-		CD3DA3F21D9B42BC001B53A0 /* pointers_tc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = pointers_tc; sourceTree = BUILT_PRODUCTS_DIR; };
 		CDC3B0E31DF261D7002A415B /* not_null_test.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = not_null_test.cxx; sourceTree = "<group>"; };
 		CDC3B0E71DF263B2002A415B /* maybe_null_test.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = maybe_null_test.cxx; sourceTree = "<group>"; };
 		CDCB3C2F24E453E30029B771 /* pointer */ = {isa = PBXFileReference; lastKnownFileType = folder; name = pointer; path = include/pointer; sourceTree = "<group>"; };
+		CDCB3C3924E456950029B771 /* pointer-test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "pointer-test.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
+		CDCB3C3D24E456950029B771 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
+		CDCB3C4A24E4589D0029B771 /* test_stubs.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = test_stubs.h; sourceTree = "<group>"; };
 		CDEC1DC52351677C0091D9F2 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
-		CD3DA3EF1D9B42BC001B53A0 /* Frameworks */ = {
+		CDCB3C3624E456950029B771 /* Frameworks */ = {
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDCB3C4224E4569C0029B771 /* GoogleMock.framework in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -74,11 +81,12 @@
 		CD125DDB1E1F27E000B53FB5 /* test */ = {
 			isa = PBXGroup;
 			children = (
+				CDCB3C4A24E4589D0029B771 /* test_stubs.h */,
 				CD0364691E1F3F0B003B7B84 /* const_propogating_ptr_test.cxx */,
 				CD03646A1E1F41DE003B7B84 /* const_ptr_test.cxx */,
-				CD03646B1E1F42A1003B7B84 /* value_ptr_test.cxx */,
-				CDC3B0E31DF261D7002A415B /* not_null_test.cxx */,
 				CDC3B0E71DF263B2002A415B /* maybe_null_test.cxx */,
+				CDC3B0E31DF261D7002A415B /* not_null_test.cxx */,
+				CD03646B1E1F42A1003B7B84 /* value_ptr_test.cxx */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -89,18 +97,35 @@
 				CDEC1DC52351677C0091D9F2 /* GoogleMock.xcodeproj */,
 				CDCB3C2F24E453E30029B771 /* pointer */,
 				CD125DDB1E1F27E000B53FB5 /* test */,
+				CDCB3C3A24E456950029B771 /* pointer-test */,
 				CD3DA3F31D9B42BC001B53A0 /* Products */,
+				CDCB3C4124E4569C0029B771 /* Frameworks */,
 			);
 			sourceTree = "<group>";
 		};
 		CD3DA3F31D9B42BC001B53A0 /* Products */ = {
 			isa = PBXGroup;
 			children = (
-				CD3DA3F21D9B42BC001B53A0 /* pointers_tc */,
+				CDCB3C3924E456950029B771 /* pointer-test.xctest */,
 			);
 			name = Products;
 			sourceTree = "<group>";
 		};
+		CDCB3C3A24E456950029B771 /* pointer-test */ = {
+			isa = PBXGroup;
+			children = (
+				CDCB3C3D24E456950029B771 /* Info.plist */,
+			);
+			path = "pointer-test";
+			sourceTree = "<group>";
+		};
+		CDCB3C4124E4569C0029B771 /* Frameworks */ = {
+			isa = PBXGroup;
+			children = (
+			);
+			name = Frameworks;
+			sourceTree = "<group>";
+		};
 		CDEC1DC62351677C0091D9F2 /* Products */ = {
 			isa = PBXGroup;
 			children = (
@@ -115,23 +140,23 @@
 /* End PBXGroup section */
 
 /* Begin PBXNativeTarget section */
-		CD3DA3F11D9B42BC001B53A0 /* pointers_tc */ = {
+		CDCB3C3824E456950029B771 /* pointer-test */ = {
 			isa = PBXNativeTarget;
-			buildConfigurationList = CD3DA3F91D9B42BC001B53A0 /* Build configuration list for PBXNativeTarget "pointers_tc" */;
+			buildConfigurationList = CDCB3C3E24E456950029B771 /* Build configuration list for PBXNativeTarget "pointer-test" */;
 			buildPhases = (
-				CDC3B0E41DF2622C002A415B /* ShellScript */,
-				CD3DA3EE1D9B42BC001B53A0 /* Sources */,
-				CD3DA3EF1D9B42BC001B53A0 /* Frameworks */,
-				CD3DA3F01D9B42BC001B53A0 /* CopyFiles */,
+				CDCB3C3524E456950029B771 /* Sources */,
+				CDCB3C3624E456950029B771 /* Frameworks */,
+				CDCB3C3724E456950029B771 /* Resources */,
 			);
 			buildRules = (
 			);
 			dependencies = (
+				CDCB3C4424E456A00029B771 /* PBXTargetDependency */,
 			);
-			name = pointers_tc;
-			productName = pointers;
-			productReference = CD3DA3F21D9B42BC001B53A0 /* pointers_tc */;
-			productType = "com.apple.product-type.tool";
+			name = "pointer-test";
+			productName = "pointer-test";
+			productReference = CDCB3C3924E456950029B771 /* pointer-test.xctest */;
+			productType = "com.apple.product-type.bundle.unit-test";
 		};
 /* End PBXNativeTarget section */
 
@@ -142,8 +167,9 @@
 				LastUpgradeCheck = 1110;
 				ORGANIZATIONNAME = "Sam Jaffe";
 				TargetAttributes = {
-					CD3DA3F11D9B42BC001B53A0 = {
-						CreatedOnToolsVersion = 7.2.1;
+					CDCB3C3824E456950029B771 = {
+						CreatedOnToolsVersion = 11.3.1;
+						ProvisioningStyle = Automatic;
 					};
 				};
 			};
@@ -166,7 +192,7 @@
 			);
 			projectRoot = "";
 			targets = (
-				CD3DA3F11D9B42BC001B53A0 /* pointers_tc */,
+				CDCB3C3824E456950029B771 /* pointer-test */,
 			);
 		};
 /* End PBXProject section */
@@ -202,38 +228,39 @@
 		};
 /* End PBXReferenceProxy section */
 
-/* Begin PBXShellScriptBuildPhase section */
-		CDC3B0E41DF2622C002A415B /* ShellScript */ = {
-			isa = PBXShellScriptBuildPhase;
+/* Begin PBXResourcesBuildPhase section */
+		CDCB3C3724E456950029B771 /* Resources */ = {
+			isa = PBXResourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
 			);
-			inputPaths = (
-				"$(SRCROOT)/test/not_null.t.h",
-				"$(SRCROOT)/test/maybe_null.t.h",
-				"$(SRCROOT)/test/const_propogating_ptr.t.h",
-				"$(SRCROOT)/test/const_ptr.t.h",
-				"$(SRCROOT)/test/value_ptr.t.h",
-			);
-			outputPaths = (
-				"$(SRCROOT)/test/pointers_tc.cpp",
-			);
 			runOnlyForDeploymentPostprocessing = 0;
-			shellPath = /bin/sh;
-			shellScript = "cxxtestgen --error-printer -o test/pointers_tc.cpp test/*.t.h\n";
 		};
-/* End PBXShellScriptBuildPhase section */
+/* End PBXResourcesBuildPhase section */
 
 /* Begin PBXSourcesBuildPhase section */
-		CD3DA3EE1D9B42BC001B53A0 /* Sources */ = {
+		CDCB3C3524E456950029B771 /* Sources */ = {
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDCB3C4724E456A60029B771 /* value_ptr_test.cxx in Sources */,
+				CDCB3C4924E456A60029B771 /* maybe_null_test.cxx in Sources */,
+				CDCB3C4624E456A60029B771 /* const_ptr_test.cxx in Sources */,
+				CDCB3C4524E456A60029B771 /* const_propogating_ptr_test.cxx in Sources */,
+				CDCB3C4824E456A60029B771 /* not_null_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
 /* End PBXSourcesBuildPhase section */
 
+/* Begin PBXTargetDependency section */
+		CDCB3C4424E456A00029B771 /* PBXTargetDependency */ = {
+			isa = PBXTargetDependency;
+			name = GoogleMock;
+			targetProxy = CDCB3C4324E456A00029B771 /* PBXContainerItemProxy */;
+		};
+/* End PBXTargetDependency section */
+
 /* Begin XCBuildConfiguration section */
 		CD3DA3F71D9B42BC001B53A0 /* Debug */ = {
 			isa = XCBuildConfiguration;
@@ -338,20 +365,45 @@
 			};
 			name = Release;
 		};
-		CD3DA3FA1D9B42BC001B53A0 /* Debug */ = {
+		CDCB3C3F24E456950029B771 /* Debug */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
-				CODE_SIGN_IDENTITY = "-";
-				HEADER_SEARCH_PATHS = "${HOME}/Documents/Programming/Resources/cxxtest-4.4/";
+				CLANG_ANALYZER_NONNULL = YES;
+				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				CLANG_ENABLE_OBJC_WEAK = YES;
+				CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
+				CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
+				CODE_SIGN_STYLE = Automatic;
+				COMBINE_HIDPI_IMAGES = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu11;
+				INFOPLIST_FILE = "pointer-test/Info.plist";
+				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
+				MACOSX_DEPLOYMENT_TARGET = 10.14;
+				MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
+				MTL_FAST_MATH = YES;
+				PRODUCT_BUNDLE_IDENTIFIER = "leumasjaffe.pointer-test";
 				PRODUCT_NAME = "$(TARGET_NAME)";
 			};
 			name = Debug;
 		};
-		CD3DA3FB1D9B42BC001B53A0 /* Release */ = {
+		CDCB3C4024E456950029B771 /* Release */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
-				CODE_SIGN_IDENTITY = "-";
-				HEADER_SEARCH_PATHS = "${HOME}/Documents/Programming/Resources/cxxtest-4.4/";
+				CLANG_ANALYZER_NONNULL = YES;
+				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				CLANG_ENABLE_OBJC_WEAK = YES;
+				CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
+				CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
+				CODE_SIGN_STYLE = Automatic;
+				COMBINE_HIDPI_IMAGES = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu11;
+				INFOPLIST_FILE = "pointer-test/Info.plist";
+				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
+				MACOSX_DEPLOYMENT_TARGET = 10.14;
+				MTL_FAST_MATH = YES;
+				PRODUCT_BUNDLE_IDENTIFIER = "leumasjaffe.pointer-test";
 				PRODUCT_NAME = "$(TARGET_NAME)";
 			};
 			name = Release;
@@ -368,11 +420,11 @@
 			defaultConfigurationIsVisible = 0;
 			defaultConfigurationName = Release;
 		};
-		CD3DA3F91D9B42BC001B53A0 /* Build configuration list for PBXNativeTarget "pointers_tc" */ = {
+		CDCB3C3E24E456950029B771 /* Build configuration list for PBXNativeTarget "pointer-test" */ = {
 			isa = XCConfigurationList;
 			buildConfigurations = (
-				CD3DA3FA1D9B42BC001B53A0 /* Debug */,
-				CD3DA3FB1D9B42BC001B53A0 /* Release */,
+				CDCB3C3F24E456950029B771 /* Debug */,
+				CDCB3C4024E456950029B771 /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
 			defaultConfigurationName = Release;

+ 24 - 36
test/const_propogating_ptr_test.cxx

@@ -5,43 +5,31 @@
 //  Created by Sam Jaffe on 1/5/17.
 //
 
-#pragma once
-
 #include "pointer/const_propogating_ptr.hpp"
 
+#include <memory>
+
 #include <gmock/gmock.h>
 
-class const_propogating_ptr_TestSuite : public CxxTest::TestSuite {
-public:
-  struct mock_object {
-    bool is_const( ) { return false; }
-    bool is_const( ) const { return true; }
-  };
-private:
-  using ptr_t = std::shared_ptr<mock_object>;
-public:
-  void test_non_const() {
-    ptr_t p { new mock_object };
-    const_propogating_ptr< ptr_t > cp { p };
-    TS_ASSERT( ! cp->is_const() );
-  }
-  
-  void test_const() {
-    ptr_t p { new mock_object };
-    const_propogating_ptr< ptr_t > const cp { p };
-    TS_ASSERT( cp->is_const() );
-  }
-  
-  void test_do_not_own() const {
-    bool has_delete{false};
-    struct test_t {
-      ~test_t() { _r = true; }
-      bool & _r;
-    };
-    test_t * test_struct = new test_t{has_delete};
-    const_propogating_ptr<test_t *>{test_struct};
-    TS_ASSERT_EQUALS(has_delete, false);
-    delete test_struct;
-    TS_ASSERT_EQUALS(has_delete, true);
-  }
-};
+#include "test_stubs.h"
+
+TEST(ConstPropogatingPtrTest, NonConstCPPCallsNonConstMethods) {
+  std::shared_ptr<const_tracking_stub> p{new const_tracking_stub};
+  const_propogating_ptr<std::shared_ptr<const_tracking_stub>> cp{p};
+  EXPECT_FALSE(cp->is_const());
+}
+
+TEST(ConstPropogatingPtrTest, ConstCPPCallsConstMethods) {
+  std::shared_ptr<const_tracking_stub> p{new const_tracking_stub};
+  const_propogating_ptr<std::shared_ptr<const_tracking_stub>> const cp{p};
+  EXPECT_TRUE(cp->is_const());
+}
+
+TEST(ConstPropogatingPtrTest, DoesNotDeleteRawPtr) {
+  bool has_delete{false};
+  destructor_sentinal * test_struct = new destructor_sentinal{has_delete};
+  const_propogating_ptr<destructor_sentinal *>{test_struct};
+  EXPECT_FALSE(has_delete);
+  delete test_struct;
+  EXPECT_TRUE(has_delete);
+}

+ 32 - 45
test/const_ptr_test.cxx

@@ -5,52 +5,39 @@
 //  Created by Sam Jaffe on 1/5/17.
 //
 
-#pragma once
-
 #include "pointer/const_ptr.hpp"
+#include "pointer/const_propogating_ptr.hpp"
 
 #include <gmock/gmock.h>
 
-class const_ptr_TestSuite : public CxxTest::TestSuite {
-public:
-  struct mock_object {
-    bool is_const( ) { return false; }
-    bool is_const( ) const { return true; }
-  };
-private:
-  using ptr_t = std::shared_ptr<mock_object>;
-public:
-  void test_non_const() {
-    ptr_t p { new mock_object };
-    const_ptr< ptr_t > cp { p };
-    TS_ASSERT( cp->is_const() );
-  }
-  
-  void test_const() {
-    ptr_t p { new mock_object };
-    const_ptr< ptr_t > const cp { p };
-    TS_ASSERT( cp->is_const() );
-  }
-  
-  void test_converts_from_cpp() {
-    ptr_t p { new mock_object };
-    const_propogating_ptr< ptr_t > cpp { p };
-    const_ptr< ptr_t > cp { p };
-    TS_ASSERT_EQUALS( cp.get(), cpp.get() );
-    TS_ASSERT( ! cpp->is_const() );
-    TS_ASSERT( cp->is_const() );
-  }
-  
-  void test_do_not_own() const {
-    bool has_delete{false};
-    struct test_t {
-      ~test_t() { _r = true; }
-      bool & _r;
-    };
-    test_t * test_struct = new test_t{has_delete};
-    const_ptr<test_t *>{test_struct};
-    TS_ASSERT_EQUALS(has_delete, false);
-    delete test_struct;
-    TS_ASSERT_EQUALS(has_delete, true);
-  }
-};
+#include "test_stubs.h"
+
+TEST(ConstPtrTest, NonConstCPCallConstMethods) {
+  std::shared_ptr<const_tracking_stub> p{new const_tracking_stub};
+  const_ptr<std::shared_ptr<const_tracking_stub>> cp{p};
+  EXPECT_TRUE(cp->is_const());
+}
+
+TEST(ConstPtrTest, ConstCPCallConstMethods) {
+  std::shared_ptr<const_tracking_stub> p{new const_tracking_stub};
+  const_ptr<std::shared_ptr<const_tracking_stub>> const cp{p};
+  EXPECT_TRUE(cp->is_const());
+}
+
+TEST(ConstPtrTest, CanConstructFromConstPropogatingPtr) {
+  std::shared_ptr<const_tracking_stub> p{new const_tracking_stub};
+  const_propogating_ptr<std::shared_ptr<const_tracking_stub>> cpp{p};
+  const_ptr<std::shared_ptr<const_tracking_stub>> cp{p};
+  EXPECT_THAT(cp.get(), cpp.get());
+  EXPECT_FALSE(cpp->is_const());
+  EXPECT_TRUE(cp->is_const());
+}
+
+TEST(ConstPtrTest, DoesNotDeleteRawPtr) {
+  bool has_delete{false};
+  destructor_sentinal * test_struct = new destructor_sentinal{has_delete};
+  const_ptr<destructor_sentinal *>{test_struct};
+  EXPECT_FALSE(has_delete);
+  delete test_struct;
+  EXPECT_TRUE(has_delete);
+}

+ 52 - 71
test/maybe_null_test.cxx

@@ -5,8 +5,6 @@
 //  Created by Sam Jaffe on 12/2/16.
 //
 
-#pragma once
-
 // Define DEBUG in order to obtain certain checks...
 #ifndef DEBUG
 #define DEBUG
@@ -18,72 +16,55 @@
 
 #include <gmock/gmock.h>
 
-class maybe_null_TestSuite : public CxxTest::TestSuite {
-public:
-  using ptr_base = std::shared_ptr<int>;
-  using ptr_t = maybe_null<ptr_base>;
-public:
-  void test_can_hold_nullptr() const {
-    ptr_t mn{nullptr};
-    TS_ASSERT_EQUALS(bool(mn), false);
-  }
-  
-  void test_is_valid_wo_nullptr() const {
-    ptr_t mn = make_with(5);
-    TS_ASSERT_EQUALS(bool(mn), true);
-  }
-  
-  void test_nullptr_throws_or_false() const {
-    ptr_t mn{nullptr};
-    TS_ASSERT_THROWS(*mn, unchecked_pointer_exception);
-  }
-  
-  void test_object_contains_ptr() const {
-    ptr_base i{new int};
-    ptr_t n{i};
-    TS_ASSERT_EQUALS(bool(n), true);
-    TS_ASSERT_EQUALS(n.get(), i.get());
-    TS_ASSERT_EQUALS(*n, *i);
-  }
-  
-  void test_object_sets_value() const {
-    static int const value1{5};
-    static int const value2{4};
-    
-    ptr_base i{new int{value1}};
-    ptr_t n{i};
-    TS_ASSERT_EQUALS(bool(n), true);
-    TS_ASSERT_EQUALS(*n, value1);
-    *n = value2;
-    TS_ASSERT_EQUALS(bool(n), true);
-    TS_ASSERT_EQUALS(*i, value2);
-  }
-  
-  void test_do_not_own() const {
-    bool has_delete{false};
-    struct test_t {
-      ~test_t() { _r = true; }
-      bool & _r;
-    };
-    test_t * test_struct = new test_t{has_delete};
-    maybe_null<test_t *>{test_struct};
-    TS_ASSERT_EQUALS(has_delete, false);
-    delete test_struct;
-    TS_ASSERT_EQUALS(has_delete, true);
-  }
-  
-  void test_type_conversion() {
-    struct base {};
-    struct derived : public base {};
-    derived d;
-    maybe_null<derived*> m{&d};
-    maybe_null<base*> b = m;
-    TS_ASSERT_EQUALS(b.get(), m.get());
-  }
-  
-private:
-  template <typename T>
-  static maybe_null<std::shared_ptr<T>> make_with(T p) {
-    return maybe_null<std::shared_ptr<T>>(std::make_shared<T>(p));
-  }
-};
+#include "test_stubs.h"
+
+TEST(MaybeNullPtrTest, NullPtrEvaluatesAsFalse) {
+  maybe_null<std::shared_ptr<int>> mn{nullptr};
+  EXPECT_FALSE(mn);
+}
+
+TEST(MaybeNullPtrTest, ValidPtrEvaluatesAsTrue) {
+  maybe_null<std::shared_ptr<int>> mn{std::make_shared<int>(0)};
+  EXPECT_TRUE(mn);
+}
+
+TEST(MaybeNullPtrTest, DereferencingNullPtrThrows) {
+  maybe_null<std::shared_ptr<int>> mn{nullptr};
+  EXPECT_THROW(*mn, unchecked_pointer_exception);
+}
+
+TEST(MaybeNullPtrTest, ContainsInnerPtrForEquality) {
+  std::shared_ptr<int> i{new int};
+  maybe_null<std::shared_ptr<int>> n{i};
+  EXPECT_TRUE(n);
+  EXPECT_THAT(n.get(), i.get());
+  EXPECT_THAT(*n, *i);
+}
+
+TEST(MaybeNullPtrTest, UpcastedPointerStillEqual) {
+  struct base {};
+  struct derived : public base {};
+  derived d;
+  maybe_null<derived*> m{&d};
+  maybe_null<base*> b = m;
+  EXPECT_THAT(b.get(), m.get());
+}
+
+TEST(MaybeNullPtrTest, CanModifyInnerValue) {
+  std::shared_ptr<int> i{new int{5}};
+  maybe_null<std::shared_ptr<int>> n{i};
+  EXPECT_TRUE(n);
+  EXPECT_THAT(*n, 5);
+  *n = 4;
+  EXPECT_TRUE(n);
+  EXPECT_THAT(*i, 4);
+}
+
+TEST(MaybeNullPtrTest, DoesNotDeleteRawPtr) {
+  bool has_delete{false};
+  destructor_sentinal * test_struct = new destructor_sentinal{has_delete};
+  maybe_null<destructor_sentinal *>{test_struct};
+  EXPECT_FALSE(has_delete);
+  delete test_struct;
+  EXPECT_TRUE(has_delete);
+}

+ 30 - 41
test/not_null_test.cxx

@@ -5,48 +5,37 @@
 //  Created by Sam Jaffe on 12/2/16.
 //
 
-#pragma once
-
 #include "pointer/not_null.hpp"
 
 #include <gmock/gmock.h>
 
-class not_null_TestSuite : public CxxTest::TestSuite {
-public:
-  using ptr_base = std::shared_ptr<int>;
-  using ptr_t = not_null<ptr_base>;
-public:
-  void test_nullptr_throws() const {
-    TS_ASSERT_THROWS(ptr_t(ptr_base(nullptr)), null_pointer_exception);
-  }
-  
-  void test_object_contains_ptr() const {
-    ptr_base i{new int};
-    ptr_t n{i};
-    TS_ASSERT_EQUALS(n.get(), i.get());
-    TS_ASSERT_EQUALS(*n, *i);
-  }
-  
-  void test_object_sets_value() const {
-    static int const value1{5};
-    static int const value2{4};
-    ptr_base i{new int(value1)};
-    ptr_t n{i};
-    TS_ASSERT_EQUALS(*n, value1);
-    *n = value2;
-    TS_ASSERT_EQUALS(*i, value2);
-  }
-  
-  void test_do_not_own() const {
-    bool has_delete{false};
-    struct test_t {
-      ~test_t() { _r = true; }
-      bool & _r;
-    };
-    test_t * test_struct = new test_t{has_delete};
-    not_null<test_t *>{test_struct};
-    TS_ASSERT_EQUALS(has_delete, false);
-    delete test_struct;
-    TS_ASSERT_EQUALS(has_delete, true);
-  }
-};
+#include "test_stubs.h"
+
+TEST(NonNullPtrTest, ThrowsIfNullptrIsSnuckIn) {
+  EXPECT_THROW(not_null<std::shared_ptr<int>>(std::shared_ptr<int>(nullptr)),
+               null_pointer_exception);
+}
+
+TEST(NonNullPtrTest, ContainsInnerPtrForEquality) {
+  std::shared_ptr<int> i{new int};
+  not_null<std::shared_ptr<int>> n{i};
+  EXPECT_THAT(n.get(), i.get());
+  EXPECT_THAT(*n, *i);
+}
+
+TEST(NonNullPtrTest, CanModifyInnerValue) {
+  std::shared_ptr<int> i{new int(5)};
+  not_null<std::shared_ptr<int>> n{i};
+  EXPECT_THAT(*n, 5);
+  *n = 4;
+  EXPECT_THAT(*i, 4);
+}
+
+TEST(NonNullPtrTest, DoesNotDeleteRawPtr) {
+  bool has_delete{false};
+  destructor_sentinal * test_struct = new destructor_sentinal{has_delete};
+  not_null<destructor_sentinal *>{test_struct};
+  EXPECT_FALSE(has_delete);
+  delete test_struct;
+  EXPECT_TRUE(has_delete);
+}

+ 24 - 0
test/test_stubs.h

@@ -0,0 +1,24 @@
+//
+//  test_stubs.h
+//  pointers
+//
+//  Created by Sam Jaffe on 8/12/20.
+//  Copyright © 2020 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+struct const_tracking_stub {
+  bool is_const() { return false; }
+  bool is_const() const { return true; }
+};
+
+struct destructor_sentinal {
+  ~destructor_sentinal() { ref_ = true; }
+  bool & ref_;
+};
+
+struct destructor_ctr {
+  ~destructor_ctr() { ++ref_; }
+  int & ref_;
+};

+ 88 - 105
test/value_ptr_test.cxx

@@ -5,14 +5,14 @@
 //  Created by Sam Jaffe on 1/5/17.
 //
 
-#pragma once
-
 #include "pointer/value_ptr.hpp"
 
 #include <stdexcept>
 
 #include <gmock/gmock.h>
 
+#include "test_stubs.h"
+
 struct copy_me {};
 struct copy_me_throw {};
 
@@ -26,113 +26,96 @@ namespace detail {
   };
 }
 
-class copy_ptr_TestSuite : public CxxTest::TestSuite {
-private:
+class base {
+public:
+  virtual ~base() {}
+  virtual base* clone() const = 0;
+  virtual int id() const = 0;
+};
 
-  class base {
-  public:
-    virtual ~base() {}
-    virtual base* clone() const = 0;
-    virtual int id() const = 0;
-  };
-  
-  class derived_0 : public base {
-  public:
-    static const constexpr int ID = 0;
-    virtual base * clone() const override { return new derived_0; }
-    virtual int id() const override { return ID; }
-  };
-  
-  class derived_1 : public base {
-  public:
-    static const constexpr int ID = 1;
-    virtual base * clone() const override { return new derived_1; }
-    virtual int id() const override { return ID; }
-  };
+class derived_0 : public base {
 public:
-  void test_copy_new_obj() {
-    value_ptr<copy_me> c1 { new copy_me };
-    value_ptr<copy_me> c2 { c1 };
-    TS_ASSERT_DIFFERS( c1.get(), c2.get() );
-  }
-  
-  void test_copy_with_nullptr() {
-    value_ptr<copy_me> c1 { nullptr };
-    value_ptr<copy_me> c2 { c1 };
-    TS_ASSERT_EQUALS( c1.get(), nullptr );
-    TS_ASSERT_EQUALS( c1.get(), c2.get() );
-  }
+  static const constexpr int ID = 0;
+  virtual base * clone() const override { return new derived_0; }
+  virtual int id() const override { return ID; }
+};
 
-  void test_copy_custom_func() {
-    using ptr_t = value_ptr<copy_me_throw>;
-    ptr_t c1 { new copy_me_throw };
-    ptr_t c2 { c1 };
-    TS_ASSERT_DIFFERS( c1.get(), c2.get() );
-  }
+class derived_1 : public base {
+public:
+  static const constexpr int ID = 1;
+  virtual base * clone() const override { return new derived_1; }
+  virtual int id() const override { return ID; }
+};
 
-  void test_copy_custom_func_with_nullptr() {
-    using ptr_t = value_ptr<copy_me_throw>;
-    ptr_t c1 { nullptr };
-    TS_ASSERT_THROWS( ptr_t{ c1 }, std::runtime_error );
-  }
-  
-  void test_copy_object_is_deep_equals() {
-    using vec_t = std::vector<int>;
-    vec_t my_vec = { 1, 3, 5, 3, 6, 1, 2, -1, 0 };
-    value_ptr<vec_t> c1{ new vec_t{ my_vec } };
-    TS_ASSERT_EQUALS( *c1, my_vec );
-    value_ptr<vec_t> c2{ c1 };
-    TS_ASSERT_EQUALS( *c2, *c1 );
-  }
-  
-  void test_clone_polymorpic_object() {
-    using ptr_t = value_ptr<base>;
-    ptr_t c0 { new derived_0 };
-    TS_ASSERT_EQUALS( ptr_t( c0 )->id(), derived_0::ID);
-    ptr_t c1 { new derived_1 };
-    TS_ASSERT_EQUALS( ptr_t( c1 )->id(), derived_1::ID);
-  }
-  
-  void test_clone_polymorphic_with_nullptr() {
-    using ptr_t = value_ptr<base>;
-    ptr_t c1 { nullptr };
-    ptr_t c2 { c1 };
-    TS_ASSERT_EQUALS( c1.get(), nullptr );
-    TS_ASSERT_EQUALS( c1.get(), c2.get() );
-  }
-  
-  void test_does_own() const {
-    bool has_delete{false};
-    struct test_t {
-      ~test_t() { _r = true; }
-      bool & _r;
-    };
-    test_t * test_struct = new test_t{has_delete};
-    TS_ASSERT_THROWS_NOTHING(value_ptr<test_t>{ std::move(test_struct) });
-    TS_ASSERT_EQUALS(has_delete, true);
-  }
+TEST(ValuePtrTest, CanCopySimpleObject) {
+  value_ptr<copy_me> c1 {new copy_me};
+  value_ptr<copy_me> c2 {c1};
+  EXPECT_THAT(c1.get(), testing::Not(c2.get()));
+}
 
-  void test_does_own_temporary() const {
-    bool has_delete{false};
-    struct test_t {
-      ~test_t() { _r = true; }
-      bool & _r;
-    };
-    TS_ASSERT_THROWS_NOTHING(value_ptr<test_t>{ new test_t{has_delete} });
-    TS_ASSERT_EQUALS(has_delete, true);
-  }
+TEST(ValuePtrTest, CopyCtorIsNullSafe) {
+  value_ptr<copy_me> c1{nullptr};
+  value_ptr<copy_me> c2{c1};
+  EXPECT_THAT(c1.get(), testing::IsNull());
+  EXPECT_THAT(c1.get(), c2.get());
+}
 
-  void test_does_copy_on_copy() const {
-    int deleted{0};
-    struct test_t {
-      ~test_t() { ++(*_r); }
-      int * _r;
-    };
-    {
-      std::unique_ptr<test_t> test_struct{new test_t{&deleted}};
-      TS_ASSERT_THROWS_NOTHING(value_ptr<test_t>::copy_of( test_struct.get() ));
-      TS_ASSERT_EQUALS(deleted, 1);
-    }
-    TS_ASSERT_EQUALS(deleted, 2);
+TEST(ValuePtrCustomTest, CanCopyWithCustomFunction) {
+  using ptr_t = value_ptr<copy_me_throw>;
+  value_ptr<copy_me_throw> c1 {new copy_me_throw};
+  value_ptr<copy_me_throw> c2 {c1};
+  EXPECT_THAT(c1.get(), testing::Not(c2.get()));
+}
+
+TEST(ValuePtrCustomTest, CustomCopyFunctionCanThrow) {
+  value_ptr<copy_me_throw> c1{nullptr};
+  EXPECT_THROW(value_ptr<copy_me_throw>{c1}, std::runtime_error);
+}
+
+TEST(ValuePtrTest, CopiedValueDeeplyEqualButNotSameAddress) {
+  std::vector<int> my_vec = { 1, 3, 5, 3, 6, 1, 2, -1, 0 };
+  value_ptr<std::vector<int>> c1{new std::vector<int>{my_vec}};
+  EXPECT_THAT(*c1, my_vec);
+  value_ptr<std::vector<int>> c2{c1};
+  EXPECT_THAT(*c2, *c1);
+  EXPECT_THAT(c2.get(), testing::Not(c1.get()));
+}
+
+TEST(ValuePtrPolymorphicTest, CanConstructPtrToBaseFromDerivedClasses) {
+  value_ptr<base> c0{new derived_0};
+  EXPECT_THAT(c0->id(), derived_0::ID);
+  value_ptr<base> c1{new derived_1};
+  EXPECT_THAT(c1->id(), derived_1::ID);
+}
+
+TEST(ValuePtrPolymorphicTest, CanConstructFromNullptr) {
+  value_ptr<base> c1 {nullptr};
+  value_ptr<base> c2 {c1};
+  EXPECT_THAT(c1.get(), testing::IsNull());
+  EXPECT_THAT(c1.get(), c2.get());
+}
+
+TEST(ValuePtrTest, OwnsGivenObject) {
+  bool has_delete{false};
+  destructor_sentinal * test_struct = new destructor_sentinal{has_delete};
+  // I cannot actually copy-construct from a pointer for safety
+  EXPECT_NO_THROW(value_ptr<destructor_sentinal>{std::move(test_struct)});
+  EXPECT_TRUE(has_delete);
+}
+
+TEST(ValuePtrTest, OwnsTemporaryObject) {
+  bool has_delete{false};
+  EXPECT_NO_THROW(value_ptr<destructor_sentinal>{
+    new destructor_sentinal{has_delete}});
+  EXPECT_TRUE(has_delete);
+}
+
+TEST(ValuePtrTest, FactoryMethodProducesCopy) {
+  int deleted{0};
+  {
+    std::unique_ptr<destructor_ctr> test_struct{new destructor_ctr{deleted}};
+    EXPECT_NO_THROW(value_ptr<destructor_ctr>::copy_of(test_struct.get()));
+    EXPECT_THAT(deleted, 1);
   }
-};
+  EXPECT_THAT(deleted, 2);
+}