forked from zyedidia/generic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_test.go
85 lines (72 loc) · 1.82 KB
/
generic_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
package generic_test
import (
"fmt"
"math"
"time"
"github.com/zyedidia/generic"
)
func ExampleMax() {
fmt.Println(generic.Max(7, 3))
fmt.Println(generic.Max(2*time.Second, 3*time.Second).Milliseconds())
// Output:
// 7
// 3000
}
func ExampleMin() {
fmt.Println(generic.Min(7, 3))
fmt.Println(generic.Min(2*time.Second, 3*time.Second).Milliseconds())
// Output:
// 3
// 2000
}
func ExampleClamp() {
fmt.Println(generic.Clamp(500, 400, 600))
fmt.Println(generic.Clamp(200, 400, 600))
fmt.Println(generic.Clamp(800, 400, 600))
fmt.Println(generic.Clamp(5*time.Second, 4*time.Second, 6*time.Second).Milliseconds())
fmt.Println(generic.Clamp(2*time.Second, 4*time.Second, 6*time.Second).Milliseconds())
fmt.Println(generic.Clamp(8*time.Second, 4*time.Second, 6*time.Second).Milliseconds())
fmt.Println(generic.Clamp(1.5, 1.4, 1.8))
fmt.Println(generic.Clamp(1.5, 1.8, 1.8))
fmt.Println(generic.Clamp(1.5, 2.1, 1.9))
// Output:
// 500
// 400
// 600
// 5000
// 4000
// 6000
// 1.5
// 1.8
// 2.1
}
func lessMagnitude(a, b float64) bool {
return math.Abs(a) < math.Abs(b)
}
func ExampleMaxFunc() {
fmt.Println(generic.MaxFunc(2.5, -3.1, lessMagnitude))
// Output:
// -3.1
}
func ExampleMinFunc() {
fmt.Println(generic.MinFunc(2.5, -3.1, lessMagnitude))
// Output:
// 2.5
}
func ExampleClampFunc() {
fmt.Println(generic.ClampFunc(1.5, 1.4, 1.8, lessMagnitude))
fmt.Println(generic.ClampFunc(1.5, 1.8, 1.8, lessMagnitude))
fmt.Println(generic.ClampFunc(1.5, 2.1, 1.9, lessMagnitude))
fmt.Println(generic.ClampFunc(-1.5, -1.4, -1.8, lessMagnitude))
fmt.Println(generic.ClampFunc(-1.5, -1.8, -1.8, lessMagnitude))
fmt.Println(generic.ClampFunc(-1.5, -2.1, -1.9, lessMagnitude))
fmt.Println(generic.ClampFunc(1.5, -1.5, -1.5, lessMagnitude))
// Output:
// 1.5
// 1.8
// 2.1
// -1.5
// -1.8
// -2.1
// 1.5
}