mirror of
https://github.com/rwinkhart/convertroman.git
synced 2026-09-02 15:17:16 -04:00
added int to bytes
This commit is contained in:
@@ -11,11 +11,13 @@ Quickly and efficiently convert to and from roman numerals in Go.
|
|||||||
goos: darwin
|
goos: darwin
|
||||||
goarch: arm64
|
goarch: arm64
|
||||||
pkg: github.com/brandenc40/romannumeral
|
pkg: github.com/brandenc40/romannumeral
|
||||||
BenchmarkIntToString-8 18048271 66.13 ns/op 24 B/op 2 allocs/op
|
BenchmarkIntToString-8 18160285 66.43 ns/op 24 B/op 2 allocs/op
|
||||||
BenchmarkStringToInt-8 17572086 67.31 ns/op 0 B/op 0 allocs/op
|
BenchmarkIntToBytes-8 18630670 63.68 ns/op 24 B/op 2 allocs/op
|
||||||
BenchmarkBytesToInt-8 17855149 64.93 ns/op 0 B/op 0 allocs/op
|
BenchmarkStringToInt-8 17537406 67.26 ns/op 0 B/op 0 allocs/op
|
||||||
|
BenchmarkBytesToInt-8 18248730 64.83 ns/op 0 B/op 0 allocs/op
|
||||||
PASS
|
PASS
|
||||||
ok github.com/brandenc40/romannumeral 5.130s
|
ok github.com/brandenc40/romannumeral 6.283s
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
@@ -51,4 +53,12 @@ func ExampleIntToString() {
|
|||||||
}
|
}
|
||||||
fmt.Println(roman == "IV") // True
|
fmt.Println(roman == "IV") // True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleIntToBytes() {
|
||||||
|
roman, err := rom.IntToBytes(4)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println(string(roman) == "IV") // True
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -52,6 +52,15 @@ func IntToString(input int) (string, error) {
|
|||||||
return string(intToRoman(input)), nil
|
return string(intToRoman(input)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IntToBytes converts an integer value to a roman numeral byte array. An error is
|
||||||
|
// returned if the integer is not between 1 and 3999.
|
||||||
|
func IntToBytes(input int) ([]byte, error) {
|
||||||
|
if outOfBounds(input) {
|
||||||
|
return nil, IntegerOutOfBounds
|
||||||
|
}
|
||||||
|
return intToRoman(input), nil
|
||||||
|
}
|
||||||
|
|
||||||
// outOfBounds checks to ensure an input value is valid for roman numerals without the need of
|
// outOfBounds checks to ensure an input value is valid for roman numerals without the need of
|
||||||
// vinculum (used for values of 4,000 and greater)
|
// vinculum (used for values of 4,000 and greater)
|
||||||
func outOfBounds(input int) bool {
|
func outOfBounds(input int) bool {
|
||||||
|
|||||||
@@ -46,6 +46,31 @@ func TestIntToString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIntToBytes(t *testing.T) {
|
||||||
|
for expected, input := range testCases {
|
||||||
|
out, err := IntToBytes(input)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("IntToBytes(%d) returned an error %s", input, err.Error())
|
||||||
|
}
|
||||||
|
if len(out) != len([]byte(expected)) {
|
||||||
|
t.Errorf("len(IntToBytes(%d)) = %d; want %d", input, len(out), len([]byte(expected)))
|
||||||
|
}
|
||||||
|
for i, char := range out {
|
||||||
|
if char != expected[i] {
|
||||||
|
t.Errorf("IntToBytes(%d) = %s; want %s", input, string(out), expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, err := IntToBytes(100000)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("IntToBytes(%d) expected an error", 100000)
|
||||||
|
}
|
||||||
|
_, err = IntToBytes(0)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("IntToBytes(%d) expected an error", 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestStringToInt(t *testing.T) {
|
func TestStringToInt(t *testing.T) {
|
||||||
for input, expected := range testCases {
|
for input, expected := range testCases {
|
||||||
out, err := StringToInt(input)
|
out, err := StringToInt(input)
|
||||||
@@ -111,6 +136,13 @@ func BenchmarkIntToString(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkIntToBytes(b *testing.B) {
|
||||||
|
b.ReportAllocs()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, _ = IntToBytes(3999)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkStringToInt(b *testing.B) {
|
func BenchmarkStringToInt(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
@@ -150,3 +182,11 @@ func ExampleIntToString() {
|
|||||||
}
|
}
|
||||||
fmt.Println(roman == "IV") // True
|
fmt.Println(roman == "IV") // True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleIntToBytes() {
|
||||||
|
roman, err := IntToBytes(4)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println(string(roman) == "IV") // True
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user