-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer_test.go
More file actions
151 lines (138 loc) · 3.13 KB
/
Copy pathbuffer_test.go
File metadata and controls
151 lines (138 loc) · 3.13 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
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
package godicom
import (
"bytes"
"os"
"path/filepath"
"testing"
)
func TestPathFromPathlike(t *testing.T) {
got, err := pathFromPathlike("test.dcm")
if err != nil {
t.Fatal(err)
}
if got != "test.dcm" {
t.Fatalf("pathFromPathlike = %q, want test.dcm", got)
}
}
func TestPathFromPathlikeEmpty(t *testing.T) {
if _, err := pathFromPathlike(""); err == nil {
t.Fatal("pathFromPathlike empty error = nil, want error")
}
}
func TestCheckBufferCurrentBehavior(t *testing.T) {
if err := CheckBuffer(nil); err != nil {
t.Fatalf("CheckBuffer(nil) = %v, want nil", err)
}
}
func TestBufferLengthBytes(t *testing.T) {
got, err := BufferLength([]byte{1, 2, 3})
if err != nil {
t.Fatal(err)
}
if got != 3 {
t.Fatalf("BufferLength = %d, want 3", got)
}
}
func TestBufferLengthFileRestoresPosition(t *testing.T) {
path := filepath.Join(t.TempDir(), "buffer.bin")
if err := os.WriteFile(path, []byte{1, 2, 3, 4}, 0o644); err != nil {
t.Fatal(err)
}
file, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer file.Close()
if _, err := file.Seek(2, 0); err != nil {
t.Fatal(err)
}
got, err := BufferLength(file)
if err != nil {
t.Fatal(err)
}
if got != 4 {
t.Fatalf("BufferLength = %d, want 4", got)
}
pos, err := file.Seek(0, 1)
if err != nil {
t.Fatal(err)
}
if pos != 2 {
t.Fatalf("file position = %d, want 2", pos)
}
}
func TestBufferLengthUnsupported(t *testing.T) {
got, err := BufferLength("not a buffer")
if err != nil {
t.Fatal(err)
}
if got != 0 {
t.Fatalf("BufferLength unsupported = %d, want 0", got)
}
}
func TestBufferEquality(t *testing.T) {
tests := []struct {
name string
a interface{}
b interface{}
expected bool
}{
{
name: "equal bytes",
a: []byte{1, 2, 3},
b: []byte{1, 2, 3},
expected: true,
},
{
name: "different length",
a: []byte{1, 2},
b: []byte{1, 2, 3},
expected: false,
},
{
name: "different content",
a: []byte{1, 2, 3},
b: []byte{1, 2, 4},
expected: false,
},
{
name: "unsupported",
a: []byte{1},
b: "not bytes",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := BufferEquality(tt.a, tt.b); got != tt.expected {
t.Fatalf("BufferEquality = %t, want %t", got, tt.expected)
}
})
}
}
func TestUnackTag(t *testing.T) {
if got := unackTag([]byte{0x10, 0x00, 0x20, 0x00}, true); got != MustTag(0x00100020) {
t.Fatalf("unackTag little = %s, want (0010,0020)", got)
}
if got := unackTag([]byte{0x00, 0x10, 0x00, 0x20}, false); got != MustTag(0x00100020) {
t.Fatalf("unackTag big = %s, want (0010,0020)", got)
}
}
func TestReadUndefinedLengthValueImplicit(t *testing.T) {
data := []byte{
0x10, 0x00, 0x10, 0x00,
0x02, 0x00, 0x00, 0x00,
'A', 'B',
0xFE, 0xFF, 0xDD, 0xE0,
0x00, 0x00, 0x00, 0x00,
}
fp := newDicomReader(bytes.NewReader(data))
fp.SetByteOrder(true)
value, err := readUndefinedLengthValue(fp, SequenceDelimiterTag, true, true)
if err != nil {
t.Fatal(err)
}
if value == nil {
t.Fatal("readUndefinedLengthValue returned nil, want empty slice")
}
}