Adding gst-python package
[platform/upstream/gst-python.git] / testsuite / test_fraction.py
1 # -*- Mode: Python -*-
2 # vi:si:et:sw=4:sts=4:ts=4
3 #
4 # gst-python - Python bindings for GStreamer
5 # Copyright (C) 2007 Johan Dahlin
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 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 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
20
21 import overrides_hack
22 overrides_hack
23
24 from common import TestCase
25
26 from gi.repository import Gst
27 Gst.init(None)
28
29 F = Gst.Fraction
30
31 class TestFraction(TestCase):
32     def testConstructor(self):
33         Gst.init(None)
34
35         frac = F(1, 2)
36         self.assertEquals(frac.num, 1)
37         self.assertEquals(frac.denom, 2)
38
39         frac = F(1)
40         self.assertEquals(frac.num, 1)
41         self.assertEquals(frac.denom, 1)
42
43         self.assertRaises(TypeError, F)
44
45     def testRepr(self):
46         Gst.init(None)
47
48         self.assertEquals(repr(F(1, 2)), '<Gst.Fraction 1/2>')
49
50     def testEqNe(self):
51         Gst.init(None)
52
53         frac = F(1, 2)
54         self.assertEquals(frac, frac)
55         self.assertEquals(F(1, 2), F(1, 2))
56         self.assertEquals(F(2, 4), F(1, 2))
57
58         self.assertNotEquals(F(1, 3), F(1, 2))
59         self.assertNotEquals(F(2, 1), F(1, 2))
60
61     def testMul(self):
62         Gst.init(None)
63
64         self.assertEquals(F(1, 2) * F(1, 2), F(1, 4))
65         self.assertEquals(F(2, 3) * F(4, 5), F(8, 15))
66         self.assertEquals(F(1, 3) * F(4), F(4, 3))
67         self.assertEquals(F(1, 3) * 4, F(4, 3))
68
69     def testRMul(self):
70         Gst.init(None)
71
72         self.assertEquals(2 * F(1, 2), F(1))
73         self.assertEquals(4 * F(1, 2), F(2))
74         self.assertEquals(-10 * F(1, 2), F(-5))
75
76     def testDiv(self):
77         Gst.init(None)
78
79         self.assertEquals(F(1, 3) / F(1, 4), F(4, 3))
80         self.assertEquals(F(2, 3) / F(4, 5), F(10, 12))
81
82         self.assertEquals(F(1, 3) / F(4), F(1, 12))
83         self.assertEquals(F(1, 3) / 4, F(1, 12))
84         self.assertEquals(F(1, 3) / 2, F(1, 6))
85         self.assertEquals(F(1, 5) / -4, F(1, -20))
86
87     def testRDiv(self):
88         Gst.init(None)
89
90         self.assertEquals(2 / F(1, 3), F(6, 1))
91         self.assertEquals(-4 / F(1, 5), F(-20, 1))
92
93     def testFloat(self):
94         Gst.init(None)
95
96         self.assertEquals(float(F(1, 2)), 0.5)
97
98     def testPropertyMarshalling(self):
99         Gst.init(None)
100
101         obj = Gst.ElementFactory.make("videoparse")
102         if not obj:
103             # no videoparse and I don't know of any elements in core or -base using
104             # fraction properties. Skip this test.
105             return
106
107         value = obj.props.framerate
108         self.failUnlessEqual(value.num, 25)
109         self.failUnlessEqual(value.denom, 1)
110
111         obj.props.framerate = Gst.Fraction(2, 1)
112         value = obj.props.framerate
113         self.failUnlessEqual(value.num, 2)
114         self.failUnlessEqual(value.denom, 1)
115
116         def bad():
117             obj.props.framerate = 1
118         self.failUnlessRaises(TypeError, bad)
119
120         value = obj.props.framerate
121         self.failUnlessEqual(value.num, 2)
122         self.failUnlessEqual(value.denom, 1)
123
124     def testGetFractionValue(self):
125         Gst.init(None)
126
127         st = Gst.Structure.from_string("video/x-raw,framerate=10/1")[0]
128         value = st["framerate"]
129
130         self.failUnlessEqual(value.num, 10)
131         self.failUnlessEqual(value.denom, 1)