mirror of
https://github.com/rwinkhart/convertroman.git
synced 2026-08-30 05:46:29 -04:00
Do not return an error on out of bounds (return "OOB" string instead)
This commit is contained in:
+6
-13
@@ -1,17 +1,10 @@
|
||||
package convertroman
|
||||
|
||||
// Currently only supports Roman Numerals without viniculum (1-3999) and will throw an error for
|
||||
// numbers outside of that range. See here for details on viniculum:
|
||||
// Currently only supports Roman Numerals without viniculum (1-3999; "OOB" is returned for values outside this range)
|
||||
// See here for details on viniculum:
|
||||
// https://en.wikipedia.org/wiki/Roman_numerals#Large_numbers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// IntegerOutOfBounds - error for when the integer provided is invalid and unable to be converted to a roman numeral
|
||||
IntegerOutOfBounds = errors.New("integer must be between 1 and 3999")
|
||||
|
||||
// lookup arrays used for converting from an int to a roman numeral extremely quickly.
|
||||
// method from here: https://rosettacode.org/wiki/Roman_numerals/Encode#Go
|
||||
r0 = [10]string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
|
||||
@@ -21,13 +14,13 @@ var (
|
||||
)
|
||||
|
||||
// FromInt converts an integer value to a roman numeral string.
|
||||
// An error is returned if the integer is not between 1 and 3999.
|
||||
func FromInt(input int) (string, error) {
|
||||
// "OOB" is returned if the integer is not between 1 and 3999.
|
||||
func FromInt(input int) string {
|
||||
// ensure provided integer is within the valid range
|
||||
if input < 1 || input > 3999 {
|
||||
return "OOB", IntegerOutOfBounds
|
||||
return "OOB"
|
||||
}
|
||||
|
||||
// convert the integer to a roman numeral string and return it
|
||||
return r3[input%1e4/1e3] + r2[input%1e3/1e2] + r1[input%100/10] + r0[input%10], nil
|
||||
return r3[input%1e4/1e3] + r2[input%1e3/1e2] + r1[input%100/10] + r0[input%10]
|
||||
}
|
||||
|
||||
+15
-16
@@ -26,35 +26,34 @@ var testCases = map[string]int{
|
||||
|
||||
func TestFromInt(t *testing.T) {
|
||||
for expected, input := range testCases {
|
||||
out, err := FromInt(input)
|
||||
if err != nil {
|
||||
t.Errorf("IntToString(%d) returned an error %s", input, err.Error())
|
||||
}
|
||||
if out != expected {
|
||||
t.Errorf("IntToString(%d) = %s; want %s", input, out, expected)
|
||||
out := FromInt(input)
|
||||
if out == "OOB" {
|
||||
t.Errorf("FromInt(%d) returned an OOB (out of bounds) error", input)
|
||||
} else if out != expected {
|
||||
t.Errorf("FromInt(%d) = %s; want %s", input, out, expected)
|
||||
}
|
||||
}
|
||||
_, err := FromInt(100000)
|
||||
if err == nil {
|
||||
t.Errorf("IntToString(%d) expected an error", 100000)
|
||||
out := FromInt(100000)
|
||||
if out != "OOB" {
|
||||
t.Errorf("FromInt(%d) expected an error", 100000)
|
||||
}
|
||||
_, err = FromInt(0)
|
||||
if err == nil {
|
||||
t.Errorf("IntToString(%d) expected an error", 0)
|
||||
out = FromInt(0)
|
||||
if out != "OOB" {
|
||||
t.Errorf("FromInt(%d) expected an error", 0)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFromInt(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = FromInt(3999)
|
||||
_ = FromInt(3999)
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleFromInt() {
|
||||
roman, err := FromInt(4)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
roman := FromInt(4)
|
||||
if roman == "OOB" {
|
||||
panic("Input integer is out of bounds")
|
||||
}
|
||||
fmt.Println(roman == "IV") // True
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user