-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
95 lines (84 loc) · 2.6 KB
/
main_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
package main
import (
"math"
"testing"
)
func TestSmallestBases(t *testing.T) {
for _, test := range testCases {
observed, _ := test.number.smallesPalindBase(test.maxNumBase)
if observed != test.expected {
t.Errorf("smallesPalindBase of %d returned %d, want %d",
test.number, observed, test.expected)
}
}
}
func TestSmallestBasesWithEndBaseTooLow(t *testing.T) {
for _, test := range testCasesBaseTooLow {
observed, _ := test.number.smallesPalindBase(test.notHighEnoughEndBase)
if observed == test.expected {
t.Errorf("smallesPalindBase of %d returned %d, want 0",
test.number, observed)
}
}
}
func TestSmallestBasesWithEndBaseBelowLimit(t *testing.T) {
belowLimitEndBase := 1
for _, test := range testCases {
observed, error := test.number.smallesPalindBase(belowLimitEndBase)
if error == nil {
t.Errorf("smallesPalindBase of %d returned %d, want -1 and error",
test.number, observed)
}
}
}
func TestRepresentationSize(t *testing.T) {
for _, test := range testCasesRepresentationSize {
observed := test.number.representationSize(test.base)
if observed != test.expected {
t.Errorf("representationSize of %d base %d returned %d, want %d",
test.number, test.base, observed, test.expected)
}
}
}
func TestDecompose(t *testing.T) {
for _, test := range testCasesDecomposition {
observed := true
size := test.number.representationSize(test.base)
baseRepresentation := make([]int, size)
test.number.decompose(baseRepresentation, test.base)
for i := range baseRepresentation {
if baseRepresentation[i] != test.expected[i] {
observed = false
break
}
}
if observed != true {
t.Errorf("decomposition was %v, want %v",
baseRepresentation, test.expected)
}
}
}
func TestIsPalindrome(t *testing.T) {
for _, test := range testCasesIsPalindrome {
size := test.number.representationSize(test.base)
baseRepresentation := make([]int, size)
test.number.decompose(baseRepresentation, test.base)
observed := isPalindrome(baseRepresentation)
if observed != test.expected {
t.Errorf("isPalindrome of %d base %d returned %t, want %t",
test.number, test.base, observed, test.expected)
}
}
}
func TestLogB(t *testing.T) {
const TOLERANCE = 0.000001
for _, test := range testCasesLogB {
observed := logB(float64(test.number), test.base)
// Note: two float64 operands are considered equal when the absolute
// value of their deltas are smaller than the tolerance.
if diff := math.Abs(observed - test.expected); diff >= TOLERANCE {
t.Errorf("logB of %d and %d returned %f, want %f",
test.number, test.base, observed, test.expected)
}
}
}