forked from ef-ds/deque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdata_test.go
131 lines (110 loc) · 3.41 KB
/
testdata_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
126
127
128
129
130
131
// Copyright (c) 2018 ef-ds
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package deque_test
// testData contains the number of items to add to the queues in each test.
type testData struct {
count int
}
// testValue is used as the value added in each push call to the queues.
// A struct is being used as structs should be more representative of real
// world uses of a queue. A second f2 field was added as the users structs
// are likely to contain more than one field.
type testValue struct {
count int
f2 int
}
var (
tests = []testData{
{count: 0},
{count: 1},
{count: 10},
{count: 100},
{count: 1000}, // 1k
{count: 10000}, //10k
{count: 100000}, // 100k
{count: 1000000}, // 1mi
}
// Used to store temp values, avoiding any compiler optimizations.
tmp interface{}
tmp2 bool
fillCount = 10000
refillCount = 100
)
// Pure slice based test queue implementation-------------------------------------------------------
// CustomSliceQueue represents an unbounded, dynamically growing deque customized
// to operate on testVale struct.
type CustomSliceQueue struct {
// The queue values.
v []*testValue
}
func NewCustomSliceQueue() *CustomSliceQueue {
return new(CustomSliceQueue).Init()
}
func (q *CustomSliceQueue) Init() *CustomSliceQueue {
q.v = make([]*testValue, 0)
return q
}
func (q *CustomSliceQueue) Len() int { return len(q.v) }
func (q *CustomSliceQueue) Front() (*testValue, bool) {
if len(q.v) == 0 {
return nil, false
}
return q.v[0], true
}
func (q *CustomSliceQueue) Back() (*testValue, bool) {
if len(q.v) == 0 {
return nil, false
}
return q.v[len(q.v)-1], true
}
func (q *CustomSliceQueue) PushFront(v *testValue) {
q.v = append(q.v, v)
copy(q.v[1:], q.v[0:])
q.v[0] = v
}
func (q *CustomSliceQueue) PushBack(v *testValue) {
q.v = append(q.v, v)
}
func (q *CustomSliceQueue) PopFront() (*testValue, bool) {
if len(q.v) == 0 {
return nil, false
}
v := q.v[0]
q.v[0] = nil // Avoid memory leaks
q.v = q.v[1:]
return v, true
}
func (q *CustomSliceQueue) PopBack() (*testValue, bool) {
if len(q.v) == 0 {
return nil, false
}
tp := len(q.v) - 1
v := q.v[tp]
q.v[tp] = nil // Avoid memory leaks
q.v = q.v[:tp]
return v, true
}
// Helper methods-----------------------------------------------------------------------------------
func getTestValue(i int) *testValue {
return &testValue{
count: i,
f2: 1, // Initializes f2 to some random value (1).
}
}