mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 22:43:30 +08:00
Code refactor for ADTFraction improvements (#3016)
Fix #3015 Co-authored-by: Yang Libin <szuyanglb@outlook.com>
This commit is contained in:
52
src/test/java/com/thealgorithms/maths/ADTFractionTest.java
Normal file
52
src/test/java/com/thealgorithms/maths/ADTFractionTest.java
Normal file
@ -0,0 +1,52 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ADTFractionTest {
|
||||
|
||||
private final ADTFraction fraction1 = new ADTFraction(3, 5);
|
||||
private final ADTFraction fraction2 = new ADTFraction(7, 8);
|
||||
|
||||
@Test
|
||||
void testConstructorWithDenominatorEqualToZero() {
|
||||
Exception exception = assertThrows(IllegalArgumentException.class, () -> new ADTFraction(1, 0));
|
||||
assertEquals("Denominator cannot be 0", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlus() {
|
||||
assertEquals(new ADTFraction(59, 40), fraction1.plus(fraction2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTimes() {
|
||||
assertEquals(new ADTFraction(12, 5), fraction1.times(4));
|
||||
assertEquals(new ADTFraction(21, 40), fraction1.times(fraction2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReciprocal() {
|
||||
assertEquals(new ADTFraction(5, 3), fraction1.reciprocal());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValue() {
|
||||
assertEquals(0.6F, fraction1.value());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() {
|
||||
ADTFraction fraction3 = new ADTFraction(3, 5);
|
||||
assertTrue(fraction1.equals(fraction3) && fraction3.equals(fraction1));
|
||||
assertEquals(fraction1.hashCode(), fraction3.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
assertEquals("3/5", fraction1.toString());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user