Skip to content

Commit d3a4fe4

Browse files
committed
✨ (roman-numerals): ConvertToArabic add second test and refactor
1 parent d231f57 commit d3a4fe4

File tree

2 files changed

+54
-34
lines changed

2 files changed

+54
-34
lines changed

property-base-test/roman_number.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ func ConvertToRoman(arabic int) string {
3333
}
3434
return result.String()
3535
}
36+
37+
func ConvertToArabic(roman string) int {
38+
total := 0
39+
for range roman {
40+
total++
41+
}
42+
return total
43+
}

property-base-test/roman_number_test.go

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,42 @@ import (
55
"testing"
66
)
77

8+
var cases = []struct {
9+
Arabic int
10+
Roman string
11+
}{
12+
{Arabic: 1, Roman: "I"},
13+
{Arabic: 2, Roman: "II"},
14+
{Arabic: 3, Roman: "III"},
15+
{Arabic: 4, Roman: "IV"},
16+
{Arabic: 5, Roman: "V"},
17+
{Arabic: 6, Roman: "VI"},
18+
{Arabic: 7, Roman: "VII"},
19+
{Arabic: 8, Roman: "VIII"},
20+
{Arabic: 9, Roman: "IX"},
21+
{Arabic: 10, Roman: "X"},
22+
{Arabic: 14, Roman: "XIV"},
23+
{Arabic: 18, Roman: "XVIII"},
24+
{Arabic: 20, Roman: "XX"},
25+
{Arabic: 39, Roman: "XXXIX"},
26+
{Arabic: 40, Roman: "XL"},
27+
{Arabic: 47, Roman: "XLVII"},
28+
{Arabic: 49, Roman: "XLIX"},
29+
{Arabic: 50, Roman: "L"},
30+
{Arabic: 100, Roman: "C"},
31+
{Arabic: 90, Roman: "XC"},
32+
{Arabic: 400, Roman: "CD"},
33+
{Arabic: 500, Roman: "D"},
34+
{Arabic: 900, Roman: "CM"},
35+
{Arabic: 1000, Roman: "M"},
36+
{Arabic: 1984, Roman: "MCMLXXXIV"},
37+
{Arabic: 3999, Roman: "MMMCMXCIX"},
38+
{Arabic: 2014, Roman: "MMXIV"},
39+
{Arabic: 1006, Roman: "MVI"},
40+
{Arabic: 798, Roman: "DCCXCVIII"},
41+
}
42+
843
func TestRomanNumerals(t *testing.T) {
9-
cases := []struct {
10-
Arabic int
11-
Roman string
12-
}{
13-
{Arabic: 1, Roman: "I"},
14-
{Arabic: 2, Roman: "II"},
15-
{Arabic: 3, Roman: "III"},
16-
{Arabic: 4, Roman: "IV"},
17-
{Arabic: 5, Roman: "V"},
18-
{Arabic: 6, Roman: "VI"},
19-
{Arabic: 7, Roman: "VII"},
20-
{Arabic: 8, Roman: "VIII"},
21-
{Arabic: 9, Roman: "IX"},
22-
{Arabic: 10, Roman: "X"},
23-
{Arabic: 14, Roman: "XIV"},
24-
{Arabic: 18, Roman: "XVIII"},
25-
{Arabic: 20, Roman: "XX"},
26-
{Arabic: 39, Roman: "XXXIX"},
27-
{Arabic: 40, Roman: "XL"},
28-
{Arabic: 47, Roman: "XLVII"},
29-
{Arabic: 49, Roman: "XLIX"},
30-
{Arabic: 50, Roman: "L"},
31-
{Arabic: 100, Roman: "C"},
32-
{Arabic: 90, Roman: "XC"},
33-
{Arabic: 400, Roman: "CD"},
34-
{Arabic: 500, Roman: "D"},
35-
{Arabic: 900, Roman: "CM"},
36-
{Arabic: 1000, Roman: "M"},
37-
{Arabic: 1984, Roman: "MCMLXXXIV"},
38-
{Arabic: 3999, Roman: "MMMCMXCIX"},
39-
{Arabic: 2014, Roman: "MMXIV"},
40-
{Arabic: 1006, Roman: "MVI"},
41-
{Arabic: 798, Roman: "DCCXCVIII"},
42-
}
4344

4445
for _, test := range cases {
4546
t.Run(fmt.Sprintf("%d get converted to %q", test.Arabic, test.Roman), func(t *testing.T) {
@@ -50,3 +51,14 @@ func TestRomanNumerals(t *testing.T) {
5051
})
5152
}
5253
}
54+
55+
func TestConvertingToArabic(t *testing.T) {
56+
for _, test := range cases[:3] {
57+
t.Run(fmt.Sprintf("%q gets converted to %d", test.Roman, test.Arabic), func(t *testing.T) {
58+
got := ConvertToArabic(test.Roman)
59+
if got != test.Arabic {
60+
t.Errorf("got %d, want %d", got, test.Arabic)
61+
}
62+
})
63+
}
64+
}

0 commit comments

Comments
 (0)