-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
192 lines (180 loc) · 4.32 KB
/
utils_test.go
File metadata and controls
192 lines (180 loc) · 4.32 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Utility functions for go.
// They can be used for int, float64 and string data types.
// Author: Lukas Rieger
// Change Log
// 2020-03-24 Creation
package utils
import (
"reflect"
"testing"
)
func TestPowerString_isPalindrome(t *testing.T) {
tests := []struct {
name string
p PowerString
want bool
}{
{"palindrome", "xouuox", true},
{"not_palindrome", "apple", false},
{"empty_string", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// when testing with -v log output will be shown
t.Logf("Running isPalindrome(%v)", tt.p)
if got := tt.p.isPalindrome(); got != tt.want {
t.Errorf("PowerString.isPalindrome() = %v, want %v", got, tt.want)
}
})
}
}
func TestPowerString_reverse(t *testing.T) {
tests := []struct {
name string
p PowerString
want string
}{
{"reverse", "cow", "woc"},
{"empty_string", "", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.p.reverse()
if string(tt.p) != tt.want {
t.Errorf("PowerString.isPalindrome() = %v, want %v", tt.p, tt.want)
}
})
}
}
func TestPowerOfTwo(t *testing.T) {
tests := []struct {
title string
exp int
answer int
}{
{"2 ^ -1", -1, 0},
{"2 ^ 0", 0, 1},
{"2 ^ 1", 1, 2},
{"2 ^ 2", 2, 4},
{"2 ^ 3", 3, 8},
}
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
title := test.title
exponent := test.exp
wanted := test.answer
if got := PowerOfTwo(exponent); wanted != got {
t.Errorf("Test %v failed. Expected %v, but got %v", title, wanted, got)
}
})
}
}
func TestMap(t *testing.T) {
type args struct {
f func(x float64) float64
xi []float64
}
tests := []struct {
name string
args args
want []float64
}{
{"map_inc", args{func(x float64) float64 { return x + 1.0 }, []float64{1, 2, 3}}, []float64{2.0, 3.0, 4.0}},
{"map_dec", args{func(x float64) float64 { return x - 1.0 }, []float64{1, 2, 3}}, []float64{0.0, 1.0, 2.0}},
{"map_sqr", args{func(x float64) float64 { return x * x }, []float64{1, 2, 3}}, []float64{1.0, 4.0, 9.0}},
{"map_empty", args{func(x float64) float64 { return x + 1.0 }, []float64{}}, []float64{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Map(tt.args.f, tt.args.xi); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Map() = %v, want %v", got, tt.want)
}
})
}
}
func TestFilter(t *testing.T) {
type args struct {
f func(x float64) bool
xi []float64
}
tests := []struct {
name string
args args
want []float64
}{
{"filter_gt_one", args{func(x float64) bool { return x > 1 }, []float64{1, 2, 3}}, []float64{2.0, 3.0}},
{"filter_ident", args{func(x float64) bool { return x == x }, []float64{1, 2, 3}}, []float64{1.0, 2.0, 3.0}},
{"filter_empty", args{func(x float64) bool { return x > 3 }, []float64{}}, []float64{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Filter(tt.args.f, tt.args.xi); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Filter() = %v, want %v", got, tt.want)
}
})
}
}
func TestSum(t *testing.T) {
type args struct {
xi []float64
}
tests := []struct {
name string
args args
want float64
}{
{"sum", args{[]float64{3.0, -1.0, 5.0}}, 7.0},
{"empty", args{[]float64{}}, 0.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Sum(tt.args.xi...); got != tt.want {
t.Errorf("Sum() = %v, want %v", got, tt.want)
}
})
}
}
func Test_gcd(t *testing.T) {
type args struct {
a int
b int
}
tests := []struct {
name string
args args
want int
}{
{"gcd1", args{20, 5}, 5},
{"gcd2", args{21, 28}, 7},
{"gcd_by_zero", args{0, 1}, 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GreatestCommonDivisor(tt.args.a, tt.args.b); got != tt.want {
t.Errorf("gcd() = %v, want %v", got, tt.want)
}
})
}
}
func Test_LowestCommonMultiple(t *testing.T) {
type args struct {
a int
b int
}
tests := []struct {
name string
args args
want int
}{
{"LowestCommonMultiple1", args{20, 5}, 20},
{"LowestCommonMultiple2", args{3, -7}, 21},
{"LowestCommonMultiple_of_zero", args{0, 1}, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := LowestCommonMultiple(tt.args.a, tt.args.b); got != tt.want {
t.Errorf("lcm() = %v, want %v", got, tt.want)
}
})
}
}