diff --git a/externalsort/io_test.go b/externalsort/io_test.go index 7bcb91d..ceb6ed7 100644 --- a/externalsort/io_test.go +++ b/externalsort/io_test.go @@ -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)) } diff --git a/externalsort/sort_test.go b/externalsort/sort_test.go index 6fac5b1..63535ad 100644 --- a/externalsort/sort_test.go +++ b/externalsort/sort_test.go @@ -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 +} diff --git a/externalsort/testdata/sort/2/in1.txt b/externalsort/testdata/sort/2/in1.txt index 31b838d..8baef1b 100644 Binary files a/externalsort/testdata/sort/2/in1.txt and b/externalsort/testdata/sort/2/in1.txt differ diff --git a/externalsort/testdata/sort/2/out.txt b/externalsort/testdata/sort/2/out.txt index 31b838d..8baef1b 100644 Binary files a/externalsort/testdata/sort/2/out.txt and b/externalsort/testdata/sort/2/out.txt differ diff --git a/externalsort/testdata/sort/3/in3.txt b/externalsort/testdata/sort/3/in3.txt index 6d9973c..2ceb12b 100644 Binary files a/externalsort/testdata/sort/3/in3.txt and b/externalsort/testdata/sort/3/in3.txt differ diff --git a/externalsort/testdata/sort/4/in1.txt b/externalsort/testdata/sort/4/in1.txt new file mode 100644 index 0000000..e69de29 diff --git a/externalsort/testdata/sort/4/in2.txt b/externalsort/testdata/sort/4/in2.txt new file mode 100644 index 0000000..e69de29 diff --git a/externalsort/testdata/sort/4/out.txt b/externalsort/testdata/sort/4/out.txt new file mode 100644 index 0000000..e69de29 diff --git a/externalsort/testdata/sort/5/in1.txt b/externalsort/testdata/sort/5/in1.txt new file mode 100644 index 0000000..8baef1b Binary files /dev/null and b/externalsort/testdata/sort/5/in1.txt differ diff --git a/externalsort/testdata/sort/5/in2.txt b/externalsort/testdata/sort/5/in2.txt new file mode 100644 index 0000000..e69de29 diff --git a/externalsort/testdata/sort/5/in3.txt b/externalsort/testdata/sort/5/in3.txt new file mode 100644 index 0000000..8baef1b Binary files /dev/null and b/externalsort/testdata/sort/5/in3.txt differ diff --git a/externalsort/testdata/sort/5/out.txt b/externalsort/testdata/sort/5/out.txt new file mode 100644 index 0000000..5d8a556 Binary files /dev/null and b/externalsort/testdata/sort/5/out.txt differ diff --git a/externalsort/testdata/sort/6/in1.txt b/externalsort/testdata/sort/6/in1.txt new file mode 100644 index 0000000..88ae695 Binary files /dev/null and b/externalsort/testdata/sort/6/in1.txt differ diff --git a/externalsort/testdata/sort/6/in2.txt b/externalsort/testdata/sort/6/in2.txt new file mode 100644 index 0000000..74c9df3 Binary files /dev/null and b/externalsort/testdata/sort/6/in2.txt differ diff --git a/externalsort/testdata/sort/6/in3.txt b/externalsort/testdata/sort/6/in3.txt new file mode 100644 index 0000000..34aaebe Binary files /dev/null and b/externalsort/testdata/sort/6/in3.txt differ diff --git a/externalsort/testdata/sort/6/out.txt b/externalsort/testdata/sort/6/out.txt new file mode 100644 index 0000000..b2cf317 Binary files /dev/null and b/externalsort/testdata/sort/6/out.txt differ