Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions conversion/romantoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions conversion/romantoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down