summaryrefslogtreecommitdiff
path: root/testsuite/test_fraction.py
blob: 46b1ed82165aedff33269e4e7ac1a739b85d5b87 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# gst-python - Python bindings for GStreamer
# Copyright (C) 2007 Johan Dahlin
#
# 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 02110-1301  USA

import gobject
gobject.threads_init()
from common import gst, TestCase

F = gst.Fraction

class TestFraction(TestCase):
    def testConstructor(self):
        frac = F(1, 2)
        self.assertEquals(frac.num, 1)
        self.assertEquals(frac.denom, 2)

        frac = F(1)
        self.assertEquals(frac.num, 1)
        self.assertEquals(frac.denom, 1)

        self.assertRaises(TypeError, F)

    def testRepr(self):
        self.assertEquals(repr(F(1, 2)), '<gst.Fraction 1/2>')

    def testEqNe(self):
        frac = F(1, 2)
        self.assertEquals(frac, frac)
        self.assertEquals(F(1, 2), F(1, 2))
        self.assertEquals(F(2, 4), F(1, 2))

        self.assertNotEquals(F(1, 3), F(1, 2))
        self.assertNotEquals(F(2, 1), F(1, 2))

    def testMul(self):
        self.assertEquals(F(1, 2) * F(1, 2), F(1, 4))
        self.assertEquals(F(2, 3) * F(4, 5), F(8, 15))
        self.assertEquals(F(1, 3) * F(4), F(4, 3))
        self.assertEquals(F(1, 3) * 4, F(4, 3))

    def testRMul(self):
        self.assertEquals(2 * F(1, 2), F(1))
        self.assertEquals(4 * F(1, 2), F(2))
        self.assertEquals(-10 * F(1, 2), F(-5))

    def testDiv(self):
        self.assertEquals(F(1, 3) / F(1, 4), F(4, 3))
        self.assertEquals(F(2, 3) / F(4, 5), F(10, 12))

        self.assertEquals(F(1, 3) / F(4), F(1, 12))
        self.assertEquals(F(1, 3) / 4, F(1, 12))
        self.assertEquals(F(1, 3) / 2, F(1, 6))
        self.assertEquals(F(1, 5) / -4, F(1, -20))

    def testRDiv(self):
        self.assertEquals(2 / F(1, 3), F(6, 1))
        self.assertEquals(-4 / F(1, 5), F(-20, 1))

    def testFloat(self):
        self.assertEquals(float(F(1, 2)), 0.5)

    def testPropertyMarshalling(self):
        try:
            obj = gst.element_factory_make("videoparse")
        except gst.ElementNotFoundError:
            # no videoparse and I don't know of any elements in core or -base using
            # fraction properties. Skip this test.
            return
        value = obj.props.framerate
        self.failUnlessEqual(value.num, 25)
        self.failUnlessEqual(value.denom, 1)

        obj.props.framerate = gst.Fraction(2, 1)
        value = obj.props.framerate
        self.failUnlessEqual(value.num, 2)
        self.failUnlessEqual(value.denom, 1)

        def bad():
            obj.props.framerate = 1
        self.failUnlessRaises(TypeError, bad)

        value = obj.props.framerate
        self.failUnlessEqual(value.num, 2)
        self.failUnlessEqual(value.denom, 1)