-
Notifications
You must be signed in to change notification settings - Fork 0
/
shuffle_test.go
125 lines (116 loc) · 2.17 KB
/
shuffle_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
121
122
123
124
125
package gotil_test
import (
"fmt"
"reflect"
"testing"
"github.com/gotilty/gotil"
)
func TestShuffle(t *testing.T) {
input := []int64{-100, -5, 30, 100, 5, 11, 1000, 33, 55}
expected := []int64{100, 33, 1000, 30, 55, -5, -100, 5, 11}
result := gotil.ShuffleSeed(input, int64(343434))
if !reflect.DeepEqual(expected, result) {
t.Errorf("FindLastBy does not works expected\ncase: %d\nexpected: %d taken: %d", input, expected, result)
}
}
func BenchmarkShuffleIntegerSlice(b *testing.B) {
input := []int64{-100, -5, 30, 100, 5, 11, 1000, 33, 55}
for n := 0; n < b.N; n++ {
gotil.Shuffle(input)
}
}
func BenchmarkShuffleStructSlice(b *testing.B) {
input := []user{
{
name: "Micheal",
age: 27,
},
{
name: "Joe",
age: 30,
},
{
name: "Olivia",
age: 42,
},
{
name: "Kevin",
age: 10,
},
}
for n := 0; n < b.N; n++ {
gotil.Shuffle(input)
}
}
func ExampleShuffle() {
// seed := time.Now().UnixNano()
seed := int64(58239238)
//Seed you will get the same sequence of pseudorandom numbers
// each time you run the program.
data := []int64{-100, -5, 30, 100}
// Input: [-100 -5 30 100]
newData := gotil.ShuffleSeed(data, seed)
fmt.Println(newData)
// Output: [-5 100 -100 30]
}
func getShuffleTestData() map[string]struct {
inputValue interface{}
output interface{}
seed int64
err error
} {
testData := map[string]struct {
inputValue interface{}
output interface{}
seed int64
err error
}{
"shuffle_numbers": {
inputValue: []int64{-100, -5, 30, 100, 5, 11, 1000, 33, 55},
output: []int64{100, 33, 1000, 30, 55, -5, -100, 5, 11},
seed: 343434,
err: nil,
},
"shuffle_struct": {
inputValue: []user{
{
name: "Micheal",
age: 27,
},
{
name: "Joe",
age: 30,
},
{
name: "Olivia",
age: 42,
},
{
name: "Kevin",
age: 10,
},
},
seed: 303030,
output: []user{
{
name: "Olivia",
age: 42,
},
{
name: "Micheal",
age: 27,
},
{
name: "Kevin",
age: 10,
},
{
name: "Joe",
age: 30,
},
},
err: nil,
},
}
return testData
}