-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
96 lines (81 loc) · 1.72 KB
/
examples_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
package comparer_test
import (
"fmt"
"github.com/gum-dev-ar/comparer"
)
func Example_default() {
c := comparer.New()
x, y, z := 2, 4, 5
if c.Equal(x, x) {
fmt.Printf("%v == %v\n", x, x)
} else {
fmt.Printf("%v != %v\n", x, x)
}
if c.Equal(x, y) {
fmt.Printf("%v == %v\n", x, y)
} else {
fmt.Printf("%v != %v\n", x, y)
}
comparison, comparable := c.Compare(x, z)
if !comparable {
fmt.Printf("%v and %v are not comparable\n", x, z)
} else if comparison < 0 {
fmt.Printf("%v < %v\n", x, z)
} else if comparison > 0 {
fmt.Printf("%v > %v\n", x, z)
} else {
fmt.Printf("%v == %v\n", x, z)
}
}
func Example_custom() {
number := func(v interface{}) (int, bool) {
switch n := v.(type) {
case int:
return n, true
default:
return 0, false
}
}
comparator := func(_ string, a interface{}, b interface{}) (int, bool) {
na, ok := number(a)
if !ok {
return 0, false
}
nb, ok := number(b)
if !ok {
return 0, false
}
if (na%2) == 0 && (nb%2) == 0 {
return 0, true
} else if (na%2) == 0 && (nb%2) != 0 {
return 1, true
} else if (na%2) != 0 && (nb%2) == 0 {
return -1, true
} else {
return 0, true
}
}
config := comparer.CustomComparator(comparator)
c := comparer.New(config)
x, y, z := 2, 4, 5
if c.Equal(x, x) {
fmt.Printf("%v == %v\n", x, x)
} else {
fmt.Printf("%v != %v\n", x, x)
}
if c.Equal(x, y) {
fmt.Printf("%v == %v\n", x, y)
} else {
fmt.Printf("%v != %v\n", x, y)
}
comparison, comparable := c.Compare(x, z)
if !comparable {
fmt.Printf("%v and %v are not comparable\n", x, z)
} else if comparison < 0 {
fmt.Printf("%v < %v\n", x, z)
} else if comparison > 0 {
fmt.Printf("%v > %v\n", x, z)
} else {
fmt.Printf("%v == %v\n", x, z)
}
}