roman: add more roman letters and test

This commit is contained in:
vinchent 2024-09-22 21:24:24 +02:00
parent 7d751cab3d
commit 6da23dc641
2 changed files with 46 additions and 18 deletions

View File

@ -8,6 +8,15 @@ type RomanNumeral struct {
} }
var allRomanNumerals = []RomanNumeral{ var allRomanNumerals = []RomanNumeral{
{1000, "M"},
{900, "CM"},
{500, "D"},
{400, "CD"},
{900, "CM"},
{100, "C"},
{90, "XC"},
{50, "L"},
{40, "XL"},
{10, "X"}, {10, "X"},
{9, "IX"}, {9, "IX"},
{5, "V"}, {5, "V"},

View File

@ -1,30 +1,49 @@
package roman package roman
import "testing" import (
"fmt"
"testing"
)
func TestRomanNemerals(t *testing.T) { func TestRomanNemerals(t *testing.T) {
cases := []struct { cases := []struct {
Description string Arabic int
Arabic int Roman string
Want string
}{ }{
{"1 gets converted to I", 1, "I"}, {Arabic: 1, Roman: "I"},
{"2 gets converted to II", 2, "II"}, {Arabic: 2, Roman: "II"},
{"3 gets converted to III", 3, "III"}, {Arabic: 3, Roman: "III"},
{"4 gets converted to IV", 4, "IV"}, {Arabic: 4, Roman: "IV"},
{"5 gets converted to V", 5, "V"}, {Arabic: 5, Roman: "V"},
{"7 gets converted to VII", 7, "VII"}, {Arabic: 6, Roman: "VI"},
{"9 gets converted to IX", 9, "IX"}, {Arabic: 7, Roman: "VII"},
{"10 gets converted to X", 10, "X"}, {Arabic: 8, Roman: "VIII"},
{"14 gets converted to XIV", 14, "XIV"}, {Arabic: 9, Roman: "IX"},
{"18 gets converted to XVIII", 18, "XVIII"}, {Arabic: 10, Roman: "X"},
{"20 gets converted to XX", 20, "XX"}, {Arabic: 14, Roman: "XIV"},
{"39 gets converted to XXXIX", 39, "XXXIX"}, {Arabic: 18, Roman: "XVIII"},
{Arabic: 20, Roman: "XX"},
{Arabic: 39, Roman: "XXXIX"},
{Arabic: 40, Roman: "XL"},
{Arabic: 47, Roman: "XLVII"},
{Arabic: 49, Roman: "XLIX"},
{Arabic: 50, Roman: "L"},
{Arabic: 100, Roman: "C"},
{Arabic: 90, Roman: "XC"},
{Arabic: 400, Roman: "CD"},
{Arabic: 500, Roman: "D"},
{Arabic: 900, Roman: "CM"},
{Arabic: 1000, Roman: "M"},
{Arabic: 1984, Roman: "MCMLXXXIV"},
{Arabic: 3999, Roman: "MMMCMXCIX"},
{Arabic: 2014, Roman: "MMXIV"},
{Arabic: 1006, Roman: "MVI"},
{Arabic: 798, Roman: "DCCXCVIII"},
} }
for _, test := range cases { for _, test := range cases {
t.Run(test.Description, func(t *testing.T) { t.Run(fmt.Sprintf("%d gets converted to %q", test.Arabic, test.Roman), func(t *testing.T) {
got := ConvertToRoman(test.Arabic) got := ConvertToRoman(test.Arabic)
want := test.Want want := test.Roman
if got != want { if got != want {
t.Errorf("got %q, want %q", got, want) t.Errorf("got %q, want %q", got, want)