1#include <TimedEventQueue.h>
2#include <stdio.h>
3
4#define DEBUG 1
5#include <Debug.h>
6
7BTimedEventQueue::queue_action DoForEachHook(media_timed_event *event, void *context);
8void DumpEvent(const media_timed_event & e);
9void DumpEvent(const media_timed_event * e);
10void InsertRemoveTest();
11void DoForEachTest();
12void MatchTest();
13void FlushTest();
14
15void DumpEvent(const media_timed_event & e)
16{
17	DumpEvent(&e);
18}
19
20void DumpEvent(const media_timed_event * e)
21{
22	if (!e) {
23		printf("NULL\n");
24		return;
25	}
26	printf("time = 0x%x, type = ",int(e->event_time));
27	switch (e->type) {
28		case BTimedEventQueue::B_NO_EVENT: printf("B_NO_EVENT\n"); break;
29		case BTimedEventQueue::B_ANY_EVENT: printf("B_ANY_EVENT\n"); break;
30		case BTimedEventQueue::B_START: printf("B_START\n"); break;
31		case BTimedEventQueue::B_STOP: printf("B_STOP\n"); break;
32		case BTimedEventQueue::B_SEEK: printf("B_SEEK\n"); break;
33		case BTimedEventQueue::B_WARP: printf("B_WARP\n"); break;
34		case BTimedEventQueue::B_TIMER: printf("B_TIMER\n"); break;
35		case BTimedEventQueue::B_HANDLE_BUFFER: printf("B_HANDLE_BUFFER\n"); break;
36		case BTimedEventQueue::B_DATA_STATUS: printf("B_DATA_STATUS\n"); break;
37		case BTimedEventQueue::B_HARDWARE: printf("B_HARDWARE\n"); break;
38		case BTimedEventQueue::B_PARAMETER: printf("B_PARAMETER\n"); break;
39		default: printf("0x%x\n",int(e->type));
40	}
41}
42
43void InsertRemoveTest()
44{
45	BTimedEventQueue *q =new BTimedEventQueue;
46	q->AddEvent(media_timed_event(0x1007,BTimedEventQueue::B_START));//
47	q->AddEvent(media_timed_event(0x1005,BTimedEventQueue::B_START));//
48	q->AddEvent(media_timed_event(0x9999,BTimedEventQueue::B_STOP));//
49	q->AddEvent(media_timed_event(0x1006,BTimedEventQueue::B_START));//
50	q->AddEvent(media_timed_event(0x1002,BTimedEventQueue::B_START));//
51	q->AddEvent(media_timed_event(0x1011,BTimedEventQueue::B_START));//
52	q->AddEvent(media_timed_event(0x1000,BTimedEventQueue::B_START));//
53	q->AddEvent(media_timed_event(0x0777,BTimedEventQueue::B_START));//
54	q->AddEvent(media_timed_event(0x1001,BTimedEventQueue::B_START));//
55	q->AddEvent(media_timed_event(0x1000,BTimedEventQueue::B_STOP));//
56	q->AddEvent(media_timed_event(0x1003,BTimedEventQueue::B_START));//
57	q->AddEvent(media_timed_event(0x1000,BTimedEventQueue::B_SEEK));//
58	ASSERT(q->EventCount() == 12);
59	ASSERT(q->HasEvents() == true);
60
61	media_timed_event e1(0x1003,BTimedEventQueue::B_START);
62	q->RemoveEvent(&e1);
63	ASSERT(q->EventCount() == 11);
64	ASSERT(q->HasEvents() == true);
65
66	media_timed_event e2(0x1007,BTimedEventQueue::B_START);
67	q->RemoveEvent(&e2);
68	ASSERT(q->EventCount() == 10);
69	ASSERT(q->HasEvents() == true);
70
71	media_timed_event e3(0x1000,BTimedEventQueue::B_STOP);
72	q->RemoveEvent(&e3);
73	ASSERT(q->EventCount() == 9);
74	ASSERT(q->HasEvents() == true);
75
76	media_timed_event e4(0x1000,BTimedEventQueue::B_SEEK);
77	q->RemoveEvent(&e4);
78	ASSERT(q->EventCount() == 8);
79	ASSERT(q->HasEvents() == true);
80
81	//remove non existing element (time)
82	media_timed_event e5(0x1111,BTimedEventQueue::B_STOP);
83	q->RemoveEvent(&e5);
84	ASSERT(q->EventCount() == 8);
85	ASSERT(q->HasEvents() == true);
86
87	//remove non existing element (type)
88	media_timed_event e6(0x1011,BTimedEventQueue::B_STOP);
89	q->RemoveEvent(&e6);
90	ASSERT(q->EventCount() == 8);
91	ASSERT(q->HasEvents() == true);
92
93	media_timed_event e7(0x1000,BTimedEventQueue::B_START);
94	q->RemoveEvent(&e7);
95	ASSERT(q->EventCount() == 7);
96	ASSERT(q->HasEvents() == true);
97
98	media_timed_event e8(0x1011,BTimedEventQueue::B_START);
99	q->RemoveEvent(&e8);
100	ASSERT(q->EventCount() == 6);
101	ASSERT(q->HasEvents() == true);
102
103	media_timed_event e9(0x1002,BTimedEventQueue::B_START);
104	q->RemoveEvent(&e9);
105	ASSERT(q->EventCount() == 5);
106	ASSERT(q->HasEvents() == true);
107
108	media_timed_event e10(0x0777,BTimedEventQueue::B_START);
109	q->RemoveEvent(&e10);
110	ASSERT(q->EventCount() == 4);
111	ASSERT(q->HasEvents() == true);
112
113	media_timed_event e11(0x9999,BTimedEventQueue::B_STOP);
114	q->RemoveEvent(&e11);
115	ASSERT(q->EventCount() == 3);
116	ASSERT(q->HasEvents() == true);
117
118	media_timed_event e12(0x1006,BTimedEventQueue::B_START);
119	q->RemoveEvent(&e12);
120	ASSERT(q->EventCount() == 2);
121	ASSERT(q->HasEvents() == true);
122
123	media_timed_event e13(0x1001,BTimedEventQueue::B_START);
124	q->RemoveEvent(&e13);
125	ASSERT(q->EventCount() == 1);
126	ASSERT(q->HasEvents() == true);
127
128	media_timed_event e14(0x1005,BTimedEventQueue::B_START);
129	q->RemoveEvent(&e14);
130	ASSERT(q->EventCount() == 0);
131	ASSERT(q->HasEvents() == false);
132
133	delete q;
134}
135
136media_timed_event DoForEachEvent;
137int DoForEachCount;
138
139BTimedEventQueue::queue_action
140DoForEachHook(media_timed_event *event, void *context)
141{
142	DoForEachEvent = *event;
143	DoForEachCount++;
144	printf("Callback, event_time = %x\n",int(event->event_time));
145	return BTimedEventQueue::B_NO_ACTION;
146}
147
148void DoForEachTest()
149{
150	BTimedEventQueue *q =new BTimedEventQueue;
151	ASSERT(q->EventCount() == 0);
152	ASSERT(q->HasEvents() == false);
153
154	q->AddEvent(media_timed_event(0x1000,BTimedEventQueue::B_SEEK));
155	q->AddEvent(media_timed_event(0x1001,BTimedEventQueue::B_START));
156	q->AddEvent(media_timed_event(0x1002,BTimedEventQueue::B_START));
157	q->AddEvent(media_timed_event(0x1003,BTimedEventQueue::B_START));
158	q->AddEvent(media_timed_event(0x1010,BTimedEventQueue::B_START));
159	q->AddEvent(media_timed_event(0x1011,BTimedEventQueue::B_START));
160	q->AddEvent(media_timed_event(0x1012,BTimedEventQueue::B_START));
161	q->AddEvent(media_timed_event(0x1004,BTimedEventQueue::B_START));
162	q->AddEvent(media_timed_event(0x1005,BTimedEventQueue::B_START));
163	q->AddEvent(media_timed_event(0x1006,BTimedEventQueue::B_STOP));
164	q->AddEvent(media_timed_event(0x1007,BTimedEventQueue::B_START));
165	q->AddEvent(media_timed_event(0x1008,BTimedEventQueue::B_START));
166	q->AddEvent(media_timed_event(0x1009,BTimedEventQueue::B_START));
167	q->AddEvent(media_timed_event(0x1013,BTimedEventQueue::B_START));
168	q->AddEvent(media_timed_event(0x1013,BTimedEventQueue::B_START));
169	q->AddEvent(media_timed_event(0x1013,BTimedEventQueue::B_SEEK));
170	ASSERT(q->EventCount() == 16);
171	ASSERT(q->HasEvents() == true);
172
173
174	printf("\n expected: 0x1000\n");
175	DoForEachCount = 0;
176	q->DoForEach(DoForEachHook,(void*)1234,0x1000,BTimedEventQueue::B_AT_TIME);
177	ASSERT(DoForEachEvent == media_timed_event(0x1000,BTimedEventQueue::B_SEEK));
178	ASSERT(DoForEachCount == 1);
179
180
181	printf("\n expected: 0x1006\n");
182	DoForEachCount = 0;
183	q->DoForEach(DoForEachHook,(void*)1234,0x1006,BTimedEventQueue::B_AT_TIME);
184	ASSERT(DoForEachEvent == media_timed_event(0x1006,BTimedEventQueue::B_STOP));
185	ASSERT(DoForEachCount == 1);
186
187	printf("\n expected: 0x1013, 0x1013, 0x1013\n");
188	DoForEachCount = 0;
189	q->DoForEach(DoForEachHook,(void*)1234,0x1013,BTimedEventQueue::B_AT_TIME);
190	ASSERT(DoForEachCount == 3);
191
192	printf("\n expected: 0x1000, 0x1001, 0x1002\n");
193	DoForEachCount = 0;
194	q->DoForEach(DoForEachHook,(void*)1234,0x1003,BTimedEventQueue::B_BEFORE_TIME,false);
195	ASSERT(DoForEachCount == 3);
196
197	printf("\n expected: 0x1000, 0x1001, 0x1002, 0x1003\n");
198	DoForEachCount = 0;
199	q->DoForEach(DoForEachHook,(void*)1234,0x1003,BTimedEventQueue::B_BEFORE_TIME,true);
200	ASSERT(DoForEachCount == 4);
201
202	printf("\n expected: 0x1013, 0x1013, 0x1013\n");
203	DoForEachCount = 0;
204	q->DoForEach(DoForEachHook,(void*)1234,0x1012,BTimedEventQueue::B_AFTER_TIME,false);
205	ASSERT(DoForEachCount == 3);
206
207	printf("\n expected: 0x1012, 0x1013, 0x1013, 0x1013\n");
208	DoForEachCount = 0;
209	q->DoForEach(DoForEachHook,(void*)1234,0x1012,BTimedEventQueue::B_AFTER_TIME,true);
210	ASSERT(DoForEachCount == 4);
211
212	printf("\n expected: none\n");
213	DoForEachCount = 0;
214	q->DoForEach(DoForEachHook,(void*)1234,0x1013,BTimedEventQueue::B_AFTER_TIME,false);
215	ASSERT(DoForEachCount == 0);
216
217	printf("\n expected: 0x1013, 0x1013, 0x1013\n");
218	DoForEachCount = 0;
219	q->DoForEach(DoForEachHook,(void*)1234,0x1013,BTimedEventQueue::B_AFTER_TIME,true);
220	ASSERT(DoForEachCount == 3);
221
222	printf("\n expected: all 16\n");
223	DoForEachCount = 0;
224	q->DoForEach(DoForEachHook,(void*)1234,0x0,BTimedEventQueue::B_ALWAYS);
225	ASSERT(DoForEachCount == 16);
226
227	printf("\n expected: none\n");
228	DoForEachCount = 0;
229	q->DoForEach(DoForEachHook,(void*)1234,0x0,BTimedEventQueue::B_ALWAYS,false,BTimedEventQueue::B_WARP);
230	ASSERT(DoForEachCount == 0);
231
232	printf("\n expected: 0x1000, 0x1013\n");
233	DoForEachCount = 0;
234	q->DoForEach(DoForEachHook,(void*)1234,0x0,BTimedEventQueue::B_ALWAYS,false,BTimedEventQueue::B_SEEK);
235	ASSERT(DoForEachCount == 2);
236
237	printf("\n expected: 0x1000, 0x1013\n");
238	DoForEachCount = 0;
239	q->DoForEach(DoForEachHook,(void*)1234,0x0999,BTimedEventQueue::B_AFTER_TIME,false,BTimedEventQueue::B_SEEK);
240	ASSERT(DoForEachCount == 2);
241
242	printf("\n expected: 0x1000, 0x1013\n");
243	DoForEachCount = 0;
244	q->DoForEach(DoForEachHook,(void*)1234,0x1014,BTimedEventQueue::B_BEFORE_TIME,false,BTimedEventQueue::B_SEEK);
245	ASSERT(DoForEachCount == 2);
246
247	printf("\n expected: none\n");
248	DoForEachCount = 0;
249	q->DoForEach(DoForEachHook,(void*)1234,0x0004,BTimedEventQueue::B_BEFORE_TIME,true);
250	ASSERT(DoForEachCount == 0);
251
252	delete q;
253}
254
255void MatchTest()
256{
257	BTimedEventQueue *q = new BTimedEventQueue;
258	ASSERT(q->EventCount() == 0);
259	ASSERT(q->HasEvents() == false);
260
261	q->AddEvent(media_timed_event(0x1000,BTimedEventQueue::B_SEEK));
262	q->AddEvent(media_timed_event(0x1001,BTimedEventQueue::B_START));
263	q->AddEvent(media_timed_event(0x1002,BTimedEventQueue::B_START));
264	q->AddEvent(media_timed_event(0x1003,BTimedEventQueue::B_START));
265	q->AddEvent(media_timed_event(0x1010,BTimedEventQueue::B_START));
266	q->AddEvent(media_timed_event(0x1011,BTimedEventQueue::B_START));
267	q->AddEvent(media_timed_event(0x1012,BTimedEventQueue::B_START));
268	q->AddEvent(media_timed_event(0x1004,BTimedEventQueue::B_START));
269	q->AddEvent(media_timed_event(0x1005,BTimedEventQueue::B_START));
270	q->AddEvent(media_timed_event(0x1006,BTimedEventQueue::B_STOP));
271	q->AddEvent(media_timed_event(0x1007,BTimedEventQueue::B_START));
272	q->AddEvent(media_timed_event(0x1008,BTimedEventQueue::B_START));
273	q->AddEvent(media_timed_event(0x1009,BTimedEventQueue::B_START));
274	q->AddEvent(media_timed_event(0x1013,BTimedEventQueue::B_START));
275	q->AddEvent(media_timed_event(0x1013,BTimedEventQueue::B_START));
276	q->AddEvent(media_timed_event(0x1013,BTimedEventQueue::B_SEEK));
277	ASSERT(q->EventCount() == 16);
278	ASSERT(q->HasEvents() == true);
279
280	printf("\nexpected: "); DumpEvent(media_timed_event(0x1006,BTimedEventQueue::B_STOP));
281	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1001,BTimedEventQueue::B_AFTER_TIME,true,BTimedEventQueue::B_STOP));
282
283	printf("\nexpected: "); DumpEvent(media_timed_event(0x1006,BTimedEventQueue::B_STOP));
284	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1006,BTimedEventQueue::B_AT_TIME,true));
285
286	printf("\nexpected: "); DumpEvent(media_timed_event(0x1006,BTimedEventQueue::B_STOP));
287	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1007,BTimedEventQueue::B_BEFORE_TIME,true,BTimedEventQueue::B_STOP));
288
289	printf("\nexpected: "); DumpEvent(media_timed_event(0x1000,BTimedEventQueue::B_SEEK));
290	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1006,BTimedEventQueue::B_BEFORE_TIME,true));
291
292	printf("\nexpected: "); DumpEvent(media_timed_event(0x1006,BTimedEventQueue::B_STOP));
293	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1006,BTimedEventQueue::B_AFTER_TIME,true));
294
295	printf("\nexpected: "); DumpEvent(0);
296	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1006,BTimedEventQueue::B_BEFORE_TIME,false,BTimedEventQueue::B_STOP));
297
298	printf("\nexpected: "); DumpEvent(0);
299	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1006,BTimedEventQueue::B_AFTER_TIME,false,BTimedEventQueue::B_STOP));
300
301	printf("\nexpected: "); DumpEvent(0);
302	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1006,BTimedEventQueue::B_AT_TIME,false,BTimedEventQueue::B_SEEK));
303
304	printf("\nexpected: "); DumpEvent(media_timed_event(0x1000,BTimedEventQueue::B_SEEK));
305	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1000,BTimedEventQueue::B_AFTER_TIME,true));
306
307	printf("\nexpected: "); DumpEvent(media_timed_event(0x1000,BTimedEventQueue::B_SEEK));
308	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1010,BTimedEventQueue::B_BEFORE_TIME,true));
309
310	printf("\nexpected: "); DumpEvent(media_timed_event(0x1000,BTimedEventQueue::B_SEEK));
311	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1007,BTimedEventQueue::B_BEFORE_TIME,false));
312
313	printf("\nexpected: "); DumpEvent(media_timed_event(0x1013,BTimedEventQueue::B_SEEK));
314	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1001,BTimedEventQueue::B_AFTER_TIME,false,BTimedEventQueue::B_SEEK));
315
316	printf("\nexpected: "); DumpEvent(media_timed_event(0x1010,BTimedEventQueue::B_START));
317	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1009,BTimedEventQueue::B_AFTER_TIME,false));
318
319	printf("\nexpected: "); DumpEvent(media_timed_event(0x1010,BTimedEventQueue::B_START));
320	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1010,BTimedEventQueue::B_AFTER_TIME,true));
321
322	printf("\nexpected: "); DumpEvent(media_timed_event(0x1010,BTimedEventQueue::B_START));
323	printf("found:    "); DumpEvent(q->FindFirstMatch(0x1010,BTimedEventQueue::B_AT_TIME,true));
324
325	delete q;
326}
327
328void FlushTest()
329{
330	BTimedEventQueue *q = new BTimedEventQueue;
331	ASSERT(q->EventCount() == 0);
332	ASSERT(q->HasEvents() == false);
333
334	q->AddEvent(media_timed_event(0x1000,BTimedEventQueue::B_SEEK));
335	q->AddEvent(media_timed_event(0x1001,BTimedEventQueue::B_START));
336	q->AddEvent(media_timed_event(0x1002,BTimedEventQueue::B_START));
337	q->AddEvent(media_timed_event(0x1003,BTimedEventQueue::B_START));
338	q->AddEvent(media_timed_event(0x1004,BTimedEventQueue::B_START));
339	q->AddEvent(media_timed_event(0x1005,BTimedEventQueue::B_START));
340	q->AddEvent(media_timed_event(0x1006,BTimedEventQueue::B_STOP));
341	q->AddEvent(media_timed_event(0x1007,BTimedEventQueue::B_START));
342	q->AddEvent(media_timed_event(0x1008,BTimedEventQueue::B_START));
343	q->AddEvent(media_timed_event(0x1009,BTimedEventQueue::B_START));
344	q->AddEvent(media_timed_event(0x1010,BTimedEventQueue::B_START));
345	q->AddEvent(media_timed_event(0x1011,BTimedEventQueue::B_START));
346	q->AddEvent(media_timed_event(0x1012,BTimedEventQueue::B_START));
347	q->AddEvent(media_timed_event(0x1013,BTimedEventQueue::B_START));
348	q->AddEvent(media_timed_event(0x1013,BTimedEventQueue::B_START));
349	q->AddEvent(media_timed_event(0x1013,BTimedEventQueue::B_SEEK));
350	ASSERT(q->EventCount() == 16);
351	ASSERT(q->HasEvents() == true);
352
353	printf("### removing 0x1007\n");
354	q->FlushEvents(0x1007, BTimedEventQueue::B_AT_TIME);
355	ASSERT(q->EventCount() == 15);
356
357	printf("### removing 0x1013\n");
358	q->FlushEvents(0x1012, BTimedEventQueue::B_AFTER_TIME,false);
359	ASSERT(q->EventCount() == 12);
360
361	printf("### removing none\n");
362	q->FlushEvents(0x1fff, BTimedEventQueue::B_AFTER_TIME,false);
363	ASSERT(q->EventCount() == 12);
364
365	printf("### removing none\n");
366	q->FlushEvents(0x1fff, BTimedEventQueue::B_AFTER_TIME,true);
367	ASSERT(q->EventCount() == 12);
368
369	printf("### removing 0x1010, 0x1011, 0x1012\n");
370	q->FlushEvents(0x1010, BTimedEventQueue::B_AFTER_TIME,true);
371	ASSERT(q->EventCount() == 9);
372
373	printf("### removing 0x1000 to 0x1005\n");
374	q->FlushEvents(0x1006, BTimedEventQueue::B_BEFORE_TIME,false);
375	ASSERT(q->EventCount() == 3);
376
377	printf("### removing 0x1006\n");
378	q->FlushEvents(0x1006, BTimedEventQueue::B_AT_TIME);
379	ASSERT(q->EventCount() == 2);
380
381	printf("### removing 0x1008 0x1009\n");
382	q->FlushEvents(0xffffff, BTimedEventQueue::B_BEFORE_TIME);
383	ASSERT(q->EventCount() == 0);
384
385	delete q;
386}
387
388
389int main()
390{
391	InsertRemoveTest();
392	DoForEachTest();
393	MatchTest();
394	FlushTest();
395	return 0;
396}
397