mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add test
This commit is contained in:
parent
62a2c78e7b
commit
1285f94cb0
@ -58,6 +58,7 @@ public final class Fraction extends Number implements Comparable<Fraction> {
|
|||||||
* @throws ArithmeticException 如果计算出的分母为 {@code zero}
|
* @throws ArithmeticException 如果计算出的分母为 {@code zero}
|
||||||
* @throws ArithmeticException 如果算法不收敛
|
* @throws ArithmeticException 如果算法不收敛
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("ReassignedVariable")
|
||||||
public static Fraction of(double value) {
|
public static Fraction of(double value) {
|
||||||
final int sign = value < 0 ? -1 : 1;
|
final int sign = value < 0 ? -1 : 1;
|
||||||
value = Math.abs(value);
|
value = Math.abs(value);
|
||||||
@ -103,7 +104,7 @@ public final class Fraction extends Number implements Comparable<Fraction> {
|
|||||||
if (i == 25) {
|
if (i == 25) {
|
||||||
throw new ArithmeticException("Unable to convert double to fraction");
|
throw new ArithmeticException("Unable to convert double to fraction");
|
||||||
}
|
}
|
||||||
return getReducedFraction((numer0 + wholeNumber * denom0) * sign, denom0);
|
return ofReduced((numer0 + wholeNumber * denom0) * sign, denom0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -282,7 +283,7 @@ public final class Fraction extends Number implements Comparable<Fraction> {
|
|||||||
* @param numerator 分子,例如在“七分之三”中的三
|
* @param numerator 分子,例如在“七分之三”中的三
|
||||||
* @param denominator 分母,例如在“七分之三”中的七
|
* @param denominator 分母,例如在“七分之三”中的七
|
||||||
*/
|
*/
|
||||||
private Fraction(final int numerator, final int denominator) {
|
public Fraction(final int numerator, final int denominator) {
|
||||||
this.numerator = numerator;
|
this.numerator = numerator;
|
||||||
this.denominator = denominator;
|
this.denominator = denominator;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
package org.dromara.hutool.core.math;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class FractionTest {
|
||||||
|
|
||||||
|
private Fraction fraction1;
|
||||||
|
private Fraction fraction2;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
// 假设 Fraction 类有一个构造函数 Fraction(int numerator, int denominator)
|
||||||
|
fraction1 = new Fraction(1, 2);
|
||||||
|
fraction2 = new Fraction(3, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void add_Fractions_ShouldReturnCorrectSum() {
|
||||||
|
final Fraction result = fraction1.add(fraction2);
|
||||||
|
assertEquals(new Fraction(5, 4), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void subtract_Fractions_ShouldReturnCorrectDifference() {
|
||||||
|
final Fraction result = fraction1.subtract(fraction2);
|
||||||
|
assertEquals(new Fraction(-1, 4), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void equals_Fractions_ShouldReturnTrueForEqualFractions() {
|
||||||
|
final Fraction fraction3 = new Fraction(1, 2);
|
||||||
|
assertEquals(fraction1, fraction3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void equals_Fractions_ShouldReturnFalseForDifferentFractions() {
|
||||||
|
assertNotEquals(fraction1, fraction2);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user