aboutsummaryrefslogtreecommitdiffstats
path: root/test/rational.py
blob: bc49e9fc07b9d5d8a39d15f0e8732c34b174dcd7 (plain) (blame)
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
# -*- coding: utf-8 -*-

# ******************************************************************************
#
# Copyright (C) 2008-2010 Olivier Tilloy <olivier@tilloy.net>
#
# This file is part of the pyexiv2 distribution.
#
# pyexiv2 is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# pyexiv2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyexiv2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
#
# Author: Olivier Tilloy <olivier@tilloy.net>
#
# ******************************************************************************

import unittest

from pyexiv2.utils import Rational


class TestRational(unittest.TestCase):

    def test_constructor(self):
        r = Rational(2, 1)
        self.assertEqual(r.numerator, 2)
        self.assertEqual(r.denominator, 1)
        self.assertRaises(ZeroDivisionError, Rational, 1, 0)

    def test_read_only(self):
        r = Rational(3, 4)
        try:
            r.numerator = 5
        except AttributeError:
            pass
        else:
            self.fail('Numerator is not read-only.')
        try:
            r.denominator = 5
        except AttributeError:
            pass
        else:
            self.fail('Denominator is not read-only.')

    def test_match_string(self):
        self.assertEqual(Rational.match_string('4/3'), (4, 3))
        self.assertEqual(Rational.match_string('-4/3'), (-4, 3))
        self.assertEqual(Rational.match_string('0/3'), (0, 3))
        self.assertEqual(Rational.match_string('0/0'), (0, 0))
        self.assertRaises(ValueError, Rational.match_string, '+3/5')
        self.assertRaises(ValueError, Rational.match_string, '3 / 5')
        self.assertRaises(ValueError, Rational.match_string, '3/-5')
        self.assertRaises(ValueError, Rational.match_string, 'invalid')

    def test_from_string(self):
        self.assertEqual(Rational.from_string('4/3'), Rational(4, 3))
        self.assertEqual(Rational.from_string('-4/3'), Rational(-4, 3))
        self.assertRaises(ValueError, Rational.from_string, '+3/5')
        self.assertRaises(ValueError, Rational.from_string, '3 / 5')
        self.assertRaises(ValueError, Rational.from_string, '3/-5')
        self.assertRaises(ValueError, Rational.from_string, 'invalid')
        self.assertRaises(ZeroDivisionError, Rational.from_string, '1/0')
        self.assertRaises(ZeroDivisionError, Rational.from_string, '0/0')

    def test_to_string(self):
        self.assertEqual(str(Rational(3, 5)), '3/5')
        self.assertEqual(str(Rational(-3, 5)), '-3/5')

    def test_repr(self):
        self.assertEqual(repr(Rational(3, 5)), 'Rational(3, 5)')
        self.assertEqual(repr(Rational(-3, 5)), 'Rational(-3, 5)')
        self.assertEqual(repr(Rational(0, 3)), 'Rational(0, 3)')

    def test_to_float(self):
        self.assertEqual(Rational(3, 6).to_float(), 0.5)
        self.assertEqual(Rational(11, 11).to_float(), 1.0)
        self.assertEqual(Rational(-2, 8).to_float(), -0.25)
        self.assertEqual(Rational(0, 3).to_float(), 0.0)

    def test_equality(self):
        r1 = Rational(2, 1)
        r2 = Rational(2, 1)
        r3 = Rational(8, 4)
        r4 = Rational(3, 2)
        self.assertEqual(r1, r2)
        self.assertEqual(r1, r3)
        self.assertNotEqual(r1, r4)