function FractionUnitTestCase::testFraction in Fraction 7
Test the Fraction class and it's methods.
File
- ./
fraction.test, line 32 - Fraction tests
Class
- FractionUnitTestCase
- Provides tests for the Fraction class.
Code
function testFraction() {
// Test creation of a fraction.
$fraction = fraction(1, 2);
$numerator = $fraction
->getNumerator();
$denominator = $fraction
->getDenominator();
$message = 'A fraction of 1/2 should return a numerator of 1.';
$this
->assertEqual($numerator, '1', $message);
$message = 'A fraction of 1/2 should return a denominator of 2.';
$this
->assertEqual($denominator, '2', $message);
// Test creation of an empty fraction.
$fraction = fraction();
$numerator = $fraction
->getNumerator();
$denominator = $fraction
->getDenominator();
$message = 'An empty fraction should return a numerator of 0.';
$this
->assertEqual($numerator, '0', $message);
$message = 'An empty fraction should return a denominator of 1.';
$this
->assertEqual($denominator, '1', $message);
// Test returning a fraction as a string.
$result = fraction(1, 2)
->toString();
$message = 'A fraction with a numerator of 1 and a denominator of 2 should return a string of "1/2".';
$this
->assertEqual($result, '1/2', $message);
// Test returning a fraction as a string with a different separator.
$result = fraction(1, 2)
->toString(':');
$message = 'A fraction with a numerator of 1 and a denominator of 2 should return a string of "1:2" (when a colon separator is specified).';
$this
->assertEqual($result, '1:2', $message);
// Test division by zero.
$result = fraction(1, 0)
->toString();
$message = 'A fraction of 1/0 should return 0/1 (avoid division by zero).';
$this
->assertEqual($result, '0/1', $message);
// Test normalization of negative fractions.
$result = fraction(-1, 2)
->toString();
$message = 'A fraction of -1/2 should normalize to -1/2.';
$this
->assertEqual($result, '-1/2', $message);
$result = fraction(1, -2)
->toString();
$message = 'A fraction of 1/-2 should normalize to -1/2.';
$this
->assertEqual($result, '-1/2', $message);
$result = fraction(-1, -2)
->toString();
$message = 'A fraction of -1/-2 should normalize to 1/2.';
$this
->assertEqual($result, '1/2', $message);
// Test converting a fraction to a decimal.
$result = fraction(1, 2)
->toDecimal(1);
$message = 'A fraction of 1/2 should return a decimal of 0.5 (with precision 1)';
$this
->assertEqual($result, '0.5', $message);
// Test auto precision for non base 10 fractions.
$result = fraction(1, 2)
->toDecimal(0, TRUE);
$message = 'A fraction of 1/2 should return a decimal of 0.5 (with precision 0, auto_precision)';
$this
->assertEqual($result, '0.5', $message);
// Test decimal precision (rounding up).
$result = fraction(1, 2)
->toDecimal(0);
$message = 'A fraction of 1/2 with no precision should round to 1.';
$this
->assertEqual($result, '1', $message);
// Test decimal precision (rounding down).
$result = fraction(2, 5)
->toDecimal(0);
$message = 'A fraction of 2/5 with no precision should round to 0.';
$this
->assertEqual($result, '0', $message);
// Test decimal automatic precision.
$result = fraction(1, 1000)
->toDecimal(2, TRUE);
$message = 'A fraction of 1/1000 with precision 2 and auto-precision enabled should round to 0.001.';
$this
->assertEqual($result, '0.001', $message);
// Test creation of a fraction from a decimal.
$result = fraction()
->fromDecimal(0.5)
->toString();
$message = 'The fromDecimal() method should create a fraction of 5/10 from a decimal of 0.5.';
$this
->assertEqual($result, '5/10', $message);
// Test calculation of greatest common divisor.
$result = fraction(5, 10)
->gcd();
$message = 'The greatest common divisor of 1/2 is 5.';
$this
->assertEqual($result, '5', $message);
// Test reduction of a fraction to its simplest form.
$result = fraction(5, 10)
->reduce()
->toString();
$message = 'A fraction of 5/10 should be reducible to 1/2.';
$this
->assertEqual($result, '1/2', $message);
// Test fraction addition.
$result = fraction(1, 2)
->add(fraction(1, 3))
->toString();
$message = '1/2 + 1/3 = 5/6';
$this
->assertEqual($result, '5/6', $message);
// Test fraction subtraction.
$result = fraction(2, 3)
->subtract(fraction(1, 7))
->toString();
$message = '2/3 - 1/7 = 11/21';
$this
->assertEqual($result, '11/21', $message);
// Test fraction multiplication.
$result = fraction(2, 5)
->multiply(fraction(1, 4))
->toString();
$message = '2/5 * 1/4 = 2/20';
$this
->assertEqual($result, '2/20', $message);
// Test fraction division.
$result = fraction(5, 7)
->divide(fraction(1, 5))
->toString();
$message = '5/7 / 1/5 = 25/7';
$this
->assertEqual($result, '25/7', $message);
// Test fraction reciprocation.
$result = fraction(1, 2)
->reciprocate()
->toString();
$message = 'The reciprocal of 1/2 is 2/1.';
$this
->assertEqual($result, '2/1', $message);
// Test that reciprocation of a zero numerator does not result in division by zero.
$result = fraction(0, 1)
->reciprocate()
->toString();
$message = 'The reciprocal of 0/1 is 0/1 (avoid division by zero).';
$this
->assertEqual($result, '0/1', $message);
}