Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clock problem #10

Merged
merged 1 commit into from
Jul 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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