diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-11-26 20:18:14 +0200 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-12-01 11:49:02 +0200 |
commit | 8d3fd199b26c770c262141e736ccf4c7830c22f6 (patch) | |
tree | f43a8b4c8bd8406900b7fa91fde03781cd37574d /test/miscellaneous | |
parent | 62990f0a21eab309f03db0d92119a9a299ae475d (diff) | |
download | qtlocation-mapboxgl-8d3fd199b26c770c262141e736ccf4c7830c22f6.tar.gz |
[tests] Add utests for ThreadLocal
Diffstat (limited to 'test/miscellaneous')
-rw-r--r-- | test/miscellaneous/thread_local.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/test/miscellaneous/thread_local.cpp b/test/miscellaneous/thread_local.cpp new file mode 100644 index 0000000000..6f9523f6ef --- /dev/null +++ b/test/miscellaneous/thread_local.cpp @@ -0,0 +1,95 @@ +#include <mbgl/util/run_loop.hpp> +#include <mbgl/util/thread.hpp> +#include <mbgl/util/thread_local.hpp> + +#include "../fixtures/util.hpp" + +using namespace mbgl::util; + +namespace { + +class TestThread { +public: + TestThread(int *number_) { + number.set(number_); + } + + ~TestThread() { + number.set(nullptr); + } + + int getNumber() { + return *number.get(); + } + +private: + static ThreadLocal<int> number; +}; + +ThreadLocal<int> TestThread::number; + +} + +TEST(ThreadLocalStorage, Basic) { + RunLoop loop; + + int number1 = 1; + int number2 = 2; + int number3 = 3; + + ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular}; + + Thread<TestThread> thread1(context, &number1); + Thread<TestThread> thread2(context, &number2); + Thread<TestThread> thread3(context, &number3); + + EXPECT_EQ(number1, thread1.invokeSync<int>(&TestThread::getNumber)); + EXPECT_EQ(number2, thread2.invokeSync<int>(&TestThread::getNumber)); + EXPECT_EQ(number3, thread3.invokeSync<int>(&TestThread::getNumber)); +} + +TEST(ThreadLocalStorage, NotSetReturnsNull) { + static ThreadLocal<int> number; + + EXPECT_EQ(nullptr, number.get()); +} + +namespace { + +struct DtorCounter { + ~DtorCounter() { ++(*value); } + unsigned *value; +}; + +class TestThreadReclaim { +public: + TestThreadReclaim(DtorCounter* counter_) { + counter.set(counter_); + } + +private: + static ThreadLocal<DtorCounter> counter; +}; + +ThreadLocal<DtorCounter> TestThreadReclaim::counter; + +} + +TEST(ThreadLocalStorage, AutoReclaim) { + RunLoop loop; + + unsigned counter = 0; + + DtorCounter* dtorCounter1 = new DtorCounter{ &counter }; + DtorCounter* dtorCounter2 = new DtorCounter{ &counter }; + + ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular}; + + auto thread1 = std::make_unique<Thread<TestThreadReclaim>>(context, dtorCounter1); + auto thread2 = std::make_unique<Thread<TestThreadReclaim>>(context, dtorCounter2); + + thread1.reset(); + thread2.reset(); + + EXPECT_EQ(counter, 2); +} |