11from __future__ import division
2-
32import unittest
43
54from rational_numbers import Rational
65
7-
86# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
97
8+
109class RationalNumbersTest (unittest .TestCase ):
1110
12- # Test addition
13- def test_add_two_positive (self ):
11+ # Tests of type: Arithmetic
12+
13+ # Addition
14+
15+ def test_add_two_positive_rational_numbers (self ):
1416 self .assertEqual (Rational (1 , 2 ) + Rational (2 , 3 ), Rational (7 , 6 ))
1517
16- def test_add_positive_and_negative (self ):
18+ def test_add_a_positive_rational_number_and_a_negative_rational_number (self ):
1719 self .assertEqual (Rational (1 , 2 ) + Rational (- 2 , 3 ), Rational (- 1 , 6 ))
1820
19- def test_add_two_negative (self ):
21+ def test_add_two_negative_rational_numbers (self ):
2022 self .assertEqual (Rational (- 1 , 2 ) + Rational (- 2 , 3 ), Rational (- 7 , 6 ))
2123
22- def test_add_opposite (self ):
24+ def test_add_a_rational_number_to_its_additive_inverse (self ):
2325 self .assertEqual (Rational (1 , 2 ) + Rational (- 1 , 2 ), Rational (0 , 1 ))
2426
25- # Test subtraction
26- def test_subtract_two_positive (self ):
27+ # Subtraction
28+
29+ def test_subtract_two_positive_rational_numbers (self ):
2730 self .assertEqual (Rational (1 , 2 ) - Rational (2 , 3 ), Rational (- 1 , 6 ))
2831
29- def test_subtract_positive_and_negative (self ):
32+ def test_subtract_a_positive_rational_number_and_a_negative_rational_number (self ):
3033 self .assertEqual (Rational (1 , 2 ) - Rational (- 2 , 3 ), Rational (7 , 6 ))
3134
32- def test_subtract_two_negative (self ):
35+ def test_subtract_two_negative_rational_numbers (self ):
3336 self .assertEqual (Rational (- 1 , 2 ) - Rational (- 2 , 3 ), Rational (1 , 6 ))
3437
35- def test_subtract_from_self (self ):
38+ def test_subtract_a_rational_number_from_itself (self ):
3639 self .assertEqual (Rational (1 , 2 ) - Rational (1 , 2 ), Rational (0 , 1 ))
3740
38- # Test multiplication
39- def test_multiply_two_positive (self ):
41+ # Multiplication
42+
43+ def test_multiply_two_positive_rational_numbers (self ):
4044 self .assertEqual (Rational (1 , 2 ) * Rational (2 , 3 ), Rational (1 , 3 ))
4145
42- def test_multiply_negative_by_positive (self ):
46+ def test_multiply_a_negative_rational_number_by_a_positive_rational_number (self ):
4347 self .assertEqual (Rational (- 1 , 2 ) * Rational (2 , 3 ), Rational (- 1 , 3 ))
4448
45- def test_multiply_two_negative (self ):
49+ def test_multiply_two_negative_rational_numbers (self ):
4650 self .assertEqual (Rational (- 1 , 2 ) * Rational (- 2 , 3 ), Rational (1 , 3 ))
4751
48- def test_multiply_reciprocal (self ):
52+ def test_multiply_a_rational_number_by_its_reciprocal (self ):
4953 self .assertEqual (Rational (1 , 2 ) * Rational (2 , 1 ), Rational (1 , 1 ))
5054
51- def test_multiply_by_one (self ):
55+ def test_multiply_a_rational_number_by_1 (self ):
5256 self .assertEqual (Rational (1 , 2 ) * Rational (1 , 1 ), Rational (1 , 2 ))
5357
54- def test_multiply_by_zero (self ):
58+ def test_multiply_a_rational_number_by_0 (self ):
5559 self .assertEqual (Rational (1 , 2 ) * Rational (0 , 1 ), Rational (0 , 1 ))
5660
57- # Test division
58- def test_divide_two_positive (self ):
61+ # Division
62+
63+ def test_divide_two_positive_rational_numbers (self ):
5964 self .assertEqual (Rational (1 , 2 ) / Rational (2 , 3 ), Rational (3 , 4 ))
6065
61- def test_divide_positive_by_negative (self ):
66+ def test_divide_a_positive_rational_number_by_a_negative_rational_number (self ):
6267 self .assertEqual (Rational (1 , 2 ) / Rational (- 2 , 3 ), Rational (- 3 , 4 ))
6368
64- def test_divide_two_negative (self ):
69+ def test_divide_two_negative_rational_numbers (self ):
6570 self .assertEqual (Rational (- 1 , 2 ) / Rational (- 2 , 3 ), Rational (3 , 4 ))
6671
67- def test_divide_by_one (self ):
72+ def test_divide_a_rational_number_by_1 (self ):
6873 self .assertEqual (Rational (1 , 2 ) / Rational (1 , 1 ), Rational (1 , 2 ))
6974
70- # Test absolute value
71- def test_absolute_value_of_positive (self ):
75+ # Tests of type: Absolute value
76+
77+ def test_absolute_value_of_a_positive_rational_number (self ):
7278 self .assertEqual (abs (Rational (1 , 2 )), Rational (1 , 2 ))
7379
74- def test_absolute_value_of_positive_negative_numerator_denominator (self ):
80+ def test_absolute_value_of_a_positive_rational_number_with_negative_numerator_and_denominator (
81+ self
82+ ):
7583 self .assertEqual (abs (Rational (- 1 , - 2 )), Rational (1 , 2 ))
7684
77- def test_absolute_value_of_negative (self ):
85+ def test_absolute_value_of_a_negative_rational_number (self ):
7886 self .assertEqual (abs (Rational (- 1 , 2 )), Rational (1 , 2 ))
7987
80- def test_absolute_value_of_negative_with_negative_denominator (self ):
88+ def test_absolute_value_of_a_negative_rational_number_with_negative_denominator (
89+ self
90+ ):
8191 self .assertEqual (abs (Rational (1 , - 2 )), Rational (1 , 2 ))
8292
8393 def test_absolute_value_of_zero (self ):
8494 self .assertEqual (abs (Rational (0 , 1 )), Rational (0 , 1 ))
8595
86- # Test exponentiation of a rational number
87- def test_raise_a_positive_rational_to_a_positive_integer_power (self ):
96+ # Tests of type: Exponentiation of a rational number
97+
98+ def test_raise_a_positive_rational_number_to_a_positive_integer_power (self ):
8899 self .assertEqual (Rational (1 , 2 ) ** 3 , Rational (1 , 8 ))
89100
90- def test_raise_a_negative_rational_to_a_positive_integer_power (self ):
101+ def test_raise_a_negative_rational_number_to_a_positive_integer_power (self ):
91102 self .assertEqual (Rational (- 1 , 2 ) ** 3 , Rational (- 1 , 8 ))
92103
93104 def test_raise_zero_to_an_integer_power (self ):
@@ -96,43 +107,43 @@ def test_raise_zero_to_an_integer_power(self):
96107 def test_raise_one_to_an_integer_power (self ):
97108 self .assertEqual (Rational (1 , 1 ) ** 4 , Rational (1 , 1 ))
98109
99- def test_raise_a_positive_rational_to_the_power_of_zero (self ):
110+ def test_raise_a_positive_rational_number_to_the_power_of_zero (self ):
100111 self .assertEqual (Rational (1 , 2 ) ** 0 , Rational (1 , 1 ))
101112
102- def test_raise_a_negative_rational_to_the_power_of_zero (self ):
113+ def test_raise_a_negative_rational_number_to_the_power_of_zero (self ):
103114 self .assertEqual (Rational (- 1 , 2 ) ** 0 , Rational (1 , 1 ))
104115
105- # Test exponentiation of a real number to a rational number
106- def test_raise_a_real_number_to_a_positive_rational (self ):
116+ # Tests of type: Exponentiation of a real number to a rational number
117+
118+ def test_raise_a_real_number_to_a_positive_rational_number (self ):
107119 self .assertAlmostEqual (8 ** Rational (4 , 3 ), 16.0 , places = 8 )
108120
109- def test_raise_a_real_number_to_a_negative_rational (self ):
110- self .assertAlmostEqual (
111- 9 ** Rational (- 1 , 2 ), 0.3333333333333333 , places = 8
112- )
121+ def test_raise_a_real_number_to_a_negative_rational_number (self ):
122+ self .assertAlmostEqual (9 ** Rational (- 1 , 2 ), 0.3333333333333333 , places = 8 )
113123
114- def test_raise_a_real_number_to_a_zero_rational (self ):
124+ def test_raise_a_real_number_to_a_zero_rational_number (self ):
115125 self .assertAlmostEqual (2 ** Rational (0 , 1 ), 1.0 , places = 8 )
116126
117- # Test reduction to lowest terms
118- def test_reduce_positive (self ):
127+ # Tests of type: Reduction to lowest terms
128+
129+ def test_reduce_a_positive_rational_number_to_lowest_terms (self ):
119130 self .assertEqual (Rational (2 , 4 ), Rational (1 , 2 ))
120131
121- def test_reduce_negative (self ):
132+ def test_reduce_a_negative_rational_number_to_lowest_terms (self ):
122133 self .assertEqual (Rational (- 4 , 6 ), Rational (- 2 , 3 ))
123134
124- def test_reduce_rational_with_negative_denominator (self ):
135+ def test_reduce_a_rational_number_with_a_negative_denominator_to_lowest_terms (self ):
125136 self .assertEqual (Rational (3 , - 9 ), Rational (- 1 , 3 ))
126137
127- def test_reduce_zero (self ):
138+ def test_reduce_zero_to_lowest_terms (self ):
128139 self .assertEqual (Rational (0 , 6 ), Rational (0 , 1 ))
129140
130- def test_reduce_integer (self ):
141+ def test_reduce_an_integer_to_lowest_terms (self ):
131142 self .assertEqual (Rational (- 14 , 7 ), Rational (- 2 , 1 ))
132143
133- def test_reduce_one (self ):
144+ def test_reduce_one_to_lowest_terms (self ):
134145 self .assertEqual (Rational (13 , 13 ), Rational (1 , 1 ))
135146
136147
137- if __name__ == ' __main__' :
148+ if __name__ == " __main__" :
138149 unittest .main ()
0 commit comments