Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / wearable / tests / check / gst / gstsegment.c
1 /* GStreamer
2  * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
3  *               2009 Wim Taymans <wim.taymans@gmail.com>
4  *
5  * gstsegment.c: Unit test for segments
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <gst/check/gstcheck.h>
24
25 /* mess with the segment structure in the bytes format */
26 GST_START_TEST (segment_seek_nosize)
27 {
28   GstSegment segment;
29   gboolean res;
30   gint64 cstart, cstop;
31   gboolean update;
32
33   gst_segment_init (&segment, GST_FORMAT_BYTES);
34
35   /* configure segment to start 100 */
36   gst_segment_set_seek (&segment, 1.0,
37       GST_FORMAT_BYTES,
38       GST_SEEK_FLAG_NONE,
39       GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
40   fail_unless (segment.start == 100);
41   fail_unless (segment.stop == -1);
42   fail_unless (update == TRUE);
43
44   /* configure segment to stop relative, should not do anything since 
45    * size is unknown. */
46   gst_segment_set_seek (&segment, 1.0,
47       GST_FORMAT_BYTES,
48       GST_SEEK_FLAG_NONE,
49       GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
50   fail_unless (segment.start == 100);
51   fail_unless (segment.stop == -1);
52   fail_unless (update == FALSE);
53
54   /* do some clipping on the open range */
55   /* completely outside */
56   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
57   fail_unless (res == FALSE);
58
59   /* touching lower bound, still outside of the segment */
60   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
61   fail_unless (res == FALSE);
62
63   /* partially inside */
64   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
65   fail_unless (res == TRUE);
66   fail_unless (cstart == 100);
67   fail_unless (cstop == 150);
68
69   /* inside, touching lower bound */
70   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
71       100, 150, &cstart, &cstop);
72   fail_unless (res == TRUE);
73   fail_unless (cstart == 100);
74   fail_unless (cstop == 150);
75
76   /* special case, 0 duration and outside segment */
77   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 90, 90, &cstart, &cstop);
78   fail_unless (res == FALSE);
79
80   /* special case, 0 duration and touching lower bound, i.e. inside segment */
81   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
82       100, 100, &cstart, &cstop);
83   fail_unless (res == TRUE);
84   fail_unless (cstart == 100);
85   fail_unless (cstop == 100);
86
87   /* special case, 0 duration and inside the segment */
88   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
89       120, 120, &cstart, &cstop);
90   fail_unless (res == TRUE);
91   fail_unless (cstart == 120);
92   fail_unless (cstop == 120);
93
94   /* completely inside */
95   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
96       150, 200, &cstart, &cstop);
97   fail_unless (res == TRUE);
98   fail_unless (cstart == 150);
99   fail_unless (cstop == 200);
100
101   /* invalid start */
102   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
103   fail_unless (res == FALSE);
104
105   /* start outside, we don't know the stop */
106   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
107   fail_unless (res == TRUE);
108   fail_unless (cstart == 100);
109   fail_unless (cstop == -1);
110
111   /* start on lower bound */
112   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
113   fail_unless (res == TRUE);
114   fail_unless (cstart == 100);
115   fail_unless (cstop == -1);
116
117   /* start inside */
118   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
119   fail_unless (res == TRUE);
120   fail_unless (cstart == 150);
121   fail_unless (cstop == -1);
122
123   /* add 100 to start, set stop to 300 */
124   gst_segment_set_seek (&segment, 1.0,
125       GST_FORMAT_BYTES,
126       GST_SEEK_FLAG_NONE,
127       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300, &update);
128   fail_unless (segment.start == 200);
129   fail_unless (segment.stop == 300);
130   fail_unless (update == TRUE);
131
132   update = FALSE;
133   /* add 100 to start (to 300), set stop to 200, this is not allowed. 
134    * nothing should be updated in the segment. A g_warning is
135    * emited. */
136   ASSERT_CRITICAL (gst_segment_set_seek (&segment, 1.0,
137           GST_FORMAT_BYTES,
138           GST_SEEK_FLAG_NONE,
139           GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update));
140   fail_unless (segment.start == 200);
141   fail_unless (segment.stop == 300);
142   /* update didn't change */
143   fail_unless (update == FALSE);
144
145   update = TRUE;
146   /* seek relative to end, should not do anything since size is
147    * unknown. */
148   gst_segment_set_seek (&segment, 1.0,
149       GST_FORMAT_BYTES,
150       GST_SEEK_FLAG_NONE,
151       GST_SEEK_TYPE_END, -300, GST_SEEK_TYPE_END, -100, &update);
152   fail_unless (segment.start == 200);
153   fail_unless (segment.stop == 300);
154   fail_unless (update == FALSE);
155
156   /* completely outside */
157   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
158   fail_unless (res == FALSE);
159
160   /* touching lower bound */
161   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 200, &cstart, &cstop);
162   fail_unless (res == FALSE);
163
164   /* partially inside */
165   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 250, &cstart, &cstop);
166   fail_unless (res == TRUE);
167   fail_unless (cstart == 200);
168   fail_unless (cstop == 250);
169
170   /* inside, touching lower bound */
171   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
172       200, 250, &cstart, &cstop);
173   fail_unless (res == TRUE);
174   fail_unless (cstart == 200);
175   fail_unless (cstop == 250);
176
177   /* completely inside */
178   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
179       250, 290, &cstart, &cstop);
180   fail_unless (res == TRUE);
181   fail_unless (cstart == 250);
182   fail_unless (cstop == 290);
183
184   /* partially inside */
185   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
186       250, 350, &cstart, &cstop);
187   fail_unless (res == TRUE);
188   fail_unless (cstart == 250);
189   fail_unless (cstop == 300);
190
191   /* invalid start */
192   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
193   fail_unless (res == FALSE);
194
195   /* start outside */
196   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
197   fail_unless (res == TRUE);
198   fail_unless (cstart == 200);
199   fail_unless (cstop == 300);
200
201   /* start on lower bound */
202   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
203   fail_unless (res == TRUE);
204   fail_unless (cstart == 200);
205   fail_unless (cstop == 300);
206
207   /* start inside */
208   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
209   fail_unless (res == TRUE);
210   fail_unless (cstart == 250);
211   fail_unless (cstop == 300);
212
213   /* start outside on boundary */
214   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 300, -1, &cstart, &cstop);
215   fail_unless (res == FALSE);
216
217   /* start completely outside */
218   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 350, -1, &cstart, &cstop);
219   fail_unless (res == FALSE);
220 }
221
222 GST_END_TEST;
223
224 /* mess with the segment structure in the bytes format */
225 GST_START_TEST (segment_seek_size)
226 {
227   GstSegment segment;
228   gboolean res;
229   gint64 cstart, cstop;
230   gboolean update;
231
232   gst_segment_init (&segment, GST_FORMAT_BYTES);
233   gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
234
235   /* configure segment to start 100 */
236   gst_segment_set_seek (&segment, 1.0,
237       GST_FORMAT_BYTES,
238       GST_SEEK_FLAG_NONE,
239       GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
240   fail_unless (segment.start == 100);
241   fail_unless (segment.stop == -1);
242   fail_unless (update == TRUE);
243
244   /* configure segment to stop relative, does not update stop
245    * since we did not set it before. */
246   gst_segment_set_seek (&segment, 1.0,
247       GST_FORMAT_BYTES,
248       GST_SEEK_FLAG_NONE,
249       GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
250   fail_unless (segment.start == 100);
251   fail_unless (segment.stop == -1);
252   fail_unless (update == FALSE);
253
254   /* do some clipping on the open range */
255   /* completely outside */
256   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
257   fail_unless (res == FALSE);
258
259   /* touching lower bound */
260   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
261   fail_unless (res == FALSE);
262
263   /* partially inside */
264   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
265   fail_unless (res == TRUE);
266   fail_unless (cstart == 100);
267   fail_unless (cstop == 150);
268
269   /* inside, touching lower bound */
270   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
271       100, 150, &cstart, &cstop);
272   fail_unless (res == TRUE);
273   fail_unless (cstart == 100);
274   fail_unless (cstop == 150);
275
276   /* completely inside */
277   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
278       150, 200, &cstart, &cstop);
279   fail_unless (res == TRUE);
280   fail_unless (cstart == 150);
281   fail_unless (cstop == 200);
282
283   /* partially inside, clip to size */
284   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
285       150, 300, &cstart, &cstop);
286   fail_unless (res == TRUE);
287   fail_unless (cstart == 150);
288   fail_unless (cstop == 200);
289
290   /* invalid start */
291   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
292   fail_unless (res == FALSE);
293
294   /* start outside */
295   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
296   fail_unless (res == TRUE);
297   fail_unless (cstart == 100);
298   fail_unless (cstop == -1);
299
300   /* start on lower bound */
301   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
302   fail_unless (res == TRUE);
303   fail_unless (cstart == 100);
304   fail_unless (cstop == -1);
305
306   /* start inside */
307   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
308   fail_unless (res == TRUE);
309   fail_unless (cstart == 150);
310   fail_unless (cstop == -1);
311
312   /* add 100 to start, set stop to 300, stop clips to 200 */
313   gst_segment_set_seek (&segment, 1.0,
314       GST_FORMAT_BYTES,
315       GST_SEEK_FLAG_NONE,
316       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300, &update);
317   fail_unless (segment.start == 200);
318   fail_unless (segment.stop == 200);
319
320   /* add 100 to start (to 300), set stop to 200, this clips start
321    * to duration */
322   gst_segment_set_seek (&segment, 1.0,
323       GST_FORMAT_BYTES,
324       GST_SEEK_FLAG_NONE,
325       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update);
326   fail_unless (segment.start == 200);
327   fail_unless (segment.stop == 200);
328   fail_unless (update == FALSE);
329
330   /* seek relative to end */
331   gst_segment_set_seek (&segment, 1.0,
332       GST_FORMAT_BYTES,
333       GST_SEEK_FLAG_NONE,
334       GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20, &update);
335   fail_unless (segment.start == 100);
336   fail_unless (segment.stop == 180);
337   fail_unless (update == TRUE);
338
339   /* completely outside */
340   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
341   fail_unless (res == FALSE);
342
343   /* touching lower bound */
344   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
345   fail_unless (res == FALSE);
346
347   /* partially inside */
348   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
349   fail_unless (res == TRUE);
350   fail_unless (cstart == 100);
351   fail_unless (cstop == 150);
352
353   /* inside, touching lower bound */
354   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
355       100, 150, &cstart, &cstop);
356   fail_unless (res == TRUE);
357   fail_unless (cstart == 100);
358   fail_unless (cstop == 150);
359
360   /* completely inside */
361   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
362       150, 170, &cstart, &cstop);
363   fail_unless (res == TRUE);
364   fail_unless (cstart == 150);
365   fail_unless (cstop == 170);
366
367   /* partially inside */
368   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
369       150, 250, &cstart, &cstop);
370   fail_unless (res == TRUE);
371   fail_unless (cstart == 150);
372   fail_unless (cstop == 180);
373
374   /* invalid start */
375   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
376   fail_unless (res == FALSE);
377
378   /* start outside */
379   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
380   fail_unless (res == TRUE);
381   fail_unless (cstart == 100);
382   fail_unless (cstop == 180);
383
384   /* start on lower bound */
385   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
386   fail_unless (res == TRUE);
387   fail_unless (cstart == 100);
388   fail_unless (cstop == 180);
389
390   /* start inside */
391   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
392   fail_unless (res == TRUE);
393   fail_unless (cstart == 150);
394   fail_unless (cstop == 180);
395
396   /* start outside on boundary */
397   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
398   fail_unless (res == FALSE);
399
400   /* start completely outside */
401   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
402   fail_unless (res == FALSE);
403 }
404
405 GST_END_TEST;
406
407 GST_START_TEST (segment_seek_reverse)
408 {
409   GstSegment segment;
410   gboolean update;
411
412   gst_segment_init (&segment, GST_FORMAT_BYTES);
413   gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
414
415   /* configure segment to stop 100 */
416   gst_segment_set_seek (&segment, -1.0,
417       GST_FORMAT_BYTES,
418       GST_SEEK_FLAG_NONE,
419       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 100, &update);
420   fail_unless (segment.start == 0);
421   fail_unless (segment.stop == 100);
422   fail_unless (segment.time == 0);
423   fail_unless (segment.last_stop == 100);
424   fail_unless (update == TRUE);
425
426   /* update */
427   gst_segment_set_seek (&segment, -1.0,
428       GST_FORMAT_BYTES,
429       GST_SEEK_FLAG_NONE,
430       GST_SEEK_TYPE_SET, 10, GST_SEEK_TYPE_CUR, -20, &update);
431   fail_unless (segment.start == 10);
432   fail_unless (segment.stop == 80);
433   fail_unless (segment.time == 10);
434   fail_unless (segment.last_stop == 80);
435   fail_unless (update == TRUE);
436
437   gst_segment_set_seek (&segment, -1.0,
438       GST_FORMAT_BYTES,
439       GST_SEEK_FLAG_NONE,
440       GST_SEEK_TYPE_SET, 20, GST_SEEK_TYPE_NONE, 0, &update);
441   fail_unless (segment.start == 20);
442   fail_unless (segment.stop == 80);
443   fail_unless (segment.time == 20);
444   fail_unless (segment.last_stop == 80);
445   fail_unless (update == FALSE);
446 }
447
448 GST_END_TEST;
449
450 /* mess with the segment structure in the bytes format */
451 GST_START_TEST (segment_seek_rate)
452 {
453   GstSegment segment;
454   gboolean update;
455
456   gst_segment_init (&segment, GST_FORMAT_BYTES);
457
458   /* configure segment to rate 2.0, format does not matter when we don't specify
459    * a start or stop position. */
460   gst_segment_set_seek (&segment, 2.0,
461       GST_FORMAT_UNDEFINED,
462       GST_SEEK_FLAG_NONE,
463       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1, &update);
464   fail_unless (segment.format == GST_FORMAT_BYTES);
465   fail_unless (segment.start == 0);
466   fail_unless (segment.stop == -1);
467   fail_unless (segment.rate == 2.0);
468   fail_unless (update == FALSE);
469
470   /* 0 is the same in all formats and should not fail */
471   gst_segment_set_seek (&segment, 2.0,
472       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
473       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1, &update);
474   fail_unless (segment.format == GST_FORMAT_BYTES);
475
476   /* set to -1 means start from 0 */
477   gst_segment_set_seek (&segment, 2.0,
478       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
479       GST_SEEK_TYPE_SET, -1, GST_SEEK_TYPE_NONE, -1, &update);
480   fail_unless (segment.format == GST_FORMAT_BYTES);
481   fail_unless (segment.start == 0);
482
483   gst_segment_set_seek (&segment, 2.0,
484       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
485       GST_SEEK_TYPE_CUR, 0, GST_SEEK_TYPE_NONE, -1, &update);
486
487   gst_segment_set_seek (&segment, 2.0,
488       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
489       GST_SEEK_TYPE_END, 0, GST_SEEK_TYPE_NONE, -1, &update);
490
491   /* -1 for end is fine too in all formats */
492   gst_segment_set_seek (&segment, 2.0,
493       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
494       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
495
496   /* 0 as relative end is fine too */
497   gst_segment_set_seek (&segment, 2.0,
498       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
499       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
500
501   gst_segment_set_seek (&segment, 2.0,
502       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
503       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
504
505   /* set a real stop position, this must happen in bytes */
506   gst_segment_set_seek (&segment, 3.0,
507       GST_FORMAT_BYTES,
508       GST_SEEK_FLAG_NONE,
509       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, 100, &update);
510   fail_unless (segment.format == GST_FORMAT_BYTES);
511   fail_unless (segment.start == 0);
512   fail_unless (segment.stop == 100);
513   fail_unless (segment.rate == 3.0);
514   /* no seek should happen, we just updated the stop position in forward
515    * playback mode.*/
516   fail_unless (update == FALSE);
517
518   /* 0 as relative end is fine too */
519   gst_segment_set_seek (&segment, 2.0,
520       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
521       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
522   fail_unless (segment.stop == 100);
523
524   gst_segment_set_seek (&segment, 2.0,
525       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
526       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
527   fail_unless (segment.stop == 100);
528
529   /* -1 for end is fine too in all formats */
530   gst_segment_set_seek (&segment, 2.0,
531       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
532       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
533   fail_unless (segment.stop == -1);
534
535   /* set some duration, stop -1 END seeks will now work with the
536    * duration, if the formats match */
537   gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
538   fail_unless (segment.duration == 200);
539
540   /* seek to end in any format with 0 should set the stop to the
541    * duration */
542   gst_segment_set_seek (&segment, 2.0,
543       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
544       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
545   fail_unless (segment.stop == 200);
546   fail_unless (segment.duration == 200);
547
548   /* subtract 100 from the end */
549   gst_segment_set_seek (&segment, 2.0,
550       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
551       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, -100, &update);
552   fail_unless (segment.stop == 100);
553   fail_unless (segment.duration == 200);
554
555   /* add 100 to the duration, this should be clamped to the duration */
556   gst_segment_set_seek (&segment, 2.0,
557       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
558       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 100, &update);
559   fail_unless (segment.stop == 200);
560   fail_unless (segment.duration == 200);
561
562   /* add 300 to the start, this should be clamped to the duration */
563   gst_segment_set_seek (&segment, 2.0,
564       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
565       GST_SEEK_TYPE_CUR, 300, GST_SEEK_TYPE_END, 0, &update);
566   fail_unless (segment.start == 200);
567   fail_unless (segment.stop == 200);
568   fail_unless (segment.duration == 200);
569
570   /* subtract 300 from the start, this should be clamped to 0 */
571   gst_segment_set_seek (&segment, 2.0,
572       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
573       GST_SEEK_TYPE_CUR, -300, GST_SEEK_TYPE_END, 0, &update);
574   fail_unless (segment.start == 0);
575   fail_unless (segment.stop == 200);
576   fail_unless (segment.duration == 200);
577 }
578
579 GST_END_TEST;
580
581 /* mess with the segment structure in the bytes format */
582 GST_START_TEST (segment_newsegment_open)
583 {
584   GstSegment segment;
585
586   gst_segment_init (&segment, GST_FORMAT_BYTES);
587
588   /* time should also work for starting from 0 */
589   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
590
591   fail_unless (segment.rate == 1.0);
592   fail_unless (segment.format == GST_FORMAT_BYTES);
593   fail_unless (segment.flags == 0);
594   fail_unless (segment.start == 0);
595   fail_unless (segment.stop == -1);
596   fail_unless (segment.time == 0);
597   fail_unless (segment.accum == 0);
598   fail_unless (segment.last_stop == 0);
599   fail_unless (segment.duration == -1);
600
601   /* we set stop but in the wrong format, stop stays open. */
602   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_TIME, 0, 200, 0);
603
604   fail_unless (segment.start == 0);
605   fail_unless (segment.stop == -1);
606   fail_unless (segment.time == 0);
607   fail_unless (segment.accum == 0);
608   fail_unless (segment.last_stop == 0);
609
610   /* update, nothing changes */
611   gst_segment_set_newsegment (&segment, TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
612
613   fail_unless (segment.start == 0);
614   fail_unless (segment.stop == -1);
615   fail_unless (segment.time == 0);
616   fail_unless (segment.accum == 0);
617   fail_unless (segment.last_stop == 0);
618
619   /* update */
620   gst_segment_set_newsegment (&segment, TRUE, 1.0,
621       GST_FORMAT_BYTES, 100, -1, 100);
622
623   fail_unless (segment.start == 100);
624   fail_unless (segment.stop == -1);
625   fail_unless (segment.time == 100);
626   fail_unless (segment.accum == 100);
627   fail_unless (segment.last_stop == 100);
628
629   /* last_stop 0, accum does not change */
630   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
631
632   fail_unless (segment.start == 0);
633   fail_unless (segment.stop == -1);
634   fail_unless (segment.time == 0);
635   fail_unless (segment.accum == 100);
636
637   gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 200);
638
639   fail_unless (segment.last_stop == 200);
640
641   /* last_stop 200, accum changes */
642   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
643
644   fail_unless (segment.start == 0);
645   fail_unless (segment.stop == -1);
646   fail_unless (segment.time == 0);
647   fail_unless (segment.accum == 300);
648   fail_unless (segment.last_stop == 0);
649 }
650
651 GST_END_TEST;
652
653
654 /* mess with the segment structure in the bytes format */
655 GST_START_TEST (segment_newsegment_closed)
656 {
657   GstSegment segment;
658
659   gst_segment_init (&segment, GST_FORMAT_BYTES);
660
661   gst_segment_set_newsegment (&segment, FALSE, 1.0,
662       GST_FORMAT_BYTES, 0, 200, 0);
663
664   fail_unless (segment.rate == 1.0);
665   fail_unless (segment.format == GST_FORMAT_BYTES);
666   fail_unless (segment.flags == 0);
667   fail_unless (segment.start == 0);
668   fail_unless (segment.stop == 200);
669   fail_unless (segment.time == 0);
670   fail_unless (segment.accum == 0);
671   fail_unless (segment.last_stop == 0);
672   fail_unless (segment.duration == -1);
673
674   /* assume we advanced to position 40 */
675   gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 40);
676   fail_unless (segment.last_stop == 40);
677
678   /* do an update to the start, last_stop is unchanged because it's bigger */
679   gst_segment_set_newsegment (&segment, TRUE, 1.0, GST_FORMAT_BYTES, 20, 200,
680       20);
681
682   fail_unless (segment.start == 20);
683   fail_unless (segment.stop == 200);
684   fail_unless (segment.time == 20);
685   fail_unless (segment.accum == 20);
686   fail_unless (segment.last_stop == 40);
687
688   /* do an update past our last_stop, it should be updated now */
689   gst_segment_set_newsegment (&segment, TRUE, 1.0, GST_FORMAT_BYTES, 50, 300,
690       50);
691
692   fail_unless (segment.start == 50);
693   fail_unless (segment.stop == 300);
694   fail_unless (segment.time == 50);
695   fail_unless (segment.accum == 50);
696   fail_unless (segment.last_stop == 50);
697
698   /* and a new accumulated one */
699   gst_segment_set_newsegment (&segment, FALSE, 1.0,
700       GST_FORMAT_BYTES, 100, 400, 300);
701
702   fail_unless (segment.start == 100);
703   fail_unless (segment.stop == 400);
704   fail_unless (segment.time == 300);
705   fail_unless (segment.accum == 300);
706
707   /* and a new updated one */
708   gst_segment_set_newsegment (&segment, TRUE, 1.0,
709       GST_FORMAT_BYTES, 100, 500, 300);
710
711   fail_unless (segment.start == 100);
712   fail_unless (segment.stop == 500);
713   fail_unless (segment.time == 300);
714   fail_unless (segment.accum == 300);
715
716   /* and a new partially updated one */
717   gst_segment_set_newsegment (&segment, TRUE, 1.0,
718       GST_FORMAT_BYTES, 200, 500, 400);
719
720   fail_unless (segment.start == 200);
721   fail_unless (segment.stop == 500);
722   fail_unless (segment.time == 400);
723   fail_unless (segment.accum == 400);
724 }
725
726 GST_END_TEST;
727
728 /* mess with the segment structure in the time format */
729 GST_START_TEST (segment_newsegment_streamtime)
730 {
731   GstSegment segment;
732   gint64 result;
733
734   gst_segment_init (&segment, GST_FORMAT_TIME);
735
736   /***************************
737    * Normal segment
738    ***************************/
739   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
740       GST_FORMAT_TIME, 0, 200, 0);
741
742   fail_unless (segment.rate == 1.0);
743   fail_unless (segment.applied_rate == 1.0);
744   fail_unless (segment.format == GST_FORMAT_TIME);
745   fail_unless (segment.flags == 0);
746   fail_unless (segment.start == 0);
747   fail_unless (segment.stop == 200);
748   fail_unless (segment.time == 0);
749   fail_unless (segment.accum == 0);
750   fail_unless (segment.last_stop == 0);
751   fail_unless (segment.duration == -1);
752
753   /* invalid time gives invalid result */
754   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
755   fail_unless (result == -1);
756
757   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
758   fail_unless (result == 0);
759
760   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
761   fail_unless (result == 100);
762
763   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
764   fail_unless (result == 200);
765
766   /* outside of the segment */
767   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
768   fail_unless (result == -1);
769
770   /*********************
771    * time shifted by 500
772    *********************/
773   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
774       GST_FORMAT_TIME, 0, 200, 500);
775
776   fail_unless (segment.accum == 200);
777
778   /* invalid time gives invalid result */
779   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
780   fail_unless (result == -1);
781
782   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
783   fail_unless (result == 500);
784
785   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
786   fail_unless (result == 600);
787
788   /* outside of the segment */
789   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
790   fail_unless (result == -1);
791
792   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
793   fail_unless (result == -1);
794
795   /*********************
796    * time offset by 500
797    *********************/
798   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
799       GST_FORMAT_TIME, 500, 700, 0);
800
801   fail_unless (segment.accum == 400);
802
803   /* invalid time gives invalid result */
804   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
805   fail_unless (result == -1);
806
807   /* before segment is invalid */
808   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
809   fail_unless (result == -1);
810
811   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
812   fail_unless (result == 0);
813
814   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
815   fail_unless (result == 100);
816
817   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
818   fail_unless (result == 200);
819
820   /* outside of the segment */
821   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
822   fail_unless (result == -1);
823
824   /*************************************
825    * time offset by 500, shifted by 200
826    *************************************/
827   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
828       GST_FORMAT_TIME, 500, 700, 200);
829
830   fail_unless (segment.accum == 600);
831
832   /* invalid time gives invalid result */
833   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
834   fail_unless (result == -1);
835
836   /* before segment is invalid */
837   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
838   fail_unless (result == -1);
839
840   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
841   fail_unless (result == 200);
842
843   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
844   fail_unless (result == 300);
845
846   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
847   fail_unless (result == 400);
848
849   /* outside of the segment */
850   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
851   fail_unless (result == -1);
852 }
853
854 GST_END_TEST;
855
856 /* mess with the segment structure in the time format */
857 GST_START_TEST (segment_newsegment_streamtime_rate)
858 {
859   GstSegment segment;
860   gint64 result;
861
862   gst_segment_init (&segment, GST_FORMAT_TIME);
863
864   /***************************
865    * Normal segment rate 2.0
866    ***************************/
867   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 1.0,
868       GST_FORMAT_TIME, 0, 200, 0);
869
870   fail_unless (segment.rate == 2.0);
871   fail_unless (segment.applied_rate == 1.0);
872   fail_unless (segment.format == GST_FORMAT_TIME);
873   fail_unless (segment.flags == 0);
874   fail_unless (segment.start == 0);
875   fail_unless (segment.stop == 200);
876   fail_unless (segment.time == 0);
877   fail_unless (segment.accum == 0);
878   fail_unless (segment.last_stop == 0);
879   fail_unless (segment.duration == -1);
880
881   /* invalid time gives invalid result */
882   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
883   fail_unless (result == -1);
884
885   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
886   fail_unless (result == 0);
887
888   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
889   fail_unless (result == 100);
890
891   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
892   fail_unless (result == 150);
893
894   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
895   fail_unless (result == 200);
896
897   /* outside of the segment */
898   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
899   fail_unless (result == -1);
900
901   /***************************************
902    * Normal segment rate 2.0, offset
903    ***************************************/
904   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 1.0,
905       GST_FORMAT_TIME, 100, 300, 0);
906
907   fail_unless (segment.accum == 100);
908
909   /* invalid time gives invalid result */
910   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
911   fail_unless (result == -1);
912
913   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
914   fail_unless (result == 0);
915
916   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
917   fail_unless (result == 100);
918
919   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 250);
920   fail_unless (result == 150);
921
922   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
923   fail_unless (result == 200);
924
925   /* outside of the segment */
926   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
927   fail_unless (result == -1);
928
929   /***************************************
930    * Normal segment rate -1.0, offset
931    ***************************************/
932
933   /* buffers will arrive from 300 to 100 in a sink, stream time
934    * calculation is unaffected by the rate */
935   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
936       GST_FORMAT_TIME, 100, 300, 0);
937
938   fail_unless (segment.accum == 200);
939
940   /* invalid time gives invalid result */
941   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
942   fail_unless (result == -1);
943
944   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
945   fail_unless (result == 0);
946
947   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
948   fail_unless (result == 100);
949
950   /***********************************************
951    * Normal segment rate -1.0, offset, time = 200
952    ***********************************************/
953   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
954       GST_FORMAT_TIME, 100, 300, 200);
955
956   /* invalid time gives invalid result */
957   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
958   fail_unless (result == -1);
959
960   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
961   fail_unless (result == 200);
962
963   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
964   fail_unless (result == 300);
965
966   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
967   fail_unless (result == 400);
968
969   /* outside of the segment */
970   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
971   fail_unless (result == -1);
972 }
973
974 GST_END_TEST;
975
976 /* mess with the segment structure in the time format */
977 GST_START_TEST (segment_newsegment_streamtime_applied_rate)
978 {
979   GstSegment segment;
980   gint64 result;
981
982   gst_segment_init (&segment, GST_FORMAT_TIME);
983
984   /***********************************************************
985    * Normal segment rate 1.0, applied rate -1.0
986    * This means the timestamps represents a stream going backwards
987    * starting from @time to 0.
988    ************************************************************/
989   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, -1.0,
990       GST_FORMAT_TIME, 0, 200, 200);
991
992   fail_unless (segment.rate == 1.0);
993   fail_unless (segment.applied_rate == -1.0);
994   fail_unless (segment.format == GST_FORMAT_TIME);
995   fail_unless (segment.flags == 0);
996   fail_unless (segment.start == 0);
997   fail_unless (segment.stop == 200);
998   fail_unless (segment.time == 200);
999   fail_unless (segment.accum == 0);
1000   fail_unless (segment.last_stop == 0);
1001   fail_unless (segment.duration == -1);
1002
1003   /* invalid time gives invalid result */
1004   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1005   fail_unless (result == -1);
1006
1007   /* we count backwards from 200 */
1008   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1009   fail_unless (result == 200);
1010
1011   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1012   fail_unless (result == 100);
1013
1014   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1015   fail_unless (result == 50);
1016
1017   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1018   fail_unless (result == 0);
1019
1020   /* outside of the segment */
1021   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1022   fail_unless (result == -1);
1023
1024   /***********************************************************
1025    * Normal segment rate 1.0, applied rate 2.0
1026    * This means the timestamps represents a stream at twice the
1027    * normal rate
1028    ************************************************************/
1029   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 2.0,
1030       GST_FORMAT_TIME, 0, 200, 0);
1031
1032   fail_unless (segment.rate == 1.0);
1033   fail_unless (segment.applied_rate == 2.0);
1034   fail_unless (segment.format == GST_FORMAT_TIME);
1035   fail_unless (segment.flags == 0);
1036   fail_unless (segment.start == 0);
1037   fail_unless (segment.stop == 200);
1038   fail_unless (segment.time == 0);
1039   fail_unless (segment.accum == 200);
1040   fail_unless (segment.last_stop == 0);
1041   fail_unless (segment.duration == -1);
1042
1043   /* invalid time gives invalid result */
1044   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1045   fail_unless (result == -1);
1046
1047   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1048   fail_unless (result == 0);
1049
1050   /* the stream prepresents a stream going twice as fast, the position 
1051    * in the segment is therefore scaled by the applied rate */
1052   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1053   fail_unless (result == 200);
1054
1055   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1056   fail_unless (result == 300);
1057
1058   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1059   fail_unless (result == 400);
1060
1061   /* outside of the segment */
1062   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1063   fail_unless (result == -1);
1064
1065   /***********************************************************
1066    * Normal segment rate 1.0, applied rate -2.0
1067    * This means the timestamps represents a stream at twice the
1068    * reverse rate
1069    ************************************************************/
1070   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, -2.0,
1071       GST_FORMAT_TIME, 0, 200, 400);
1072
1073   fail_unless (segment.rate == 1.0);
1074   fail_unless (segment.applied_rate == -2.0);
1075   fail_unless (segment.format == GST_FORMAT_TIME);
1076   fail_unless (segment.flags == 0);
1077   fail_unless (segment.start == 0);
1078   fail_unless (segment.stop == 200);
1079   fail_unless (segment.time == 400);
1080   /* previous segment lasted 200, rate of 2.0 was already applied */
1081   fail_unless (segment.accum == 400);
1082   fail_unless (segment.last_stop == 0);
1083   fail_unless (segment.duration == -1);
1084
1085   /* invalid time gives invalid result */
1086   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1087   fail_unless (result == -1);
1088
1089   /* we count backwards from 400 */
1090   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1091   fail_unless (result == 400);
1092
1093   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1094   fail_unless (result == 200);
1095
1096   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1097   fail_unless (result == 100);
1098
1099   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1100   fail_unless (result == 0);
1101
1102   /* outside of the segment */
1103   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1104   fail_unless (result == -1);
1105
1106   /***********************************************************
1107    * Normal segment rate 1.0, applied rate -2.0
1108    * This means the timestamps represents a stream at twice the
1109    * reverse rate, start time cannot compensate the complete
1110    * duration of the segment so we stop at 0
1111    ************************************************************/
1112   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, -2.0,
1113       GST_FORMAT_TIME, 0, 200, 200);
1114
1115   fail_unless (segment.rate == 1.0);
1116   fail_unless (segment.applied_rate == -2.0);
1117   fail_unless (segment.format == GST_FORMAT_TIME);
1118   fail_unless (segment.flags == 0);
1119   fail_unless (segment.start == 0);
1120   fail_unless (segment.stop == 200);
1121   fail_unless (segment.time == 200);
1122   fail_unless (segment.accum == 600);
1123   fail_unless (segment.last_stop == 0);
1124   fail_unless (segment.duration == -1);
1125
1126   /* invalid time gives invalid result */
1127   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1128   fail_unless (result == -1);
1129
1130   /* we count backwards from 200 */
1131   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1132   fail_unless (result == 200);
1133
1134   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1135   fail_unless (result == 0);
1136
1137   /* clamp at 0 */
1138   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1139   fail_unless (result == 0);
1140
1141   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1142   fail_unless (result == 0);
1143
1144   /* outside of the segment */
1145   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1146   fail_unless (result == -1);
1147 }
1148
1149 GST_END_TEST;
1150
1151 /* mess with the segment structure in the time format */
1152 GST_START_TEST (segment_newsegment_streamtime_applied_rate_rate)
1153 {
1154   GstSegment segment;
1155   gint64 result;
1156
1157   gst_segment_init (&segment, GST_FORMAT_TIME);
1158
1159   /***********************************************************
1160    * Segment rate 2.0, applied rate 2.0
1161    * this means we have a double speed stream that we should
1162    * speed up by a factor of 2.0 some more. the resulting
1163    * stream will be played at four times the speed. 
1164    ************************************************************/
1165   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 2.0,
1166       GST_FORMAT_TIME, 0, 200, 0);
1167
1168   fail_unless (segment.rate == 2.0);
1169   fail_unless (segment.applied_rate == 2.0);
1170   fail_unless (segment.format == GST_FORMAT_TIME);
1171   fail_unless (segment.flags == 0);
1172   fail_unless (segment.start == 0);
1173   fail_unless (segment.stop == 200);
1174   fail_unless (segment.time == 0);
1175   fail_unless (segment.accum == 0);
1176   fail_unless (segment.last_stop == 0);
1177   fail_unless (segment.duration == -1);
1178
1179   /* invalid time gives invalid result */
1180   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1181   fail_unless (result == -1);
1182
1183   /* only applied rate affects our calculation of the stream time */
1184   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1185   fail_unless (result == 0);
1186
1187   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1188   fail_unless (result == 200);
1189
1190   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1191   fail_unless (result == 300);
1192
1193   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1194   fail_unless (result == 400);
1195
1196   /* outside of the segment */
1197   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1198   fail_unless (result == -1);
1199
1200   /***********************************************************
1201    * Segment rate 2.0, applied rate -1.0
1202    * this means we have a reverse stream that we should
1203    * speed up by a factor of 2.0
1204    ************************************************************/
1205   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, -1.0,
1206       GST_FORMAT_TIME, 0, 200, 200);
1207
1208   fail_unless (segment.rate == 2.0);
1209   fail_unless (segment.applied_rate == -1.0);
1210   fail_unless (segment.format == GST_FORMAT_TIME);
1211   fail_unless (segment.flags == 0);
1212   fail_unless (segment.start == 0);
1213   fail_unless (segment.stop == 200);
1214   fail_unless (segment.time == 200);
1215   /* previous segment lasted 100 */
1216   fail_unless (segment.accum == 100);
1217   fail_unless (segment.last_stop == 0);
1218   fail_unless (segment.duration == -1);
1219
1220   /* invalid time gives invalid result */
1221   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1222   fail_unless (result == -1);
1223
1224   /* only applied rate affects our calculation of the stream time */
1225   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1226   fail_unless (result == 200);
1227
1228   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1229   fail_unless (result == 100);
1230
1231   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1232   fail_unless (result == 50);
1233
1234   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1235   fail_unless (result == 0);
1236
1237   /* outside of the segment */
1238   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1239   fail_unless (result == -1);
1240
1241   /***********************************************************
1242    * Segment rate -1.0, applied rate -1.0
1243    * this means we have a reverse stream that we should
1244    * reverse to get the normal stream again.
1245    ************************************************************/
1246   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, -1.0,
1247       GST_FORMAT_TIME, 0, 200, 200);
1248
1249   fail_unless (segment.rate == -1.0);
1250   fail_unless (segment.applied_rate == -1.0);
1251   fail_unless (segment.format == GST_FORMAT_TIME);
1252   fail_unless (segment.flags == 0);
1253   fail_unless (segment.start == 0);
1254   fail_unless (segment.stop == 200);
1255   fail_unless (segment.time == 200);
1256   /* accumulated 100 of previous segment to make 200 */
1257   fail_unless (segment.accum == 200);
1258   fail_unless (segment.last_stop == 200);
1259   fail_unless (segment.duration == -1);
1260
1261   /* invalid time gives invalid result */
1262   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1263   fail_unless (result == -1);
1264
1265   /* only applied rate affects our calculation of the stream time */
1266   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1267   fail_unless (result == 200);
1268
1269   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1270   fail_unless (result == 100);
1271
1272   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1273   fail_unless (result == 50);
1274
1275   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1276   fail_unless (result == 0);
1277
1278   /* outside of the segment */
1279   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1280   fail_unless (result == -1);
1281
1282   /***********************************************************
1283    * Segment rate -1.0, applied rate -1.0
1284    * this means we have a reverse stream that we should
1285    * reverse to get the normal stream again.
1286    ************************************************************/
1287   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 2.0,
1288       GST_FORMAT_TIME, 0, 200, 0);
1289
1290   fail_unless (segment.rate == -1.0);
1291   fail_unless (segment.applied_rate == 2.0);
1292   fail_unless (segment.format == GST_FORMAT_TIME);
1293   fail_unless (segment.flags == 0);
1294   fail_unless (segment.start == 0);
1295   fail_unless (segment.stop == 200);
1296   fail_unless (segment.time == 0);
1297   fail_unless (segment.accum == 400);
1298   fail_unless (segment.last_stop == 200);
1299   fail_unless (segment.duration == -1);
1300
1301   /* invalid time gives invalid result */
1302   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1303   fail_unless (result == -1);
1304
1305   /* only applied rate affects our calculation of the stream time */
1306   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1307   fail_unless (result == 0);
1308
1309   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1310   fail_unless (result == 200);
1311
1312   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1313   fail_unless (result == 300);
1314
1315   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1316   fail_unless (result == 400);
1317
1318   /* outside of the segment */
1319   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1320   fail_unless (result == -1);
1321 }
1322
1323 GST_END_TEST;
1324
1325 /* mess with the segment structure in the time format */
1326 GST_START_TEST (segment_newsegment_runningtime)
1327 {
1328   GstSegment segment;
1329   gint64 result;
1330
1331   gst_segment_init (&segment, GST_FORMAT_TIME);
1332
1333   /***************************
1334    * Normal segment
1335    ***************************/
1336   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
1337       GST_FORMAT_TIME, 0, 200, 0);
1338
1339   fail_unless (segment.rate == 1.0);
1340   fail_unless (segment.applied_rate == 1.0);
1341   fail_unless (segment.format == GST_FORMAT_TIME);
1342   fail_unless (segment.flags == 0);
1343   fail_unless (segment.start == 0);
1344   fail_unless (segment.stop == 200);
1345   fail_unless (segment.time == 0);
1346   fail_unless (segment.accum == 0);
1347   fail_unless (segment.last_stop == 0);
1348   fail_unless (segment.duration == -1);
1349
1350   /* invalid time gives invalid result */
1351   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1352   fail_unless (result == -1);
1353
1354   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1355   fail_unless (result == 0);
1356   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1357   fail_unless (result == 0);
1358
1359   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1360   fail_unless (result == 100);
1361   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1362   fail_unless (result == 100);
1363
1364   /* at edge is exactly the segment duration */
1365   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1366   fail_unless (result == 200);
1367   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1368   fail_unless (result == 200);
1369
1370   /* outside of the segment */
1371   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 300);
1372   fail_unless (result == -1);
1373   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 300);
1374   fail_unless (result == -1);
1375
1376   /***********************************************************
1377    * time shifted by 500, check if accumulation worked.
1378    * Rate convert to twice the speed which means scaling down
1379    * all positions by 2.0 in this segment.
1380    * Then time argument is not used at all here.
1381    ***********************************************************/
1382   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 1.0,
1383       GST_FORMAT_TIME, 0, 200, 500);
1384
1385   /* normal speed gives elapsed of 200 */
1386   fail_unless (segment.accum == 200);
1387
1388   /* invalid time gives invalid result */
1389   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1390   fail_unless (result == -1);
1391
1392   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1393   fail_unless (result == 200);
1394   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1395   fail_unless (result == 0);
1396
1397   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1398   fail_unless (result == 250);
1399   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1400   fail_unless (result == 100);
1401
1402   /* outside of the segment */
1403   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1404   fail_unless (result == -1);
1405   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 310);
1406   fail_unless (result == -1);
1407
1408   /********************************************
1409    * time offset by 500
1410    * applied rate is not used for running time
1411    ********************************************/
1412   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 2.0,
1413       GST_FORMAT_TIME, 500, 700, 0);
1414
1415   /* previous segment played at double speed gives elapsed time of
1416    * 100 added to previous accum of 200 gives 300. */
1417   fail_unless (segment.accum == 300);
1418
1419   /* invalid time gives invalid result */
1420   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1421   fail_unless (result == -1);
1422
1423   /* before segment is invalid */
1424   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1425   fail_unless (result == -1);
1426   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 200);
1427   fail_unless (result == -1);
1428
1429   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1430   fail_unless (result == 300);
1431   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1432   fail_unless (result == 500);
1433
1434   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1435   fail_unless (result == 400);
1436   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1437   fail_unless (result == 600);
1438
1439   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1440   fail_unless (result == 500);
1441   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1442   fail_unless (result == 700);
1443
1444   /* outside of the segment */
1445   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1446   fail_unless (result == -1);
1447   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1448   fail_unless (result == -1);
1449
1450   /**********************************************************
1451    * time offset by 500, shifted by 200
1452    * Negative rate makes the running time go backwards 
1453    * relative to the segment stop position. again time
1454    * is ignored.
1455    **********************************************************/
1456   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
1457       GST_FORMAT_TIME, 500, 700, 200);
1458
1459   fail_unless (segment.accum == 500);
1460
1461   /* invalid time gives invalid result */
1462   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1463   fail_unless (result == -1);
1464
1465   /* before segment is invalid */
1466   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1467   fail_unless (result == -1);
1468   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 400);
1469   fail_unless (result == -1);
1470
1471   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1472   fail_unless (result == 700);
1473   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1474   fail_unless (result == 500);
1475
1476   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1477   fail_unless (result == 600);
1478   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1479   fail_unless (result == 600);
1480
1481   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1482   fail_unless (result == 500);
1483   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1484   fail_unless (result == 700);
1485
1486   /* outside of the segment */
1487   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1488   fail_unless (result == -1);
1489   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1490   fail_unless (result == -1);
1491
1492   /**********************************************************
1493    * time offset by 500, shifted by 200
1494    * Negative rate makes the running time go backwards at
1495    * twice speed relative to the segment stop position. again 
1496    * time is ignored.
1497    **********************************************************/
1498   gst_segment_set_newsegment_full (&segment, FALSE, -2.0, -2.0,
1499       GST_FORMAT_TIME, 500, 700, 200);
1500
1501   fail_unless (segment.accum == 700);
1502
1503   /* invalid time gives invalid result */
1504   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1505   fail_unless (result == -1);
1506
1507   /* before segment is invalid */
1508   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1509   fail_unless (result == -1);
1510   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1511   fail_unless (result == -1);
1512
1513   /* total scaled segment time is 100, accum is 700, so we get 800 */
1514   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1515   fail_unless (result == 800);
1516   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1517   fail_unless (result == 500);
1518
1519   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1520   fail_unless (result == 750);
1521   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1522   fail_unless (result == 600);
1523
1524   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1525   fail_unless (result == 700);
1526   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1527   fail_unless (result == 700);
1528
1529   /* outside of the segment */
1530   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1531   fail_unless (result == -1);
1532   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 900);
1533   fail_unless (result == -1);
1534
1535   /* see if negative rate closed segment correctly */
1536   gst_segment_set_newsegment_full (&segment, FALSE, -2.0, -1.0,
1537       GST_FORMAT_TIME, 500, 700, 200);
1538
1539   /* previous segment lasted 100, and was at 700 so we should get 800 */
1540   fail_unless (segment.accum == 800);
1541   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1542   fail_unless (result == 700);
1543 }
1544
1545 GST_END_TEST;
1546
1547 /* mess with the segment structure in the time format */
1548 GST_START_TEST (segment_newsegment_accum)
1549 {
1550   GstSegment segment;
1551   gint64 result;
1552
1553   gst_segment_init (&segment, GST_FORMAT_TIME);
1554
1555   /***************************
1556    * Normal reverse segment
1557    ***************************/
1558   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
1559       GST_FORMAT_TIME, 0, 200, 0);
1560
1561   fail_unless (segment.rate == -1.0);
1562   fail_unless (segment.applied_rate == 1.0);
1563   fail_unless (segment.format == GST_FORMAT_TIME);
1564   fail_unless (segment.flags == 0);
1565   fail_unless (segment.start == 0);
1566   fail_unless (segment.stop == 200);
1567   fail_unless (segment.time == 0);
1568   fail_unless (segment.accum == 0);
1569   fail_unless (segment.last_stop == 200);
1570   fail_unless (segment.duration == -1);
1571
1572   /* invalid time gives invalid result */
1573   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1574   fail_unless (result == -1);
1575
1576   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1577   fail_unless (result == 0);
1578   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1579   fail_unless (result == 200);
1580
1581   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1582   fail_unless (result == 50);
1583   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1584   fail_unless (result == 150);
1585
1586   /* update segment, this accumulates 50 from the previous segment. */
1587   gst_segment_set_newsegment_full (&segment, TRUE, -2.0, 1.0,
1588       GST_FORMAT_TIME, 0, 150, 0);
1589
1590   fail_unless (segment.rate == -2.0);
1591   fail_unless (segment.applied_rate == 1.0);
1592   fail_unless (segment.format == GST_FORMAT_TIME);
1593   fail_unless (segment.flags == 0);
1594   fail_unless (segment.start == 0);
1595   fail_unless (segment.stop == 150);
1596   fail_unless (segment.time == 0);
1597   fail_unless (segment.accum == 50);
1598   fail_unless (segment.last_stop == 150);
1599   fail_unless (segment.duration == -1);
1600
1601   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1602   fail_unless (result == 50);
1603   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1604   fail_unless (result == 150);
1605
1606   /* 50 accumulated + 50 / 2 */
1607   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1608   fail_unless (result == 75);
1609   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1610   fail_unless (result == 100);
1611
1612   /* update segment, this does not accumulate anything. */
1613   gst_segment_set_newsegment_full (&segment, TRUE, 1.0, 1.0,
1614       GST_FORMAT_TIME, 100, 200, 100);
1615
1616   fail_unless (segment.rate == 1.0);
1617   fail_unless (segment.applied_rate == 1.0);
1618   fail_unless (segment.format == GST_FORMAT_TIME);
1619   fail_unless (segment.flags == 0);
1620   fail_unless (segment.start == 100);
1621   fail_unless (segment.stop == 200);
1622   fail_unless (segment.time == 100);
1623   fail_unless (segment.accum == 50);
1624   fail_unless (segment.last_stop == 150);
1625   fail_unless (segment.duration == -1);
1626
1627   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1628   fail_unless (result == 50);
1629   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1630   fail_unless (result == 100);
1631
1632   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1633   fail_unless (result == 100);
1634   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1635   fail_unless (result == 150);
1636 }
1637
1638 GST_END_TEST;
1639
1640 /* mess with the segment structure in the time format */
1641 GST_START_TEST (segment_newsegment_accum2)
1642 {
1643   GstSegment segment;
1644   gint64 result;
1645
1646   gst_segment_init (&segment, GST_FORMAT_TIME);
1647
1648   /***************************
1649    * Normal reverse segment
1650    ***************************/
1651   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
1652       GST_FORMAT_TIME, 0, 200, 0);
1653
1654   fail_unless (segment.rate == -1.0);
1655   fail_unless (segment.applied_rate == 1.0);
1656   fail_unless (segment.format == GST_FORMAT_TIME);
1657   fail_unless (segment.flags == 0);
1658   fail_unless (segment.start == 0);
1659   fail_unless (segment.stop == 200);
1660   fail_unless (segment.time == 0);
1661   fail_unless (segment.accum == 0);
1662   fail_unless (segment.last_stop == 200);
1663   fail_unless (segment.duration == -1);
1664
1665   /* invalid time gives invalid result */
1666   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1667   fail_unless (result == -1);
1668   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1669   fail_unless (result == -1);
1670
1671   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1672   fail_unless (result == 0);
1673   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1674   fail_unless (result == 200);
1675
1676   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1677   fail_unless (result == 50);
1678   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1679   fail_unless (result == 150);
1680
1681   /* close segment, this accumulates nothing. */
1682   gst_segment_set_newsegment_full (&segment, TRUE, -1.0, 1.0,
1683       GST_FORMAT_TIME, 150, 200, 0);
1684
1685   fail_unless (segment.rate == -1.0);
1686   fail_unless (segment.applied_rate == 1.0);
1687   fail_unless (segment.format == GST_FORMAT_TIME);
1688   fail_unless (segment.flags == 0);
1689   fail_unless (segment.start == 150);
1690   fail_unless (segment.stop == 200);
1691   fail_unless (segment.time == 0);
1692   fail_unless (segment.accum == 0);
1693   fail_unless (segment.last_stop == 200);
1694   fail_unless (segment.duration == -1);
1695
1696   /* new segment, this accumulates 50. */
1697   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
1698       GST_FORMAT_TIME, 150, 300, 150);
1699
1700   fail_unless (segment.rate == 1.0);
1701   fail_unless (segment.applied_rate == 1.0);
1702   fail_unless (segment.format == GST_FORMAT_TIME);
1703   fail_unless (segment.flags == 0);
1704   fail_unless (segment.start == 150);
1705   fail_unless (segment.stop == 300);
1706   fail_unless (segment.time == 150);
1707   fail_unless (segment.accum == 50);
1708   fail_unless (segment.last_stop == 150);
1709   fail_unless (segment.duration == -1);
1710
1711   /* invalid time gives invalid result */
1712   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1713   fail_unless (result == -1);
1714
1715   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1716   fail_unless (result == 50);
1717   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1718   fail_unless (result == 150);
1719
1720   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1721   fail_unless (result == 100);
1722   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1723   fail_unless (result == 200);
1724 }
1725
1726 GST_END_TEST;
1727
1728 GST_START_TEST (segment_copy)
1729 {
1730   GstSegment *copy;
1731   GstSegment segment = { 0.0, };
1732
1733   /* this is a boxed type copy function, we support copying NULL */
1734   fail_unless (gst_segment_copy (NULL) == NULL);
1735
1736   gst_segment_init (&segment, GST_FORMAT_TIME);
1737
1738   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
1739       GST_FORMAT_TIME, 0, 200, 0);
1740
1741   copy = gst_segment_copy (&segment);
1742   fail_unless (copy != NULL);
1743   /* we inited the struct on the stack to zeroes, so direct comparison should
1744    * be ok here despite the padding field and regardless of implementation */
1745   fail_unless (memcmp (copy, &segment, sizeof (GstSegment)) == 0);
1746   gst_segment_free (copy);
1747 }
1748
1749 GST_END_TEST;
1750
1751 static Suite *
1752 gst_segment_suite (void)
1753 {
1754   Suite *s = suite_create ("GstSegment");
1755   TCase *tc_chain = tcase_create ("segments");
1756
1757   tcase_set_timeout (tc_chain, 20);
1758
1759   suite_add_tcase (s, tc_chain);
1760   tcase_add_test (tc_chain, segment_seek_nosize);
1761   tcase_add_test (tc_chain, segment_seek_size);
1762   tcase_add_test (tc_chain, segment_seek_reverse);
1763   tcase_add_test (tc_chain, segment_seek_rate);
1764   tcase_add_test (tc_chain, segment_newsegment_open);
1765   tcase_add_test (tc_chain, segment_newsegment_closed);
1766   tcase_add_test (tc_chain, segment_newsegment_streamtime);
1767   tcase_add_test (tc_chain, segment_newsegment_streamtime_rate);
1768   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate);
1769   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate_rate);
1770   tcase_add_test (tc_chain, segment_newsegment_runningtime);
1771   tcase_add_test (tc_chain, segment_newsegment_accum);
1772   tcase_add_test (tc_chain, segment_newsegment_accum2);
1773   tcase_add_test (tc_chain, segment_copy);
1774
1775   return s;
1776 }
1777
1778 GST_CHECK_MAIN (gst_segment);