-
Notifications
You must be signed in to change notification settings - Fork 0
/
romannumeral_test.go
190 lines (173 loc) · 4.68 KB
/
romannumeral_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package romannumeral
import (
"fmt"
"testing"
)
var testCases = 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 TestIntToString(t *testing.T) {
for expected, input := range testCases {
out, err := IntToString(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)
}
}
_, err := IntToString(100000)
if err == nil {
t.Errorf("IntToString(%d) expected an error", 100000)
}
_, err = IntToString(0)
if err == nil {
t.Errorf("IntToString(%d) expected an error", 0)
}
}
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) {
for input, expected := range testCases {
out, err := StringToInt(input)
if err != nil {
t.Errorf("StringToInt(%s) returned an error %s", input, err.Error())
}
if out != expected {
t.Errorf("StringToInt(%s) = %d; want %d", input, out, expected)
}
}
_, err := StringToInt("IVCMXCIX")
if err == nil {
t.Error("StringToInt(IVCMXCIX) expected an error")
}
val, err := StringToInt("")
if val != 0 {
t.Errorf("StringToInt(\"\") = %d; want 0", val)
}
if err != nil {
t.Errorf("StringToInt(\"\") returned an error %s", err.Error())
}
}
func TestBytesToInt(t *testing.T) {
for input, expected := range testCases {
out, err := BytesToInt([]byte(input))
if err != nil {
t.Errorf("StringToInt(%s) returned an error %s", input, err.Error())
}
if out != expected {
t.Errorf("StringToInt(%s) = %d; want %d", input, out, expected)
}
}
_, err := BytesToInt([]byte("IVCMXCIX"))
if err == nil {
t.Error("BytesToInt(IVCMXCIX) expected an error")
}
var in []byte
val, err := BytesToInt(in)
if val != 0 {
t.Errorf("BytesToInt(nil) = %d; want 0", val)
}
if err != nil {
t.Errorf("BytesToInt(nil) returned an error %s", err.Error())
}
in = []byte("")
val, err = BytesToInt(in)
if val != 0 {
t.Errorf("BytesToInt([]byte(\"\")) = %d; want 0", val)
}
if err != nil {
t.Errorf("BytesToInt([]byte(\"\")) returned an error %s", err.Error())
}
}
func BenchmarkIntToString(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = IntToString(3999)
}
}
func BenchmarkIntToBytes(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = IntToBytes(3999)
}
}
func BenchmarkStringToInt(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = StringToInt("MMMCMXCIX")
}
}
func BenchmarkBytesToInt(b *testing.B) {
in := []byte("MMMCMXCIX")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = BytesToInt(in)
}
}
func ExampleStringToInt() {
integer, err := StringToInt("IV")
if err != nil {
panic(err)
}
fmt.Println(integer == 4) // True
}
func ExampleBytesToInt() {
input := []byte("IV")
integer, err := BytesToInt(input)
if err != nil {
panic(err)
}
fmt.Println(integer == 4) // True
}
func ExampleIntToString() {
roman, err := IntToString(4)
if err != nil {
panic(err)
}
fmt.Println(roman == "IV") // True
}
func ExampleIntToBytes() {
roman, err := IntToBytes(4)
if err != nil {
panic(err)
}
fmt.Println(string(roman) == "IV") // True
}