comparison UnitTestsSources/MultiThreadingTests.cpp @ 9:1fb480a156fd

build unit tests
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Jun 2015 11:07:20 +0200
parents fd402e53d263
children e59bf2554e59
comparison
equal deleted inserted replaced
8:6d59828e2662 9:1fb480a156fd
28 * You should have received a copy of the GNU General Public License 28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. 29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/ 30 **/
31 31
32 32
33 #include "../Core/MultiThreading/ThreadedCommandProcessor.h" 33 #include "../CppClient/ThreadedCommandProcessor.h"
34 #include "../Core/MultiThreading/ArrayFilledByThreads.h" 34 #include "../CppClient/ArrayFilledByThreads.h"
35 35
36 36 #include <gtest/gtest.h>
37 using namespace Orthanc;
38 37
39 namespace 38 namespace
40 { 39 {
41 class DynamicInteger : public ICommand 40 class DynamicInteger : public Orthanc::ICommand
42 { 41 {
43 private: 42 private:
44 int value_; 43 int value_;
45 std::set<int>& target_; 44 std::set<int>& target_;
46 45
62 target_.insert(value_); 61 target_.insert(value_);
63 return true; 62 return true;
64 } 63 }
65 }; 64 };
66 65
67 class MyFiller : public ArrayFilledByThreads::IFiller 66 class MyFiller : public OrthancClient::ArrayFilledByThreads::IFiller
68 { 67 {
69 private: 68 private:
70 int size_; 69 int size_;
71 unsigned int created_; 70 unsigned int created_;
72 std::set<int> set_; 71 std::set<int> set_;
79 virtual size_t GetFillerSize() 78 virtual size_t GetFillerSize()
80 { 79 {
81 return size_; 80 return size_;
82 } 81 }
83 82
84 virtual IDynamicObject* GetFillerItem(size_t index) 83 virtual Orthanc::IDynamicObject* GetFillerItem(size_t index)
85 { 84 {
86 static boost::mutex mutex; 85 static boost::mutex mutex;
87 boost::mutex::scoped_lock lock(mutex); 86 boost::mutex::scoped_lock lock(mutex);
88 created_++; 87 created_++;
89 return new DynamicInteger(index * 2, set_); 88 return new DynamicInteger(index * 2, set_);
104 103
105 104
106 TEST(MultiThreading, ArrayFilledByThreadEmpty) 105 TEST(MultiThreading, ArrayFilledByThreadEmpty)
107 { 106 {
108 MyFiller f(0); 107 MyFiller f(0);
109 ArrayFilledByThreads a(f); 108 OrthancClient::ArrayFilledByThreads a(f);
110 a.SetThreadCount(1); 109 a.SetThreadCount(1);
111 ASSERT_EQ(0, a.GetSize()); 110 ASSERT_EQ(0, a.GetSize());
112 } 111 }
113 112
114 113
115 TEST(MultiThreading, ArrayFilledByThread1) 114 TEST(MultiThreading, ArrayFilledByThread1)
116 { 115 {
117 MyFiller f(100); 116 MyFiller f(100);
118 ArrayFilledByThreads a(f); 117 OrthancClient::ArrayFilledByThreads a(f);
119 a.SetThreadCount(1); 118 a.SetThreadCount(1);
120 ASSERT_EQ(100, a.GetSize()); 119 ASSERT_EQ(100, a.GetSize());
121 for (size_t i = 0; i < a.GetSize(); i++) 120 for (size_t i = 0; i < a.GetSize(); i++)
122 { 121 {
123 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); 122 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue());
126 125
127 126
128 TEST(MultiThreading, ArrayFilledByThread4) 127 TEST(MultiThreading, ArrayFilledByThread4)
129 { 128 {
130 MyFiller f(100); 129 MyFiller f(100);
131 ArrayFilledByThreads a(f); 130 OrthancClient::ArrayFilledByThreads a(f);
132 a.SetThreadCount(4); 131 a.SetThreadCount(4);
133 ASSERT_EQ(100, a.GetSize()); 132 ASSERT_EQ(100, a.GetSize());
134 for (size_t i = 0; i < a.GetSize(); i++) 133 for (size_t i = 0; i < a.GetSize(); i++)
135 { 134 {
136 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); 135 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue());
154 153
155 154
156 155
157 TEST(MultiThreading, CommandProcessor) 156 TEST(MultiThreading, CommandProcessor)
158 { 157 {
159 ThreadedCommandProcessor p(4); 158 OrthancClient::ThreadedCommandProcessor p(4);
160 159
161 std::set<int> s; 160 std::set<int> s;
162 161
163 for (size_t i = 0; i < 100; i++) 162 for (size_t i = 0; i < 100; i++)
164 { 163 {