2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali-test-suite-utils.h>
19 #include <dali/devel-api/threading/semaphore.h>
20 #include <dali/public-api/dali-core.h>
27 int UtcDaliSemaphoreTryAcquire(void)
29 using namespace std::chrono_literals;
30 constexpr auto waitTime{100ms};
32 tet_infoline("Testing Dali::Semaphore try acquire methods");
33 Dali::Semaphore<3> sem(0);
35 DALI_TEST_EQUALS(false, sem.TryAcquire(), TEST_LOCATION);
36 DALI_TEST_EQUALS(false, sem.TryAcquireFor(waitTime), TEST_LOCATION);
37 DALI_TEST_EQUALS(false, sem.TryAcquireUntil(std::chrono::system_clock::now() + waitTime), TEST_LOCATION);
41 DALI_TEST_EQUALS(true, sem.TryAcquire(), TEST_LOCATION);
42 DALI_TEST_EQUALS(true, sem.TryAcquireFor(waitTime), TEST_LOCATION);
43 DALI_TEST_EQUALS(true, sem.TryAcquireUntil(std::chrono::system_clock::now() + waitTime), TEST_LOCATION);
45 DALI_TEST_EQUALS(false, sem.TryAcquire(), TEST_LOCATION);
46 DALI_TEST_EQUALS(false, sem.TryAcquireFor(waitTime), TEST_LOCATION);
47 DALI_TEST_EQUALS(false, sem.TryAcquireUntil(std::chrono::system_clock::now() + waitTime), TEST_LOCATION);
52 int UtcDaliSemaphoreInvalidArguments(void)
54 tet_infoline("Testing Dali::Semaphore invalid arguments");
56 Dali::Semaphore<2> sem(0);
58 DALI_TEST_THROWS(sem.Release(3), std::invalid_argument);
59 DALI_TEST_THROWS(sem.Release(-1), std::invalid_argument);
61 DALI_TEST_THROWS(sem.Release(2), std::invalid_argument);
63 DALI_TEST_THROWS(sem.Release(1), std::invalid_argument);
65 DALI_TEST_THROWS(Dali::Semaphore<1>(2), std::invalid_argument);
66 DALI_TEST_THROWS(Dali::Semaphore<>(-1), std::invalid_argument);
71 int UtcDaliSemaphoreAcquire(void)
73 tet_infoline("Testing Dali::Semaphore multithread acquire");
75 using namespace std::chrono_literals;
77 constexpr std::ptrdiff_t numTasks{2};
79 auto f = [](Dali::Semaphore<numTasks>& sem, bool& flag) {
84 auto flag1{false}, flag2{false};
85 Dali::Semaphore<numTasks> sem(0);
87 auto fut1 = std::async(std::launch::async, f, std::ref(sem), std::ref(flag1));
88 auto fut2 = std::async(std::launch::async, f, std::ref(sem), std::ref(flag2));
90 DALI_TEST_EQUALS(std::future_status::timeout, fut1.wait_for(100ms), TEST_LOCATION);
91 DALI_TEST_EQUALS(std::future_status::timeout, fut2.wait_for(100ms), TEST_LOCATION);
92 DALI_TEST_EQUALS(false, flag1, TEST_LOCATION);
93 DALI_TEST_EQUALS(false, flag2, TEST_LOCATION);
94 sem.Release(numTasks);
96 DALI_TEST_EQUALS(true, flag1, TEST_LOCATION);
98 DALI_TEST_EQUALS(true, flag2, TEST_LOCATION);