-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiso8211_test.go
More file actions
90 lines (85 loc) · 2.12 KB
/
iso8211_test.go
File metadata and controls
90 lines (85 loc) · 2.12 KB
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
// Copyright 2015 Thomas Burke <tburke@tb99.com>. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package iso8211
import (
"fmt"
"os"
"reflect"
"testing"
)
func TestFieldTypeFormat(t *testing.T) {
var f FieldType
f.FormatControls = []byte("(A)")
v := f.Format()
e := SubFieldType{reflect.String, 0, nil}
if len(v) != 1 || !reflect.DeepEqual(v[0], e) {
t.Error("Expected ", e, ", got ", v)
}
var f2 FieldType
f2.FormatControls = []byte("(b11,2b24,A(3),B(40))")
f2.ArrayDescriptor = []byte("A!B!C!D!E")
v = f2.Format()
a := []SubFieldType{
{reflect.Uint8, 1, []byte{'A'}},
{reflect.Int32, 4, []byte{'B'}},
{reflect.Int32, 4, []byte{'C'}},
{reflect.String, 3, []byte{'D'}},
{reflect.Array, 5, []byte{'E'}}}
if len(v) != len(a) {
t.Error("Format did not return the expected number of values")
} else {
for i, o := range v {
if !reflect.DeepEqual(o, a[i]) {
t.Error("At ", i, " Expected ", a[i], ", got ", o)
}
}
}
}
func TestS57File(t *testing.T) {
f, err := os.Open("testdata/US5MD12M.001")
if err != nil {
t.Error("Unexpected error: ", err)
}
defer f.Close()
var l LeadRecord
if l.Read(f) != nil {
t.Error("Error reading the lead record")
}
var d DataRecord
d.Lead = &l
if d.Read(f) != nil {
t.Error("Error reading Data record 1")
}
if len(d.Fields) != 3 && d.Fields[0].SubFields[0] != 1 {
t.Error("Data record 1 is not what we expected.")
}
if d.Read(f) != nil {
t.Error("Error reading Data record 2")
}
if len(d.Fields) != 4 && d.Fields[0].SubFields[0] != 2 {
t.Error("Data record 2 is not what we expected.")
}
if len(d.Fields[3].SubFields) != 6 && d.Fields[3].SubFields[4] != 148 {
t.Error("Data record 2, Field 4 is not what we expected.", d.Fields[3])
}
if d.Read(f) == nil {
t.Error("Should be at EOF")
}
}
func Example() {
f, err := os.Open("testdata/US5MD12M.001")
if err != nil {
fmt.Println("No file. ", err)
}
var l LeadRecord
l.Read(f)
var d DataRecord
d.Lead = &l
for d.Read(f) == nil {
fmt.Printf("Rec: %d\n", d.Fields[0].SubFields[0])
}
// Output:
// Rec: 1
// Rec: 2
}