Revert "[Tizen] Appendix log for ttrace + Print keycode and timestamp"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Semaphore.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali-test-suite-utils.h>
19 #include <dali/devel-api/threading/semaphore.h>
20 #include <dali/public-api/dali-core.h>
21 #include <algorithm>
22 #include <chrono>
23 #include <future>
24 #include <stdexcept>
25 #include <thread>
26
27 int UtcDaliSemaphoreTryAcquire(void)
28 {
29   using namespace std::chrono_literals;
30   constexpr auto waitTime{100ms};
31
32   tet_infoline("Testing Dali::Semaphore try acquire methods");
33   Dali::Semaphore<3> sem(0);
34
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);
38
39   sem.Release(3);
40
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);
44
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);
48
49   END_TEST;
50 }
51
52 int UtcDaliSemaphoreInvalidArguments(void)
53 {
54   tet_infoline("Testing Dali::Semaphore invalid arguments");
55
56   Dali::Semaphore<2> sem(0);
57
58   DALI_TEST_THROWS(sem.Release(3), std::invalid_argument);
59   DALI_TEST_THROWS(sem.Release(-1), std::invalid_argument);
60   sem.Release(1);
61   DALI_TEST_THROWS(sem.Release(2), std::invalid_argument);
62   sem.Release(1);
63   DALI_TEST_THROWS(sem.Release(1), std::invalid_argument);
64
65   DALI_TEST_THROWS(Dali::Semaphore<1>(2), std::invalid_argument);
66   DALI_TEST_THROWS(Dali::Semaphore<>(-1), std::invalid_argument);
67
68   END_TEST;
69 }
70
71 int UtcDaliSemaphoreAcquire(void)
72 {
73   tet_infoline("Testing Dali::Semaphore multithread acquire");
74
75   using namespace std::chrono_literals;
76
77   constexpr std::ptrdiff_t numTasks{2};
78
79   auto f = [](Dali::Semaphore<numTasks>& sem, bool& flag) {
80     sem.Acquire();
81     flag = true;
82   };
83
84   auto                      flag1{false}, flag2{false};
85   Dali::Semaphore<numTasks> sem(0);
86
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));
89
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);
95   fut1.wait();
96   DALI_TEST_EQUALS(true, flag1, TEST_LOCATION);
97   fut2.wait();
98   DALI_TEST_EQUALS(true, flag2, TEST_LOCATION);
99
100   END_TEST;
101 }