forked from hschendel/stl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadwrite_test.go
268 lines (236 loc) · 6.61 KB
/
readwrite_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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package stl
// Tests for reading and writing STL files.
import (
"io/ioutil"
"os"
"strconv"
"testing"
)
const TestFilenameSimpleAscii = "testdata/simple_ascii.stl"
const TestFilenameSimpleBinary = "testdata/simple_bin.stl"
const TestFilenameComplexAscii = "testdata/complex_ascii.stl"
const TestFilenameComplexBinary = "testdata/complex_bin.stl"
func TestIsAsciiFile(t *testing.T) {
asciiFile, openAsciiErr := os.Open(TestFilenameSimpleAscii)
if openAsciiErr != nil {
t.Fatal(openAsciiErr)
}
defer asciiFile.Close()
isAscii, _, err := isAsciiFile(asciiFile)
if err != nil {
t.Fatal(err)
}
if !isAscii {
t.Error("ASCII file not detected as ASCII")
}
binaryFile, openBinaryErr := os.Open(TestFilenameSimpleBinary)
if openBinaryErr != nil {
t.Fatal(openBinaryErr)
}
defer binaryFile.Close()
isAscii, _, err = isAsciiFile(binaryFile)
if err != nil {
t.Fatal(err)
}
if isAscii {
t.Error("Binary file detected as ASCII")
}
}
func TestReadFile_Ascii(t *testing.T) {
solid, err := ReadFile(TestFilenameSimpleAscii)
if err != nil {
t.Fatal("Error in ReadFile: " + err.Error())
}
testSolid := makeTestSolid()
testSolid.IsAscii = true
testSolid.BinaryHeader = nil
if !solid.sameOrderAlmostEqual(testSolid) {
t.Error("Not as expected")
t.Log("Expected:\n", testSolid)
t.Log("Found:\n", solid)
}
}
func BenchmarkReadFile_Ascii_Simple(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := ReadFile(TestFilenameSimpleAscii)
if err != nil {
b.Fatal("Error in ReadFile: " + err.Error())
}
}
}
func BenchmarkReadFile_Ascii_Complex(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := ReadFile(TestFilenameComplexAscii)
if err != nil {
b.Fatal("Error in ReadFile: " + err.Error())
}
}
}
func TestReadFile_Binary(t *testing.T) {
solid, err := ReadFile(TestFilenameSimpleBinary)
if err != nil {
t.Fatal("Error in ReadFile: " + err.Error())
}
testSolid := makeTestSolid()
testSolid.IsAscii = false
testSolid.BinaryHeader = make([]byte, 80)
copy(testSolid.BinaryHeader, string(testSolid.Name))
if !solid.sameOrderAlmostEqual(testSolid) {
t.Error("Not as expected")
t.Log("Expected:\n", testSolid)
t.Log("Found:\n", solid)
}
}
func BenchmarkReadFile_Binary_Simple(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := ReadFile(TestFilenameSimpleBinary)
if err != nil {
b.Fatal("Error in ReadFile: " + err.Error())
}
}
}
func BenchmarkReadFile_Binary_Complex(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := ReadFile(TestFilenameComplexBinary)
if err != nil {
b.Fatal("Error in ReadFile: " + err.Error())
}
}
}
func TestReadAll_Binary(t *testing.T) {
file, openErr := os.Open(TestFilenameSimpleBinary)
if openErr != nil {
t.Fatal(openErr)
}
defer file.Close()
solid, err := ReadAll(file)
if err != nil {
t.Fatal("Error in ReadAll: " + err.Error())
}
testSolid := makeTestSolid()
testSolid.IsAscii = false
testSolid.BinaryHeader = make([]byte, 80)
copy(testSolid.BinaryHeader, string(testSolid.Name))
if !solid.sameOrderAlmostEqual(testSolid) {
t.Error("Not as expected")
t.Log("Expected:\n", testSolid)
t.Log("Found:\n", solid)
}
}
func TestWriteFile_Ascii(t *testing.T) {
tmpDirName, tmpErr := ioutil.TempDir(os.TempDir(), "stl_test")
if tmpErr != nil {
t.Fatal(tmpErr.Error())
}
defer os.RemoveAll(tmpDirName)
tmpFileName := tmpDirName + string(os.PathSeparator) + "test_out_ascii.stl"
testSolid := makeTestSolid()
testSolid.IsAscii = true
err := testSolid.WriteFile(tmpFileName)
if err != nil {
t.Fatal(err.Error())
}
eq, cmpErr := cmpFiles(TestFilenameSimpleAscii, tmpFileName)
if cmpErr != nil {
t.Fatal(cmpErr)
}
if !eq {
t.Log("Generated file:")
data, readErr := ioutil.ReadFile(tmpFileName)
if readErr != nil {
t.Fatal(readErr)
}
t.Log(string(data))
t.Error("Was expected to look like " + TestFilenameSimpleAscii)
}
}
func BenchmarkWriteSmallFile_Ascii(b *testing.B) {
tmpDirName, tmpErr := ioutil.TempDir(os.TempDir(), "stl_test")
if tmpErr != nil {
b.Fatal(tmpErr.Error())
}
defer os.RemoveAll(tmpDirName)
testSolid := makeTestSolid()
testSolid.IsAscii = true
var tmpFileName string
for i := 0; i < b.N; i++ {
// generate a new file name for every turn, as overwriting the same file again
// and again seems to delay some systems.
tmpFileName = tmpDirName + string(os.PathSeparator) + strconv.Itoa(i) + "_bench_out_ascii.stl"
err := testSolid.WriteFile(tmpFileName)
if err != nil {
b.Fatal("Error in WriteFile: " + err.Error())
}
}
}
func TestWriteFile_Binary(t *testing.T) {
tmpDirName, tmpErr := ioutil.TempDir(os.TempDir(), "stl_test")
if tmpErr != nil {
t.Fatal(tmpErr.Error())
}
defer os.RemoveAll(tmpDirName)
tmpFileName := tmpDirName + string(os.PathSeparator) + "test_out_binary.stl"
testSolid := makeTestSolid()
testSolid.IsAscii = false
err := testSolid.WriteFile(tmpFileName)
if err != nil {
t.Fatal(err.Error())
}
eq, cmpErr := cmpFiles(TestFilenameSimpleBinary, tmpFileName)
if cmpErr != nil {
t.Fatal(cmpErr)
}
if !eq {
t.Log("Generated file:")
data, readErr := ioutil.ReadFile(tmpFileName)
if readErr != nil {
t.Fatal(readErr)
}
t.Log(string(data))
t.Error("Was expected to look like " + TestFilenameSimpleBinary)
}
}
func BenchmarkWriteSmallFile_Binary(b *testing.B) {
tmpDirName, tmpErr := ioutil.TempDir(os.TempDir(), "stl_test")
if tmpErr != nil {
b.Fatal(tmpErr.Error())
}
defer os.RemoveAll(tmpDirName)
testSolid := makeTestSolid()
testSolid.IsAscii = false
var tmpFileName string
for i := 0; i < b.N; i++ {
// generate a new file name for every turn, as overwriting the same file again
// and again seems to delay some systems.
tmpFileName = tmpDirName + string(os.PathSeparator) + strconv.Itoa(i) + "_bench_out_binary_small.stl"
err := testSolid.WriteFile(tmpFileName)
if err != nil {
b.Fatal("Error in WriteFile: " + err.Error())
}
}
}
// Does assume ReadFile to work correctly
func BenchmarkWriteMediumFile_Binary(b *testing.B) {
b.StopTimer()
tmpDirName, tmpErr := ioutil.TempDir(os.TempDir(), "stl_test")
if tmpErr != nil {
b.Fatal(tmpErr)
}
defer os.RemoveAll(tmpDirName)
testSolid, readErr := ReadFile(TestFilenameComplexBinary)
if readErr != nil {
b.Fatal(readErr)
}
testSolid.IsAscii = false // to be sure ;-)
b.StartTimer()
var tmpFileName string
for i := 0; i < b.N; i++ {
// generate a new file name for every turn, as overwriting the same file again
// and again seems to delay some systems.
tmpFileName = tmpDirName + string(os.PathSeparator) + strconv.Itoa(i) + "_bench_out_binary_medium.stl"
err := testSolid.WriteFile(tmpFileName)
if err != nil {
b.Fatal("Error in WriteFile: " + err.Error())
}
}
}