1 // Copyright 2020 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "System/Synchronization.hpp"
16
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include <thread>
21
TEST(EventCounter,ConstructUnsignalled)22 TEST(EventCounter, ConstructUnsignalled)
23 {
24 sw::CountedEvent ev;
25 ASSERT_FALSE(ev.signalled());
26 }
27
TEST(EventCounter,ConstructSignalled)28 TEST(EventCounter, ConstructSignalled)
29 {
30 sw::CountedEvent ev(true);
31 ASSERT_TRUE(ev.signalled());
32 }
33
TEST(EventCounter,Reset)34 TEST(EventCounter, Reset)
35 {
36 sw::CountedEvent ev(true);
37 ev.reset();
38 ASSERT_FALSE(ev.signalled());
39 }
40
TEST(EventCounter,AddUnsignalled)41 TEST(EventCounter, AddUnsignalled)
42 {
43 sw::CountedEvent ev;
44 ev.add();
45 ASSERT_FALSE(ev.signalled());
46 }
47
TEST(EventCounter,AddDoneUnsignalled)48 TEST(EventCounter, AddDoneUnsignalled)
49 {
50 sw::CountedEvent ev;
51 ev.add();
52 ev.done();
53 ASSERT_TRUE(ev.signalled());
54 }
55
TEST(EventCounter,Wait)56 TEST(EventCounter, Wait)
57 {
58 sw::CountedEvent ev;
59 bool b = false;
60
61 ev.add();
62 auto t = std::thread([=, &b] {
63 b = true;
64 ev.done();
65 });
66
67 ev.wait();
68 ASSERT_TRUE(b);
69 t.join();
70 }
71
TEST(EventCounter,WaitNoTimeout)72 TEST(EventCounter, WaitNoTimeout)
73 {
74 sw::CountedEvent ev;
75 bool b = false;
76
77 ev.add();
78 auto t = std::thread([=, &b] {
79 b = true;
80 ev.done();
81 });
82
83 ASSERT_TRUE(ev.wait(std::chrono::system_clock::now() + std::chrono::seconds(10)));
84 ASSERT_TRUE(b);
85 t.join();
86 }
87
TEST(EventCounter,WaitTimeout)88 TEST(EventCounter, WaitTimeout)
89 {
90 sw::CountedEvent ev;
91 ev.add();
92 ASSERT_FALSE(ev.wait(std::chrono::system_clock::now() + std::chrono::milliseconds(1)));
93 }
94