-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact_test.go
244 lines (235 loc) · 6.53 KB
/
compact_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package timetable_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/portfoliotree/timetable"
)
type Table = timetable.Compact[Value]
func TestNew(t *testing.T) {
for _, tt := range []struct {
Name string
Lists []List
Then func(t *testing.T, table *Table)
}{
{
Name: "with nil input",
Lists: []List(nil),
Then: func(t *testing.T, table *Table) {
assert.Len(t, table.Times(), 0)
assert.Equal(t, table.NumberOfColumns(), 0)
},
},
{
Name: "with an empty list",
Lists: []List{{}},
Then: func(t *testing.T, table *Table) {
assert.Len(t, table.Times(), 0)
assert.Equal(t, table.NumberOfColumns(), 1)
},
},
{
Name: "with one list",
Lists: []List{{elT(day1), elT(day2)}},
Then: func(t *testing.T, table *Table) {
assert.Equal(t, 2, table.NumberOfRows())
assert.Equal(t, 1, table.NumberOfColumns())
assert.Equal(t, 2, table.NumberOfCells())
assert.Equal(t, []time.Time{date(day1), date(day2)}, table.UnderlyingTimes())
assert.Equal(t, [][]Value{{0, 0}}, table.UnderlyingValues())
},
},
{
Name: "with aligned single elements",
Lists: []List{
{elV(day0, 1)},
{elV(day0, 2)},
{elV(day0, 3)},
},
Then: func(t *testing.T, table *Table) {
assert.Len(t, table.Times(), 1, "it returns one row")
assert.Equal(t, 3, table.NumberOfColumns(), "it gives the correct column count")
assert.Equal(t, 1, table.NumberOfRows(), "it gives the correct row count")
assert.Equal(t, 3, table.NumberOfCells())
values := table.Values()
assert.Equal(t, [][]Value{{1}, {2}, {3}}, values)
},
},
{
Name: "with an additional empty list",
Lists: []List{
{elV(day0, 1)},
{},
},
Then: func(t *testing.T, table *Table) {
assert.Len(t, table.Times(), 0)
assert.Equal(t, 0, table.NumberOfRows())
assert.Equal(t, 2, table.NumberOfColumns())
},
},
{
Name: "with three shuffled matched elements per list",
Lists: []List{
{elV(day2, 1), elV(day1, 10), elV(day0, 100)},
{elV(day1, 20), elV(day2, 2), elV(day0, 200)},
{elV(day2, 3), elV(day0, 300), elV(day1, 30)},
},
Then: func(t *testing.T, table *Table) {
assert.Len(t, table.Times(), 3)
assert.Equal(t, 3, table.NumberOfRows())
assert.Equal(t, 3, table.NumberOfColumns())
assert.Equal(t, 9, table.NumberOfCells())
values := table.UnderlyingValues()
assert.Equal(t, [][]Value{
{100, 10, 1},
{200, 20, 2},
{300, 30, 3},
}, values)
assert.Equal(t, []time.Time{
date(day0), date(day1), date(day2),
}, table.UnderlyingTimes())
if row, ok := table.Row(date(day0)); assert.True(t, ok) {
assert.Equal(t, []Value{100, 200, 300}, row)
}
if row, ok := table.Row(date(day1)); assert.True(t, ok) {
assert.Equal(t, []Value{10, 20, 30}, row)
}
if row, ok := table.Row(date(day2)); assert.True(t, ok) {
assert.Equal(t, []Value{1, 2, 3}, row)
}
if row, ok := table.Column(0); assert.True(t, ok) {
assert.Equal(t, List{elV(day0, 100), elV(day1, 10), elV(day2, 1)}, row)
}
if row, ok := table.Column(1); assert.True(t, ok) {
assert.Equal(t, List{elV(day0, 200), elV(day1, 20), elV(day2, 2)}, row)
}
if row, ok := table.Column(2); assert.True(t, ok) {
assert.Equal(t, List{elV(day0, 300), elV(day1, 30), elV(day2, 3)}, row)
}
},
},
{
Name: "with different dates",
Lists: []List{
{elV(day1, 1)},
{elV(day2, 2)},
{elV(day3, 3)},
},
Then: func(t *testing.T, table *Table) {
assert.Equal(t, 0, table.NumberOfRows())
assert.Equal(t, 3, table.NumberOfColumns())
assert.Len(t, table.Times(), 0)
assert.Equal(t, [][]Value{{}, {}, {}}, table.Values())
},
},
{
Name: "with a second column with more history",
Lists: []List{
{elV(day1, 2)},
{elV(day0, 10), elV(day1, 20)},
},
Then: func(t *testing.T, table *Table) {
assert.Equal(t, 1, table.NumberOfRows())
assert.Equal(t, 2, table.NumberOfColumns())
assert.Equal(t, []time.Time{date(day1)}, table.Times())
assert.Equal(t, [][]Value{
{2},
{20},
}, table.Values())
},
},
{
Name: "with a second column with less history",
Lists: []List{
{elT(day0), elV(day1, 2)},
{elV(day1, 3)},
},
Then: func(t *testing.T, table *Table) {
assert.Len(t, table.Times(), 1)
assert.Equal(t, 1, table.NumberOfRows())
assert.Equal(t, 2, table.NumberOfColumns())
assert.Equal(t, []time.Time{date(day1)}, table.Times())
assert.Equal(t, [][]Value{{2}, {3}}, table.Values())
},
},
{
Name: "with no overlap",
Lists: []List{
{elT(day0), elV(day2, 2)},
{elV(day1, 3)},
},
Then: func(t *testing.T, table *Table) {
assert.Len(t, table.Times(), 0)
assert.Equal(t, 0, table.NumberOfRows())
assert.Equal(t, 2, table.NumberOfColumns())
assert.Equal(t, [][]Value{{}, {}}, table.Values())
},
},
{
Name: "with second asset having more data than the first overlap",
Lists: []List{
{elV(day0, 1), elV(day1, 2), elV(day3, 4)},
{elV(day0, 10), elV(day1, 20), elV(day2, 30), elV(day3, 40)},
},
Then: func(t *testing.T, table *Table) {
assert.Equal(t, 4, table.NumberOfRows())
assert.Equal(t, 2, table.NumberOfColumns())
assert.Equal(t, [][]Value{
{1, 2, 0, 4},
{10, 20, 30, 40},
}, table.Values())
},
},
{
Name: "with second asset having less data than the first overlap",
Lists: []List{
{elV(day0, 10), elV(day1, 20), elV(day2, 30), elV(day3, 40)},
{elV(day0, 1), elV(day1, 2), elV(day3, 4)},
},
Then: func(t *testing.T, table *Table) {
assert.Equal(t, 4, table.NumberOfRows())
assert.Equal(t, 2, table.NumberOfColumns())
assert.Equal(t, [][]Value{
{10, 20, 30, 40},
{1, 2, 0, 4},
}, table.Values())
},
},
} {
t.Run(tt.Name, func(t *testing.T) {
result := timetable.New(tt.Lists...)
tt.Then(t, result)
})
}
}
func TestCompact_Row(t *testing.T) {
t.Run("row not found", func(t *testing.T) {
table := timetable.New(List{
elV(day0, 1),
elV(day1, 2),
})
row, ok := table.Row(date(day2))
assert.False(t, ok)
assert.Len(t, row, 0)
})
}
func TestCompact_Column(t *testing.T) {
t.Run("column out of range", func(t *testing.T) {
table := timetable.New(List{
elV(day0, 1),
elV(day1, 2),
})
column, ok := table.Column(2)
assert.False(t, ok)
assert.Len(t, column, 0)
})
t.Run("negative range", func(t *testing.T) {
table := timetable.New(List{
elV(day0, 1),
elV(day1, 2),
})
column, ok := table.Column(-1)
assert.False(t, ok)
assert.Len(t, column, 0)
})
}