-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
251 lines (234 loc) · 5.69 KB
/
main_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
245
246
247
248
249
250
251
package challenge_encoding
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"os"
"reflect"
"regexp"
"testing"
)
func ExampleUnmarshall() {
input := `4
6 10
8 21
5 10
1 5
6
2 0 10 5 9 69
`
output := &firstTestFile{}
err := Unmarshall(output, bytes.NewReader([]byte(input)))
if err != nil {
fmt.Println("Could not unmarshall")
}
fmt.Printf("Output has %d as nCases.\nFirst call of example is %d %d", output.NCases, output.Calls[0].Start, output.Calls[0].End)
// Output: Output has 4 as nCases.
//First call of example is 6 10
}
func TestUnmarshall(t *testing.T) {
testCases := []struct{
name string
input string
parser interface{}
expected interface{}
}{
{
"First test",
`4
6 10
8 21
5 10
1 5
6
2 0 10 5 9 69
`,
&firstTestFile{},
firstTestFile{
NCases: 4,
Calls: []firstTestCall{{6, 10}, {8, 21}, {5, 10}, {1, 5}},
NQueries: 6,
Queries: []query{{2}, {0}, {10}, {5}, {9}, {69}},
},
},
{
"First test",
`4
6,10
8,21
5,10
1,5
6
2 0 10 5 9 69
`,
&firstTestFileCustomDelimiter{},
firstTestFileCustomDelimiter{
NCases: 4,
Calls: []firstTestCallCustomDelimiter{{6, 10}, {8, 21}, {5, 10}, {1, 5}},
NQueries: 6,
Queries: []query{{2}, {0}, {10}, {5}, {9}, {69}},
},
},
{
"Tuenti challenge 9th question",
`3
七十二千 OPERATOR 二三百十二 = 四二千百十二
五十百二 OPERATOR 五十六 = 千十万二百一五六
千八百二 OPERATOR 七百四千十七 = 千五百三六十五
`,
&tuentiChallengeQuestion9{},
tuentiChallengeQuestion9{
NCases: 3,
Cases: []tuentiChallengeQuestion9Case{
{"七十二千", "OPERATOR", "二三百十二", "=", "四二千百十二"},
{"五十百二", "OPERATOR", "五十六", "=", "千十万二百一五六"},
{"千八百二", "OPERATOR", "七百四千十七", "=", "千五百三六十五"},
},
},
},
{
"Tuenti challenge 11th question",
`2
2
2.0 2.5
0.0 3.14
12.0 100.0
4 5
20
6.0
4
0.3 0.4 0.5 0.6
0.15 0.18 1.15 1.6
28.8 216.0 27.0 432.0
1532 770 1250 1630
3330
2.0
`,
&tuentiChallengeQuestion11{},
tuentiChallengeQuestion11{
NCases: 2,
Cases: []tuentiChallengeQuestion11Case{
{
NMoons: 2,
Distances: []float64{2.0, 2.5},
Positions: []float64{0.0, 3.14},
Periods: []float64{12.0, 100.0},
Weights: []int{4, 5},
Capacity: 20,
Range: 6.0,
},
{
NMoons: 4,
Distances: []float64{0.3, 0.4, 0.5, 0.6},
Positions: []float64{0.15, 0.18, 1.15, 1.6},
Periods: []float64{28.8, 216.0, 27.0, 432.0},
Weights: []int{1532, 770, 1250, 1630},
Capacity: 3330,
Range: 2.0,
},
},
},
},
{
"Test different types",
`1
2
3 4
5
`,
&testDifferentTypes{},
testDifferentTypes{
SomeUint: 1,
SomeInt64: 2,
SomeInt32: 3,
SomeAlias: 4,
SomeInnerStruct: testInnerStruct{
SomeAlias: 5,
},
},
},
}
for _, tc := range(testCases) {
input := regexp.MustCompile(`\n\s+`).ReplaceAll([]byte(tc.input), []byte("\n"))
err := Unmarshall(tc.parser, bytes.NewReader(input))
if err != nil {
t.Error(err)
return
}
assert.Equal(t, tc.expected, reflect.ValueOf(tc.parser).Elem().Interface())
}
}
func TestParseFromFile (t *testing.T) {
reader, err := os.Open("./resources/problem11Input.txt")
if err != nil {
t.Error(err)
return
}
challenge := &tuentiChallengeQuestion11{}
err = Unmarshall(challenge, reader)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, 10, challenge.NCases)
}
type firstTestFile struct {
NCases int `index:"0"`
Calls []firstTestCall `index:"1" indexed:"NCases"`
NQueries int `index:"2"`
Queries []query `index:"3" indexed:"NQueries" elem_delimiter:"space"`
}
type firstTestFileCustomDelimiter struct {
NCases int `index:"0"`
Calls []firstTestCallCustomDelimiter `index:"1" indexed:"NCases"`
NQueries int `index:"2"`
Queries []query `index:"3" indexed:"NQueries" elem_delimiter:"space"`
}
type query struct {
Time int `index:"0" delimiter:"space"`
}
type firstTestCall struct {
Start int `index:"0" delimiter:"space"`
End int `index:"1"`
}
type firstTestCallCustomDelimiter struct {
Start int `index:"0" delimiter:","`
End int `index:"1"`
}
type tuentiChallengeQuestion9 struct {
NCases int `index:"0"`
Cases []tuentiChallengeQuestion9Case `index:"1" indexed:"NCases"`
}
type tuentiChallengeQuestion9Case struct {
Lhs1 string `index:"0" delimiter:"space"`
Operator string `index:"1" delimiter:"space"`
Lhs2 string `index:"2" delimiter:"space"`
Equal string `index:"3" delimiter:"space"`
Rhs string `index:"4" delimiter:"space"`
}
type tuentiChallengeQuestion11 struct {
NCases int `index:"0"`
Cases []tuentiChallengeQuestion11Case `index:"1" indexed:"NCases"`
}
type tuentiChallengeQuestion11Case struct {
NMoons int `index:"0"`
Distances []float64 `index:"1" elem_delimiter:"space" indexed:"NMoons"`
Positions []float64 `index:"2" elem_delimiter:"space" indexed:"NMoons"`
Periods []float64 `index:"3" elem_delimiter:"space" indexed:"NMoons"`
Weights []int `index:"4" elem_delimiter:"space" indexed:"NMoons"`
Capacity int `index:"5"`
Range float64 `index:"6"`
}
type testDifferentTypes struct {
SomeUint uint `index:"0"`
SomeInt64 int64 `index:"1"`
SomeInt32 int32 `index:"2" delimiter:"space"`
SomeAlias myAlias `index:"3"`
SomeInnerStruct testInnerStruct `index:"4"`
}
type testInnerStruct struct {
SomeAlias myAlias `index:"0"`
}
type myAlias int
// TODO test fixed size arrays
// TODO error test on [][]