tizen 2.3 release
[framework/web/wearable/wrt-commons.git] / tests / core / test_once.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        test_once.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of once tests
21  */
22 #include <functional>
23
24 #include <dpl/test/test_runner.h>
25 #include <dpl/once.h>
26 #include <dpl/waitable_event.h>
27 #include <dpl/waitable_handle.h>
28 #include <dpl/thread.h>
29 #include <dpl/atomic.h>
30 #include <memory>
31
32 RUNNER_TEST_GROUP_INIT(DPL)
33
34 namespace // anonymous
35 {
36 gint g_counter;
37
38 void Delegate()
39 {
40     ++g_counter;
41 }
42 } // namespace anonymous
43
44 RUNNER_TEST(Once_DoubleCall)
45 {
46     g_counter = 0;
47
48     DPL::Once once;
49
50     once.Call(&Delegate);
51     once.Call(&Delegate);
52
53     RUNNER_ASSERT_MSG(g_counter == 1, "Counter value is: " << g_counter);
54 }
55
56 class MyThread :
57     public DPL::Thread
58 {
59   protected:
60     virtual int ThreadEntry()
61     {
62         DPL::WaitForSingleHandle(m_event->GetHandle());
63         m_once->Call(std::bind(&MyThread::Call, this));
64         return 0;
65     }
66
67     void Call()
68     {
69         ++*m_atom;
70     }
71
72   public:
73     MyThread(DPL::WaitableEvent *event, DPL::Once *once, DPL::Atomic *atom) :
74         m_event(event), m_once(once), m_atom(atom)
75     {}
76
77   private:
78     DPL::WaitableEvent *m_event;
79     DPL::Once *m_once;
80     DPL::Atomic *m_atom;
81 };
82
83 /*
84 Name: Once_MultiThreadCall
85 Description: tests once call wrapper for use by multiple threads
86 Expected: function should be called just once from one of running threads
87 */
88 RUNNER_TEST(Once_MultiThreadCall)
89 {
90     const size_t NUM_THREADS = 20;
91     typedef std::shared_ptr<MyThread> ThreadPtr;
92
93     ThreadPtr threads[NUM_THREADS];
94     DPL::WaitableEvent event;
95     DPL::Once once;
96     DPL::Atomic atom;
97
98     for (size_t i = 0; i < NUM_THREADS; ++i) {
99         (threads[i] = ThreadPtr(new MyThread(&event, &once, &atom)))->Run();
100     }
101
102     event.Signal();
103
104     for (size_t i = 0; i < NUM_THREADS; ++i) {
105         threads[i]->Quit();
106     }
107
108     RUNNER_ASSERT_MSG(atom == 1, "Atom value is: " << atom);
109 }