-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactoradic_test.go
116 lines (107 loc) · 3.19 KB
/
factoradic_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
package factoradic
import (
"fmt"
"testing"
)
func TestNumber(t *testing.T) {
var tests = []struct {
base10 int32
factorialBase string
}{
{base10: -1, factorialBase: "###"},
{base10: 0, factorialBase: "0"},
{base10: 1, factorialBase: "1"},
{base10: 2, factorialBase: "10"},
{base10: 3, factorialBase: "11"},
{base10: 4, factorialBase: "20"},
{base10: 5, factorialBase: "21"},
{base10: 6, factorialBase: "100"},
{base10: 7, factorialBase: "101"},
{base10: 21, factorialBase: "311"},
{base10: 22, factorialBase: "320"},
{base10: 23, factorialBase: "321"},
{base10: 24, factorialBase: "1000"},
{base10: 25, factorialBase: "1001"},
{base10: 5038, factorialBase: "654320"},
{base10: 5039, factorialBase: "654321"},
{base10: 5040, factorialBase: "1000000"},
{base10: 999998, factorialBase: "266251210"},
{base10: 999999, factorialBase: "266251211"},
{base10: 1000000, factorialBase: "266251220"},
{base10: 1000001, factorialBase: "266251221"},
{base10: 3628799, factorialBase: "987654321"},
{base10: 3628800, factorialBase: "###"},
}
for _, test := range tests {
want := test.factorialBase
got := Number(test.base10).String()
if want != got {
t.Errorf("(%d) want %s, got %s", test.base10, want, got)
}
}
}
func TestParseNumber(t *testing.T) {
var tests = []struct {
base10 int32
factorialBase string
err bool
}{
{factorialBase: "0", base10: 0},
{factorialBase: "1", base10: 1},
{factorialBase: "10", base10: 2},
{factorialBase: "11", base10: 3},
{factorialBase: "20", base10: 4},
{factorialBase: "21", base10: 5},
{factorialBase: "100", base10: 6},
{factorialBase: "101", base10: 7},
{factorialBase: "311", base10: 21},
{factorialBase: "320", base10: 22},
{factorialBase: "321", base10: 23},
{factorialBase: "1000", base10: 24},
{factorialBase: "1001", base10: 25},
{factorialBase: "654320", base10: 5038},
{factorialBase: "654321", base10: 5039},
{factorialBase: "1000000", base10: 5040},
{factorialBase: "266251210", base10: 999998},
{factorialBase: "266251211", base10: 999999},
{factorialBase: "266251220", base10: 1000000},
{factorialBase: "266251221", base10: 1000001},
{factorialBase: "987654321", base10: 3628799},
{factorialBase: "", err: true},
{factorialBase: "2", err: true},
{factorialBase: "102", err: true},
{factorialBase: "Z", err: true},
{factorialBase: "1111111111", err: true},
{factorialBase: "30", err: true},
{factorialBase: "400", err: true},
}
for _, test := range tests {
want := Number(test.base10)
got, err := ParseNumber(test.factorialBase)
if !test.err && err != nil {
t.Errorf("(%s) unexpected error %v", test.factorialBase, err)
continue
}
if test.err {
if err == nil {
t.Errorf("(%s) expected error, got base 10 value %d", test.factorialBase, got)
}
continue
}
if want != got {
t.Errorf("(%s) want base 10 value %d, got %d", test.factorialBase, want, got)
}
}
}
func ExampleNumber() {
var n Number = 23
fmt.Printf("Your number is %s.", n)
// Output:
// Your number is 321.
}
func ExampleParseNumber() {
n, _ := ParseNumber("321")
fmt.Printf("Your number in base 10 is %d.", n)
// Output:
// Your number in base 10 is 23.
}