tizen 2.4 release
[framework/web/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
30 #include <atomic>
31 #include <memory>
32
33 RUNNER_TEST_GROUP_INIT(DPL)
34
35 namespace // anonymous
36 {
37 int g_counter;
38
39 void Delegate()
40 {
41     ++g_counter;
42 }
43 } // namespace anonymous
44
45 RUNNER_TEST(Once_DoubleCall)
46 {
47     g_counter = 0;
48
49     DPL::Once once;
50
51     once.Call(&Delegate);
52     once.Call(&Delegate);
53
54     RUNNER_ASSERT_MSG(g_counter == 1, "Counter value is: " << g_counter);
55 }
56
57 class MyThread :
58     public DPL::Thread
59 {
60   protected:
61     virtual int ThreadEntry()
62     {
63         DPL::WaitForSingleHandle(m_event->GetHandle());
64         m_once->Call(std::bind(&MyThread::Call, this));
65         return 0;
66     }
67
68     void Call()
69     {
70         ++*m_atom;
71     }
72
73   public:
74     MyThread(DPL::WaitableEvent *event, DPL::Once *once, std::atomic<int> *atom) :
75         m_event(event), m_once(once), m_atom(atom)
76     {}
77
78   private:
79     DPL::WaitableEvent *m_event;
80     DPL::Once *m_once;
81     std::atomic<int> *m_atom;
82 };
83
84 /*
85 Name: Once_MultiThreadCall
86 Description: tests once call wrapper for use by multiple threads
87 Expected: function should be called just once from one of running threads
88 */
89 RUNNER_TEST(Once_MultiThreadCall)
90 {
91     const size_t NUM_THREADS = 20;
92     typedef std::shared_ptr<MyThread> ThreadPtr;
93
94     ThreadPtr threads[NUM_THREADS];
95     DPL::WaitableEvent event;
96     DPL::Once once;
97     std::atomic<int> atom(0);
98
99     for (size_t i = 0; i < NUM_THREADS; ++i) {
100         (threads[i] = ThreadPtr(new MyThread(&event, &once, &atom)))->Run();
101     }
102
103     event.Signal();
104
105     for (size_t i = 0; i < NUM_THREADS; ++i) {
106         threads[i]->Quit();
107     }
108
109     RUNNER_ASSERT_MSG(atom == 1, "Atom value is: " << atom);
110 }