aherbert commented on a change in pull request #82: URL: https://github.com/apache/commons-numbers/pull/82#discussion_r478530080
########## File path: commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonsLangPortedFractionTest.java ########## @@ -0,0 +1,1161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.numbers.fraction; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Test cases for the {@link Fraction} class. + * + * <p>This class is ported from commons lang to demonstrate interoperability of + * the Fraction class in numbers.</p> + */ +public class CommonsLangPortedFractionTest { + + private static final int SKIP = 500; //53 + + //-------------------------------------------------------------------------- + @Test + public void testConstants() { + assertEquals(0, Fraction.ZERO.getNumerator()); + assertEquals(1, Fraction.ZERO.getDenominator()); + + assertEquals(1, Fraction.ONE.getNumerator()); + assertEquals(1, Fraction.ONE.getDenominator()); + + /* + * All these constants need not be supported. + * Users can create whatever constants they require. + * The special constants ZERO and ONE are for the additive and multiplicative identity. + * + * assertEquals(1, Fraction.ONE_HALF.getNumerator()); + * assertEquals(2, Fraction.ONE_HALF.getDenominator()); + * + * assertEquals(1, Fraction.ONE_THIRD.getNumerator()); + * assertEquals(3, Fraction.ONE_THIRD.getDenominator()); + * + * assertEquals(2, Fraction.TWO_THIRDS.getNumerator()); + * assertEquals(3, Fraction.TWO_THIRDS.getDenominator()); + * + * assertEquals(1, Fraction.ONE_QUARTER.getNumerator()); + * assertEquals(4, Fraction.ONE_QUARTER.getDenominator()); + * + * assertEquals(1, Fraction.TWO_QUARTERS.getNumerator()); + * assertEquals(2, Fraction.TWO_QUARTERS.getDenominator()); + * + * assertEquals(3, Fraction.THREE_QUARTERS.getNumerator()); + * assertEquals(4, Fraction.THREE_QUARTERS.getDenominator()); + * + * assertEquals(1, Fraction.ONE_FIFTH.getNumerator()); + * assertEquals(5, Fraction.ONE_FIFTH.getDenominator()); + * + * assertEquals(2, Fraction.TWO_FIFTHS.getNumerator()); + * assertEquals(5, Fraction.TWO_FIFTHS.getDenominator()); + * + * assertEquals(3, Fraction.THREE_FIFTHS.getNumerator()); + * assertEquals(5, Fraction.THREE_FIFTHS.getDenominator()); + * + * assertEquals(4, Fraction.FOUR_FIFTHS.getNumerator()); + * assertEquals(5, Fraction.FOUR_FIFTHS.getDenominator()); + */ + } + + @Test + public void testFactory_int_int() { + Fraction f = null; + + // zero + f = Fraction.of(0, 1); + assertEquals(0, f.getNumerator()); + assertEquals(1, f.getDenominator()); + + f = Fraction.of(0, 2); + assertEquals(0, f.getNumerator()); + assertEquals(1, f.getDenominator()); + + // normal + f = Fraction.of(1, 1); + assertEquals(1, f.getNumerator()); + assertEquals(1, f.getDenominator()); + + f = Fraction.of(2, 1); + assertEquals(2, f.getNumerator()); + assertEquals(1, f.getDenominator()); + + f = Fraction.of(23, 345); + assertEquals(1, f.getNumerator()); + assertEquals(15, f.getDenominator()); + + // improper + f = Fraction.of(22, 7); + assertEquals(22, f.getNumerator()); + assertEquals(7, f.getDenominator()); + + // negatives + f = Fraction.of(-6, 10); + assertEquals(-3, f.getNumerator()); + assertEquals(5, f.getDenominator()); + + f = Fraction.of(6, -10); + assertEquals(3, f.getNumerator()); + assertEquals(-5, f.getDenominator()); + + f = Fraction.of(-6, -10); + assertEquals(-3, f.getNumerator()); + assertEquals(-5, f.getDenominator()); + + // zero denominator + assertThrows(ArithmeticException.class, () -> Fraction.of(1, 0)); + assertThrows(ArithmeticException.class, () -> Fraction.of(2, 0)); + assertThrows(ArithmeticException.class, () -> Fraction.of(-3, 0)); + + // lang cannot represent the unsimplified fraction with MIN_VALUE as the denominator + // assertThrows(ArithmeticException.class, () -> Fraction.getFraction(4, Integer.MIN_VALUE)); + // assertThrows(ArithmeticException.class, () -> Fraction.getFraction(1, Integer.MIN_VALUE)); + // numbers will always simplify the fraction + f = Fraction.of(4, Integer.MIN_VALUE); + assertEquals(-1, f.signum()); + assertEquals(1, f.getNumerator()); + assertEquals(Integer.MIN_VALUE / 4, f.getDenominator()); + // numbers can use MIN_VALUE as the denominator + f = Fraction.of(1, Integer.MIN_VALUE); + assertEquals(-1, f.signum()); + assertEquals(1, f.getNumerator()); + assertEquals(Integer.MIN_VALUE, f.getDenominator()); + } + +/* + * Removed as not supported in numbers. + * + * @Test + * public void testFactory_int_int_int() { + * Fraction f = null; + * + * // zero + * f = Fraction.of(0, 0, 2); + * assertEquals(0, f.getNumerator()); + * assertEquals(1, f.getDenominator()); + * + * f = Fraction.of(2, 0, 2); + * assertEquals(2, f.getNumerator()); + * assertEquals(1, f.getDenominator()); + * + * f = Fraction.of(0, 1, 2); + * assertEquals(1, f.getNumerator()); + * assertEquals(2, f.getDenominator()); + * + * // normal + * f = Fraction.of(1, 1, 2); + * assertEquals(3, f.getNumerator()); + * assertEquals(2, f.getDenominator()); + * + * // negatives + * assertThrows(ArithmeticException.class, () -> Fraction.of(1, -6, -10)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(1, -6, -10)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(1, -6, -10)); + * + * // negative whole + * f = Fraction.of(-1, 6, 10); + * assertEquals(-8, f.getNumerator()); + * assertEquals(5, f.getDenominator()); + * + * assertThrows(ArithmeticException.class, () -> Fraction.of(-1, -6, 10)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(-1, 6, -10)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(-1, -6, -10)); + * + * // zero denominator + * assertThrows(ArithmeticException.class, () -> Fraction.of(0, 1, 0)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(1, 2, 0)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(-1, -3, 0)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(Integer.MAX_VALUE, 1, 2)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(-Integer.MAX_VALUE, 1, 2)); + * + * // very large + * f = Fraction.of(-1, 0, Integer.MAX_VALUE); + * assertEquals(-1, f.getNumerator()); + * assertEquals(1, f.getDenominator()); + * + * // negative denominators not allowed in this constructor. + * assertThrows(ArithmeticException.class, () -> Fraction.of(0, 4, Integer.MIN_VALUE)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(1, 1, Integer.MAX_VALUE)); + * assertThrows(ArithmeticException.class, () -> Fraction.of(-1, 2, Integer.MAX_VALUE)); + * } + */ + + @Test + public void testReducedFactory_int_int() { + Fraction f = null; + + // zero + f = Fraction.of(0, 1); + assertEquals(0, f.getNumerator()); + assertEquals(1, f.getDenominator()); + + // normal + f = Fraction.of(1, 1); + assertEquals(1, f.getNumerator()); + assertEquals(1, f.getDenominator()); + + f = Fraction.of(2, 1); + assertEquals(2, f.getNumerator()); + assertEquals(1, f.getDenominator()); + + // improper + f = Fraction.of(22, 7); + assertEquals(22, f.getNumerator()); + assertEquals(7, f.getDenominator()); + + // negatives + f = Fraction.of(-6, 10); + assertEquals(-3, f.getNumerator()); + assertEquals(5, f.getDenominator()); + + f = Fraction.of(6, -10); + assertEquals(3, f.getNumerator()); + assertEquals(-5, f.getDenominator()); + + f = Fraction.of(-6, -10); + assertEquals(-3, f.getNumerator()); + assertEquals(-5, f.getDenominator()); + + // zero denominator + assertThrows(ArithmeticException.class, () -> Fraction.of(1, 0)); + assertThrows(ArithmeticException.class, () -> Fraction.of(2, 0)); + assertThrows(ArithmeticException.class, () -> Fraction.of(-3, 0)); + + // reduced + f = Fraction.of(0, 2); + assertEquals(0, f.getNumerator()); + assertEquals(1, f.getDenominator()); + + f = Fraction.of(2, 2); + assertEquals(1, f.getNumerator()); + assertEquals(1, f.getDenominator()); + + f = Fraction.of(2, 4); + assertEquals(1, f.getNumerator()); + assertEquals(2, f.getDenominator()); + + f = Fraction.of(15, 10); + assertEquals(3, f.getNumerator()); + assertEquals(2, f.getDenominator()); + + f = Fraction.of(121, 22); + assertEquals(11, f.getNumerator()); + assertEquals(2, f.getDenominator()); + + // Extreme values + // OK, can reduce before negating + f = Fraction.of(-2, Integer.MIN_VALUE); + assertEquals(-1, f.getNumerator()); + assertEquals(Integer.MIN_VALUE / 2, f.getDenominator()); + + // lang requires the sign to be in the numerator so this would throw. + // numbers allows the sign to be in the denominator so this does not throw. Review comment: Move this comment below the commented out assertion from lang. This is then the same as the other methods that comment out the lang assertion and then show the numbers functionality. If you rebase and squash then this is ready for merge. Thanks. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
