diff --git a/conversion/romantoint.go b/conversion/romantoint.go index 629952320..c6df21a53 100644 --- a/conversion/romantoint.go +++ b/conversion/romantoint.go @@ -55,5 +55,9 @@ func RomanToInt(input string) (int, error) { if len(input) > 0 { return 0, errors.New("invalid roman numeral") } + // Only standard form in the range 1 to 3999 is supported. + if output > 3999 { + return 0, errors.New("roman numeral must be in range 1 to 3999") + } return output, nil } diff --git a/conversion/romantoint_test.go b/conversion/romantoint_test.go index 0fd34b2c3..41a1b6a39 100644 --- a/conversion/romantoint_test.go +++ b/conversion/romantoint_test.go @@ -43,6 +43,11 @@ func TestRomanToInt(t *testing.T) { if err != nil { t.Errorf("RomanToInt(\"\") returned an error %s", err.Error()) } + + // MMMM is 4000, which is outside the supported 1-3999 range. + if _, err := RomanToInt("MMMM"); err == nil { + t.Error("RomanToInt(MMMM) expected an error") + } } func BenchmarkRomanToInt(b *testing.B) {