Fix last commit
[carla.git] / source / tests.old / RtLinkedList.cpp
blob8fe8edf105877841798f006d7d2abbce130b3d2e
1 /*
2 * Carla Tests
3 * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "RtLinkedList.hpp"
20 #include "CarlaString.hpp"
21 #include "CarlaMutex.hpp"
23 const unsigned short MIN_RT_EVENTS = 5;
24 const unsigned short MAX_RT_EVENTS = 12;
26 struct MyData {
27 char str[234];
28 int id;
31 struct PostRtEvents {
32 CarlaMutex mutex;
33 RtLinkedList<MyData>::Pool dataPool;
34 RtLinkedList<MyData> data;
35 RtLinkedList<MyData> dataPendingRT;
37 PostRtEvents() noexcept
38 : dataPool(MIN_RT_EVENTS, MAX_RT_EVENTS),
39 data(dataPool),
40 dataPendingRT(dataPool) {}
42 ~PostRtEvents() noexcept
44 clear();
47 void appendRT(const MyData& event) noexcept
49 dataPendingRT.append(event);
52 void clear() noexcept
54 mutex.lock();
55 data.clear();
56 dataPendingRT.clear();
57 mutex.unlock();
60 void trySplice() noexcept
62 if (mutex.tryLock())
64 dataPendingRT.moveTo(data, true);
65 mutex.unlock();
69 } postRtEvents;
71 void run5Tests();
72 void run5Tests()
74 ushort k = 0;
75 MyData allMyData[MAX_RT_EVENTS];
77 // Make a safe copy of events while clearing them
78 postRtEvents.mutex.lock();
80 while (! postRtEvents.data.isEmpty())
82 static MyData kFallback = { { '\0' }, 0 };
83 const MyData& my(postRtEvents.data.getFirst(kFallback, true));
84 allMyData[k++] = my;
87 postRtEvents.mutex.unlock();
89 carla_stdout("Post-Rt Event Count: %i", k);
90 assert(k == 5);
92 // data should be empty now
93 assert(postRtEvents.data.count() == 0);
94 assert(postRtEvents.data.isEmpty());
95 assert(postRtEvents.dataPendingRT.count() == 0);
96 assert(postRtEvents.dataPendingRT.isEmpty());
98 // Handle events now
99 for (ushort i=0; i < k; ++i)
101 const MyData& my(allMyData[i]);
103 carla_stdout("Got data: %i %s", my.id, my.str);
106 // append events past minimum size
107 MyData dummyData = { { '\0' }, 0 };
108 // 5 initial go ok
109 assert(postRtEvents.dataPendingRT.append(dummyData));
110 assert(postRtEvents.dataPendingRT.append(dummyData));
111 assert(postRtEvents.dataPendingRT.append(dummyData));
112 assert(postRtEvents.dataPendingRT.append(dummyData));
113 assert(postRtEvents.dataPendingRT.append(dummyData));
114 // afterwards it fails
115 assert(! postRtEvents.dataPendingRT.append(dummyData));
116 assert(! postRtEvents.dataPendingRT.append(dummyData));
117 carla_stdout("here %i", __LINE__);
119 // adding sleepy works
120 assert(postRtEvents.dataPendingRT.append_sleepy(dummyData));
121 carla_stdout("here %i", __LINE__);
123 // now atomic works too, size was increased
124 assert(postRtEvents.dataPendingRT.append(dummyData));
125 assert(postRtEvents.dataPendingRT.append(dummyData));
126 assert(postRtEvents.dataPendingRT.append(dummyData));
127 assert(postRtEvents.dataPendingRT.append(dummyData));
128 carla_stdout("here %i", __LINE__);
130 // fails here now
131 assert(! postRtEvents.dataPendingRT.append(dummyData));
132 carla_stdout("here %i", __LINE__);
134 // adding sleepy still works
135 assert(postRtEvents.dataPendingRT.append_sleepy(dummyData));
136 carla_stdout("here %i", __LINE__);
138 // now atomic works for 1 more
139 assert(postRtEvents.dataPendingRT.append(dummyData));
140 assert(! postRtEvents.dataPendingRT.append(dummyData));
141 carla_stdout("here %i", __LINE__);
143 // and adding sleepy no longer works
144 assert(! postRtEvents.dataPendingRT.append_sleepy(dummyData));
145 carla_stdout("here %i", __LINE__);
147 // cleanup
148 postRtEvents.trySplice();
149 postRtEvents.clear();
152 int main()
154 MyData m1; m1.id = 1; std::strcpy(m1.str, "1");
155 MyData m2; m2.id = 2; std::strcpy(m2.str, "2");
156 MyData m3; m3.id = 3; std::strcpy(m3.str, "3");
157 MyData m4; m4.id = 4; std::strcpy(m4.str, "4");
158 MyData m5; m5.id = 5; std::strcpy(m5.str, "5");
160 // start
161 assert(postRtEvents.data.count() == 0);
162 assert(postRtEvents.data.isEmpty());
163 assert(postRtEvents.dataPendingRT.count() == 0);
164 assert(postRtEvents.dataPendingRT.isEmpty());
166 // single append
167 postRtEvents.appendRT(m1);
168 assert(postRtEvents.data.count() == 0);
169 assert(postRtEvents.dataPendingRT.count() == 1);
170 postRtEvents.trySplice();
171 assert(postRtEvents.data.count() == 1);
172 assert(postRtEvents.dataPendingRT.count() == 0);
174 // +3 appends
175 postRtEvents.appendRT(m2);
176 postRtEvents.appendRT(m4);
177 postRtEvents.appendRT(m3);
178 assert(postRtEvents.data.count() == 1);
179 assert(postRtEvents.dataPendingRT.count() == 3);
180 postRtEvents.trySplice();
181 assert(postRtEvents.data.count() == 4);
182 assert(postRtEvents.dataPendingRT.count() == 0);
184 for (RtLinkedList<MyData>::Itenerator it = postRtEvents.data.begin2(); it.valid(); it.next())
186 static const MyData kFallback = { { '\0' }, 0 };
187 const MyData& my(it.getValue(kFallback));
189 carla_stdout("FOR DATA!!!: %i %s", my.id, my.str);
191 if (my.id == 2)
193 // +1 append
194 postRtEvents.dataPendingRT.insertAt(m5, it);
195 assert(postRtEvents.data.count() == 4);
196 assert(postRtEvents.dataPendingRT.count() == 1);
200 #if 0
201 for (const MyData& my : postRtEvents.data)
202 carla_stdout("FOR DATA!!! NEW AUTO Itenerator!!!: %i %s", my.id, my.str);
203 #endif
205 postRtEvents.trySplice();
206 assert(postRtEvents.data.count() == 5);
207 assert(postRtEvents.dataPendingRT.count() == 0);
209 run5Tests();
211 // reset
212 postRtEvents.clear();
213 assert(postRtEvents.data.count() == 0);
214 assert(postRtEvents.data.isEmpty());
215 assert(postRtEvents.dataPendingRT.count() == 0);
216 assert(postRtEvents.dataPendingRT.isEmpty());
218 // test non-rt
219 const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
220 const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
222 LinkedList<uint32_t> evIns, evOuts;
223 evIns.append(CARLA_EVENT_DATA_ATOM);
224 evOuts.append(CARLA_EVENT_DATA_ATOM);
225 evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
227 if (evIns.count() > 0)
229 for (size_t j=0, count=evIns.count(); j < count; ++j)
231 const uint32_t& type(evIns.getAt(j, 0));
233 if (type == CARLA_EVENT_DATA_ATOM)
234 pass();
235 else if (type == CARLA_EVENT_DATA_MIDI_LL)
236 pass();
240 evIns.clear();
241 evOuts.clear();
243 return 0;