externalsort: simplify reader's and writer's contracts; add huge test
This commit is contained in:
parent
259a0d4ed8
commit
0125456ca7
17 changed files with 20 additions and 10 deletions
|
@ -51,8 +51,9 @@ func TestLineReader(t *testing.T) {
|
|||
{
|
||||
name: "linebreak",
|
||||
in: `abc
|
||||
|
||||
`,
|
||||
expected: []string{"abc\n"},
|
||||
expected: []string{"abc", ""},
|
||||
},
|
||||
{
|
||||
name: "multiple-rows",
|
||||
|
@ -61,7 +62,7 @@ func TestLineReader(t *testing.T) {
|
|||
b
|
||||
b
|
||||
`,
|
||||
expected: []string{"a\n", "\n", "b\n", "b\n"},
|
||||
expected: []string{"a", "", "b", "b"},
|
||||
},
|
||||
{
|
||||
name: "large-row",
|
||||
|
@ -98,7 +99,6 @@ b
|
|||
lines, err := readAll(lineReader)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, strings.Join(lines, ""), tc.in)
|
||||
require.Len(t, lines, len(tc.expected),
|
||||
"expected: %+v, got: %+v", tc.expected, lines)
|
||||
require.Equal(t, tc.expected, lines)
|
||||
|
@ -108,16 +108,22 @@ b
|
|||
|
||||
type brokenReader int
|
||||
|
||||
func (b brokenReader) Read(data []byte) (n int, err error) {
|
||||
func (r brokenReader) Read(data []byte) (n int, err error) {
|
||||
return 0, errors.New("read is broken")
|
||||
}
|
||||
|
||||
type eofReader int
|
||||
|
||||
func (r eofReader) Read(p []byte) (n int, err error) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
func TestLineReader_error(t *testing.T) {
|
||||
_, err := NewReader(new(brokenReader)).ReadLine()
|
||||
require.Error(t, err)
|
||||
require.False(t, errors.Is(err, io.EOF))
|
||||
|
||||
r := newStringReader("")
|
||||
r := NewReader(new(eofReader))
|
||||
_, err = r.ReadLine()
|
||||
require.True(t, errors.Is(err, io.EOF))
|
||||
}
|
||||
|
@ -129,10 +135,11 @@ func TestLineWriter(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
name: "empty",
|
||||
lines: []string{""},
|
||||
},
|
||||
{
|
||||
name: "simple",
|
||||
lines: []string{"a\n", "b\n", "c\n"},
|
||||
lines: []string{"a", "b", "c"},
|
||||
},
|
||||
{
|
||||
name: "large-line",
|
||||
|
@ -153,7 +160,8 @@ func TestLineWriter(t *testing.T) {
|
|||
}
|
||||
|
||||
require.NoError(t, w.Flush())
|
||||
require.Equal(t, strings.Join(tc.lines, ""), buf.String())
|
||||
expected := strings.Join(tc.lines, "\n") + "\n"
|
||||
require.Equal(t, expected, buf.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,8 @@ func TestMerge(t *testing.T) {
|
|||
out: `0
|
||||
1
|
||||
1
|
||||
1`,
|
||||
1
|
||||
`,
|
||||
},
|
||||
{
|
||||
// Merge believes lines are read in sorted order.
|
||||
|
@ -36,7 +37,8 @@ func TestMerge(t *testing.T) {
|
|||
in: []string{`1
|
||||
0`},
|
||||
out: `1
|
||||
0`,
|
||||
0
|
||||
`,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
|
BIN
externalsort/testdata/sort/1/out.txt
vendored
BIN
externalsort/testdata/sort/1/out.txt
vendored
Binary file not shown.
BIN
externalsort/testdata/sort/3/out.txt
vendored
BIN
externalsort/testdata/sort/3/out.txt
vendored
Binary file not shown.
BIN
externalsort/testdata/sort/6/in2.txt
vendored
BIN
externalsort/testdata/sort/6/in2.txt
vendored
Binary file not shown.
BIN
externalsort/testdata/sort/6/out.txt
vendored
BIN
externalsort/testdata/sort/6/out.txt
vendored
Binary file not shown.
BIN
externalsort/testdata/sort/7/in1.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in1.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/in10.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in10.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/in2.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in2.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/in3.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in3.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/in4.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in4.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/in5.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in5.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/in6.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in6.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/in7.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in7.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/in8.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in8.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/in9.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/in9.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/7/out.txt
vendored
Normal file
BIN
externalsort/testdata/sort/7/out.txt
vendored
Normal file
Binary file not shown.
Loading…
Reference in a new issue