summaryrefslogtreecommitdiff
path: root/testsuite/test_adapter.py
blob: 71e0a3d3b7b66d0b9118767145d140e870ab698a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# gst-python - Python bindings for GStreamer
# Copyright (C) 2009 Edward Hervey
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307  USA

from common import gobject, gst, unittest, TestCase

class AdapterTest(TestCase):

    def setUp(self):
        TestCase.setUp(self)
        self.adapter = gst.Adapter()

    def tearDown(self):
        self.adapter = None
        TestCase.tearDown(self)

    def testAvailable(self):
        # starts empty
        self.assertEquals(self.adapter.available(), 0)
        self.assertEquals(self.adapter.available_fast(), 0)

        # let's give it 4 bytes
        self.adapter.push(gst.Buffer("1234"))
        self.assertEquals(self.adapter.available_fast(), 4)

        # let's give it another 5 bytes
        self.adapter.push(gst.Buffer("56789"))
        # we now have 9 bytes
        self.assertEquals(self.adapter.available(), 9)
        # but can only do a fast take of 4 bytes (the first buffer)
        self.assertEquals(self.adapter.available_fast(), 4)

    def testPeek(self):
        self.adapter.push(gst.Buffer("0123456789"))

        # let's peek at 5 bytes
        b = self.adapter.peek(5)
        # it can return more than 5 bytes
        self.assert_(len(b) >= 5)
        self.assertEquals(b, "01234")

        # it's still 10 bytes big
        self.assertEquals(self.adapter.available(), 10)

        # if we try to peek more than what's available, we'll have None
        self.assertEquals(self.adapter.peek(11), None)

    def testFlush(self):
        self.adapter.push(gst.Buffer("0123456789"))
        self.assertEquals(self.adapter.available(), 10)

        self.adapter.flush(5)
        self.assertEquals(self.adapter.available(), 5)

        # it flushed the first 5 bytes
        self.assertEquals(self.adapter.peek(5), "56789")

        self.adapter.flush(5)
        self.assertEquals(self.adapter.available(), 0)

    def testTake(self):
        self.adapter.push(gst.Buffer("0123456789"))
        self.assertEquals(self.adapter.available(), 10)

        s = self.adapter.take(5)
        self.assertEquals(s, "01234")
        self.assertEquals(self.adapter.available(), 5)