2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
17 * @file test_semaphore.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of semaphore tests
22 #include <dpl/test/test_runner.h>
23 #include <dpl/lexical_cast.h>
24 #include <dpl/semaphore.h>
25 #include <dpl/thread.h>
26 #include <dpl/log/log.h>
30 RUNNER_TEST_GROUP_INIT(DPL)
32 class SemaphoreThread :
38 std::string m_semaphoreName;
41 SemaphoreThread(int delta,
44 const std::string &semaphoreName) :
48 m_semaphoreName(semaphoreName)
52 virtual int ThreadEntry()
54 DPL::Semaphore semaphore(m_semaphoreName);
56 for (int i = 0; i < m_times; ++i) {
57 // Take scoped semaphore lock
58 DPL::Semaphore::ScopedLock lock(&semaphore);
67 Name: Semaphore_NamedIncrementDecrement
68 Description: Checks if semaphore are working
69 Expected: value should not change after all
71 RUNNER_TEST(Semaphore_NamedIncrementDecrement)
73 std::string semaphoreName =
74 "dpl_test_semaphore_" +
75 DPL::lexical_cast<std::string>(std::time(NULL));
78 SemaphoreThread threadA(-1, 10000, &value, semaphoreName);
79 SemaphoreThread threadB(+1, 10000, &value, semaphoreName);
87 RUNNER_ASSERT_MSG(value == 0, "Final value is: " << value);