Ben Clayton | 95813a8 | 2019-08-08 11:47:27 +0100 | [diff] [blame] | 1 | // Copyright 2019 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 "Yarn_test.hpp" |
| 16 | |
| 17 | #include "Yarn/WaitGroup.hpp" |
| 18 | |
| 19 | TEST(WithoutBoundScheduler, WaitGroupDone) |
| 20 | { |
| 21 | yarn::WaitGroup wg(2); // Should not require a scheduler. |
| 22 | wg.done(); |
| 23 | wg.done(); |
| 24 | } |
| 25 | |
| 26 | #if YARN_DEBUG_ENABLED |
| 27 | TEST(WithoutBoundScheduler, WaitGroupDoneTooMany) |
| 28 | { |
| 29 | yarn::WaitGroup wg(2); // Should not require a scheduler. |
| 30 | wg.done(); |
| 31 | wg.done(); |
| 32 | EXPECT_DEATH(wg.done(), "done\\(\\) called too many times"); |
| 33 | } |
| 34 | #endif // YARN_DEBUG_ENABLED |
| 35 | |
| 36 | TEST_P(WithBoundScheduler, WaitGroup_OneTask) |
| 37 | { |
| 38 | yarn::WaitGroup wg(1); |
| 39 | std::atomic<int> counter = {0}; |
| 40 | yarn::schedule([&counter, wg] { |
| 41 | counter++; |
| 42 | wg.done(); |
| 43 | }); |
| 44 | wg.wait(); |
| 45 | ASSERT_EQ(counter.load(), 1); |
| 46 | } |
| 47 | |
| 48 | TEST_P(WithBoundScheduler, WaitGroup_10Tasks) |
| 49 | { |
| 50 | yarn::WaitGroup wg(10); |
| 51 | std::atomic<int> counter = {0}; |
| 52 | for (int i = 0; i < 10; i++) |
| 53 | { |
| 54 | yarn::schedule([&counter, wg] { |
| 55 | counter++; |
| 56 | wg.done(); |
| 57 | }); |
| 58 | } |
| 59 | wg.wait(); |
| 60 | ASSERT_EQ(counter.load(), 10); |
| 61 | } |