浏览代码

Fix #if guard.
Adding test cases to scope_guard.

Samuel Jaffe 9 年之前
父节点
当前提交
3bb5ab224b
共有 3 个文件被更改,包括 149 次插入10 次删除
  1. 1 1
      scope_guard.hpp
  2. 90 0
      scope_guard.t.h
  3. 58 9
      scope_guard.xcodeproj/project.pbxproj

+ 1 - 1
scope_guard.hpp

@@ -16,7 +16,7 @@ struct scope_exit {
   std::function<void()> _f;
 };
 
-#if _cplusplus > 201402L
+#if __cplusplus > 201402L
 template <bool B>
 struct scope_exception {
   template <typename F> scope_exception(F f) : _f(f), _except(std::uncaught_exceptions()) {}

+ 90 - 0
scope_guard.t.h

@@ -0,0 +1,90 @@
+//
+//  scope_guard_tc.h
+//  scope_guard
+//
+//  Created by Sam Jaffe on 1/10/17.
+//
+#pragma once
+
+#include <cxxtest/TestSuite.h>
+#include <memory>
+
+#include "scope_guard.hpp"
+
+class scope_guard_TestSuite : public CxxTest::TestSuite {
+public:
+  using ptr = std::shared_ptr<int>;
+  static ptr make( int i ) { return std::make_shared<int>(i); }
+  
+#if __cplusplus > 201402L
+  struct example {
+    ~example() {
+      scope(success) { ++(*_p); };
+    }
+    ptr _p;
+  };
+#endif
+public:
+  void test_scope_exit_reset() const {
+    ptr p = make( 5 );
+    {
+      scope(exit) { p.reset(); };
+      TS_ASSERT( p );
+    }
+    TS_ASSERT( ! p );
+  }
+  
+#if __cplusplus > 201402L
+  void test_scope_failure_no_effect() const {
+    ptr p = make( 5 );
+    try {
+      scope(failure) { p.reset(); };
+    } catch (...) {
+    }
+    TS_ASSERT( p );
+  }
+
+  void test_scope_failure_on_exception() const {
+    ptr p = make( 5 );
+    try {
+      scope(failure) { p.reset(); };
+      throw 5;
+    } catch (...) {
+    }
+    TS_ASSERT( ! p );
+  }
+  
+  void test_scope_success_works() const {
+    ptr p = make( 5 );
+    try {
+      scope(success) { p.reset(); };
+    } catch (...) {
+    }
+    TS_ASSERT( ! p );
+  }
+  
+  void test_scope_success_on_exception_no_effect() const {
+    ptr p = make( 5 );
+    try {
+      scope(success) { p.reset(); };
+      throw 5;
+    } catch (...) {
+    }
+    TS_ASSERT( p );
+  }
+  
+  void test_multi_layer_scoping() const {
+    ptr p = make( 5 );
+    ptr r = p;
+    try {
+      scope(failure) { p.reset(); };
+      example ex { r };
+      throw 5;
+    } catch (...) {
+    }
+    TS_ASSERT( ! p );
+    TS_ASSERT_EQUALS( *r, 6 );
+  }
+
+#endif
+};

+ 58 - 9
scope_guard.xcodeproj/project.pbxproj

@@ -6,6 +6,10 @@
 	objectVersion = 46;
 	objects = {
 
+/* Begin PBXBuildFile section */
+		CD3A75FA1E25B46F008C6DC2 /* scope_guard_tc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3A75F81E25B46F008C6DC2 /* scope_guard_tc.cpp */; };
+/* End PBXBuildFile section */
+
 /* Begin PBXCopyFilesBuildPhase section */
 		CD3DA4071D9B42F4001B53A0 /* CopyFiles */ = {
 			isa = PBXCopyFilesBuildPhase;
@@ -19,7 +23,9 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
-		CD3DA4091D9B42F4001B53A0 /* scope_guard */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = scope_guard; sourceTree = BUILT_PRODUCTS_DIR; };
+		CD3A75F81E25B46F008C6DC2 /* scope_guard_tc.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = scope_guard_tc.cpp; sourceTree = "<group>"; };
+		CD3A75F91E25B46F008C6DC2 /* scope_guard.t.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = scope_guard.t.h; sourceTree = "<group>"; };
+		CD3DA4091D9B42F4001B53A0 /* scope_guard_tc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = scope_guard_tc; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD3DA4131D9B4302001B53A0 /* scope_guard.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = scope_guard.hpp; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
@@ -34,10 +40,28 @@
 /* End PBXFrameworksBuildPhase section */
 
 /* Begin PBXGroup section */
-		CD3DA4001D9B42F4001B53A0 = {
+		CD3A75FB1E25B480008C6DC2 /* test */ = {
+			isa = PBXGroup;
+			children = (
+				CD3A75F91E25B46F008C6DC2 /* scope_guard.t.h */,
+				CD3A75F81E25B46F008C6DC2 /* scope_guard_tc.cpp */,
+			);
+			name = test;
+			sourceTree = "<group>";
+		};
+		CD3A75FC1E25B486008C6DC2 /* src */ = {
 			isa = PBXGroup;
 			children = (
 				CD3DA4131D9B4302001B53A0 /* scope_guard.hpp */,
+			);
+			name = src;
+			sourceTree = "<group>";
+		};
+		CD3DA4001D9B42F4001B53A0 = {
+			isa = PBXGroup;
+			children = (
+				CD3A75FC1E25B486008C6DC2 /* src */,
+				CD3A75FB1E25B480008C6DC2 /* test */,
 				CD3DA40A1D9B42F4001B53A0 /* Products */,
 			);
 			sourceTree = "<group>";
@@ -45,7 +69,7 @@
 		CD3DA40A1D9B42F4001B53A0 /* Products */ = {
 			isa = PBXGroup;
 			children = (
-				CD3DA4091D9B42F4001B53A0 /* scope_guard */,
+				CD3DA4091D9B42F4001B53A0 /* scope_guard_tc */,
 			);
 			name = Products;
 			sourceTree = "<group>";
@@ -53,10 +77,11 @@
 /* End PBXGroup section */
 
 /* Begin PBXNativeTarget section */
-		CD3DA4081D9B42F4001B53A0 /* scope_guard */ = {
+		CD3DA4081D9B42F4001B53A0 /* scope_guard_tc */ = {
 			isa = PBXNativeTarget;
-			buildConfigurationList = CD3DA4101D9B42F4001B53A0 /* Build configuration list for PBXNativeTarget "scope_guard" */;
+			buildConfigurationList = CD3DA4101D9B42F4001B53A0 /* Build configuration list for PBXNativeTarget "scope_guard_tc" */;
 			buildPhases = (
+				CD3A75FD1E25B4BE008C6DC2 /* ShellScript */,
 				CD3DA4051D9B42F4001B53A0 /* Sources */,
 				CD3DA4061D9B42F4001B53A0 /* Frameworks */,
 				CD3DA4071D9B42F4001B53A0 /* CopyFiles */,
@@ -65,9 +90,9 @@
 			);
 			dependencies = (
 			);
-			name = scope_guard;
+			name = scope_guard_tc;
 			productName = scope_guard;
-			productReference = CD3DA4091D9B42F4001B53A0 /* scope_guard */;
+			productReference = CD3DA4091D9B42F4001B53A0 /* scope_guard_tc */;
 			productType = "com.apple.product-type.tool";
 		};
 /* End PBXNativeTarget section */
@@ -96,16 +121,35 @@
 			projectDirPath = "";
 			projectRoot = "";
 			targets = (
-				CD3DA4081D9B42F4001B53A0 /* scope_guard */,
+				CD3DA4081D9B42F4001B53A0 /* scope_guard_tc */,
 			);
 		};
 /* End PBXProject section */
 
+/* Begin PBXShellScriptBuildPhase section */
+		CD3A75FD1E25B4BE008C6DC2 /* ShellScript */ = {
+			isa = PBXShellScriptBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			inputPaths = (
+				"$(SRCROOT)/scope_guard.t.h",
+			);
+			outputPaths = (
+				"$(SRCROOT)/scope_guard_tc.cpp",
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+			shellPath = /bin/sh;
+			shellScript = "cxxtestgen --error-printer -o scope_guard_tc.cpp scope_guard.t.h";
+		};
+/* End PBXShellScriptBuildPhase section */
+
 /* Begin PBXSourcesBuildPhase section */
 		CD3DA4051D9B42F4001B53A0 /* Sources */ = {
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD3A75FA1E25B46F008C6DC2 /* scope_guard_tc.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -194,6 +238,8 @@
 		CD3DA4111D9B42F4001B53A0 /* Debug */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				HEADER_SEARCH_PATHS = /usr/local/include/;
 				PRODUCT_NAME = "$(TARGET_NAME)";
 			};
 			name = Debug;
@@ -201,6 +247,8 @@
 		CD3DA4121D9B42F4001B53A0 /* Release */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				HEADER_SEARCH_PATHS = /usr/local/include/;
 				PRODUCT_NAME = "$(TARGET_NAME)";
 			};
 			name = Release;
@@ -217,13 +265,14 @@
 			defaultConfigurationIsVisible = 0;
 			defaultConfigurationName = Release;
 		};
-		CD3DA4101D9B42F4001B53A0 /* Build configuration list for PBXNativeTarget "scope_guard" */ = {
+		CD3DA4101D9B42F4001B53A0 /* Build configuration list for PBXNativeTarget "scope_guard_tc" */ = {
 			isa = XCConfigurationList;
 			buildConfigurations = (
 				CD3DA4111D9B42F4001B53A0 /* Debug */,
 				CD3DA4121D9B42F4001B53A0 /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 /* End XCConfigurationList section */
 	};