forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
romantointeger_test.go
53 lines (48 loc) · 1.81 KB
/
romantointeger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package conversion
import "testing"
var romanTestCases = map[string]int{
"I": 1, "II": 2, "III": 3, "IV": 4, "V": 5, "VI": 6,
"VII": 7, "VIII": 8, "IX": 9, "X": 10, "XI": 11, "XII": 12,
"XIII": 13, "XIV": 14, "XV": 15, "XVI": 16, "XVII": 17,
"XVIII": 18, "XIX": 19, "XX": 20, "XXXI": 31, "XXXII": 32,
"XXXIII": 33, "XXXIV": 34, "XXXV": 35, "XXXVI": 36, "XXXVII": 37,
"XXXVIII": 38, "XXXIX": 39, "XL": 40, "XLI": 41, "XLII": 42,
"XLIII": 43, "XLIV": 44, "XLV": 45, "XLVI": 46, "XLVII": 47,
"XLVIII": 48, "XLIX": 49, "L": 50, "LXXXIX": 89, "XC": 90,
"XCI": 91, "XCII": 92, "XCIII": 93, "XCIV": 94, "XCV": 95,
"XCVI": 96, "XCVII": 97, "XCVIII": 98, "XCIX": 99, "C": 100,
"CI": 101, "CII": 102, "CIII": 103, "CIV": 104, "CV": 105,
"CVI": 106, "CVII": 107, "CVIII": 108, "CIX": 109, "CXLIX": 149,
"CCCXLIX": 349, "CDLVI": 456, "D": 500, "DCIV": 604, "DCCLXXXIX": 789,
"DCCCXLIX": 849, "CMIV": 904, "M": 1000, "MVII": 1007, "MLXVI": 1066,
"MCCXXXIV": 1234, "MDCCLXXVI": 1776, "MMXXI": 2021, "MMDCCCVI": 2806,
"MMCMXCIX": 2999, "MMM": 3000, "MMMCMLXXIX": 3979, "MMMCMXCIX": 3999,
}
func TestRomanToInteger(t *testing.T) {
for input, expected := range romanTestCases {
out, err := RomanToInteger(input)
if err != nil {
t.Errorf("RomanToInteger(%s) returned an error %s", input, err.Error())
}
if out != expected {
t.Errorf("RomanToInteger(%s) = %d; want %d", input, out, expected)
}
}
_, err := RomanToInteger("IVCMXCIX")
if err == nil {
t.Error("RomanToInteger(IVCMXCIX) expected an error")
}
val, err := RomanToInteger("")
if val != 0 {
t.Errorf("RomanToInteger(\"\") = %d; want 0", val)
}
if err != nil {
t.Errorf("RomanToInteger(\"\") returned an error %s", err.Error())
}
}
func BenchmarkRomanToInteger(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = RomanToInteger("MMMCMXCIX")
}
}