externalsort: run sort tests on copies of testdata files as sort may overwrite them; fix sort logic; add tests.
This commit is contained in:
parent
bd4ee323bd
commit
259a0d4ed8
16 changed files with 40 additions and 7 deletions
|
@ -41,13 +41,19 @@ func TestLineReader(t *testing.T) {
|
|||
{
|
||||
name: "empty",
|
||||
in: "",
|
||||
expected: []string{""},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "one-row",
|
||||
in: "abc",
|
||||
expected: []string{"abc"},
|
||||
},
|
||||
{
|
||||
name: "linebreak",
|
||||
in: `abc
|
||||
`,
|
||||
expected: []string{"abc\n"},
|
||||
},
|
||||
{
|
||||
name: "multiple-rows",
|
||||
in: `a
|
||||
|
@ -55,7 +61,7 @@ func TestLineReader(t *testing.T) {
|
|||
b
|
||||
b
|
||||
`,
|
||||
expected: []string{"a\n", "\n", "b\n", "b\n", ""},
|
||||
expected: []string{"a\n", "\n", "b\n", "b\n"},
|
||||
},
|
||||
{
|
||||
name: "large-row",
|
||||
|
@ -112,9 +118,6 @@ func TestLineReader_error(t *testing.T) {
|
|||
require.False(t, errors.Is(err, io.EOF))
|
||||
|
||||
r := newStringReader("")
|
||||
_, err = r.ReadLine()
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = r.ReadLine()
|
||||
require.True(t, errors.Is(err, io.EOF))
|
||||
}
|
||||
|
|
|
@ -3,7 +3,9 @@ package externalsort
|
|||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -85,12 +87,16 @@ func TestSort(t *testing.T) {
|
|||
testCaseDir := path.Join(testDir, d)
|
||||
|
||||
t.Run(d, func(t *testing.T) {
|
||||
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("sort%s-", d))
|
||||
require.NoError(t, err)
|
||||
defer func() { _ = os.RemoveAll(tmpDir) }()
|
||||
|
||||
in, out := readTestCase(testCaseDir)
|
||||
in = copyFiles(t, in, tmpDir)
|
||||
|
||||
var buf bytes.Buffer
|
||||
w := bufio.NewWriter(&buf)
|
||||
err := Sort(w, in...)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, Sort(w, in...))
|
||||
|
||||
expected, err := ioutil.ReadFile(out)
|
||||
require.NoError(t, err)
|
||||
|
@ -116,3 +122,27 @@ func listDirs(t *testing.T, dir string) []string {
|
|||
|
||||
return dirs
|
||||
}
|
||||
|
||||
func copyFiles(t *testing.T, in []string, dir string) []string {
|
||||
t.Helper()
|
||||
|
||||
var ret []string
|
||||
for _, f := range in {
|
||||
ret = append(ret, copyFile(t, f, dir))
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func copyFile(t *testing.T, f, dir string) string {
|
||||
t.Helper()
|
||||
|
||||
data, err := ioutil.ReadFile(f)
|
||||
require.NoError(t, err)
|
||||
|
||||
dst := path.Join(dir, path.Base(f))
|
||||
err = ioutil.WriteFile(dst, data, 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
return dst
|
||||
}
|
||||
|
|
BIN
externalsort/testdata/sort/2/in1.txt
vendored
BIN
externalsort/testdata/sort/2/in1.txt
vendored
Binary file not shown.
BIN
externalsort/testdata/sort/2/out.txt
vendored
BIN
externalsort/testdata/sort/2/out.txt
vendored
Binary file not shown.
BIN
externalsort/testdata/sort/3/in3.txt
vendored
BIN
externalsort/testdata/sort/3/in3.txt
vendored
Binary file not shown.
0
externalsort/testdata/sort/4/in1.txt
vendored
Normal file
0
externalsort/testdata/sort/4/in1.txt
vendored
Normal file
0
externalsort/testdata/sort/4/in2.txt
vendored
Normal file
0
externalsort/testdata/sort/4/in2.txt
vendored
Normal file
0
externalsort/testdata/sort/4/out.txt
vendored
Normal file
0
externalsort/testdata/sort/4/out.txt
vendored
Normal file
BIN
externalsort/testdata/sort/5/in1.txt
vendored
Normal file
BIN
externalsort/testdata/sort/5/in1.txt
vendored
Normal file
Binary file not shown.
0
externalsort/testdata/sort/5/in2.txt
vendored
Normal file
0
externalsort/testdata/sort/5/in2.txt
vendored
Normal file
BIN
externalsort/testdata/sort/5/in3.txt
vendored
Normal file
BIN
externalsort/testdata/sort/5/in3.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/5/out.txt
vendored
Normal file
BIN
externalsort/testdata/sort/5/out.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/6/in1.txt
vendored
Normal file
BIN
externalsort/testdata/sort/6/in1.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/6/in2.txt
vendored
Normal file
BIN
externalsort/testdata/sort/6/in2.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/6/in3.txt
vendored
Normal file
BIN
externalsort/testdata/sort/6/in3.txt
vendored
Normal file
Binary file not shown.
BIN
externalsort/testdata/sort/6/out.txt
vendored
Normal file
BIN
externalsort/testdata/sort/6/out.txt
vendored
Normal file
Binary file not shown.
Loading…
Reference in a new issue