Skip to content

Commit

Permalink
Continue to search Java errors on Travis.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaillard committed Aug 9, 2016
1 parent b6071ee commit bf02f51
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = function(grunt) {
options: {
bin : 'vendor/bin/phpunit',
configuration : 'phpunit.xml.dist',
group : 'NikonE990Test'
// group : 'NikonE990Test'
}

}, /* PHPUnit Task */
Expand Down
261 changes: 261 additions & 0 deletions src/main/php/Gomoob/MetadataExtractor/Lang/Rational.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,266 @@
*/
class Rational extends \Exception
{
/** Holds the numerator. */
private $numerator;

/** Holds the denominator. */
private $denominator;

/**
* Creates a new instance of Rational. Rational objects are immutable, so
* once you've set your numerator and denominator values here, you're stuck
* with them!
*/
public function __construct($numerator, $denominator)
{
$this->numerator = $numerator;
$this->denominator = $denominator;
}

/**
* Returns the value of the specified number as a <code>double</code>.
* This may involve rounding.
*
* @return double the numeric value represented by this object after conversion to type <code>double</code>.
*/
public function doubleValue()
{
return $this->numerator == 0 ? 0.0 : doubleval($this->numerator) / doubleval($this->denominator);
}

/**
* Returns the value of the specified number as a <code>float</code>.
* This may involve rounding.
*
* @return float the numeric value represented by this object after conversion to type <code>float</code>.
*/
public function floatValue()
{
return _numerator == 0 ? 0.0 : floatval($this->numerator) / floatval($this->denominator);
}

/**
* Returns the value of the specified number as a <code>byte</code>.
* This may involve rounding or truncation. This implementation simply
* casts the result of {@link Rational#doubleValue} to <code>byte</code>.
*
* @return the numeric value represented by this object after conversion
* to type <code>byte</code>.
*/
public function byteValue()
{
// TODO: Invalid in PHP
return $this->doubleValue();
}

/**
* Returns the value of the specified number as an <code>int</code>.
* This may involve rounding or truncation. This implementation simply
* casts the result of {@link Rational#doubleValue} to <code>int</code>.
*
* @return the numeric value represented by this object after conversion
* to type <code>int</code>.
*/
public function intValue()
{
return intval($this->doubleValue());
}

/**
* Returns the value of the specified number as a <code>long</code>.
* This may involve rounding or truncation. This implementation simply
* casts the result of {@link Rational#doubleValue} to <code>long</code>.
*
* @return the numeric value represented by this object after conversion
* to type <code>long</code>.
*/
public function longValue()
{
return intval($this->doubleValue());
}

/**
* Returns the value of the specified number as a <code>short</code>.
* This may involve rounding or truncation. This implementation simply
* casts the result of {@link Rational#doubleValue} to <code>short</code>.
*
* @return the numeric value represented by this object after conversion
* to type <code>short</code>.
*/
public function shortValue()
{
return intval($this->doubleValue());
}


/** Returns the denominator. */
public function getDenominator()
{
return $this->denominator;
}

/** Returns the numerator. */
public function getNumerator()
{
return $this->numerator;
}

/**
* Returns the reciprocal value of this object as a new Rational.
*
* @return the reciprocal in a new object
*/
public function getReciprocal()
{
return new Rational($this->denominator, $this->numerator);
}

/** Checks if this {@link Rational} number is an Integer, either positive or negative. */
public function isInteger()
{
return $this->denominator == 1 ||
($this->denominator != 0 && ($this->numerator % $this->denominator == 0)) ||
($this->denominator == 0 && $this->numerator == 0);
}

/** Checks if either the numerator or denominator are zero. */
public function isZero()
{
return $this->numerator == 0 || $this->denominator == 0;
}

/**
* Returns a string representation of the object of form <code>numerator/denominator</code>.
*
* @return a string representation of the object.
*/
public function toString()
{
return $this->numerator + "/" + $this->denominator;
}

/** Returns the simplest representation of this {@link Rational}'s value possible. */
public function toSimpleString($allowDecimal)
{
if ($this->denominator == 0 && $this->numerator != 0) {
return $this->toString();
} elseif ($this->isInteger()) {
return strval($this->intValue());
} elseif ($this->numerator != 1 && $this->denominator % $this->numerator == 0) {
// common factor between denominator and numerator
$newDenominator = $this->denominator / $this->numerator;
return (new Rational(1, $newDenominator))->toSimpleString($allowDecimal);
} else {
$simplifiedInstance = $this->getSimplifiedInstance();
if ($allowDecimal) {
$doubleString = Double.toString($simplifiedInstance->doubleValue());
if ($doubleString->length() < 5) {
return $doubleString;
}
}
return $simplifiedInstance->toString();
}
}

/**
* Decides whether a brute-force simplification calculation should be avoided
* by comparing the maximum number of possible calculations with some threshold.
*
* @return true if the simplification should be performed, otherwise false
*/
private function tooComplexForSimplification()
{
$maxPossibleCalculations = (((double) (Math.min($this->denominator, $this->numerator) - 1) / 5.0) + 2);
$maxSimplificationCalculations = 1000;
return $maxPossibleCalculations > $maxSimplificationCalculations;
}

/**
* Compares two {@link Rational} instances, returning true if they are mathematically
* equivalent (in consistence with {@link Rational#equals(Object)} method).
*
* @param that the {@link Rational} to compare this instance to.
* @return the value {@code 0} if this {@link Rational} is
* equal to the argument {@link Rational} mathematically; a value less
* than {@code 0} if this {@link Rational} is less
* than the argument {@link Rational}; and a value greater
* than {@code 0} if this {@link Rational} is greater than the argument
* {@link Rational}.
*/
public function compareTo(Rational $that)
{
return $this->doubleValue() < $that->doubleValue() ? -1 :
(($this->doubleValue() == $that->doubleValue()) ? 0 : 1);
}

/**
* Compares two {@link Rational} instances, returning true if they are mathematically
* equivalent.
*
* @param obj the {@link Rational} to compare this instance to.
* @return true if instances are mathematically equivalent, otherwise false. Will also
* return false if <code>obj</code> is not an instance of {@link Rational}.
*/
public function equals($obj)
{
if ($obj === null || !($obj instanceof Rational)) {
return false;
}

$that = $obj;
return $this->doubleValue() === $that->doubleValue();
}

public function hashCode()
{
return (23 * inval($this->denominator)) + intval($this->numerator);
}

/**
* <p>
* Simplifies the {@link Rational} number.</p>
* <p>
* Prime number series: 1, 2, 3, 5, 7, 9, 11, 13, 17</p>
* <p>
* To reduce a rational, need to see if both numerator and denominator are divisible
* by a common factor. Using the prime number series in ascending order guarantees
* the minimum number of checks required.</p>
* <p>
* However, generating the prime number series seems to be a hefty task. Perhaps
* it's simpler to check if both d &amp; n are divisible by all numbers from 2 {@literal ->}
* (Math.min(denominator, numerator) / 2). In doing this, one can check for 2
* and 5 once, then ignore all even numbers, and all numbers ending in 0 or 5.
* This leaves four numbers from every ten to check.</p>
* <p>
* Therefore, the max number of pairs of modulus divisions required will be:</p>
* <pre><code>
* 4 Math.min(denominator, numerator) - 1
* -- * ------------------------------------ + 2
* 10 2
*
* Math.min(denominator, numerator) - 1
* = ------------------------------------ + 2
* 5
* </code></pre>
*
* @return a simplified instance, or if the Rational could not be simplified,
* returns itself (unchanged)
*/
public function getSimplifiedInstance()
{
if ($this->tooComplexForSimplification()) {
return $this;
}
for ($factor = 2; $factor <= Math.min($this->denominator, $this->numerator); $factor++) {
if (($factor % 2 == 0 && $factor > 2) || ($factor % 5 == 0 && $factor > 5)) {
continue;
}
if ($this->denominator % $factor == 0 && $this->numerator % $factor == 0) {
// found a common factor
return new Rational($this->numerator / $factor, $this->denominator / $factor);
}
}
return this;
}
}
8 changes: 5 additions & 3 deletions src/test/php/Gomoob/BinaryDriver/JavaDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public function testCreate()
// Calls the method to be tested
$javaDriver = JavaDriver::create($logger);

$javaDriver->on('error', function ($line) {
echo '[ERROR] ' . $line . PHP_EOL;
});

// TODO: To be deleted
$output = $javaDriver->command(['-version']);
var_dump($output);

$javaDriver->on('error', function ($line) {
echo '[ERROR] ' . $line . PHP_EOL;
});


// Makes a simple call to ensure it works
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function testReadMetadata()
// Checks 'Exif IFD0' directory
$directory = $metadata->getDirectories()[0];

$tag = $directory->getTagName(ExifIFD0Directory::TAG_X_RESOLUTION);
var_dump($tag);
// $tag = $directory->getTagName(ExifIFD0Directory::TAG_X_RESOLUTION);
// var_dump($tag);

// TODO: Continue testing
}
Expand Down

0 comments on commit bf02f51

Please sign in to comment.