Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Created May 3, 2013 01:56
Show Gist options
  • Save al-the-x/5506734 to your computer and use it in GitHub Desktop.
Save al-the-x/5506734 to your computer and use it in GitHub Desktop.
Coding Dojo at OrlandoPHP.org: Roman Numerals
<?php
/**
* Given a decimal number like 1, 3 7, 13, 57, 42, convert to its Roman Numeral
* equivalent. For example, "46" is "XLVI", "973" is "CMLXXIII", "1984" is
* "MCMLXXXIV".
*/
function toRoman($decimal){
$romanNumerals = array(
"1"=>"I",
"2"=>"II",
"3" => "III",
"4" => "IV",
"5"=>"V",
"10"=>"X",
"50"=>"L",
100 => 'C',
500 => 'D',
1000 => 'M',
);
#array_key_exists
return @$romanNumerals[$decimal];
}
<?php
require_once 'main.php';
class Test extends PHPUnit_Framework_TestCase
{
function test_something ( )
{
//$this->assertTrue(false, 'This will never work');
//$this->assertEquals(true, false, 'Some message');
//$this->assertFalse((boolean) 0);
}
// call function toRoman() with 46, assertEquals() "XLVI"
/**
* in | out
* 1 | I
* 5 | V
* 10 | X
* 50 | L
* 100 | C
* 500 | D
* 1000 | M
* 2 | II
* 3 | III
* 4 | IV
* 6 | VI
* 7 | VII
* 8 | VIII
* 9 | IX
* 20 | XX
* 30 | XXX
* 40 | XL
* 46 | XLVI
* 60 | LX
* 70 | LXX
* 80 | LXXX
* 90 | XC
* 200 | CC
* 300 | CCC
* 973 | CMLXXIII
*/
function getRomanNumerals()
{
return array(
array('I', 1),
array('V', 5),
array('X', 10),
array('L', 50),
array('C', 100),
array('D', 500),
array('M', 1000),
);
}
/**
* Check out "Data Providers" in the PHPUnit manual: http://j.mp/154sCgt
*
* @dataProvider getRomanNumerals
*/
function test_toRoman($numeral, $decimal)
{
$this->assertEquals($numeral, toRoman($decimal), 'Input: ' . $decimal);
}
function test_toRomanReturnsIForOne()
{
$this->assertEquals('I', toRoman(1));
}
function test_toRomanReturnsVForFive()
{
$this->assertEquals('V', toRoman(5), 'Input of 5 should return V');
}
function test_toRomanReturnsXForTen()
{
$this->assertEquals('X', toRoman(10), 'Input 10 should return X');
}
function test_toRomanReturnsLForFifty()
{
$this->assertEquals('L', toRoman(50), 'Input 50 should return L');
}
function test_toRomanReturnsIIForTwo()
{
$this->assertEquals('II', toRoman(2), 'Input 2 should return II');
}
function test_toRomanReturnsIIIForThree()
{
$this->assertEquals('III', toRoman(3), 'Input 3 should return III');
}
function test_toRomanReturnsIVForFour()
{
$this->assertEquals('IV', toRoman(4), 'Input 4 should return IV');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment