diff --git a/externalsort/README.md b/externalsort/README.md index 4e93e9d..51de944 100644 --- a/externalsort/README.md +++ b/externalsort/README.md @@ -14,15 +14,14 @@ type LineReader interface { ReadLine() (string, error) } -type LineWriterFlusher interface { +type LineWriter interface { Write(l string) error - Flush() error } ``` и два конструктора: ``` func NewReader(r io.Reader) LineReader -func NewWriterFlusher(w io.Writer) LineWriterFlusher +func NewWriter(w io.Writer) LineWriter ``` `NewLineReader` оборачивает переданный `io.Reader` в `LineReader`. @@ -38,7 +37,7 @@ func NewWriterFlusher(w io.Writer) LineWriterFlusher Функция слияния произвольного количества отсортированных групп строк: ``` -func Merge(w LineWriterFlusher, readers ...LineReader) error +func Merge(w LineWriter, readers ...LineReader) error ``` `Merge` по необходимости читает из reader'ов и пишет во writer. diff --git a/externalsort/io.go b/externalsort/io.go index 9867cb8..13f68ce 100644 --- a/externalsort/io.go +++ b/externalsort/io.go @@ -6,7 +6,6 @@ type LineReader interface { ReadLine() (string, error) } -type LineWriterFlusher interface { +type LineWriter interface { Write(l string) error - Flush() error } diff --git a/externalsort/io_test.go b/externalsort/io_test.go index 7ff8c19..7bcb91d 100644 --- a/externalsort/io_test.go +++ b/externalsort/io_test.go @@ -1,6 +1,7 @@ package externalsort import ( + "bufio" "bytes" "errors" "io" @@ -118,7 +119,7 @@ func TestLineReader_error(t *testing.T) { require.True(t, errors.Is(err, io.EOF)) } -func TestLineWriterFlusher(t *testing.T) { +func TestLineWriter(t *testing.T) { for _, tc := range []struct { name string lines []string @@ -130,17 +131,25 @@ func TestLineWriterFlusher(t *testing.T) { name: "simple", lines: []string{"a\n", "b\n", "c\n"}, }, + { + name: "large-line", + lines: []string{strings.Repeat("xx", 2049), "x", "y"}, + }, + { + name: "huge-line", + lines: []string{strings.Repeat("?", 65537), "?", "!"}, + }, } { t.Run(tc.name, func(t *testing.T) { var buf bytes.Buffer - w := NewWriterFlusher(&buf) + w := bufio.NewWriter(&buf) + lw := NewWriter(w) for _, l := range tc.lines { - require.NoError(t, w.Write(l)) + require.NoError(t, lw.Write(l)) } require.NoError(t, w.Flush()) - require.Equal(t, strings.Join(tc.lines, ""), buf.String()) }) } diff --git a/externalsort/sort.go b/externalsort/sort.go index 5760d49..170d925 100644 --- a/externalsort/sort.go +++ b/externalsort/sort.go @@ -10,11 +10,11 @@ func NewReader(r io.Reader) LineReader { panic("implement me") } -func NewWriterFlusher(w io.Writer) LineWriterFlusher { +func NewWriter(w io.Writer) LineWriter { panic("implement me") } -func Merge(w LineWriterFlusher, readers ...LineReader) error { +func Merge(w LineWriter, readers ...LineReader) error { panic("implement me") } diff --git a/externalsort/sort_test.go b/externalsort/sort_test.go index 054dce1..6fac5b1 100644 --- a/externalsort/sort_test.go +++ b/externalsort/sort_test.go @@ -1,6 +1,7 @@ package externalsort import ( + "bufio" "bytes" "io/ioutil" "path" @@ -38,16 +39,18 @@ func TestMerge(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { out := &bytes.Buffer{} - w := NewWriterFlusher(out) + w := bufio.NewWriter(out) + lw := NewWriter(w) var readers []LineReader for _, s := range tc.in { readers = append(readers, newStringReader(s)) } - err := Merge(w, readers...) + err := Merge(lw, readers...) require.NoError(t, err) + require.NoError(t, w.Flush()) require.Equal(t, tc.out, out.String()) }) } @@ -85,12 +88,14 @@ func TestSort(t *testing.T) { in, out := readTestCase(testCaseDir) var buf bytes.Buffer - err := Sort(&buf, in...) + w := bufio.NewWriter(&buf) + err := Sort(w, in...) require.NoError(t, err) expected, err := ioutil.ReadFile(out) require.NoError(t, err) + require.NoError(t, w.Flush()) require.Equal(t, string(expected), buf.String()) }) }