summaryrefslogtreecommitdiff
path: root/test/test_guard.cpp
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2011-06-04 18:01:24 +0000
committerNick Lewycky <nicholas@mxc.ca>2011-06-04 18:01:24 +0000
commit5dfef8de38c88d0bbccdb0b0f86a696f6d53dba8 (patch)
tree5d8dcd31b5d77ddd5b56c716c05f197be0cc5c40 /test/test_guard.cpp
parent05df4f911281af38267ca3e1f9a0bb3a13bf7b51 (diff)
downloadlibcxxabi-5dfef8de38c88d0bbccdb0b0f86a696f6d53dba8.tar.gz
Add more tests for cxa_guard methods. This includes our first two tests using
actual threads! There's no build file for libcxxabi, so I'll tell you that I built it with this: $ g++-4.6 -pthread -std=gnu++0x test_guard.cpp ../src/cxa_guard.o -o test_guard git-svn-id: https://llvm.org/svn/llvm-project/libcxxabi/trunk@132644 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/test_guard.cpp')
-rw-r--r--test/test_guard.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/test/test_guard.cpp b/test/test_guard.cpp
index c69b8e7..d31e577 100644
--- a/test/test_guard.cpp
+++ b/test/test_guard.cpp
@@ -10,7 +10,9 @@
#include "cxxabi.h"
#include <cassert>
+#include <thread>
+// Ensure that we initialize each variable once and only once.
namespace test1 {
static int run_count = 0;
int increment() {
@@ -32,6 +34,8 @@ namespace test1 {
}
}
+// When initialization fails, ensure that we try to initialize it again next
+// time.
namespace test2 {
static int run_count = 0;
int increment() {
@@ -52,8 +56,79 @@ namespace test2 {
}
}
+// Check that we can initialize a second value while initializing a first.
+namespace test3 {
+ int zero() {
+ return 0;
+ }
+
+ int one() {
+ static int b = zero();
+ return 0;
+ }
+
+ void test() {
+ static int a = one();
+ }
+}
+
+// A simple thread test of two threads racing to initialize a variable. This
+// isn't guaranteed to catch any particular threading problems.
+namespace test4 {
+ static int run_count = 0;
+ int increment() {
+ ++run_count;
+ return 0;
+ }
+
+ void helper() {
+ static int a = increment();
+ }
+
+ void test() {
+ std::thread t1(helper), t2(helper);
+ t1.join();
+ t2.join();
+ assert(run_count == 1);
+ }
+}
+
+// Check that we don't re-initialize a static variable even when it's
+// encountered from two different threads.
+namespace test5 {
+ static int run_count = 0;
+ int zero() {
+ ++run_count;
+ return 0;
+ }
+
+ int one() {
+ static int b = zero();
+ return 0;
+ }
+
+ void another_helper() {
+ static int a = one();
+ }
+
+ void helper() {
+ static int a = one();
+ std::thread t(another_helper);
+ t.join();
+ }
+
+ void test() {
+ std::thread t(helper);
+ t.join();
+ assert(run_count == 1);
+ }
+}
+
int main()
{
test1::test();
test2::test();
+ test3::test();
+ test4::test();
+ test5::test();
}