Skip to content
This repository was archived by the owner on May 31, 2020. It is now read-only.

Commit 937d4e2

Browse files
committed
Add another test
Signed-off-by: Lehner Florian <[email protected]>
1 parent 23b41c5 commit 937d4e2

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

datatypes_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func TestExtractDataElement(t *testing.T) {
212212
{name: "MiUint64", data: []byte{0x11, 0x22, 0x33, 0x44, 0x11, 0x22, 0x33, 0x44}, order: binary.LittleEndian, dataType: MiUint64, numberOfBytes: 8, step: 8, ele: 4914309075945333265},
213213
{name: "MiDouble", data: []byte{0x11, 0x22, 0x33, 0x44, 0x11, 0x22, 0x33, 0x44}, order: binary.LittleEndian, dataType: MiDouble, numberOfBytes: 8, step: 8, ele: 3.529429556587807e+20},
214214
{name: "MiMatrix", data: verySimpleMatrix, order: binary.LittleEndian, dataType: MiMatrix, numberOfBytes: 1, step: 144, ele: []interface{}{MatMatrix{Name: "MaTrIx", Flags: 0x6, Class: 0x6, Dim: Dim{X: 3, Y: 3, Z: 0}, Content: NumPrt{RealPart: []interface{}{1, 0, 1, 0, 1, 0, 1, 0, 1}, ImaginaryPart: interface{}(nil)}}}},
215-
{name: "MiStruct", data: verySimpleStruct, order: binary.LittleEndian, dataType: MiMatrix, numberOfBytes: 1, step: 344, ele: []interface{}{MatMatrix{Name: "testing_struct", Flags: 0x2, Class: 0x2, Dim: Dim{X: 1, Y: 1, Z: 0}, Content: StructPrt{FieldNames: []string{"field1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", "field2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"}, FieldValues: map[string][]interface{}{"field1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00": []interface{}{MatMatrix{Name: "", Flags: 0x6, Class: 0x6, Dim: Dim{X: 1, Y: 1, Z: 0}, Content: NumPrt{RealPart: []interface{}{1}, ImaginaryPart: interface{}(nil)}}}, "field2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00": []interface{}{MatMatrix{Name: "", Flags: 0x6, Class: 0x6, Dim: Dim{X: 1, Y: 1, Z: 0}, Content: NumPrt{RealPart: []interface{}{2}, ImaginaryPart: interface{}(nil)}}}}}}}},
215+
{name: "MiStruct", data: verySimpleStruct, order: binary.LittleEndian, dataType: MiMatrix, numberOfBytes: 1, step: 344, ele: []interface{}{MatMatrix{Name: "testing_struct", Flags: 0x2, Class: 0x2, Dim: Dim{X: 1, Y: 1, Z: 0}, Content: StructPrt{FieldNames: []string{"field1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", "field2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"}, FieldValues: map[string][]interface{}{"field1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00": {MatMatrix{Name: "", Flags: 0x6, Class: 0x6, Dim: Dim{X: 1, Y: 1, Z: 0}, Content: NumPrt{RealPart: []interface{}{1}, ImaginaryPart: interface{}(nil)}}}, "field2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00": {MatMatrix{Name: "", Flags: 0x6, Class: 0x6, Dim: Dim{X: 1, Y: 1, Z: 0}, Content: NumPrt{RealPart: []interface{}{2}, ImaginaryPart: interface{}(nil)}}}}}}}},
216216
{name: "Mi3dMatrix", data: verySimple3DMatrix, order: binary.LittleEndian, dataType: MiMatrix, numberOfBytes: 1, step: 1048, ele: []interface{}{MatMatrix{Name: "matrix3d", Flags: 0x806, Class: 0x6, Dim: Dim{X: 3, Y: 4, Z: 5}, Content: NumPrt{RealPart: []interface{}{42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, ImaginaryPart: []interface{}{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}}}},
217217
{name: "MiCell", data: verySimpleCell, order: binary.LittleEndian, dataType: MiMatrix, numberOfBytes: 1, step: 128, ele: []interface{}{MatMatrix{Name: "cell", Flags: 0x1, Class: 0x1, Dim: Dim{X: 1, Y: 1, Z: 3}, Content: CellPrt{Cells: []MatMatrix{{Name: "", Flags: 0x6, Class: 0x6, Dim: Dim{X: 0, Y: 0, Z: 0}, Content: NumPrt{RealPart: []interface{}(nil), ImaginaryPart: interface{}(nil)}}, {Name: "", Flags: 0x6, Class: 0x6, Dim: Dim{X: 0, Y: 0, Z: 0}, Content: NumPrt{RealPart: []interface{}(nil), ImaginaryPart: interface{}(nil)}}, {Name: "", Flags: 0x6, Class: 0x6, Dim: Dim{X: 0, Y: 0, Z: 0}, Content: NumPrt{RealPart: []interface{}(nil), ImaginaryPart: interface{}(nil)}}}}}}},
218218
{name: "MxCharClass", data: verySimpleChar, order: binary.LittleEndian, dataType: MiMatrix, numberOfBytes: 1, step: 160, ele: []interface{}{MatMatrix{Name: "coll2", Flags: 0x4, Class: 0x4, Dim: Dim{X: 3, Y: 13, Z: 0}, Content: CharPrt{Chars: []string{"string1 ", "STRING2 ", "# a b c d e f"}}}}},

matf_test.go

+45-13
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,51 @@ func TestMatf(t *testing.T) {
312312
}
313313
defer simple.Close()
314314

315-
x, err := Open(simple.Name())
316-
if err != nil {
317-
t.Fatalf("Could not open test file: %v", err)
315+
corrupt, ferr := ioutil.TempFile(tdir, "corrupt.mat")
316+
if ferr != nil {
317+
t.Fatal(ferr)
318+
}
319+
defer os.Remove(corrupt.Name())
320+
321+
ferr = ioutil.WriteFile(corrupt.Name(), noMatf, 0644)
322+
if ferr != nil {
323+
t.Fatal(ferr)
318324
}
319-
defer Close(x)
320-
321-
for {
322-
mat, err := ReadDataElement(x)
323-
if err == io.EOF {
324-
break
325-
} else if err != nil {
326-
t.Fatalf("Could not open test file: %v", err)
327-
}
328-
_ = mat
325+
defer simple.Close()
326+
327+
tests := []struct {
328+
name string
329+
file string
330+
err string
331+
}{
332+
{name: "simple", file: simple.Name()},
333+
{name: "corrupt", file: corrupt.Name(), err: "Could not read enough bytes"},
334+
}
335+
336+
for _, tc := range tests {
337+
t.Run(tc.name, func(t *testing.T) {
338+
x, err := Open(tc.file)
339+
if err != nil {
340+
if matched, _ := regexp.MatchString(tc.err, err.Error()); !matched {
341+
t.Fatalf("Error matching regex: %v \t Got: %v", tc.err, err)
342+
} else {
343+
return
344+
}
345+
t.Fatalf("Expected no error, got: %v", err)
346+
} else if len(tc.err) != 0 {
347+
t.Fatalf("Expected error, got none")
348+
}
349+
defer Close(x)
350+
351+
for {
352+
mat, err := ReadDataElement(x)
353+
if err == io.EOF {
354+
break
355+
} else if err != nil {
356+
t.Fatalf("Could not open test file: %v", err)
357+
}
358+
_ = mat
359+
}
360+
})
329361
}
330362
}

0 commit comments

Comments
 (0)