You are here

fraction.test in Fraction 7

Fraction tests

File

fraction.test
View source
<?php

/**
 * @file
 * Fraction tests
 */

/**
 * Base class for all test cases.
 */
abstract class FractionBaseTestCase extends DrupalWebTestCase {
  public function setUp(array $modules = array()) {
    parent::setUp(array_merge($modules, array(
      'fraction',
    )));
  }

}

/**
 * Provides tests for the Fraction class.
 */
class FractionUnitTestCase extends FractionBaseTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Fraction unit tests',
      'description' => 'Test that the Fraction class works properly.',
      'group' => 'Fraction',
    );
  }

  /**
   * Test the Fraction class and it's methods.
   */
  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);
  }

}
class FractionEntityTestCase extends FractionBaseTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Fraction Entity integration tests',
      'description' => 'Tests that Fraction integrates with Entity well.',
      'group' => 'Fraction',
      'dependencies' => array(
        'entity',
      ),
    );
  }
  public function setUp(array $modules = array()) {
    parent::setUp(array(
      'entity',
      'fraction_entity_test',
    ));
  }
  public function testEntityIntegration() {

    // Initial data.
    $fraction_first = fraction_from_decimal('20.15');
    $fraction_second = fraction_from_decimal('14.28');

    // Create an entity.
    $entity = entity_create('fraction_entity_test', array(
      'title' => t('Title of Fraction Entity Test'),
      'type' => 'fraction_entity_test_bundle',
    ));
    $entity_wrapped = entity_metadata_wrapper('fraction_entity_test', $entity);
    $entity_wrapped->fraction_field = array(
      'numerator' => $fraction_first
        ->getNumerator(),
      'denominator' => $fraction_first
        ->getDenominator(),
    );
    $entity_wrapped
      ->save();

    // Check the value of fraction field.
    $entities = entity_load('fraction_entity_test', array(
      $entity_wrapped->id
        ->value(),
    ));
    $entity_created = reset($entities);
    $entity_created_wrapped = entity_metadata_wrapper('fraction_entity_test', $entity_created);
    $fraction_first_created = fraction()
      ->setNumerator($entity_created_wrapped->fraction_field->numerator
      ->value())
      ->setDenominator($entity_created_wrapped->fraction_field->denominator
      ->value());
    $this
      ->assertEqual($fraction_first
      ->toDecimal(), $fraction_first_created
      ->toDecimal(), t('Expected value !expected equals real !real', array(
      '!expected' => $fraction_first
        ->toDecimal(),
      '!real' => $fraction_first_created
        ->toDecimal(),
    )));

    // Update the value of fraction field.
    $entity_wrapped->fraction_field = array(
      'numerator' => $fraction_second
        ->getNumerator(),
      'denominator' => $fraction_second
        ->getDenominator(),
    );
    $entity_wrapped
      ->save();

    // Check the value of fraction field.
    $entities = entity_load('fraction_entity_test', array(
      $entity_wrapped->id
        ->value(),
    ));
    $entity_updated = reset($entities);
    $entity_updated_wrapped = entity_metadata_wrapper('fraction_entity_test', $entity_updated);
    $fraction_second_updated = fraction()
      ->setNumerator($entity_updated_wrapped->fraction_field->numerator
      ->value())
      ->setDenominator($entity_updated_wrapped->fraction_field->denominator
      ->value());
    $this
      ->assertEqual($fraction_second
      ->toDecimal(), $fraction_second_updated
      ->toDecimal(), t('Expected value !expected equals real !real', array(
      '!expected' => $fraction_second
        ->toDecimal(),
      '!real' => $fraction_second_updated
        ->toDecimal(),
    )));
  }

}

Classes

Namesort descending Description
FractionBaseTestCase Base class for all test cases.
FractionEntityTestCase
FractionUnitTestCase Provides tests for the Fraction class.