-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_test.go
133 lines (128 loc) · 4.26 KB
/
split_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
package stl
import (
"bufio"
"bytes"
"fmt"
"testing"
)
func Test_splitTrianglesASCII(t *testing.T) {
for _, tst := range []struct {
in []byte
eof bool
want struct {
advance int
token []byte
err error
}
}{
// Empty input and EOF
{
in: []byte{},
eof: true,
want: struct {
advance int
token []byte
err error
}{
advance: 0,
token: nil,
err: nil,
},
},
// Full token found
{
in: []byte("facet normal 0.05082 -0.24321 -0.96864\n outer loop\n vertex -1000 0 0\n vertex 0 -358 -934\n vertex 0 -407 -914\n endloop\n endfacet\n"),
eof: false,
want: struct {
advance int
token []byte
err error
}{
advance: 136,
token: []byte("facet normal 0.05082 -0.24321 -0.96864\n outer loop\n vertex -1000 0 0\n vertex 0 -358 -934\n vertex 0 -407 -914\n endloop\n endfacet"),
err: nil,
},
},
// Beginning of token
{
in: []byte("facet normal 0.05082 -0.24321 -0.96864\n"),
eof: false,
want: struct {
advance int
token []byte
err error
}{
advance: 0,
token: nil,
err: nil,
},
},
// Full token found, but there is more in data
{
in: []byte("facet normal 0.05082 -0.24321 -0.96864\n outer loop\n vertex -1000 0 0\n vertex 0 -358 -934\n vertex 0 -407 -914\n endloop\n endfacet\nfacet normal 0.05082 -0.24321 -0.96864\n outer loop\n vertex -1000 0 0\n vertex 0 -358 -934\n vertex 0 -407 -914\n endloop\n endfacet\n"),
eof: false,
want: struct {
advance int
token []byte
err error
}{
advance: 136,
token: []byte("facet normal 0.05082 -0.24321 -0.96864\n outer loop\n vertex -1000 0 0\n vertex 0 -358 -934\n vertex 0 -407 -914\n endloop\n endfacet"),
err: nil,
},
},
// End of input, ignoring "endsolid"
{
in: []byte("endsolid ASCII_STL_of_a_sphericon_by_CMG_Lee\n"),
eof: true,
want: struct {
advance int
token []byte
err error
}{
advance: 0,
token: nil,
err: nil,
},
},
} {
tst := tst
t.Run(fmt.Sprintf("splitTrianglesASCII - %q", tst.in), func(t *testing.T) {
t.Parallel()
gotAdvance, gotToken, gotError := splitTrianglesASCII(tst.in, tst.eof)
if gotAdvance != tst.want.advance {
t.Errorf("got %d; want %d", gotAdvance, tst.want.advance)
}
if !bytes.Equal(gotToken, tst.want.token) {
t.Errorf("got \n%q\nwant\n%q", string(gotToken), string(tst.want.token))
}
if gotError == nil && tst.want.err != nil || gotError != nil && tst.want.err == nil {
t.Errorf("unexpected nil")
t.FailNow()
}
if gotError != nil && tst.want.err != nil && gotError.Error() != tst.want.err.Error() {
t.Errorf("got %s; want %s", gotError.Error(), tst.want.err.Error())
}
})
}
}
func Test_splitTrianglesScannerASCII(t *testing.T) {
data := "solid ASCII_STL_of_a_sphericon_by_CMG_Lee\n facet normal 0.05082 -0.24321 -0.96864\n outer loop\n vertex -1000 0 0\n vertex 0 -358 -934\n vertex 0 -407 -914\n endloop\n endfacet\n facet normal -0.05382 -0.80723 0.58777\n outer loop\n vertex 0 -1000 0\n vertex 995 0 105\n vertex 988 0 156\n endloop\n endfacet\n facet normal -0.06315 -0.82099 0.56743\n outer loop\n vertex 0 -1000 0\n vertex 999 0 52\n vertex 995 0 105\n endloop\n endfacet\nendsolid ASCII_STL_of_a_sphericon_by_CMG_Lee\n"
tokens := []string{
" facet normal 0.05082 -0.24321 -0.96864\n outer loop\n vertex -1000 0 0\n vertex 0 -358 -934\n vertex 0 -407 -914\n endloop\n endfacet",
" facet normal -0.05382 -0.80723 0.58777\n outer loop\n vertex 0 -1000 0\n vertex 995 0 105\n vertex 988 0 156\n endloop\n endfacet",
" facet normal -0.06315 -0.82099 0.56743\n outer loop\n vertex 0 -1000 0\n vertex 999 0 52\n vertex 995 0 105\n endloop\n endfacet",
}
// Create a buffered reader to get past the first line, which is the header
buf := bufio.NewReader(bytes.NewReader([]byte(data)))
_, _, _ = buf.ReadLine()
// Create a scanner that takes in the splitTriangles split func
scanner := bufio.NewScanner(buf)
scanner.Split(splitTrianglesASCII)
// Check that tokens are taken out in order
for i := 0; scanner.Scan(); i++ {
if scanner.Text() != tokens[i] {
t.Errorf("got %q\n; want %q\n", scanner.Text(), tokens[i])
}
}
}