5 #include "flutter/testing/testing.h"
22 TEST(ThreadTest, CanCreateMutex) {
31 TEST(ThreadTest, CanCreateMutexLock) {
39 TEST(ThreadTest, CanCreateRWMutex) {
49 FML_ALLOW_UNUSED_LOCAL(b);
53 TEST(ThreadTest, CanCreateRWMutexLock) {
66 FML_ALLOW_UNUSED_LOCAL(b);
72 TEST(StringsTest, CanSPrintF) {
73 ASSERT_EQ(
SPrintF(
"%sx%d",
"Hello", 12),
"Hellox12");
75 ASSERT_EQ(
SPrintF(
"Hello"),
"Hello");
76 ASSERT_EQ(
SPrintF(
"%sx%.2f",
"Hello", 12.122222),
"Hellox12.12");
85 TEST(ConditionVariableTest, WaitUntil) {
88 for (
size_t i = 0; i < 2; ++i) {
92 std::chrono::high_resolution_clock::now() +
93 std::chrono::milliseconds{10},
100 ASSERT_FALSE(result);
105 ASSERT_EQ(test.rando_ivar, 12u);
108 TEST(ConditionVariableTest, WaitFor) {
111 for (
size_t i = 0; i < 2; ++i) {
114 test.
mutex, std::chrono::milliseconds{10},
116 test.rando_ivar = 12;
121 ASSERT_FALSE(result);
126 ASSERT_EQ(test.rando_ivar, 12u);
129 TEST(ConditionVariableTest, WaitForever) {
132 for (
size_t i = 0; i < 2; ++i) {
135 test.rando_ivar = 12;
143 ASSERT_EQ(test.rando_ivar, 12u);
146 TEST(ConditionVariableTest, TestsCriticalSectionAfterWaitForUntil) {
147 std::vector<std::thread> threads;
148 const auto kThreadCount = 10u;
154 std::condition_variable start_cv;
155 std::mutex start_mtx;
157 auto start_predicate = [&start]() {
return start; };
158 auto thread_main = [&]() {
160 std::unique_lock start_lock(start_mtx);
161 start_cv.wait(start_lock, start_predicate);
165 cv.
WaitFor(mtx, std::chrono::milliseconds{0u}, []() {
return true; });
167 std::this_thread::sleep_for(std::chrono::milliseconds{100u});
172 for (
size_t i = 0; i < kThreadCount; i++) {
173 threads.emplace_back(thread_main);
178 std::scoped_lock start_lock(start_mtx);
181 start_cv.notify_all();
184 ASSERT_EQ(threads.size(), kThreadCount);
185 for (
size_t i = 0; i < kThreadCount; i++) {
188 ASSERT_EQ(sum, kThreadCount);
191 TEST(ConditionVariableTest, TestsCriticalSectionAfterWait) {
192 std::vector<std::thread> threads;
193 const auto kThreadCount = 10u;
199 std::condition_variable start_cv;
200 std::mutex start_mtx;
202 auto start_predicate = [&start]() {
return start; };
203 auto thread_main = [&]() {
205 std::unique_lock start_lock(start_mtx);
206 start_cv.wait(start_lock, start_predicate);
210 cv.
Wait(mtx, []() {
return true; });
212 std::this_thread::sleep_for(std::chrono::milliseconds{100u});
217 for (
size_t i = 0; i < kThreadCount; i++) {
218 threads.emplace_back(thread_main);
223 std::scoped_lock start_lock(start_mtx);
226 start_cv.notify_all();
229 ASSERT_EQ(threads.size(), kThreadCount);
230 for (
size_t i = 0; i < kThreadCount; i++) {
233 ASSERT_EQ(sum, kThreadCount);