Add file-line information to test failure messages.
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 9 Oct 2016 04:29:18 +0000 (00:29 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 9 Oct 2016 04:50:33 +0000 (00:50 -0400)
For EXPECT_UAF, attempt to catch exceptions in non-ZCPOINTER_TRACK_REFS mode,
in case the stdlib throws one.

Makefile
test.cc

index 935231b4da068721333b339c9f48f08162baf280..2245bf2741ee397bf003daf400f94dada0cb5f23 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,18 +1,24 @@
 CXX := clang++
 CXXFLAGS := -g -std=c++11
 
+ifeq ($(OPTIMIZED),1)
+       CXXFLAGS += -O2
+endif
+
 FILES := test.cc zcpointer.cc
 
+DEPS := $(FILES) zcpointer.h Makefile
+
 all: test-zc test-tr
 
 test: test-zc test-tr
        ./test-zc
        ./test-tr
 
-test-zc: $(FILES) zcpointer.h
+test-zc: $(DEPS)
        $(CXX) $(CXXFLAGS) $(FILES) -o $@
 
-test-tr: $(FILES) zcpointer.h
+test-tr: $(DEPS)
        $(CXX) -DZCPOINTER_TRACK_REFS=1 $(CXXFLAGS) $(FILES) -o $@
 
 clean:
diff --git a/test.cc b/test.cc
index 8769f4cac1fb7e6d92a58670f36a8e9e002a7a78..e8cbdf295c6d009e2f821eef65b29120c821ef2c 100644 (file)
--- a/test.cc
+++ b/test.cc
@@ -13,6 +13,7 @@
 // limitations under the License.
 
 #include <iostream>
+#include <stdexcept>
 #include <vector>
 
 #include "zcpointer.h"
@@ -24,14 +25,23 @@ class C {
   void DoThing() {}
 };
 
-#define EXPECT(expr) do { if (!(expr)) { throw std::logic_error(#expr); } } while(0)
+class TestFailure : public std::logic_error {
+ public:
+  using std::logic_error::logic_error;
+};
+
+#define STRING(x) QUOTE(x)
+#define QUOTE(x) #x
+#define AT_FILE_LINE " @ " __FILE__ ":" STRING(__LINE__)
+
+#define EXPECT(expr) do { if (!(expr)) { throw TestFailure(#expr AT_FILE_LINE); } } while(0)
 
 #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS
 
 #define EXPECT_UAF(expr) do { \
     try { \
       (expr); \
-      throw std::logic_error("Expected use-after-free: " #expr); \
+      throw TestFailure("Expected use-after-free: " #expr AT_FILE_LINE); \
     } catch (zc::UseAfterFreeError) {} \
   } while(0)
 
@@ -39,7 +49,11 @@ class C {
 
 #define EXPECT_UAF(expr) do { \
     std::cout << ">>> ZCPOINTER_TRACK_REFS not enabled, cannot catch UAF" << std::endl; \
-    (expr); \
+    try { \
+      (expr); \
+    } catch (std::logic_error& e) { \
+      std::cout << ">>> Caught error: " << typeid(e).name() << ": " << e.what() << std::endl; \
+    } \
   } while(0)
 
 #endif
@@ -176,7 +190,7 @@ int main() {
     try {
       test.test();
       std::cout << "+++ PASS " << test.name << " +++" << std::endl;
-    } catch (const std::logic_error& e) {
+    } catch (const TestFailure& e) {
       passed = false;
       std::cout << "!!! FAIL " << test.name
                 << ": Assertion failure: " << e.what() << " ===" << std::endl;