-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers_test.go
120 lines (108 loc) · 2.94 KB
/
numbers_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
package numbers
import (
"testing"
)
func TestAddDelim(t *testing.T) {
s := AddDelimiters(32.3)
if s != "32.300" {
t.Errorf("expected 32.3, not '%s'", s)
}
s = AddDelimiters(3032.3)
if s != "3,032" {
t.Errorf("expected 3,032.3000, not '%s'", s)
}
s = AddDelimiters(123131231233032.3)
if s != "123,131,231,233,032" {
t.Errorf("expected 3,032.3000, not '%s'", s)
}
s = AddDelimiters(1123131231233032.3)
if s != "1,123,131,231,233,032" {
t.Errorf("expected 3,032.300, not '%s'", s)
}
s = AddDelimiters(12123131231233032.3)
if s != "12,123,131,231,233,032" {
t.Errorf("expected 3,032.300, not '%s'", s)
}
s = AddDelimitersInt(12123131231233032)
if s != "12,123,131,231,233,032" {
t.Errorf("expected 3,032.300, not '%s'", s)
}
}
type dtest struct {
num float64
result string
}
var dispTests = []dtest{{999.0, "999"},
{0.3145, "0.315"},
{1300000, "1.30M"},
{871300000, "871.30M"},
{1300000000, "1.30G"},
{13000000000, "13.00G"},
{1300000000000, "1.30T"},
{1300000000000000, "1.30Q"},
{0.0, "0"},
{0.0000, "0"},
{1001.0, "1,001"}}
func TestDisplay(t *testing.T) {
for i, v := range dispTests {
s := Display(v.num)
if s != v.result {
t.Errorf("%d: expected '%s', got '%s'", i, v.result, s)
}
}
}
func TestPercentage(t *testing.T) {
if Percentage(100.0, 0.0) != 0.0 {
t.Errorf("expected 0, got %f", Percentage(100.0, 0.0))
}
if PercentageMid(100.0, 0.0) != 200.0 {
t.Errorf("expected 200%%, got %f", PercentageMid(100.0, 0.0))
}
if Percentage(110.0, 100.0) != 10.0 {
t.Errorf("expected 10%%, got %f", Percentage(110.0, 100.0))
}
if PercentageMid(110.0, 100.0) != 9.523809523809524 {
t.Errorf("expected 10%%, got %f", PercentageMid(110.0, 100.0))
}
}
func TestScale(t *testing.T) {
if Scale(1.23, 1) != 2.0 {
t.Errorf("expected 2.0, got %f", Scale(1.23, 1))
}
if ScaleDown(1.23, 1) != 1.0 {
t.Errorf("down scale to 1.0, got %f", ScaleDown(1.23, 1))
}
if ScaleDown(2.9, 1) != 2.0 {
t.Errorf("down scale to 2.0, got %f", ScaleDown(2.9, 1))
}
if ScaleDown(2934.0, 1) != 2000.0 {
t.Errorf("down scale to 2000.0, got %f", ScaleDown(2934.0, 1))
}
if Scale(2934.0, 1) != 3000.0 {
t.Errorf("up scale to 3000.0, got %f", Scale(2934.0, 1))
}
if Scale(-2.3, 1) != -3.0 {
t.Errorf("expected -3.0, got %f", Scale(-2.3, 1))
}
if Scale(0.25, 1) != 0.3 {
t.Errorf("expected 0.3, got %f", Scale(0.25, 1))
}
if Scale(0.00003, 1) != 0.00003 {
t.Errorf("expected 0.00003, got %f", Scale(0.00003, 1))
}
if Scale(0.000023, 1) != 0.00003 {
t.Errorf("expected 0.00003, got %f", Scale(0.000023, 1))
}
if Scale(-0.000023, 1) != -0.00003 {
t.Errorf("expected -0.00003, got %f", Scale(-0.000023, 1))
}
if Scale(6210.0, 2) != 6300.0 {
t.Errorf("Scale(6210, 2) == %v, expected 6300.0", Scale(6210.0, 2))
}
if ScaleDown(6210.0, 2) != 6200.0 {
t.Errorf("ScaleDown(6210, 2) == %v, expected 6200.0", ScaleDown(6210.0, 2))
}
if Scale(0.251, 2) != 0.26 {
t.Errorf("scale 0.251, 2 = %v, expected 0.26", Scale(0.251, 2))
}
}