Skip to content

Commit

Permalink
Merge pull request #10 from jrdnull/master
Browse files Browse the repository at this point in the history
Add clock problem
  • Loading branch information
kytrinyx committed Jul 5, 2014
2 parents 2801f3c + 1ff720d commit 25b8e92
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 1 deletion.
113 changes: 113 additions & 0 deletions clock/clock_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

require_once realpath(__DIR__ . '/clock.php');

class ClockTest extends PHPUnit_Framework_TestCase
{
public function testOnTheHour()
{
$clock = new Clock(8);

$this->assertEquals('08:00', $clock->__toString());
}

public function testPastTheHour()
{
$this->markTestSkipped();

$clock = new Clock(11, 9);

$this->assertEquals('11:09', $clock->__toString());
}

public function testAddingAFewMinutes()
{
$this->markTestSkipped();

$clock = new Clock(10);

$clock = $clock->add(3);

$this->assertEquals('10:03', $clock->__toString());
}

public function testAddingOverAnHour()
{
$this->markTestSkipped();

$clock = new Clock(10);

$clock = $clock->add(61);

$this->assertEquals('11:01', $clock->__toString());
}

public function testWrapAroundAtMidnight()
{
$this->markTestSkipped();

$clock = new Clock(23, 30);

$clock = $clock->add(60);

$this->assertEquals('00:30', $clock->__toString());
}

public function testSubtractMinutes()
{
$this->markTestSkipped();

$clock = new Clock(10);

$clock = $clock->sub(90);

$this->assertEquals('08:30', $clock->__toString());
}

public function testWrapAroundBackwards()
{
$this->markTestSkipped();

$clock = new Clock(0, 30);

$clock = $clock->sub(60);

$this->assertEquals('23:30', $clock->__toString());
}

public function testWrapAroundDay()
{
$this->markTestSkipped();

$clock = new Clock(5, 32);

$clock = $clock->add(25 * 60);

$this->assertEquals('06:32', $clock->__toString());
}

public function testWrapAroundDayBackwards()
{
$this->markTestSkipped();

$clock = new Clock(5, 32);

$clock = $clock->sub(25 * 60);

$this->assertEquals('04:32', $clock->__toString());
}

public function testEquivalentClocks()
{
$this->markTestSkipped();

$this->assertEquals(new Clock(15, 37), new Clock(15, 37));
}

public function testInequivalentClocks()
{
$this->markTestSkipped();

$this->assertNotEquals(new Clock(01, 01), new Clock(18, 32));
}
}
61 changes: 61 additions & 0 deletions clock/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

class Clock
{
private $minutes;

/**
* @param int $hour
* @param int $minutes
*/
public function __construct($hour, $minutes = 0)
{
$this->minutes = $hour * 60 + $minutes;
}

/**
* Returns a new Clock incremented by $minutes
*
* @param int $minutes
* @return Clock
*/
public function add($minutes)
{
return $this->build($this->minutes + $minutes);
}

/**
* Returns a new Clock deincremented by $minutes
*
* @param int $minutes
* @return Clock
*/
public function sub($minutes)
{
return $this->build($this->minutes - $minutes);
}

/**
* Returns the string representation of the receiver
*
* @return string
*/
public function __toString()
{
return sprintf('%02d:%02d', $this->minutes / 60, $this->minutes % 60);
}

/**
* @param $minutes
* @return Clock
*/
private function build($minutes)
{
if ($minutes < 60) {
$minutes += 24 * 60;
}

$hour = floor($minutes / 60);
return new Clock($hour < 24 ? $hour : $hour - 24, $minutes % 60);
}
}
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"active": false,
"problems": [
"trinary",
"wordy"
"wordy",
"clock"
],
"deprecated": [

Expand Down

0 comments on commit 25b8e92

Please sign in to comment.