Created
February 8, 2013 04:26
-
-
Save al-the-x/4736583 to your computer and use it in GitHub Desktop.
Roman Numeral converter from OrlandoPHP meeting on 02/07/2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Roman_converter | |
{ | |
public $numerals = array( | |
'I' => 1, | |
'II' => 2, | |
'V' => 5, | |
'X' => 10, | |
'L' => 50, | |
'C' => 100, | |
'D' => 500, | |
'M' => 1000, | |
); | |
private $current_numeral; | |
public function convert($numeral) { | |
// 'I' => 1 | |
// 'II' => 2 | |
if(strlen($numeral) == 1) { | |
return $this->numerals[$numeral]; | |
} | |
if(strlen($numeral) == 2) { | |
return $this->numerals[$numeral[0]] + $this->numerals[$numeral[1]]; | |
} | |
} | |
public function getCurrentNumeral() { | |
return $this->current_numeral; | |
} | |
public function setCurrentNumeral($numeral) { | |
$this->current_numeral = $numeral; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'PHPUnit/Framework.php'; | |
require_once 'main.php'; | |
class Test extends PHPUnit_Framework_TestCase | |
{ | |
function test_convert_single_digits() { | |
$concrete = new Roman_converter(); | |
$this->assertEquals(1,$concrete->convert('I')); | |
$this->assertEquals(5, $concrete->convert('V')); | |
$this->assertEquals(10, $concrete->convert('X')); | |
$this->assertEquals(50, $concrete->convert('L')); | |
$this->assertEquals(100, $concrete->convert('C')); | |
$this->assertEquals(500, $concrete->convert('D')); | |
$this->assertEquals(1000, $concrete->convert('M')); | |
} | |
function test_convert_with_multiples() | |
{ | |
$c = new Roman_converter; | |
$this->assertEquals(2, $c->convert('II')); | |
} | |
function test_class() | |
{ | |
$class = class_exists('Roman_converter'); | |
$this->assertTrue($class); | |
$this->concrete = new Roman_converter; | |
$numerals = isset($this->concrete->numerals); | |
$this->assertTrue($numerals); | |
} | |
function test_numeralnotempty() { | |
$this->concrete = new Roman_converter; | |
$empty = is_null($this->concrete->getCurrentNumeral()); | |
$this->assertTrue($empty); | |
$num = is_null($this->concrete->setCurrentNumeral("1")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment