1// Copyright 2019 The Marl Authors.
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// https://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 "marl_test.h"
16
17#include "marl/waitgroup.h"
18
19TEST_F(WithoutBoundScheduler, WaitGroupDone) {
20 marl::WaitGroup wg(2); // Should not require a scheduler.
21 wg.done();
22 wg.done();
23}
24
25#if MARL_DEBUG_ENABLED && GTEST_HAS_DEATH_TEST
26TEST_F(WithoutBoundScheduler, WaitGroupDoneTooMany) {
27 marl::WaitGroup wg(2); // Should not require a scheduler.
28 wg.done();
29 wg.done();
30 EXPECT_DEATH(wg.done(), "done\\(\\) called too many times");
31}
32#endif // MARL_DEBUG_ENABLED && GTEST_HAS_DEATH_TEST
33
34TEST_P(WithBoundScheduler, WaitGroup_OneTask) {
35 marl::WaitGroup wg(1);
36 std::atomic<int> counter = {0};
37 marl::schedule([&counter, wg] {
38 counter++;
39 wg.done();
40 });
41 wg.wait();
42 ASSERT_EQ(counter.load(), 1);
43}
44
45TEST_P(WithBoundScheduler, WaitGroup_10Tasks) {
46 marl::WaitGroup wg(10);
47 std::atomic<int> counter = {0};
48 for (int i = 0; i < 10; i++) {
49 marl::schedule([&counter, wg] {
50 counter++;
51 wg.done();
52 });
53 }
54 wg.wait();
55 ASSERT_EQ(counter.load(), 10);
56}
57