This repository was archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfilesplitter_test.go
224 lines (210 loc) · 5.02 KB
/
filesplitter_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
// Copyright 2017 Pilosa Corp.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
package pdk
import (
"io"
"io/ioutil"
"os"
"reflect"
"testing"
)
func TestFileFragment(t *testing.T) {
tests := []struct {
data string
start int64
end int64
exp string
}{
{
data: "abcdefghijklmnopqrstuvwxyz",
start: 0,
end: 3,
exp: "abc",
},
{
data: "abcdefghijklmnopqrstuvwxyz",
start: 1,
end: 3,
exp: "bc",
},
{
data: "abcdefghijklmnopqrstuvwxyz",
start: 22,
end: 26,
exp: "wxyz",
},
{
data: "abcdefghijklmnopqrstuvwxyz",
start: 0,
end: 26,
exp: "abcdefghijklmnopqrstuvwxyz",
},
}
for i, test := range tests {
f := mustWriteAndOpenFile(t, []byte(test.data))
ff, err := NewFileFragment(f, test.start, test.end)
if err != nil {
t.Fatal(err)
}
actual, err := ioutil.ReadAll(ff)
if err != nil {
t.Fatal(err)
}
if string(actual) != test.exp {
t.Fatalf("test %d: expected '%s', but got '%s'", i, test.exp, actual)
}
}
}
func TestSearchReader(t *testing.T) {
tests := []struct {
data string
start int64
expIdx int64
expErr error
}{
{
data: "abcd\nefgh",
start: 0,
expIdx: 5,
expErr: nil,
},
{
data: "abcd\nefgh",
start: 5,
expIdx: 4,
expErr: io.EOF,
},
{
data: string3000(),
start: 1,
expIdx: 2500,
expErr: nil,
},
}
for i, test := range tests {
f := mustWriteAndOpenFile(t, []byte(test.data))
_, err := f.Seek(test.start, io.SeekStart)
if err != nil {
t.Fatal(err)
}
actIdx, actErr := searchReader(f, '\n')
if actIdx != test.expIdx || actErr != test.expErr {
t.Fatalf("test %d: expected idx: %d, and err: %v, but got idx: %d and err: %v", i, test.expIdx, test.expErr, actIdx, actErr)
}
}
}
func TestSeekAndSearch(t *testing.T) {
tests := []struct {
data string
splitSize int64
expOff int64
expErr error
}{
{
data: "abcd\nefgh",
splitSize: 5,
expOff: 9,
expErr: io.EOF,
},
}
for i, test := range tests {
f := mustWriteAndOpenFile(t, []byte(test.data))
actOff, actErr := seekAndSearch(f, test.splitSize, '\n')
if actOff != test.expOff || actErr != test.expErr {
t.Fatalf("test %d: expected idx: %d, and err: %v, but got idx: %d and err: %v", i, test.expOff, test.expErr, actOff, actErr)
}
}
}
func TestSplitFileLines(t *testing.T) {
tests := []struct {
data string
numParts int64
exp []string
}{
{
data: "abcdef\ngh",
numParts: 2,
exp: []string{"abcdef\n", "gh"},
},
{
data: "aaaa\nbbbb\ncccc\ndd\n",
numParts: 4,
exp: []string{"aaaa\n", "bbbb\n", "cccc\n", "dd\n"},
},
}
for i, test := range tests {
f := mustWriteAndOpenFile(t, []byte(test.data))
frags, err := SplitFileLines(f, test.numParts)
if err != nil {
t.Fatal(err)
}
var actual []string
for _, frag := range frags {
bytes, err := ioutil.ReadAll(frag)
if err != nil {
t.Fatal(err)
}
actual = append(actual, string(bytes))
}
if !reflect.DeepEqual(actual, test.exp) {
t.Fatalf("test %d: expected: %#v, but got %#v", i, test.exp, actual)
}
}
}
func string3000() string {
var ret string
for i := 0; i < 300; i++ {
ret = ret + "1234567890"
if i == 249 {
ret += "\n"
}
}
return ret
}
func mustWriteAndOpenFile(t *testing.T, data []byte) *os.File {
tf, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
n, err := tf.Write(data)
if n < len(data) {
t.Fatal("Didn't write the whole data")
}
if err != nil {
t.Fatal(err)
}
_, err = tf.Seek(0, io.SeekStart)
if err != nil {
t.Fatal(err)
}
return tf
}