-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_test.go
More file actions
154 lines (126 loc) · 2.92 KB
/
Copy pathnumber_test.go
File metadata and controls
154 lines (126 loc) · 2.92 KB
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
package cols_test
import (
"fmt"
"testing"
"github.com/egon12/cols"
)
type SumAbleObj struct {
Name string
Group string
Amount float64
}
func ExampleSumBy() {
in := []SumAbleObj{
{Name: "trans_1", Group: "account_1", Amount: 1.0},
{Name: "trans_2", Group: "account_1", Amount: 3.0},
{Name: "trans_3", Group: "account_2", Amount: 2.5},
{Name: "trans_4", Group: "account_2", Amount: 7.9},
}
got := cols.SumBy(
in,
func(o SumAbleObj) string { return o.Group },
func(o SumAbleObj) float64 { return o.Amount },
)
fmt.Println(got)
// Output: map[account_1:4 account_2:10.4]
}
func ExampleSum() {
in := []Obj{
{Name: "Peter", BirthDate: 31},
{Name: "Simon", BirthDate: 29},
{Name: "Kikorosuma", BirthDate: 10},
}
got := cols.Sum(in, func(o Obj) int32 {
return o.BirthDate
})
fmt.Println(got)
// Output: 70
}
func ExampleMax() {
in := []Obj{
{Name: "Peter", BirthDate: 31},
{Name: "Simon", BirthDate: 29},
{Name: "Kikorosuma", BirthDate: 10},
}
got := cols.Max(in, func(o Obj) int32 {
return o.BirthDate
})
fmt.Println(got)
// Output: 31
}
func ExampleMin() {
in := []Obj{
{Name: "Peter", BirthDate: 31},
{Name: "Simon", BirthDate: 29},
{Name: "Kikorosuma", BirthDate: 10},
}
got := cols.Min(in, func(o Obj) int32 {
return o.BirthDate
})
fmt.Println(got)
// Output: 10
}
func ExampleMaxStruct() {
in := []Obj{
{Name: "Peter", BirthDate: 31},
{Name: "Simon", BirthDate: 29},
{Name: "Kikorosuma", BirthDate: 10},
}
got := cols.MaxStruct(in, func(o Obj) int32 {
return o.BirthDate
})
fmt.Println(got)
// Output: {Peter 31}
}
func ExampleMinStruct() {
in := []Obj{
{Name: "Peter", BirthDate: 31},
{Name: "Simon", BirthDate: 29},
{Name: "Kikorosuma", BirthDate: 10},
}
got := cols.MinStruct(in, func(o Obj) int32 {
return o.BirthDate
})
fmt.Println(got)
// Output: {Kikorosuma 10}
}
func ExampleSumNumber() {
in1 := []int{1, 2, 3, 4}
got := cols.SumNumber(in1)
fmt.Println(got)
// Output: 10
}
func ExampleMaxNumber() {
got := cols.MaxNumber([]int32{10, 20, 100, 1, 2, 5})
fmt.Println(got)
// Output: 100
}
func ExampleMinNumber() {
got := cols.MinNumber([]int32{10, 20, 100, 1, 2, 5})
fmt.Println(got)
// Output: 1
}
func TestMaxNumber(t *testing.T) {
t.Run("func MaxNumber should return default when input length is zero",
func(t *testing.T) {
if cols.MaxNumber([]float64(nil)) != float64(0.0) {
t.Error("result MaxNumber not 0.0")
}
})
t.Run("func MaxNumber should able to receive input in type alias",
func(t *testing.T) {
type mynum float32
var in = append([]mynum{}, 1, 2, 3, 4)
if cols.MaxNumber(in) != mynum(4.0) {
t.Errorf("result MaxNumber not 4.0 but: %v", cols.MaxNumber(in))
}
})
}
func TestMinNumber(t *testing.T) {
t.Run("func MinNumber should return default when input length is zero",
func(t *testing.T) {
if cols.MinNumber([]float64(nil)) != float64(0.0) {
t.Error("result MaxNumber not 0.0")
}
})
}