-
Notifications
You must be signed in to change notification settings - Fork 2
/
dauction_test.go
197 lines (170 loc) · 4.97 KB
/
dauction_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
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
193
194
195
196
197
package dauction_test
import (
"fmt"
"math/rand"
"sort"
"testing"
"time"
"github.com/kchristidis/dauction"
"github.com/stretchr/testify/assert"
)
// The default value for the acceptable relative error
// during clearing price and unit number calculations.
const defaultEpsilon = 1E-5
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
func TestSortBids(t *testing.T) {
bidPrices := sort.Float64Slice{
4.20,
2.05,
8.14,
}
var sellers []dauction.Bid
for i := range bidPrices {
sellers = append(sellers, dauction.Bid{PricePerUnit: bidPrices[i]})
}
sellersGroup := dauction.BidCollection(sellers)
// Test in increasing order.
sort.Sort(bidPrices)
sort.Sort(sellersGroup)
for i := range sellers {
assert.InEpsilon(t, bidPrices[i], sellersGroup[i].PricePerUnit, defaultEpsilon)
}
// Test in decreasing order.
sort.Sort(sort.Reverse(sellersGroup))
sort.Sort(sort.Reverse(bidPrices))
for i := range sellers {
assert.InEpsilon(t, bidPrices[i], sellersGroup[i].PricePerUnit, defaultEpsilon)
}
}
func TestStackBids(t *testing.T) {
bids := []dauction.Bid{
{PricePerUnit: 2.0, Units: 10},
{PricePerUnit: 1.0, Units: 10},
{PricePerUnit: 3.0, Units: 10},
}
bc := dauction.BidCollection(bids)
actualResult := dauction.Stack(bc, dauction.Buyers)
buyersResult := []dauction.Bid{
{PricePerUnit: 1.0, Units: 30},
{PricePerUnit: 2.0, Units: 20},
{PricePerUnit: 3.0, Units: 10},
}
for i := range actualResult {
assert.InDelta(t, buyersResult[i].PricePerUnit, actualResult[i].PricePerUnit, defaultEpsilon,
fmt.Sprintf("Got %s, but expected %s", actualResult[i], buyersResult[i]))
assert.InDelta(t, buyersResult[i].Units, actualResult[i].Units, defaultEpsilon,
fmt.Sprintf("Got %s, but expected %s", actualResult[i], buyersResult[i]))
}
sellersResult := []dauction.Bid{
{PricePerUnit: 3.0, Units: 30},
{PricePerUnit: 2.0, Units: 20},
{PricePerUnit: 1.0, Units: 10},
}
actualResult = dauction.Stack(bc, dauction.Sellers)
for i := range actualResult {
assert.InDelta(t, sellersResult[i].PricePerUnit, actualResult[i].PricePerUnit, defaultEpsilon,
fmt.Sprintf("Got %s, but expected %s", actualResult[i], sellersResult[i]))
assert.InDelta(t, sellersResult[i].Units, actualResult[i].Units, defaultEpsilon,
fmt.Sprintf("Got %s, but expected %s", actualResult[i], sellersResult[i]))
}
}
func TestTrades(t *testing.T) {
type testCase struct {
buyers, sellers dauction.BidCollection
expected dauction.Bid
err error
}
var testCases []testCase
testCases = append(testCases, testCase{
buyers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 10, Units: 1},
},
sellers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 8, Units: 1},
},
expected: dauction.Bid{
PricePerUnit: 9,
Units: 1,
},
err: nil,
})
testCases = append(testCases, testCase{
buyers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 8, Units: 1},
},
sellers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 10, Units: 1},
},
err: dauction.ErrNoPrice,
})
testCases = append(testCases, testCase{
buyers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 10, Units: 4},
},
sellers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 9, Units: 2},
},
expected: dauction.Bid{
PricePerUnit: 9.5,
Units: 2,
},
err: nil,
})
testCases = append(testCases, testCase{
buyers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 6.5, Units: 2},
dauction.Bid{PricePerUnit: 10, Units: 2},
},
sellers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 6, Units: 2},
},
expected: dauction.Bid{
PricePerUnit: 8,
Units: 2,
},
err: nil,
})
testCases = append(testCases, testCase{
buyers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 6.5, Units: 2},
dauction.Bid{PricePerUnit: 10, Units: 2},
},
sellers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 6.5, Units: 2},
dauction.Bid{PricePerUnit: 11, Units: 2},
},
expected: dauction.Bid{
PricePerUnit: 8.25,
Units: 2,
},
err: nil,
})
testCases = append(testCases, testCase{
buyers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 8, Units: 2},
dauction.Bid{PricePerUnit: 10, Units: 2},
},
sellers: dauction.BidCollection{
dauction.Bid{PricePerUnit: 6, Units: 1},
dauction.Bid{PricePerUnit: 9, Units: 1},
},
expected: dauction.Bid{
PricePerUnit: 9.5,
Units: 2,
},
err: nil,
})
for _, tc := range testCases {
actual, err := dauction.Settle(tc.buyers, tc.sellers)
if tc.err == nil {
assert.NoError(t, err, "Expected the market to clear")
} else {
assert.EqualError(t, tc.err, dauction.ErrNoPrice.Error())
continue
}
assert.InEpsilon(t, tc.expected.PricePerUnit, actual.PricePerUnit, defaultEpsilon,
"Expected the market to settle at %s, got %s instead", tc.expected, actual)
assert.InEpsilon(t, tc.expected.Units, actual.Units, defaultEpsilon,
"Expected the market to settle at %s, got %s instead", tc.expected, actual)
}
}