tests/check/: use the new macro
[platform/upstream/gstreamer.git] / tests / check / gst / gstsegment.c
1 /* GStreamer
2  * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
3  *
4  * gstsegment.c: Unit test for segments
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 /* mess with the segment structure in the bytes format */
25 GST_START_TEST (segment_seek_nosize)
26 {
27   GstSegment segment;
28   gboolean res;
29   gint64 cstart, cstop;
30   gboolean update;
31
32   gst_segment_init (&segment, GST_FORMAT_BYTES);
33
34   /* configure segment to start 100 */
35   gst_segment_set_seek (&segment, 1.0,
36       GST_FORMAT_BYTES,
37       GST_SEEK_FLAG_NONE,
38       GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
39   fail_unless (segment.start == 100);
40   fail_unless (segment.stop == -1);
41
42   /* configure segment to stop relative, should not do anything since 
43    * size is unknown. */
44   gst_segment_set_seek (&segment, 1.0,
45       GST_FORMAT_BYTES,
46       GST_SEEK_FLAG_NONE,
47       GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
48   fail_unless (segment.start == 100);
49   fail_unless (segment.stop == -1);
50
51   /* do some clipping on the open range */
52   /* completely outside */
53   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
54   fail_unless (res == FALSE);
55
56   /* touching lower bound, still outside of the segment */
57   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
58   fail_unless (res == FALSE);
59
60   /* partially inside */
61   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
62   fail_unless (res == TRUE);
63   fail_unless (cstart == 100);
64   fail_unless (cstop == 150);
65
66   /* inside, touching lower bound */
67   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
68       100, 150, &cstart, &cstop);
69   fail_unless (res == TRUE);
70   fail_unless (cstart == 100);
71   fail_unless (cstop == 150);
72
73   /* completely inside */
74   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
75       150, 200, &cstart, &cstop);
76   fail_unless (res == TRUE);
77   fail_unless (cstart == 150);
78   fail_unless (cstop == 200);
79
80   /* invalid start */
81   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
82   fail_unless (res == FALSE);
83
84   /* start outside, we don't know the stop */
85   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
86   fail_unless (res == TRUE);
87   fail_unless (cstart == 100);
88   fail_unless (cstop == -1);
89
90   /* start on lower bound */
91   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
92   fail_unless (res == TRUE);
93   fail_unless (cstart == 100);
94   fail_unless (cstop == -1);
95
96   /* start inside */
97   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
98   fail_unless (res == TRUE);
99   fail_unless (cstart == 150);
100   fail_unless (cstop == -1);
101
102   /* add 100 to start, set stop to 300 */
103   gst_segment_set_seek (&segment, 1.0,
104       GST_FORMAT_BYTES,
105       GST_SEEK_FLAG_NONE,
106       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300, &update);
107   fail_unless (segment.start == 200);
108   fail_unless (segment.stop == 300);
109
110   /* add 100 to start (to 300), set stop to 200, this is not allowed. 
111    * nothing should be updated in the segment. A g_warning is
112    * emited. */
113   ASSERT_CRITICAL (gst_segment_set_seek (&segment, 1.0,
114           GST_FORMAT_BYTES,
115           GST_SEEK_FLAG_NONE,
116           GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update));
117   fail_unless (segment.start == 200);
118   fail_unless (segment.stop == 300);
119
120   /* seek relative to end, should not do anything since size is
121    * unknown. */
122   gst_segment_set_seek (&segment, 1.0,
123       GST_FORMAT_BYTES,
124       GST_SEEK_FLAG_NONE,
125       GST_SEEK_TYPE_END, -300, GST_SEEK_TYPE_END, -100, &update);
126   fail_unless (segment.start == 200);
127   fail_unless (segment.stop == 300);
128
129   /* completely outside */
130   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
131   fail_unless (res == FALSE);
132
133   /* touching lower bound */
134   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 200, &cstart, &cstop);
135   fail_unless (res == FALSE);
136
137   /* partially inside */
138   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 250, &cstart, &cstop);
139   fail_unless (res == TRUE);
140   fail_unless (cstart == 200);
141   fail_unless (cstop == 250);
142
143   /* inside, touching lower bound */
144   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
145       200, 250, &cstart, &cstop);
146   fail_unless (res == TRUE);
147   fail_unless (cstart == 200);
148   fail_unless (cstop == 250);
149
150   /* completely inside */
151   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
152       250, 290, &cstart, &cstop);
153   fail_unless (res == TRUE);
154   fail_unless (cstart == 250);
155   fail_unless (cstop == 290);
156
157   /* partially inside */
158   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
159       250, 350, &cstart, &cstop);
160   fail_unless (res == TRUE);
161   fail_unless (cstart == 250);
162   fail_unless (cstop == 300);
163
164   /* invalid start */
165   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
166   fail_unless (res == FALSE);
167
168   /* start outside */
169   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
170   fail_unless (res == TRUE);
171   fail_unless (cstart == 200);
172   fail_unless (cstop == 300);
173
174   /* start on lower bound */
175   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
176   fail_unless (res == TRUE);
177   fail_unless (cstart == 200);
178   fail_unless (cstop == 300);
179
180   /* start inside */
181   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
182   fail_unless (res == TRUE);
183   fail_unless (cstart == 250);
184   fail_unless (cstop == 300);
185
186   /* start outside on boundary */
187   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 300, -1, &cstart, &cstop);
188   fail_unless (res == FALSE);
189
190   /* start completely outside */
191   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 350, -1, &cstart, &cstop);
192   fail_unless (res == FALSE);
193 }
194
195 GST_END_TEST;
196
197 /* mess with the segment structure in the bytes format */
198 GST_START_TEST (segment_seek_size)
199 {
200   GstSegment segment;
201   gboolean res;
202   gint64 cstart, cstop;
203   gboolean update;
204
205   gst_segment_init (&segment, GST_FORMAT_BYTES);
206   gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
207
208   /* configure segment to start 100 */
209   gst_segment_set_seek (&segment, 1.0,
210       GST_FORMAT_BYTES,
211       GST_SEEK_FLAG_NONE,
212       GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
213   fail_unless (segment.start == 100);
214   fail_unless (segment.stop == -1);
215
216   /* configure segment to stop relative, does not update stop
217    * since we did not set it before. */
218   gst_segment_set_seek (&segment, 1.0,
219       GST_FORMAT_BYTES,
220       GST_SEEK_FLAG_NONE,
221       GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
222   fail_unless (segment.start == 100);
223   fail_unless (segment.stop == -1);
224
225   /* do some clipping on the open range */
226   /* completely outside */
227   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
228   fail_unless (res == FALSE);
229
230   /* touching lower bound */
231   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
232   fail_unless (res == FALSE);
233
234   /* partially inside */
235   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
236   fail_unless (res == TRUE);
237   fail_unless (cstart == 100);
238   fail_unless (cstop == 150);
239
240   /* inside, touching lower bound */
241   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
242       100, 150, &cstart, &cstop);
243   fail_unless (res == TRUE);
244   fail_unless (cstart == 100);
245   fail_unless (cstop == 150);
246
247   /* completely inside */
248   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
249       150, 200, &cstart, &cstop);
250   fail_unless (res == TRUE);
251   fail_unless (cstart == 150);
252   fail_unless (cstop == 200);
253
254   /* partially inside, clip to size */
255   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
256       150, 300, &cstart, &cstop);
257   fail_unless (res == TRUE);
258   fail_unless (cstart == 150);
259   fail_unless (cstop == 200);
260
261   /* invalid start */
262   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
263   fail_unless (res == FALSE);
264
265   /* start outside */
266   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
267   fail_unless (res == TRUE);
268   fail_unless (cstart == 100);
269   fail_unless (cstop == -1);
270
271   /* start on lower bound */
272   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
273   fail_unless (res == TRUE);
274   fail_unless (cstart == 100);
275   fail_unless (cstop == -1);
276
277   /* start inside */
278   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
279   fail_unless (res == TRUE);
280   fail_unless (cstart == 150);
281   fail_unless (cstop == -1);
282
283   /* add 100 to start, set stop to 300, stop clips to 200 */
284   gst_segment_set_seek (&segment, 1.0,
285       GST_FORMAT_BYTES,
286       GST_SEEK_FLAG_NONE,
287       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300, &update);
288   fail_unless (segment.start == 200);
289   fail_unless (segment.stop == 200);
290
291   /* add 100 to start (to 300), set stop to 200, this clips start
292    * to duration */
293   gst_segment_set_seek (&segment, 1.0,
294       GST_FORMAT_BYTES,
295       GST_SEEK_FLAG_NONE,
296       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update);
297   fail_unless (segment.start == 200);
298   fail_unless (segment.stop == 200);
299
300   /* seek relative to end */
301   gst_segment_set_seek (&segment, 1.0,
302       GST_FORMAT_BYTES,
303       GST_SEEK_FLAG_NONE,
304       GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20, &update);
305   fail_unless (segment.start == 100);
306   fail_unless (segment.stop == 180);
307
308   /* completely outside */
309   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
310   fail_unless (res == FALSE);
311
312   /* touching lower bound */
313   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
314   fail_unless (res == FALSE);
315
316   /* partially inside */
317   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
318   fail_unless (res == TRUE);
319   fail_unless (cstart == 100);
320   fail_unless (cstop == 150);
321
322   /* inside, touching lower bound */
323   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
324       100, 150, &cstart, &cstop);
325   fail_unless (res == TRUE);
326   fail_unless (cstart == 100);
327   fail_unless (cstop == 150);
328
329   /* completely inside */
330   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
331       150, 170, &cstart, &cstop);
332   fail_unless (res == TRUE);
333   fail_unless (cstart == 150);
334   fail_unless (cstop == 170);
335
336   /* partially inside */
337   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
338       150, 250, &cstart, &cstop);
339   fail_unless (res == TRUE);
340   fail_unless (cstart == 150);
341   fail_unless (cstop == 180);
342
343   /* invalid start */
344   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
345   fail_unless (res == FALSE);
346
347   /* start outside */
348   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
349   fail_unless (res == TRUE);
350   fail_unless (cstart == 100);
351   fail_unless (cstop == 180);
352
353   /* start on lower bound */
354   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
355   fail_unless (res == TRUE);
356   fail_unless (cstart == 100);
357   fail_unless (cstop == 180);
358
359   /* start inside */
360   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
361   fail_unless (res == TRUE);
362   fail_unless (cstart == 150);
363   fail_unless (cstop == 180);
364
365   /* start outside on boundary */
366   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
367   fail_unless (res == FALSE);
368
369   /* start completely outside */
370   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
371   fail_unless (res == FALSE);
372 }
373
374 GST_END_TEST;
375
376 /* mess with the segment structure in the bytes format */
377 GST_START_TEST (segment_newsegment_open)
378 {
379   GstSegment segment;
380
381   gst_segment_init (&segment, GST_FORMAT_BYTES);
382
383   /* time should also work for starting from 0 */
384   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
385
386   fail_unless (segment.rate == 1.0);
387   fail_unless (segment.format == GST_FORMAT_BYTES);
388   fail_unless (segment.flags == 0);
389   fail_unless (segment.start == 0);
390   fail_unless (segment.stop == -1);
391   fail_unless (segment.time == 0);
392   fail_unless (segment.accum == 0);
393   fail_unless (segment.last_stop == 0);
394   fail_unless (segment.duration == -1);
395
396   /* we set stop but in the wrong format, stop stays open. */
397   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_TIME, 0, 200, 0);
398
399   fail_unless (segment.start == 0);
400   fail_unless (segment.stop == -1);
401   fail_unless (segment.time == 0);
402   fail_unless (segment.accum == 0);
403
404   /* update, nothing changes */
405   gst_segment_set_newsegment (&segment, TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
406
407   fail_unless (segment.start == 0);
408   fail_unless (segment.stop == -1);
409   fail_unless (segment.time == 0);
410   fail_unless (segment.accum == 0);
411
412   /* update */
413   gst_segment_set_newsegment (&segment, TRUE, 1.0,
414       GST_FORMAT_BYTES, 100, -1, 100);
415
416   fail_unless (segment.start == 100);
417   fail_unless (segment.stop == -1);
418   fail_unless (segment.time == 100);
419   fail_unless (segment.accum == 100);
420
421   /* last_stop 0, accum does not change */
422   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
423
424   fail_unless (segment.start == 0);
425   fail_unless (segment.stop == -1);
426   fail_unless (segment.time == 0);
427   fail_unless (segment.accum == 100);
428
429   gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 200);
430
431   /* last_stop 200, accum changes */
432   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
433
434   fail_unless (segment.start == 0);
435   fail_unless (segment.stop == -1);
436   fail_unless (segment.time == 0);
437   fail_unless (segment.accum == 300);
438
439 }
440
441 GST_END_TEST;
442
443
444 /* mess with the segment structure in the bytes format */
445 GST_START_TEST (segment_newsegment_closed)
446 {
447   GstSegment segment;
448
449   gst_segment_init (&segment, GST_FORMAT_BYTES);
450
451   gst_segment_set_newsegment (&segment, FALSE, 1.0,
452       GST_FORMAT_BYTES, 0, 200, 0);
453
454   fail_unless (segment.rate == 1.0);
455   fail_unless (segment.format == GST_FORMAT_BYTES);
456   fail_unless (segment.flags == 0);
457   fail_unless (segment.start == 0);
458   fail_unless (segment.stop == 200);
459   fail_unless (segment.time == 0);
460   fail_unless (segment.accum == 0);
461   fail_unless (segment.last_stop == 0);
462   fail_unless (segment.duration == -1);
463
464   /* do an update */
465   gst_segment_set_newsegment (&segment, TRUE, 1.0, GST_FORMAT_BYTES, 0, 300, 0);
466
467   fail_unless (segment.start == 0);
468   fail_unless (segment.stop == 300);
469   fail_unless (segment.time == 0);
470   fail_unless (segment.accum == 0);
471
472   /* and a new accumulated one */
473   gst_segment_set_newsegment (&segment, FALSE, 1.0,
474       GST_FORMAT_BYTES, 100, 400, 300);
475
476   fail_unless (segment.start == 100);
477   fail_unless (segment.stop == 400);
478   fail_unless (segment.time == 300);
479   fail_unless (segment.accum == 300);
480
481   /* and a new updated one */
482   gst_segment_set_newsegment (&segment, TRUE, 1.0,
483       GST_FORMAT_BYTES, 100, 500, 300);
484
485   fail_unless (segment.start == 100);
486   fail_unless (segment.stop == 500);
487   fail_unless (segment.time == 300);
488   fail_unless (segment.accum == 300);
489
490   /* and a new partially updated one */
491   gst_segment_set_newsegment (&segment, TRUE, 1.0,
492       GST_FORMAT_BYTES, 200, 500, 400);
493
494   fail_unless (segment.start == 200);
495   fail_unless (segment.stop == 500);
496   fail_unless (segment.time == 400);
497   fail_unless (segment.accum == 400);
498 }
499
500 GST_END_TEST;
501
502 /* mess with the segment structure in the time format */
503 GST_START_TEST (segment_newsegment_streamtime)
504 {
505   GstSegment segment;
506   gint64 result;
507
508   gst_segment_init (&segment, GST_FORMAT_TIME);
509
510   /***************************
511    * Normal segment
512    ***************************/
513   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
514       GST_FORMAT_TIME, 0, 200, 0);
515
516   fail_unless (segment.rate == 1.0);
517   fail_unless (segment.applied_rate == 1.0);
518   fail_unless (segment.format == GST_FORMAT_TIME);
519   fail_unless (segment.flags == 0);
520   fail_unless (segment.start == 0);
521   fail_unless (segment.stop == 200);
522   fail_unless (segment.time == 0);
523   fail_unless (segment.accum == 0);
524   fail_unless (segment.last_stop == 0);
525   fail_unless (segment.duration == -1);
526
527   /* invalid time gives invalid result */
528   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
529   fail_unless (result == -1);
530
531   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
532   fail_unless (result == 0);
533
534   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
535   fail_unless (result == 100);
536
537   /* outside of the segment */
538   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
539   fail_unless (result == -1);
540
541   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
542   fail_unless (result == -1);
543
544   /*********************
545    * time shifted by 500
546    *********************/
547   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
548       GST_FORMAT_TIME, 0, 200, 500);
549
550   fail_unless (segment.accum == 200);
551
552   /* invalid time gives invalid result */
553   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
554   fail_unless (result == -1);
555
556   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
557   fail_unless (result == 500);
558
559   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
560   fail_unless (result == 600);
561
562   /* outside of the segment */
563   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
564   fail_unless (result == -1);
565
566   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
567   fail_unless (result == -1);
568
569   /*********************
570    * time offset by 500
571    *********************/
572   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
573       GST_FORMAT_TIME, 500, 700, 0);
574
575   fail_unless (segment.accum == 400);
576
577   /* invalid time gives invalid result */
578   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
579   fail_unless (result == -1);
580
581   /* before segment is invalid */
582   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
583   fail_unless (result == -1);
584
585   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
586   fail_unless (result == 0);
587
588   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
589   fail_unless (result == 100);
590
591   /* outside of the segment */
592   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
593   fail_unless (result == -1);
594
595   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
596   fail_unless (result == -1);
597
598   /*************************************
599    * time offset by 500, shifted by 200
600    *************************************/
601   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
602       GST_FORMAT_TIME, 500, 700, 200);
603
604   fail_unless (segment.accum == 600);
605
606   /* invalid time gives invalid result */
607   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
608   fail_unless (result == -1);
609
610   /* before segment is invalid */
611   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
612   fail_unless (result == -1);
613
614   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
615   fail_unless (result == 200);
616
617   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
618   fail_unless (result == 300);
619
620   /* outside of the segment */
621   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
622   fail_unless (result == -1);
623
624   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
625   fail_unless (result == -1);
626 }
627
628 GST_END_TEST;
629
630 /* mess with the segment structure in the time format */
631 GST_START_TEST (segment_newsegment_streamtime_rate)
632 {
633   GstSegment segment;
634   gint64 result;
635
636   gst_segment_init (&segment, GST_FORMAT_TIME);
637
638   /***************************
639    * Normal segment rate 2.0
640    ***************************/
641   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 1.0,
642       GST_FORMAT_TIME, 0, 200, 0);
643
644   fail_unless (segment.rate == 2.0);
645   fail_unless (segment.applied_rate == 1.0);
646   fail_unless (segment.format == GST_FORMAT_TIME);
647   fail_unless (segment.flags == 0);
648   fail_unless (segment.start == 0);
649   fail_unless (segment.stop == 200);
650   fail_unless (segment.time == 0);
651   fail_unless (segment.accum == 0);
652   fail_unless (segment.last_stop == 0);
653   fail_unless (segment.duration == -1);
654
655   /* invalid time gives invalid result */
656   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
657   fail_unless (result == -1);
658
659   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
660   fail_unless (result == 0);
661
662   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
663   fail_unless (result == 100);
664
665   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
666   fail_unless (result == 150);
667
668   /* outside of the segment */
669   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
670   fail_unless (result == -1);
671
672   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
673   fail_unless (result == -1);
674
675   /***************************************
676    * Normal segment rate 2.0, offset
677    ***************************************/
678   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 1.0,
679       GST_FORMAT_TIME, 100, 300, 0);
680
681   fail_unless (segment.accum == 100);
682
683   /* invalid time gives invalid result */
684   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
685   fail_unless (result == -1);
686
687   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
688   fail_unless (result == 0);
689
690   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
691   fail_unless (result == 100);
692
693   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 250);
694   fail_unless (result == 150);
695
696   /* outside of the segment */
697   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
698   fail_unless (result == -1);
699
700   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
701   fail_unless (result == -1);
702
703   /***************************************
704    * Normal segment rate -1.0, offset
705    ***************************************/
706
707   /* buffers will arrive from 300 to 100 in a sink, stream time
708    * calculation is unaffected by the rate */
709   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
710       GST_FORMAT_TIME, 100, 300, 0);
711
712   fail_unless (segment.accum == 200);
713
714   /* invalid time gives invalid result */
715   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
716   fail_unless (result == -1);
717
718   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
719   fail_unless (result == 0);
720
721   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
722   fail_unless (result == 100);
723
724   /***********************************************
725    * Normal segment rate -1.0, offset, time = 200
726    ***********************************************/
727   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
728       GST_FORMAT_TIME, 100, 300, 200);
729
730   /* invalid time gives invalid result */
731   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
732   fail_unless (result == -1);
733
734   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
735   fail_unless (result == 200);
736
737   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
738   fail_unless (result == 300);
739
740   /* outside of the segment */
741   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
742   fail_unless (result == -1);
743
744   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
745   fail_unless (result == -1);
746 }
747
748 GST_END_TEST;
749
750 /* mess with the segment structure in the time format */
751 GST_START_TEST (segment_newsegment_streamtime_applied_rate)
752 {
753   GstSegment segment;
754   gint64 result;
755
756   gst_segment_init (&segment, GST_FORMAT_TIME);
757
758   /***********************************************************
759    * Normal segment rate 1.0, applied rate -1.0
760    * This means the timestamps represents a stream going backwards
761    * starting from @time to 0.
762    ************************************************************/
763   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, -1.0,
764       GST_FORMAT_TIME, 0, 200, 200);
765
766   fail_unless (segment.rate == 1.0);
767   fail_unless (segment.applied_rate == -1.0);
768   fail_unless (segment.format == GST_FORMAT_TIME);
769   fail_unless (segment.flags == 0);
770   fail_unless (segment.start == 0);
771   fail_unless (segment.stop == 200);
772   fail_unless (segment.time == 200);
773   fail_unless (segment.accum == 0);
774   fail_unless (segment.last_stop == 0);
775   fail_unless (segment.duration == -1);
776
777   /* invalid time gives invalid result */
778   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
779   fail_unless (result == -1);
780
781   /* we count backwards from 200 */
782   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
783   fail_unless (result == 200);
784
785   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
786   fail_unless (result == 100);
787
788   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
789   fail_unless (result == 50);
790
791   /* outside of the segment */
792   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
793   fail_unless (result == -1);
794
795   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
796   fail_unless (result == -1);
797
798   /***********************************************************
799    * Normal segment rate 1.0, applied rate 2.0
800    * This means the timestamps represents a stream at twice the
801    * normal rate
802    ************************************************************/
803   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 2.0,
804       GST_FORMAT_TIME, 0, 200, 0);
805
806   fail_unless (segment.rate == 1.0);
807   fail_unless (segment.applied_rate == 2.0);
808   fail_unless (segment.format == GST_FORMAT_TIME);
809   fail_unless (segment.flags == 0);
810   fail_unless (segment.start == 0);
811   fail_unless (segment.stop == 200);
812   fail_unless (segment.time == 0);
813   fail_unless (segment.accum == 200);
814   fail_unless (segment.last_stop == 0);
815   fail_unless (segment.duration == -1);
816
817   /* invalid time gives invalid result */
818   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
819   fail_unless (result == -1);
820
821   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
822   fail_unless (result == 0);
823
824   /* the stream prepresents a stream going twice as fast, the position 
825    * in the segment is therefore scaled by the applied rate */
826   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
827   fail_unless (result == 200);
828
829   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
830   fail_unless (result == 300);
831
832   /* outside of the segment */
833   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
834   fail_unless (result == -1);
835
836   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
837   fail_unless (result == -1);
838
839   /***********************************************************
840    * Normal segment rate 1.0, applied rate -2.0
841    * This means the timestamps represents a stream at twice the
842    * reverse rate
843    ************************************************************/
844   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, -2.0,
845       GST_FORMAT_TIME, 0, 200, 400);
846
847   fail_unless (segment.rate == 1.0);
848   fail_unless (segment.applied_rate == -2.0);
849   fail_unless (segment.format == GST_FORMAT_TIME);
850   fail_unless (segment.flags == 0);
851   fail_unless (segment.start == 0);
852   fail_unless (segment.stop == 200);
853   fail_unless (segment.time == 400);
854   /* previous segment lasted 200, rate of 2.0 was already applied */
855   fail_unless (segment.accum == 400);
856   fail_unless (segment.last_stop == 0);
857   fail_unless (segment.duration == -1);
858
859   /* invalid time gives invalid result */
860   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
861   fail_unless (result == -1);
862
863   /* we count backwards from 400 */
864   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
865   fail_unless (result == 400);
866
867   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
868   fail_unless (result == 200);
869
870   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
871   fail_unless (result == 100);
872
873   /* outside of the segment */
874   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
875   fail_unless (result == -1);
876
877   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
878   fail_unless (result == -1);
879
880   /***********************************************************
881    * Normal segment rate 1.0, applied rate -2.0
882    * This means the timestamps represents a stream at twice the
883    * reverse rate, start time cannot compensate the complete
884    * duration of the segment so we stop at 0
885    ************************************************************/
886   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, -2.0,
887       GST_FORMAT_TIME, 0, 200, 200);
888
889   fail_unless (segment.rate == 1.0);
890   fail_unless (segment.applied_rate == -2.0);
891   fail_unless (segment.format == GST_FORMAT_TIME);
892   fail_unless (segment.flags == 0);
893   fail_unless (segment.start == 0);
894   fail_unless (segment.stop == 200);
895   fail_unless (segment.time == 200);
896   fail_unless (segment.accum == 600);
897   fail_unless (segment.last_stop == 0);
898   fail_unless (segment.duration == -1);
899
900   /* invalid time gives invalid result */
901   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
902   fail_unless (result == -1);
903
904   /* we count backwards from 200 */
905   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
906   fail_unless (result == 200);
907
908   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
909   fail_unless (result == 0);
910
911   /* clamp at 0 */
912   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
913   fail_unless (result == 0);
914
915   /* outside of the segment */
916   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
917   fail_unless (result == -1);
918
919   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
920   fail_unless (result == -1);
921 }
922
923 GST_END_TEST;
924
925 /* mess with the segment structure in the time format */
926 GST_START_TEST (segment_newsegment_streamtime_applied_rate_rate)
927 {
928   GstSegment segment;
929   gint64 result;
930
931   gst_segment_init (&segment, GST_FORMAT_TIME);
932
933   /***********************************************************
934    * Segment rate 2.0, applied rate 2.0
935    * this means we have a double speed stream that we should
936    * speed up by a factor of 2.0 some more. the resulting
937    * stream will be played at four times the speed. 
938    ************************************************************/
939   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 2.0,
940       GST_FORMAT_TIME, 0, 200, 0);
941
942   fail_unless (segment.rate == 2.0);
943   fail_unless (segment.applied_rate == 2.0);
944   fail_unless (segment.format == GST_FORMAT_TIME);
945   fail_unless (segment.flags == 0);
946   fail_unless (segment.start == 0);
947   fail_unless (segment.stop == 200);
948   fail_unless (segment.time == 0);
949   fail_unless (segment.accum == 0);
950   fail_unless (segment.last_stop == 0);
951   fail_unless (segment.duration == -1);
952
953   /* invalid time gives invalid result */
954   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
955   fail_unless (result == -1);
956
957   /* only applied rate affects our calculation of the stream time */
958   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
959   fail_unless (result == 0);
960
961   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
962   fail_unless (result == 200);
963
964   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
965   fail_unless (result == 300);
966
967   /* outside of the segment */
968   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
969   fail_unless (result == -1);
970
971   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
972   fail_unless (result == -1);
973
974   /***********************************************************
975    * Segment rate 2.0, applied rate -1.0
976    * this means we have a reverse stream that we should
977    * speed up by a factor of 2.0
978    ************************************************************/
979   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, -1.0,
980       GST_FORMAT_TIME, 0, 200, 200);
981
982   fail_unless (segment.rate == 2.0);
983   fail_unless (segment.applied_rate == -1.0);
984   fail_unless (segment.format == GST_FORMAT_TIME);
985   fail_unless (segment.flags == 0);
986   fail_unless (segment.start == 0);
987   fail_unless (segment.stop == 200);
988   fail_unless (segment.time == 200);
989   /* previous segment lasted 100 */
990   fail_unless (segment.accum == 100);
991   fail_unless (segment.last_stop == 0);
992   fail_unless (segment.duration == -1);
993
994   /* invalid time gives invalid result */
995   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
996   fail_unless (result == -1);
997
998   /* only applied rate affects our calculation of the stream time */
999   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1000   fail_unless (result == 200);
1001
1002   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1003   fail_unless (result == 100);
1004
1005   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1006   fail_unless (result == 50);
1007
1008   /* outside of the segment */
1009   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1010   fail_unless (result == -1);
1011
1012   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1013   fail_unless (result == -1);
1014
1015   /***********************************************************
1016    * Segment rate -1.0, applied rate -1.0
1017    * this means we have a reverse stream that we should
1018    * reverse to get the normal stream again.
1019    ************************************************************/
1020   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, -1.0,
1021       GST_FORMAT_TIME, 0, 200, 200);
1022
1023   fail_unless (segment.rate == -1.0);
1024   fail_unless (segment.applied_rate == -1.0);
1025   fail_unless (segment.format == GST_FORMAT_TIME);
1026   fail_unless (segment.flags == 0);
1027   fail_unless (segment.start == 0);
1028   fail_unless (segment.stop == 200);
1029   fail_unless (segment.time == 200);
1030   /* accumulated 100 of previous segment to make 200 */
1031   fail_unless (segment.accum == 200);
1032   fail_unless (segment.last_stop == 0);
1033   fail_unless (segment.duration == -1);
1034
1035   /* invalid time gives invalid result */
1036   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1037   fail_unless (result == -1);
1038
1039   /* only applied rate affects our calculation of the stream time */
1040   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1041   fail_unless (result == 200);
1042
1043   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1044   fail_unless (result == 100);
1045
1046   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1047   fail_unless (result == 50);
1048
1049   /* outside of the segment */
1050   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1051   fail_unless (result == -1);
1052
1053   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1054   fail_unless (result == -1);
1055
1056   /***********************************************************
1057    * Segment rate -1.0, applied rate -1.0
1058    * this means we have a reverse stream that we should
1059    * reverse to get the normal stream again.
1060    ************************************************************/
1061   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 2.0,
1062       GST_FORMAT_TIME, 0, 200, 0);
1063
1064   fail_unless (segment.rate == -1.0);
1065   fail_unless (segment.applied_rate == 2.0);
1066   fail_unless (segment.format == GST_FORMAT_TIME);
1067   fail_unless (segment.flags == 0);
1068   fail_unless (segment.start == 0);
1069   fail_unless (segment.stop == 200);
1070   fail_unless (segment.time == 0);
1071   fail_unless (segment.accum == 400);
1072   fail_unless (segment.last_stop == 0);
1073   fail_unless (segment.duration == -1);
1074
1075   /* invalid time gives invalid result */
1076   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1077   fail_unless (result == -1);
1078
1079   /* only applied rate affects our calculation of the stream time */
1080   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1081   fail_unless (result == 0);
1082
1083   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1084   fail_unless (result == 200);
1085
1086   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1087   fail_unless (result == 300);
1088
1089   /* outside of the segment */
1090   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1091   fail_unless (result == -1);
1092
1093   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1094   fail_unless (result == -1);
1095 }
1096
1097 GST_END_TEST;
1098
1099 /* mess with the segment structure in the time format */
1100 GST_START_TEST (segment_newsegment_runningtime)
1101 {
1102   GstSegment segment;
1103   gint64 result;
1104
1105   gst_segment_init (&segment, GST_FORMAT_TIME);
1106
1107   /***************************
1108    * Normal segment
1109    ***************************/
1110   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
1111       GST_FORMAT_TIME, 0, 200, 0);
1112
1113   fail_unless (segment.rate == 1.0);
1114   fail_unless (segment.applied_rate == 1.0);
1115   fail_unless (segment.format == GST_FORMAT_TIME);
1116   fail_unless (segment.flags == 0);
1117   fail_unless (segment.start == 0);
1118   fail_unless (segment.stop == 200);
1119   fail_unless (segment.time == 0);
1120   fail_unless (segment.accum == 0);
1121   fail_unless (segment.last_stop == 0);
1122   fail_unless (segment.duration == -1);
1123
1124   /* invalid time gives invalid result */
1125   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1126   fail_unless (result == -1);
1127
1128   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1129   fail_unless (result == 0);
1130
1131   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1132   fail_unless (result == 100);
1133
1134   /* outside of the segment */
1135   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1136   fail_unless (result == -1);
1137
1138   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 300);
1139   fail_unless (result == -1);
1140
1141   /***********************************************************
1142    * time shifted by 500, check if accumulation worked.
1143    * Rate convert to twice the speed which means scaling down
1144    * all positions by 2.0 in this segment.
1145    * Then time argument is not used at all here.
1146    ***********************************************************/
1147   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 1.0,
1148       GST_FORMAT_TIME, 0, 200, 500);
1149
1150   /* normal speed gives elapsed of 200 */
1151   fail_unless (segment.accum == 200);
1152
1153   /* invalid time gives invalid result */
1154   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1155   fail_unless (result == -1);
1156
1157   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1158   fail_unless (result == 200);
1159
1160   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1161   fail_unless (result == 250);
1162
1163   /* outside of the segment */
1164   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1165   fail_unless (result == -1);
1166
1167   /********************************************
1168    * time offset by 500
1169    * applied rate is not used for running time
1170    ********************************************/
1171   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 2.0,
1172       GST_FORMAT_TIME, 500, 700, 0);
1173
1174   /* previous segment played at double speed gives elapsed time of
1175    * 100 added to previous accum of 200 gives 300. */
1176   fail_unless (segment.accum == 300);
1177
1178   /* invalid time gives invalid result */
1179   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1180   fail_unless (result == -1);
1181
1182   /* before segment is invalid */
1183   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1184   fail_unless (result == -1);
1185
1186   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1187   fail_unless (result == 300);
1188
1189   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1190   fail_unless (result == 400);
1191
1192   /* outside of the segment */
1193   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1194   fail_unless (result == -1);
1195
1196   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1197   fail_unless (result == -1);
1198
1199   /**********************************************************
1200    * time offset by 500, shifted by 200
1201    * Negative rate makes the running time go backwards 
1202    * relative to the segment stop position. again time
1203    * is ignored.
1204    **********************************************************/
1205   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
1206       GST_FORMAT_TIME, 500, 700, 200);
1207
1208   fail_unless (segment.accum == 500);
1209
1210   /* invalid time gives invalid result */
1211   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1212   fail_unless (result == -1);
1213
1214   /* before segment is invalid */
1215   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1216   fail_unless (result == -1);
1217
1218   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1219   fail_unless (result == 700);
1220
1221   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1222   fail_unless (result == 600);
1223
1224   /* outside of the segment */
1225   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1226   fail_unless (result == -1);
1227
1228   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1229   fail_unless (result == -1);
1230
1231   /**********************************************************
1232    * time offset by 500, shifted by 200
1233    * Negative rate makes the running time go backwards at
1234    * twice speed relative to the segment stop position. again 
1235    * time is ignored.
1236    **********************************************************/
1237   gst_segment_set_newsegment_full (&segment, FALSE, -2.0, -2.0,
1238       GST_FORMAT_TIME, 500, 700, 200);
1239
1240   fail_unless (segment.accum == 700);
1241
1242   /* invalid time gives invalid result */
1243   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1244   fail_unless (result == -1);
1245
1246   /* before segment is invalid */
1247   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1248   fail_unless (result == -1);
1249
1250   /* total scaled segment time is 100, accum is 700, so we get 800 */
1251   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1252   fail_unless (result == 800);
1253
1254   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1255   fail_unless (result == 750);
1256
1257   /* outside of the segment */
1258   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1259   fail_unless (result == -1);
1260
1261   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1262   fail_unless (result == -1);
1263
1264   /* see if negative rate closed segment correctly */
1265   gst_segment_set_newsegment_full (&segment, FALSE, -2.0, -1.0,
1266       GST_FORMAT_TIME, 500, 700, 200);
1267
1268   /* previous segment lasted 100, and was at 700 so we should get 800 */
1269   fail_unless (segment.accum == 800);
1270 }
1271
1272 GST_END_TEST;
1273
1274 Suite *
1275 gst_segment_suite (void)
1276 {
1277   Suite *s = suite_create ("GstSegment");
1278   TCase *tc_chain = tcase_create ("segments");
1279
1280   tcase_set_timeout (tc_chain, 20);
1281
1282   suite_add_tcase (s, tc_chain);
1283   tcase_add_test (tc_chain, segment_seek_nosize);
1284   tcase_add_test (tc_chain, segment_seek_size);
1285   tcase_add_test (tc_chain, segment_newsegment_open);
1286   tcase_add_test (tc_chain, segment_newsegment_closed);
1287   tcase_add_test (tc_chain, segment_newsegment_streamtime);
1288   tcase_add_test (tc_chain, segment_newsegment_streamtime_rate);
1289   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate);
1290   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate_rate);
1291   tcase_add_test (tc_chain, segment_newsegment_runningtime);
1292
1293   return s;
1294 }
1295
1296 GST_CHECK_MAIN (gst_segment);