From 259a0d4ed8f9a6eff7b06f39a286c30401c03e73 Mon Sep 17 00:00:00 2001 From: Arseny Balobanov Date: Fri, 13 Mar 2020 03:38:48 +0300 Subject: [PATCH] externalsort: run sort tests on copies of testdata files as sort may overwrite them; fix sort logic; add tests. --- externalsort/io_test.go | 13 ++++++---- externalsort/sort_test.go | 34 +++++++++++++++++++++++++-- externalsort/testdata/sort/2/in1.txt | Bin 4 -> 4 bytes externalsort/testdata/sort/2/out.txt | Bin 4 -> 4 bytes externalsort/testdata/sort/3/in3.txt | Bin 3 -> 3 bytes externalsort/testdata/sort/4/in1.txt | Bin externalsort/testdata/sort/4/in2.txt | Bin externalsort/testdata/sort/4/out.txt | Bin externalsort/testdata/sort/5/in1.txt | Bin 0 -> 4 bytes externalsort/testdata/sort/5/in2.txt | Bin externalsort/testdata/sort/5/in3.txt | Bin 0 -> 4 bytes externalsort/testdata/sort/5/out.txt | Bin 0 -> 8 bytes externalsort/testdata/sort/6/in1.txt | Bin 0 -> 2 bytes externalsort/testdata/sort/6/in2.txt | Bin 0 -> 5 bytes externalsort/testdata/sort/6/in3.txt | Bin 0 -> 5 bytes externalsort/testdata/sort/6/out.txt | Bin 0 -> 13 bytes 16 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 externalsort/testdata/sort/4/in1.txt create mode 100644 externalsort/testdata/sort/4/in2.txt create mode 100644 externalsort/testdata/sort/4/out.txt create mode 100644 externalsort/testdata/sort/5/in1.txt create mode 100644 externalsort/testdata/sort/5/in2.txt create mode 100644 externalsort/testdata/sort/5/in3.txt create mode 100644 externalsort/testdata/sort/5/out.txt create mode 100644 externalsort/testdata/sort/6/in1.txt create mode 100644 externalsort/testdata/sort/6/in2.txt create mode 100644 externalsort/testdata/sort/6/in3.txt create mode 100644 externalsort/testdata/sort/6/out.txt 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 31b838dc8b2523346900b78de3fee14cc01751e2..8baef1b4abc478178b004d62031cf7fe6db6f903 100644 GIT binary patch literal 4 LcmYdHO6CFp1AYN9 literal 4 Lcmd-|OiBg-0(Jp0 diff --git a/externalsort/testdata/sort/2/out.txt b/externalsort/testdata/sort/2/out.txt index 31b838dc8b2523346900b78de3fee14cc01751e2..8baef1b4abc478178b004d62031cf7fe6db6f903 100644 GIT binary patch literal 4 LcmYdHO6CFp1AYN9 literal 4 Lcmd-|OiBg-0(Jp0 diff --git a/externalsort/testdata/sort/3/in3.txt b/externalsort/testdata/sort/3/in3.txt index 6d9973c347758e2ff0be1601f7ff4566d022f7ec..2ceb12b8de7f62bffbd686ccddd5d4af9afc6169 100644 GIT binary patch literal 3 KcmXrwG5`Po-2iO> literal 3 KcmXruG6Dbq*#K<- diff --git a/externalsort/testdata/sort/4/in1.txt b/externalsort/testdata/sort/4/in1.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/externalsort/testdata/sort/4/in2.txt b/externalsort/testdata/sort/4/in2.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/externalsort/testdata/sort/4/out.txt b/externalsort/testdata/sort/4/out.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/externalsort/testdata/sort/5/in1.txt b/externalsort/testdata/sort/5/in1.txt new file mode 100644 index 0000000000000000000000000000000000000000..8baef1b4abc478178b004d62031cf7fe6db6f903 GIT binary patch literal 4 LcmYdHO6CFp1AYN9 literal 0 HcmV?d00001 diff --git a/externalsort/testdata/sort/5/in2.txt b/externalsort/testdata/sort/5/in2.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/externalsort/testdata/sort/5/in3.txt b/externalsort/testdata/sort/5/in3.txt new file mode 100644 index 0000000000000000000000000000000000000000..8baef1b4abc478178b004d62031cf7fe6db6f903 GIT binary patch literal 4 LcmYdHO6CFp1AYN9 literal 0 HcmV?d00001 diff --git a/externalsort/testdata/sort/5/out.txt b/externalsort/testdata/sort/5/out.txt new file mode 100644 index 0000000000000000000000000000000000000000..5d8a5566f0e8b2794b60559ecaa3cf8986e23d47 GIT binary patch literal 8 NcmYdHO6E!gVgL)g0$~6E literal 0 HcmV?d00001 diff --git a/externalsort/testdata/sort/6/in1.txt b/externalsort/testdata/sort/6/in1.txt new file mode 100644 index 0000000000000000000000000000000000000000..88ae695471db76683c59f75f306004843201e78c GIT binary patch literal 2 Jcmd<80001v0D}Mk literal 0 HcmV?d00001 diff --git a/externalsort/testdata/sort/6/in2.txt b/externalsort/testdata/sort/6/in2.txt new file mode 100644 index 0000000000000000000000000000000000000000..74c9df37f2c12ff4299cf03866de248575ee6053 GIT binary patch literal 5 Mcmd<8