Fixed two typos in the update news
[supercollider.git] / testsuite / supernova / server_dsp_thread_test.cpp
blob15effffefd0cf6115ac582b6ee98608203e47cb6
1 #include <boost/test/unit_test.hpp>
3 #include <vector>
5 #include "dsp_thread_queue/dsp_thread.hpp"
6 #include "server/memory_pool.hpp"
8 namespace
11 volatile int gint;
13 struct dummy_runnable
15 dummy_runnable(void):
16 i(0)
19 void operator()(uint dummy)
21 ++i;
23 for (int j = 0; j != 1000; ++j)
25 int l = gint;
26 l += 1;
27 gint = l;
31 int i;
34 dummy_runnable dummy;
37 template <typename Alloc>
38 void run_test_1(void)
40 nova::dsp_threads<dummy_runnable, nova::nop_thread_init, Alloc> t(1);
43 BOOST_AUTO_TEST_CASE( dsp_thread_test_1 )
45 nova::rt_pool.init(1024*1024*128);
46 run_test_1<std::allocator<void*> >();
47 run_test_1<nova::rt_pool_allocator<void*> >();
50 template <typename Alloc>
51 void run_test_2(void)
53 nova::dsp_threads<dummy_runnable, nova::nop_thread_init, Alloc> t(5);
54 t.start_threads();
55 t.terminate_threads();
59 BOOST_AUTO_TEST_CASE( dsp_thread_test_2 )
61 run_test_2<std::allocator<void*> >();
62 run_test_2<nova::rt_pool_allocator<void*> >();
65 template <typename Alloc>
66 void run_test_3(void)
68 nova::dsp_threads<dummy_runnable, nova::nop_thread_init, Alloc> t(2);
69 t.start_threads();
70 t.run();
71 t.terminate_threads();
74 BOOST_AUTO_TEST_CASE( dsp_thread_test_3 )
76 run_test_3<std::allocator<void*> >();
77 run_test_3<nova::rt_pool_allocator<void*> >();
80 template <typename Alloc>
81 void run_test_4(void)
83 typedef typename nova::dsp_thread_queue_item<dummy_runnable, Alloc> dsp_thread_queue_item;
84 typedef typename nova::dsp_thread_queue<dummy_runnable, Alloc> dsp_thread_queue;
86 typedef typename nova::dsp_threads<dummy_runnable, nova::nop_thread_init, Alloc> dsp_threads;
87 typedef std::unique_ptr<dsp_thread_queue> dsp_thread_queue_ptr;
89 dsp_threads t(1);
90 t.start_threads();
92 dsp_thread_queue_ptr q (new dsp_thread_queue(2));
94 dsp_thread_queue_item * item1 = q->allocate_queue_item(dummy, typename dsp_thread_queue_item::successor_list(), 1);
96 typename dsp_thread_queue_item::successor_list sl(1);
97 sl[0] = item1;
99 dsp_thread_queue_item * item2 = q->allocate_queue_item(dummy, sl, 0);
100 q->add_initially_runnable(item2);
102 t.reset_queue(std::move(q));
104 t.run();
106 t.terminate_threads();
108 BOOST_REQUIRE_EQUAL(item1->get_job().i, 1);
109 BOOST_REQUIRE_EQUAL(item2->get_job().i, 1);
114 BOOST_AUTO_TEST_CASE( dsp_thread_test_4 )
116 run_test_4<std::allocator<void*> >();
117 run_test_4<nova::rt_pool_allocator<void*> >();
120 template <typename Alloc>
121 void run_test_5(void)
123 typedef nova::dsp_thread_queue_item<dummy_runnable, Alloc> dsp_thread_queue_item;
124 typedef nova::dsp_thread_queue<dummy_runnable, Alloc> dsp_thread_queue;
125 typedef nova::dsp_threads<dummy_runnable, nova::nop_thread_init, Alloc> dsp_threads;
127 typedef std::unique_ptr<dsp_thread_queue> dsp_thread_queue_ptr;
129 dsp_threads t(2);
130 t.start_threads();
132 dsp_thread_queue_ptr q (new dsp_thread_queue(5));
134 dsp_thread_queue_item * item1 = q->allocate_queue_item(dummy, typename dsp_thread_queue_item::successor_list(), 4);
136 typename dsp_thread_queue_item::successor_list sl(1);
137 sl[0] = item1;
139 dsp_thread_queue_item * item2 = q->allocate_queue_item(dummy, sl, 0);
140 q->add_initially_runnable(item2);
142 dsp_thread_queue_item * item3 = q->allocate_queue_item(dummy, sl, 0);
143 q->add_initially_runnable(item3);
145 dsp_thread_queue_item * item4 = q->allocate_queue_item(dummy, sl, 0);
146 q->add_initially_runnable(item4);
148 dsp_thread_queue_item * item5 = q->allocate_queue_item(dummy, sl, 0);
149 q->add_initially_runnable(item5);
151 t.reset_queue(std::move(q));
153 t.run();
155 t.terminate_threads();
157 BOOST_REQUIRE_EQUAL(item1->get_job().i, 1);
158 BOOST_REQUIRE_EQUAL(item2->get_job().i, 1);
159 BOOST_REQUIRE_EQUAL(item3->get_job().i, 1);
160 BOOST_REQUIRE_EQUAL(item4->get_job().i, 1);
161 BOOST_REQUIRE_EQUAL(item5->get_job().i, 1);
166 BOOST_AUTO_TEST_CASE( dsp_thread_test_5 )
168 run_test_5<std::allocator<void*> >();
169 run_test_5<nova::rt_pool_allocator<void*> >();
172 template <typename Alloc>
173 void run_test_6(void)
175 typedef nova::dsp_thread_queue_item<dummy_runnable, Alloc> dsp_thread_queue_item;
176 typedef nova::dsp_thread_queue<dummy_runnable, Alloc> dsp_thread_queue;
177 typedef nova::dsp_threads<dummy_runnable, nova::nop_thread_init, Alloc> dsp_threads;
179 typedef std::unique_ptr<dsp_thread_queue> dsp_thread_queue_ptr;
181 dsp_threads t(2);
182 t.start_threads();
184 dsp_thread_queue_ptr q (new dsp_thread_queue(20));
186 std::vector<dsp_thread_queue_item*> items;
188 for (int i = 0; i != 20; ++i)
190 items.push_back(q->allocate_queue_item(dummy, typename dsp_thread_queue_item::successor_list(), 0));
191 q->add_initially_runnable(items.back());
194 t.reset_queue(std::move(q));
196 const int iterations = 10000;
198 for (int i = 0; i != iterations; ++i)
200 for (int item = 0; item != 20; ++item)
201 BOOST_REQUIRE_EQUAL(items[item]->get_job().i, i);
202 t.run();
205 t.terminate_threads();
207 for (int i = 0; i != 20; ++i)
208 BOOST_REQUIRE_EQUAL(items[i]->get_job().i, iterations);
211 BOOST_AUTO_TEST_CASE( dsp_thread_test_6 )
213 run_test_6<std::allocator<void*> >();
214 run_test_6<nova::rt_pool_allocator<void*> >();