This repository has been archived by the owner on Nov 16, 2017. It is now read-only.
forked from gillesdemey/go-dicom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_test.go
130 lines (121 loc) · 3.47 KB
/
parser_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
package dicom_test
import (
"encoding/binary"
"github.com/yasushi-saito/go-dicom"
"github.com/yasushi-saito/go-dicom/dicomio"
"github.com/yasushi-saito/go-dicom/dicomuid"
"reflect"
"testing"
)
func testWriteDataElement(t *testing.T, bo binary.ByteOrder, implicit dicomio.IsImplicitVR) {
// Encode two scalar elements.
e := dicomio.NewBytesEncoder(bo, implicit)
var values []interface{}
values = append(values, string("FooHah"))
dicom.WriteElement(e, &dicom.Element{
Tag: dicom.Tag{0x0018, 0x9755},
Value: values})
values = nil
values = append(values, uint32(1234))
values = append(values, uint32(2345))
dicom.WriteElement(e, &dicom.Element{
Tag: dicom.Tag{0x0020, 0x9057},
Value: values})
data := e.Bytes()
// Read them back.
d := dicomio.NewBytesDecoder(data, bo, implicit)
elem0 := dicom.ReadElement(d, dicom.ReadOptions{})
if d.Error() != nil {
t.Fatal(d.Error())
}
tag := dicom.Tag{0x18, 0x9755}
if elem0.Tag != tag {
t.Error("Bad tag", elem0)
}
if len(elem0.Value) != 1 {
t.Error("Bad value", elem0)
}
if elem0.Value[0].(string) != "FooHah" {
t.Error("Bad value", elem0)
}
tag = dicom.Tag{Group: 0x20, Element: 0x9057}
elem1 := dicom.ReadElement(d, dicom.ReadOptions{})
if d.Error() != nil {
t.Fatal(d.Error())
}
if elem1.Tag != tag {
t.Error("Bad tag")
}
if len(elem1.Value) != 2 {
t.Error("Bad value", elem1)
}
if elem1.Value[0].(uint32) != 1234 {
t.Error("Bad value", elem1)
}
if elem1.Value[1].(uint32) != 2345 {
t.Error("Bad value", elem1)
}
if err := d.Finish(); err != nil {
t.Error(err)
}
}
func TestWriteDataElementImplicit(t *testing.T) {
testWriteDataElement(t, binary.LittleEndian, dicomio.ImplicitVR)
}
func TestWriteDataElementExplicit(t *testing.T) {
testWriteDataElement(t, binary.LittleEndian, dicomio.ExplicitVR)
}
func TestWriteDataElementBigEndianExplicit(t *testing.T) {
testWriteDataElement(t, binary.BigEndian, dicomio.ExplicitVR)
}
func TestReadWriteFileHeader(t *testing.T) {
e := dicomio.NewBytesEncoder(binary.LittleEndian, dicomio.ImplicitVR)
dicom.WriteFileHeader(
e,
[]*dicom.Element{
dicom.MustNewElement(dicom.TagTransferSyntaxUID, dicomuid.ImplicitVRLittleEndian),
dicom.MustNewElement(dicom.TagMediaStorageSOPClassUID, "1.2.840.10008.5.1.4.1.1.1.2"),
dicom.MustNewElement(dicom.TagMediaStorageSOPInstanceUID, "1.2.3.4.5.6.7"),
})
bytes := e.Bytes()
d := dicomio.NewBytesDecoder(bytes, binary.LittleEndian, dicomio.ImplicitVR)
elems := dicom.ParseFileHeader(d)
if err := d.Finish(); err != nil {
t.Fatal(err)
}
elem, err := dicom.FindElementByTag(elems, dicom.TagTransferSyntaxUID)
if err != nil {
t.Fatal(err)
}
if elem.MustGetString() != dicomuid.ImplicitVRLittleEndian {
t.Error(elem)
}
elem, err = dicom.FindElementByTag(elems, dicom.TagMediaStorageSOPClassUID)
if err != nil {
t.Fatal(err)
}
if elem.MustGetString() != "1.2.840.10008.5.1.4.1.1.1.2" {
t.Error(elem)
}
elem, err = dicom.FindElementByTag(elems, dicom.TagMediaStorageSOPInstanceUID)
if err != nil {
t.Fatal(err)
}
if elem.MustGetString() != "1.2.3.4.5.6.7" {
t.Error(elem)
}
}
func TestNewElement(t *testing.T) {
elem, err := dicom.NewElement(dicom.TagTriggerSamplePosition, uint32(10), uint32(11))
if err != nil {
t.Fatal(err)
}
if elem.Tag != dicom.TagTriggerSamplePosition || !reflect.DeepEqual(elem.MustGetUint32s(), []uint32{10, 11}) {
t.Error(elem)
}
// Pass a wrong value type.
elem, err = dicom.NewElement(dicom.TagTriggerSamplePosition, "foo")
if err == nil {
t.Fatal("Should fail")
}
}