Throw a custom exception type and attempt to catch it in the test program.
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 8 Oct 2016 21:56:52 +0000 (17:56 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 8 Oct 2016 21:56:52 +0000 (17:56 -0400)
This allows all the tests to run. It is possible to inspect the exception by
breaking on __cxa_throw.

test.cc
zcpointer.cc
zcpointer.h

diff --git a/test.cc b/test.cc
index 4ea07e03b8630a7e2056dc03097f077b01ed71b1..23c310a5bc28236d413f3f64e4f23f871c6fb7d5 100644 (file)
--- a/test.cc
+++ b/test.cc
@@ -66,6 +66,26 @@ void TestPtr() {
   ref->DoThing();
 }
 
+#define TEST_FUNC(fn) { #fn , Test##fn }
+
 int main() {
-  TestPtr();
+  struct {
+    const char* name;
+    void (*test)();
+  } kTests[] = {
+    TEST_FUNC(Reset),
+    TEST_FUNC(Move),
+    TEST_FUNC(Ptr),
+  };
+
+  for (const auto& test : kTests) {
+    std::cout << "=== BEGIN " << test.name << " ===" << std::endl;
+    try {
+      test.test();
+      std::cout << "=== FAIL " << test.name
+                << ": Did not receive UseAfterFreeException ===" << std::endl;
+    } catch (zc::UseAfterFreeError) {
+      std::cout << "=== PASS " << test.name << " ===" << std::endl;
+    }
+  }
 }
index 1dc22ba901209bd8793dbba740d9be9e53c23ae8..4f4afdf8513e3ef4cbc6708607e3514bf567ecf3 100644 (file)
 
 #include <stdexcept>
 
+#include "zcpointer.h"
+
 namespace zc {
 namespace internal {
 
 void RaiseUseAfterFree(const char* error) {
-  throw std::logic_error(error);
+  throw zc::UseAfterFreeError(error);
 }
 
 }  // namespace internal
index 31550514b7ce0c1be38f47db0aae7c2ac154ec31..10251c157cd936287596a93a6f4c804e62950f58 100644 (file)
 #include <limits>
 #include <memory>
 #include <forward_list>
+#include <stdexcept>
 
 namespace zc {
 
+class UseAfterFreeError : public std::logic_error {
+ public:
+  using std::logic_error::logic_error;
+};
+
 #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS
 
 template <typename T> class ref;