11package com .thealgorithms .maths ;
22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
45import static org .junit .jupiter .api .Assertions .assertThrows ;
56import static org .junit .jupiter .api .Assertions .assertTrue ;
67
@@ -17,27 +18,186 @@ void testConstructorWithDenominatorEqualToZero() {
1718 assertEquals ("Denominator cannot be 0" , exception .getMessage ());
1819 }
1920
21+ @ Test
22+ void testConstructorReducesFractionToLowestTerms () {
23+ ADTFraction fraction = new ADTFraction (6 , 8 );
24+ assertEquals (3 , fraction .numerator ());
25+ assertEquals (4 , fraction .denominator ());
26+ assertEquals ("3/4" , fraction .toString ());
27+ }
28+
29+ @ Test
30+ void testConstructorReducesFractionWithLargeCommonDivisor () {
31+ assertEquals (new ADTFraction (1 , 1 ), new ADTFraction (Integer .MAX_VALUE , Integer .MAX_VALUE ));
32+ assertEquals (new ADTFraction (1 , 3 ), new ADTFraction (715827882 , 2147483646 ));
33+ }
34+
35+ @ Test
36+ void testConstructorNormalizesZeroNumerator () {
37+ ADTFraction fraction = new ADTFraction (0 , -5 );
38+ assertEquals (0 , fraction .numerator ());
39+ assertEquals (1 , fraction .denominator ());
40+ assertEquals ("0/1" , fraction .toString ());
41+ }
42+
43+ @ Test
44+ void testConstructorMovesSignToNumerator () {
45+ ADTFraction fraction = new ADTFraction (1 , -2 );
46+ assertEquals (-1 , fraction .numerator ());
47+ assertEquals (2 , fraction .denominator ());
48+ assertEquals ("-1/2" , fraction .toString ());
49+ }
50+
51+ @ Test
52+ void testConstructorNormalizesTwoNegatives () {
53+ assertEquals (new ADTFraction (1 , 2 ), new ADTFraction (-1 , -2 ));
54+ assertEquals ("1/2" , new ADTFraction (-2 , -4 ).toString ());
55+ }
56+
57+ @ Test
58+ void testEquivalentFractionsAreEqual () {
59+ assertEquals (new ADTFraction (1 , 2 ), new ADTFraction (2 , 4 ));
60+ assertEquals (new ADTFraction (1 , 2 ).hashCode (), new ADTFraction (2 , 4 ).hashCode ());
61+ assertEquals (new ADTFraction (-1 , 2 ), new ADTFraction (1 , -2 ));
62+ assertEquals (new ADTFraction (-1 , 2 ).hashCode (), new ADTFraction (1 , -2 ).hashCode ());
63+ assertNotEquals (new ADTFraction (1 , 2 ), new ADTFraction (1 , 3 ));
64+ }
65+
66+ @ Test
67+ void testFractionEquivalenceAndSignNormalizationFromIssue () {
68+ ADTFraction half = new ADTFraction (1 , 2 );
69+ ADTFraction twoQuarters = new ADTFraction (2 , 4 );
70+ assertEquals (half , twoQuarters , "Equivalent fractions should be equal" );
71+
72+ ADTFraction neg1 = new ADTFraction (-1 , 2 );
73+ ADTFraction neg2 = new ADTFraction (1 , -2 );
74+ assertEquals (neg1 , neg2 , "Fractions with negative signs in numerator or denominator should be equal" );
75+ }
76+
77+ @ Test
78+ void testConstructorHandlesIntegerMinValueDenominatorWhenReducible () {
79+ ADTFraction fraction = new ADTFraction (2 , Integer .MIN_VALUE );
80+ assertEquals (-1 , fraction .numerator ());
81+ assertEquals (1 << 30 , fraction .denominator ());
82+ }
83+
84+ @ Test
85+ void testConstructorHandlesIntegerMinValueNumeratorWhenReducible () {
86+ ADTFraction fraction = new ADTFraction (Integer .MIN_VALUE , 2 );
87+ assertEquals (Integer .MIN_VALUE / 2 , fraction .numerator ());
88+ assertEquals (1 , fraction .denominator ());
89+ }
90+
91+ @ Test
92+ void testConstructorRejectsCanonicalFormOutsideIntegerRange () {
93+ assertThrows (ArithmeticException .class , () -> new ADTFraction (1 , Integer .MIN_VALUE ));
94+ assertThrows (ArithmeticException .class , () -> new ADTFraction (Integer .MIN_VALUE , -3 ));
95+ }
96+
2097 @ Test
2198 public void testPlus () {
2299 assertEquals (new ADTFraction (59 , 40 ), fraction1 .plus (fraction2 ));
23100 }
24101
102+ @ Test
103+ void testPlusReducesResult () {
104+ assertEquals (new ADTFraction (1 , 1 ), new ADTFraction (1 , 2 ).plus (new ADTFraction (1 , 2 )));
105+ assertEquals (new ADTFraction (1 , 1 ), new ADTFraction (1 , 3 ).plus (new ADTFraction (2 , 3 )));
106+ assertEquals (new ADTFraction (5 , 12 ), new ADTFraction (1 , 6 ).plus (new ADTFraction (1 , 4 )));
107+ assertEquals (new ADTFraction (-1 , 2 ), new ADTFraction (1 , 2 ).plus (new ADTFraction (-1 , 1 )));
108+ }
109+
110+ @ Test
111+ void testPlusWithZero () {
112+ assertEquals (fraction1 , fraction1 .plus (new ADTFraction (0 , 7 )));
113+ assertEquals (fraction1 , new ADTFraction (0 , 7 ).plus (fraction1 ));
114+ }
115+
116+ @ Test
117+ void testPlusDoesNotOverflowForReducibleDenominators () {
118+ ADTFraction tenthOfThousandth = new ADTFraction (1 , 100000 );
119+ assertEquals (new ADTFraction (1 , 50000 ), tenthOfThousandth .plus (tenthOfThousandth ));
120+ }
121+
122+ @ Test
123+ void testPlusWithOppositeFractionsGivesZero () {
124+ assertEquals (new ADTFraction (0 , 1 ), new ADTFraction (Integer .MAX_VALUE , 3 ).plus (new ADTFraction (-Integer .MAX_VALUE , 3 )));
125+ }
126+
127+ @ Test
128+ void testPlusThrowsWhenResultDoesNotFitIntoInteger () {
129+ ADTFraction largest = new ADTFraction (Integer .MAX_VALUE , 1 );
130+ assertThrows (ArithmeticException .class , () -> largest .plus (largest ));
131+ }
132+
25133 @ Test
26134 public void testTimes () {
27135 assertEquals (new ADTFraction (12 , 5 ), fraction1 .times (4 ));
28136 assertEquals (new ADTFraction (21 , 40 ), fraction1 .times (fraction2 ));
29137 }
30138
139+ @ Test
140+ void testTimesReducesResult () {
141+ assertEquals (new ADTFraction (1 , 2 ), new ADTFraction (2 , 3 ).times (new ADTFraction (3 , 4 )));
142+ assertEquals (new ADTFraction (1 , 2 ), new ADTFraction (-2 , 3 ).times (new ADTFraction (-3 , 4 )));
143+ assertEquals (new ADTFraction (-1 , 2 ), new ADTFraction (-2 , 3 ).times (new ADTFraction (3 , 4 )));
144+ assertEquals (new ADTFraction (-6 , 5 ), new ADTFraction (-3 , 5 ).times (2 ));
145+ }
146+
147+ @ Test
148+ void testTimesByZero () {
149+ assertEquals (new ADTFraction (0 , 1 ), fraction1 .times (0 ));
150+ assertEquals (new ADTFraction (0 , 1 ), new ADTFraction (0 , 7 ).times (fraction1 ));
151+ assertEquals (new ADTFraction (0 , 1 ), fraction1 .times (new ADTFraction (0 , 7 )));
152+ }
153+
154+ @ Test
155+ void testTimesDoesNotOverflowWhenFactorsCancel () {
156+ assertEquals (new ADTFraction (1 , 1 ), new ADTFraction (Integer .MAX_VALUE , 3 ).times (new ADTFraction (3 , Integer .MAX_VALUE )));
157+ assertEquals (new ADTFraction (1 , 1 ), new ADTFraction (46368 , 75025 ).times (new ADTFraction (75025 , 46368 )));
158+ }
159+
160+ @ Test
161+ void testTimesThrowsWhenResultDoesNotFitIntoInteger () {
162+ assertThrows (ArithmeticException .class , () -> new ADTFraction (Integer .MAX_VALUE , 1 ).times (2 ));
163+ }
164+
165+ @ Test
166+ void testRepeatedAdditionStaysExact () {
167+ ADTFraction sum = new ADTFraction (0 , 1 );
168+ for (int i = 0 ; i < 1000 ; i ++) {
169+ sum = sum .plus (new ADTFraction (1 , 1000 ));
170+ }
171+ assertEquals (new ADTFraction (1 , 1 ), sum );
172+ }
173+
31174 @ Test
32175 public void testReciprocal () {
33176 assertEquals (new ADTFraction (5 , 3 ), fraction1 .reciprocal ());
34177 }
35178
179+ @ Test
180+ void testReciprocalOfNegativeFraction () {
181+ assertEquals (new ADTFraction (-5 , 3 ), new ADTFraction (3 , -5 ).reciprocal ());
182+ }
183+
184+ @ Test
185+ void testReciprocalOfZeroThrows () {
186+ ADTFraction zero = new ADTFraction (0 , 1 );
187+ Exception exception = assertThrows (IllegalArgumentException .class , zero ::reciprocal );
188+ assertEquals ("Denominator cannot be 0" , exception .getMessage ());
189+ }
190+
36191 @ Test
37192 public void testValue () {
38193 assertEquals (0.6F , fraction1 .value ());
39194 }
40195
196+ @ Test
197+ void testValueOfNegativeFraction () {
198+ assertEquals (-0.5F , new ADTFraction (1 , -2 ).value ());
199+ }
200+
41201 @ Test
42202 public void testEqualsAndHashCode () {
43203 ADTFraction fraction3 = new ADTFraction (3 , 5 );
0 commit comments