|
1 | 1 | package allyourbase
|
2 | 2 |
|
3 |
| -import "testing" |
4 |
| - |
5 |
| -// Note: ConvertToBase should accept leading zeroes in its input, |
6 |
| -// but never emit leading zeroes in its output. |
7 |
| -// Exception: If the value of the output is zero, represent it with a single zero. |
8 |
| -var testCases = []struct { |
9 |
| - description string |
10 |
| - inputBase uint64 |
11 |
| - outputBase uint64 |
12 |
| - inputDigits []uint64 |
13 |
| - outputDigits []uint64 |
14 |
| - error error |
15 |
| -}{ |
16 |
| - { |
17 |
| - description: "single bit one to decimal", |
18 |
| - inputBase: 2, |
19 |
| - inputDigits: []uint64{1}, |
20 |
| - outputBase: 10, |
21 |
| - outputDigits: []uint64{1}, |
22 |
| - }, |
23 |
| - { |
24 |
| - description: "binary to single decimal", |
25 |
| - inputBase: 2, |
26 |
| - inputDigits: []uint64{1, 0, 1}, |
27 |
| - outputBase: 10, |
28 |
| - outputDigits: []uint64{5}, |
29 |
| - }, |
30 |
| - { |
31 |
| - description: "single decimal to binary", |
32 |
| - inputBase: 10, |
33 |
| - inputDigits: []uint64{5}, |
34 |
| - outputBase: 2, |
35 |
| - outputDigits: []uint64{1, 0, 1}, |
36 |
| - }, |
37 |
| - { |
38 |
| - description: "binary to multiple decimal", |
39 |
| - inputBase: 2, |
40 |
| - inputDigits: []uint64{1, 0, 1, 0, 1, 0}, |
41 |
| - outputBase: 10, |
42 |
| - outputDigits: []uint64{4, 2}, |
43 |
| - }, |
44 |
| - { |
45 |
| - description: "decimal to binary", |
46 |
| - inputBase: 10, |
47 |
| - inputDigits: []uint64{4, 2}, |
48 |
| - outputBase: 2, |
49 |
| - outputDigits: []uint64{1, 0, 1, 0, 1, 0}, |
50 |
| - }, |
51 |
| - { |
52 |
| - description: "trinary to hexadecimal", |
53 |
| - inputBase: 3, |
54 |
| - inputDigits: []uint64{1, 1, 2, 0}, |
55 |
| - outputBase: 16, |
56 |
| - outputDigits: []uint64{2, 10}, |
57 |
| - }, |
58 |
| - { |
59 |
| - description: "hexadecimal to trinary", |
60 |
| - inputBase: 16, |
61 |
| - inputDigits: []uint64{2, 10}, |
62 |
| - outputBase: 3, |
63 |
| - outputDigits: []uint64{1, 1, 2, 0}, |
64 |
| - }, |
65 |
| - { |
66 |
| - description: "15-bit integer", |
67 |
| - inputBase: 97, |
68 |
| - inputDigits: []uint64{3, 46, 60}, |
69 |
| - outputBase: 73, |
70 |
| - outputDigits: []uint64{6, 10, 45}, |
71 |
| - }, |
72 |
| - { |
73 |
| - description: "empty list", |
74 |
| - inputBase: 2, |
75 |
| - inputDigits: []uint64{}, |
76 |
| - outputBase: 10, |
77 |
| - outputDigits: []uint64{0}, |
78 |
| - error: nil, |
79 |
| - }, |
80 |
| - { |
81 |
| - description: "single zero", |
82 |
| - inputBase: 10, |
83 |
| - inputDigits: []uint64{0}, |
84 |
| - outputBase: 2, |
85 |
| - outputDigits: []uint64{0}, |
86 |
| - }, |
87 |
| - { |
88 |
| - description: "multiple zeros", |
89 |
| - inputBase: 10, |
90 |
| - inputDigits: []uint64{0, 0, 0}, |
91 |
| - outputBase: 2, |
92 |
| - outputDigits: []uint64{0}, |
93 |
| - }, |
94 |
| - { |
95 |
| - description: "leading zeros", |
96 |
| - inputBase: 7, |
97 |
| - inputDigits: []uint64{0, 6, 0}, |
98 |
| - outputBase: 10, |
99 |
| - outputDigits: []uint64{4, 2}, |
100 |
| - }, |
101 |
| - { |
102 |
| - description: "invalid positive digit", |
103 |
| - inputBase: 2, |
104 |
| - inputDigits: []uint64{1, 2, 1, 0, 1, 0}, |
105 |
| - outputBase: 10, |
106 |
| - outputDigits: nil, |
107 |
| - error: ErrInvalidDigit, |
108 |
| - }, |
109 |
| - { |
110 |
| - description: "first base is one", |
111 |
| - inputBase: 1, |
112 |
| - inputDigits: []uint64{}, |
113 |
| - outputBase: 10, |
114 |
| - outputDigits: nil, |
115 |
| - error: ErrInvalidBase, |
116 |
| - }, |
117 |
| - { |
118 |
| - description: "second base is one", |
119 |
| - inputBase: 2, |
120 |
| - inputDigits: []uint64{1, 0, 1, 0, 1, 0}, |
121 |
| - outputBase: 1, |
122 |
| - outputDigits: nil, |
123 |
| - error: ErrInvalidBase, |
124 |
| - }, |
125 |
| - { |
126 |
| - description: "first base is zero", |
127 |
| - inputBase: 0, |
128 |
| - inputDigits: []uint64{}, |
129 |
| - outputBase: 10, |
130 |
| - outputDigits: nil, |
131 |
| - error: ErrInvalidBase, |
132 |
| - }, |
133 |
| - { |
134 |
| - description: "second base is zero", |
135 |
| - inputBase: 10, |
136 |
| - inputDigits: []uint64{7}, |
137 |
| - outputBase: 0, |
138 |
| - outputDigits: nil, |
139 |
| - error: ErrInvalidBase, |
140 |
| - }, |
141 |
| -} |
142 |
| - |
143 |
| -func digitsEqual(a, b []uint64) bool { |
144 |
| - if len(a) != len(b) { |
145 |
| - return false |
146 |
| - } |
147 |
| - |
148 |
| - for i := 0; i < len(a); i++ { |
149 |
| - if a[i] != b[i] { |
150 |
| - return false |
151 |
| - } |
152 |
| - } |
153 |
| - |
154 |
| - return true |
155 |
| -} |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
156 | 7 |
|
157 | 8 | func TestConvertToBase(t *testing.T) {
|
158 | 9 | for _, c := range testCases {
|
159 |
| - output, err := ConvertToBase(c.inputBase, c.inputDigits, c.outputBase) |
160 |
| - if err != c.error { |
161 |
| - t.Fatalf(`FAIL: %s |
162 |
| - Expected error: %v |
163 |
| - Got: %v`, c.description, c.error, err) |
164 |
| - } |
165 |
| - |
166 |
| - if !digitsEqual(c.outputDigits, output) { |
| 10 | + output := ConvertToBase(c.inputBase, c.inputDigits, c.outputBase) |
| 11 | + if !reflect.DeepEqual(c.expected, output) { |
167 | 12 | t.Fatalf(`FAIL: %s
|
168 | 13 | Input base: %d
|
169 |
| - Input digits: %v |
| 14 | + Input digits: %#v |
170 | 15 | Output base: %d
|
171 |
| - Expected output digits: %v |
172 |
| - Got: %v`, c.description, c.inputBase, c.inputDigits, c.outputBase, c.outputDigits, output) |
173 |
| - } else { |
174 |
| - t.Logf("PASS: %s", c.description) |
| 16 | + Expected output digits: %#v |
| 17 | + Got: %#v`, c.description, c.inputBase, c.inputDigits, c.outputBase, c.expected, output) |
175 | 18 | }
|
| 19 | + t.Logf("PASS: %s", c.description) |
176 | 20 | }
|
177 | 21 | }
|
0 commit comments