diff options
| author | Pierre Bourdon <delroth@gmail.com> | 2014-04-14 00:26:23 +0200 |
|---|---|---|
| committer | Pierre Bourdon <delroth@gmail.com> | 2014-04-14 10:54:07 +0200 |
| commit | f9fb39d383bb462293ea1104e29c5e6651e2d0e3 (patch) | |
| tree | ac797b01d1aa85bbf4a888fe16112b0458958b6e /Source/UnitTests/Common/FlagTest.cpp | |
| parent | 1b9addd594536a76b42379306ce048b0d119973e (diff) | |
Common: Add a 'Flag' class that is used to encapsulate a boolean flag manipulated from several threads
Diffstat (limited to 'Source/UnitTests/Common/FlagTest.cpp')
| -rw-r--r-- | Source/UnitTests/Common/FlagTest.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Source/UnitTests/Common/FlagTest.cpp b/Source/UnitTests/Common/FlagTest.cpp new file mode 100644 index 0000000000..ef73d27728 --- /dev/null +++ b/Source/UnitTests/Common/FlagTest.cpp @@ -0,0 +1,60 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include <gtest/gtest.h> +#include <thread> + +#include "Common/Flag.h" + +using Common::Flag; + +TEST(Flag, Simple) +{ + Flag f; + EXPECT_FALSE(f.IsSet()); + + f.Set(); + EXPECT_TRUE(f.IsSet()); + + f.Clear(); + EXPECT_FALSE(f.IsSet()); + + f.Set(false); + EXPECT_FALSE(f.IsSet()); + + Flag f2(true); + EXPECT_TRUE(f2.IsSet()); +} + +TEST(Flag, MultiThreaded) +{ + Flag f; + int count = 0; + const int ITERATIONS_COUNT = 100000; + + auto setter = [&f]() { + for (int i = 0; i < ITERATIONS_COUNT; ++i) + { + while (f.IsSet()); + f.Set(); + } + }; + + auto clearer = [&f, &count]() { + for (int i = 0; i < ITERATIONS_COUNT; ++i) + { + while (!f.IsSet()); + count++; + f.Clear(); + } + }; + + std::thread setter_thread(setter); + std::thread clearer_thread(clearer); + + setter_thread.join(); + clearer_thread.join(); + + EXPECT_EQ(ITERATIONS_COUNT, count); +} |
