-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_test.go
256 lines (236 loc) · 8.59 KB
/
decoder_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
package marc
import (
"bufio"
"bytes"
"fmt"
"testing"
)
var sampleMARC = `01142cam 2200301 a 4500001001300000003000400013005001700017008004100034010001700075020002500092040001800117042000900135050002600144082001600170100003200186245008600218250001200304260005200316300004900368500004000417520022800457650003300685650003300718650002400751650002100775650002300796700002100819 92005291 DLC19930521155141.9920219s1993 caua j 000 0 eng a 92005291 a0152038655 :c$15.95 aDLCcDLCdDLC alcac00aPS3537.A618bA88 199300a811/.522201 aSandburg, Carl,d1878-1967.10aArithmetic /cCarl Sandburg ; illustrated as an anamorphic adventure by Ted Rand. a1st ed. aSan Diego :bHarcourt Brace Jovanovich,cc1993. a1 v. (unpaged) :bill. (some col.) ;c26 cm. aOne Mylar sheet included in pocket. aA poem about numbers and their characteristics. Features anamorphic, or distorted, drawings which can be restored to normal by viewing from a particular angle or by viewing the image's reflection in the provided Mylar cone. 0aArithmeticxJuvenile poetry. 0aChildren's poetry, American. 1aArithmeticxPoetry. 1aAmerican poetry. 1aVisual perception.1 aRand, Ted,eill.`
var sampleLineMARC = `*000 c
*0010010463
*008871001 a 0 nob r
*015 $a29$bBibliofilID
*019 $bl
*0823 $a242
*090 $c242$dKar
*100 0$aKarlén, Barbro$d1954-$jsv.$310008600
*24010$aI begynnelsen skapade Gud
*24510$aI begynnelsen skapte Gud$bdikt og salmer i norsk gjendiktning ved Inger Hagerup ; prosaen overs. av Gunnel Malmström ; illus. av Vanja Hübinette-Simonson
*260 $aOslo$bAschehoug$c1968
*300 $a97 s.$bill.
*574 $aOriginaltittel: I begynnelsen skapade Gud
*655 $aAndaktsbøker$310008700
*70010$aHagerup, Inger$d1905-1985$jn.$310008800
*850 $aDEICHM$sn
^`
var sampleMARCXML = `
<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="http://www.loc.gov/MARC21/slim">
<record>
<leader>01142cam 2200301 a 4500</leader>
<controlfield tag="001"> 92005291 </controlfield>
<controlfield tag="003">DLC</controlfield>
<controlfield tag="005">19930521155141.9</controlfield>
<controlfield tag="008">920219s1993 caua j 000 0 eng </controlfield>
<datafield tag="010" ind1=" " ind2=" ">
<subfield code="a"> 92005291 </subfield>
</datafield>
<datafield tag="020" ind1=" " ind2=" ">
<subfield code="a">0152038655 :</subfield>
<subfield code="c">$15.95</subfield>
</datafield>
<datafield tag="040" ind1=" " ind2=" ">
<subfield code="a">DLC</subfield>
<subfield code="c">DLC</subfield>
<subfield code="d">DLC</subfield>
</datafield>
<datafield tag="042" ind1=" " ind2=" ">
<subfield code="a">lcac</subfield>
</datafield>
<datafield tag="050" ind1="0" ind2="0">
<subfield code="a">PS3537.A618</subfield>
<subfield code="b">A88 1993</subfield>
</datafield>
<datafield tag="082" ind1="0" ind2="0">
<subfield code="a">811/.52</subfield>
<subfield code="2">20</subfield>
</datafield>
<datafield tag="100" ind1="1" ind2=" ">
<subfield code="a">Sandburg, Carl,</subfield>
<subfield code="d">1878-1967.</subfield>
</datafield>
<datafield tag="245" ind1="1" ind2="0">
<subfield code="a">Arithmetic /</subfield>
<subfield code="c">Carl Sandburg ; illustrated as an anamorphic adventure by Ted Rand.</subfield>
</datafield>
<datafield tag="250" ind1=" " ind2=" ">
<subfield code="a">1st ed.</subfield>
</datafield>
<datafield tag="260" ind1=" " ind2=" ">
<subfield code="a">San Diego :</subfield>
<subfield code="b">Harcourt Brace Jovanovich,</subfield>
<subfield code="c">c1993.</subfield>
</datafield>
<datafield tag="300" ind1=" " ind2=" ">
<subfield code="a">1 v. (unpaged) :</subfield>
<subfield code="b">ill. (some col.) ;</subfield>
<subfield code="c">26 cm.</subfield>
</datafield>
<datafield tag="500" ind1=" " ind2=" ">
<subfield code="a">One Mylar sheet included in pocket.</subfield>
</datafield>
<datafield tag="520" ind1=" " ind2=" ">
<subfield code="a">A poem about numbers and their characteristics. Features anamorphic, or distorted, drawings which can be restored to normal by viewing from a particular angle or by viewing the image's reflection in the provided Mylar cone.</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="0">
<subfield code="a">Arithmetic</subfield>
<subfield code="x">Juvenile poetry.</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="0">
<subfield code="a">Children's poetry, American.</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="1">
<subfield code="a">Arithmetic</subfield>
<subfield code="x">Poetry.</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="1">
<subfield code="a">American poetry.</subfield>
</datafield>
<datafield tag="650" ind1=" " ind2="1">
<subfield code="a">Visual perception.</subfield>
</datafield>
<datafield tag="700" ind1="1" ind2=" ">
<subfield code="a">Rand, Ted,</subfield>
<subfield code="e">ill.</subfield>
</datafield>
</record>
</collection>`
func TestDetectFormat(t *testing.T) {
tests := []struct {
input string
want Format
}{
{sampleMARC, MARC},
{sampleMARCXML, MARCXML},
{sampleLineMARC, LineMARC},
{"abc", unknown},
}
for _, test := range tests {
if f := DetectFormat([]byte(test.input)); f != test.want {
t.Errorf("DetectFormat => %v; want %v", f, test.want)
}
}
}
func TestDecodeMARC(t *testing.T) { testDecodeRecord(t, sampleMARC, MARC) }
func TestDecodeLineMARC(t *testing.T) { testDecodeRecord(t, sampleLineMARC, LineMARC) }
func TestDecodeMARCXML(t *testing.T) { testDecodeRecord(t, sampleMARCXML, MARCXML) }
func testDecodeRecord(t *testing.T, input string, f Format) {
dec := NewDecoder(bytes.NewBufferString(input), f)
r, err := dec.DecodeAll()
if err != nil {
t.Fatal(err)
}
if len(r) != 1 {
t.Fatalf("expected 1 record; got %d", len(r))
}
}
func TestDecodeEncodeRoundtrip(t *testing.T) {
tests := []struct{ inF, outF Format }{
{MARC, MARC},
//{MARC, LineMARC},
//{LineMARC, LineMARC},
//{LineMarc, MARC},
{MARC, MARCXML},
{MARCXML, MARCXML},
//{MARCXML, LineMARC},
//{LineMarc, LineMarc},
//{LineMARC, MARCXML},
{MARCXML, MARC},
}
// buffer used for decoding and encoding
b := bytes.NewBufferString(sampleMARC)
for _, test := range tests {
dec := NewDecoder(b, test.inF)
r, err := dec.Decode()
if err != nil {
t.Fatal(err)
}
b.Reset()
enc := NewEncoder(b, test.outF)
err = enc.Encode(r)
if err != nil {
t.Fatal(err)
}
enc.Flush()
//fmt.Println(b.String())
dec = NewDecoder(b, test.outF)
r2, err := dec.Decode()
if err != nil {
t.Fatal(err)
}
if !r.Eq(r2) {
fmt.Printf("%v\n\n", r)
fmt.Printf("%v\n\n", r2)
t.Fatalf("decode %s -> encode %s roundtrip failed", test.inF, test.outF)
}
b.Reset()
enc = NewEncoder(b, test.outF)
err = enc.Encode(r)
if err != nil {
t.Fatal(err)
}
enc.Flush()
}
}
func BenchmarkDecodeBaseline(b *testing.B) {
for n := 0; n < b.N; n++ {
r := bufio.NewReader(bytes.NewBufferString(sampleLineMARC))
b.SetBytes(int64(len(sampleLineMARC)))
_, err := r.ReadBytes(0x5E)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkDecodeMARC(b *testing.B) { benchmarkDecode(b, sampleMARC, MARC) }
func BenchmarkDecodeLineMARC(b *testing.B) { benchmarkDecode(b, sampleLineMARC, LineMARC) }
func BenchmarkDecodeMARCXML(b *testing.B) { benchmarkDecode(b, sampleMARCXML, MARCXML) }
func benchmarkDecode(b *testing.B, sample string, f Format) {
for n := 0; n < b.N; n++ {
b.SetBytes(int64(len(sample)))
dec := NewDecoder(bytes.NewBufferString(sample), f)
_, err := dec.Decode()
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkEncodeBaseline(b *testing.B) {
var w bytes.Buffer
for n := 0; n < b.N; n++ {
b.SetBytes(int64(len(sampleMARC)))
_, err := w.WriteString(sampleMARC)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkEncodeMARC(b *testing.B) { benchmarkEncode(b, sampleMARC, MARC) }
func BenchmarkEncodeLineMARC(b *testing.B) { benchmarkEncode(b, sampleLineMARC, LineMARC) }
func BenchmarkEncodeMARCXML(b *testing.B) { benchmarkEncode(b, sampleMARCXML, MARCXML) }
func benchmarkEncode(b *testing.B, sample string, f Format) {
var w bytes.Buffer
dec := NewDecoder(bytes.NewBufferString(sample), f)
rec, err := dec.Decode()
if err != nil {
b.Fatal(err)
}
for n := 0; n < b.N; n++ {
b.SetBytes(int64(len(sample)))
enc := NewEncoder(&w, f)
err := enc.Encode(rec)
if err != nil {
b.Fatal(err)
}
}
}