externalsort: run sort tests on copies of testdata files as sort may overwrite them; fix sort logic; add tests.

This commit is contained in:
Arseny Balobanov 2020-03-13 03:38:48 +03:00
parent bd4ee323bd
commit 259a0d4ed8
16 changed files with 40 additions and 7 deletions

View file

@ -41,13 +41,19 @@ func TestLineReader(t *testing.T) {
{ {
name: "empty", name: "empty",
in: "", in: "",
expected: []string{""}, expected: nil,
}, },
{ {
name: "one-row", name: "one-row",
in: "abc", in: "abc",
expected: []string{"abc"}, expected: []string{"abc"},
}, },
{
name: "linebreak",
in: `abc
`,
expected: []string{"abc\n"},
},
{ {
name: "multiple-rows", name: "multiple-rows",
in: `a in: `a
@ -55,7 +61,7 @@ func TestLineReader(t *testing.T) {
b b
b b
`, `,
expected: []string{"a\n", "\n", "b\n", "b\n", ""}, expected: []string{"a\n", "\n", "b\n", "b\n"},
}, },
{ {
name: "large-row", name: "large-row",
@ -112,9 +118,6 @@ func TestLineReader_error(t *testing.T) {
require.False(t, errors.Is(err, io.EOF)) require.False(t, errors.Is(err, io.EOF))
r := newStringReader("") r := newStringReader("")
_, err = r.ReadLine()
require.NoError(t, err)
_, err = r.ReadLine() _, err = r.ReadLine()
require.True(t, errors.Is(err, io.EOF)) require.True(t, errors.Is(err, io.EOF))
} }

View file

@ -3,7 +3,9 @@ package externalsort
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"os"
"path" "path"
"strings" "strings"
"testing" "testing"
@ -85,12 +87,16 @@ func TestSort(t *testing.T) {
testCaseDir := path.Join(testDir, d) testCaseDir := path.Join(testDir, d)
t.Run(d, func(t *testing.T) { 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, out := readTestCase(testCaseDir)
in = copyFiles(t, in, tmpDir)
var buf bytes.Buffer var buf bytes.Buffer
w := bufio.NewWriter(&buf) w := bufio.NewWriter(&buf)
err := Sort(w, in...) require.NoError(t, Sort(w, in...))
require.NoError(t, err)
expected, err := ioutil.ReadFile(out) expected, err := ioutil.ReadFile(out)
require.NoError(t, err) require.NoError(t, err)
@ -116,3 +122,27 @@ func listDirs(t *testing.T, dir string) []string {
return dirs 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
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

0
externalsort/testdata/sort/4/in1.txt vendored Normal file
View file

0
externalsort/testdata/sort/4/in2.txt vendored Normal file
View file

0
externalsort/testdata/sort/4/out.txt vendored Normal file
View 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
View 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

Binary file not shown.

BIN
externalsort/testdata/sort/6/in1.txt vendored Normal file

Binary file not shown.

BIN
externalsort/testdata/sort/6/in2.txt vendored Normal file

Binary file not shown.

BIN
externalsort/testdata/sort/6/in3.txt vendored Normal file

Binary file not shown.

BIN
externalsort/testdata/sort/6/out.txt vendored Normal file

Binary file not shown.