aboutsummaryrefslogtreecommitdiffstats
path: root/test/rational.py
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2009-10-20 09:59:06 +0200
committerOlivier Tilloy <olivier@fluendo.com>2009-10-20 09:59:06 +0200
commitbd14365ded7630441a1f08d303114398df463b9c (patch)
tree3c66400f11604116c8c8daeebf6f12e4673f1a6a /test/rational.py
parent4a7092691ef7ab3f11c8edcf4f28c8b42bf5342f (diff)
parentb6182ecb49d515f34b55666ec7e85f4ae4bc431a (diff)
downloadpyexiv2-bd14365ded7630441a1f08d303114398df463b9c.tar.gz
Code split into various python modules.
Diffstat (limited to 'test/rational.py')
-rw-r--r--test/rational.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/test/rational.py b/test/rational.py
new file mode 100644
index 0000000..070bf6c
--- /dev/null
+++ b/test/rational.py
@@ -0,0 +1,80 @@
+# -*- coding: utf-8 -*-
+
+# ******************************************************************************
+#
+# Copyright (C) 2008-2009 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_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')
+
+ def test_to_string(self):
+ self.assertEqual(str(Rational(3, 5)), '3/5')
+ self.assertEqual(str(Rational(-3, 5)), '-3/5')
+
+ 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)