Rob Pike | 3f5966d | 2010-08-03 08:04:33 +1000 | [diff] [blame] | 1 | // Copyright 2010 The Go Authors. All rights reserved. |
Brad Fitzpatrick | 719cde2 | 2010-07-28 11:30:00 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package io_test |
| 6 | |
| 7 | import ( |
Rob Pike | 3f5966d | 2010-08-03 08:04:33 +1000 | [diff] [blame] | 8 | "bytes" |
| 9 | "crypto/sha1" |
| 10 | "fmt" |
Russ Cox | 965845a | 2011-11-02 15:54:16 -0400 | [diff] [blame] | 11 | . "io" |
Russ Cox | 211618c | 2014-05-12 23:38:35 -0400 | [diff] [blame] | 12 | "io/ioutil" |
Brad Fitzpatrick | 719cde2 | 2010-07-28 11:30:00 -0700 | [diff] [blame] | 13 | "strings" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | func TestMultiReader(t *testing.T) { |
| 18 | var mr Reader |
| 19 | var buf []byte |
| 20 | nread := 0 |
| 21 | withFooBar := func(tests func()) { |
| 22 | r1 := strings.NewReader("foo ") |
Brad Fitzpatrick | ca83cd2 | 2011-05-11 12:11:32 -0700 | [diff] [blame] | 23 | r2 := strings.NewReader("") |
| 24 | r3 := strings.NewReader("bar") |
| 25 | mr = MultiReader(r1, r2, r3) |
Brad Fitzpatrick | 719cde2 | 2010-07-28 11:30:00 -0700 | [diff] [blame] | 26 | buf = make([]byte, 20) |
| 27 | tests() |
| 28 | } |
Russ Cox | c06cf03 | 2011-11-01 21:48:52 -0400 | [diff] [blame] | 29 | expectRead := func(size int, expected string, eerr error) { |
Brad Fitzpatrick | 719cde2 | 2010-07-28 11:30:00 -0700 | [diff] [blame] | 30 | nread++ |
| 31 | n, gerr := mr.Read(buf[0:size]) |
| 32 | if n != len(expected) { |
| 33 | t.Errorf("#%d, expected %d bytes; got %d", |
| 34 | nread, len(expected), n) |
| 35 | } |
| 36 | got := string(buf[0:n]) |
| 37 | if got != expected { |
| 38 | t.Errorf("#%d, expected %q; got %q", |
| 39 | nread, expected, got) |
| 40 | } |
| 41 | if gerr != eerr { |
| 42 | t.Errorf("#%d, expected error %v; got %v", |
| 43 | nread, eerr, gerr) |
| 44 | } |
| 45 | buf = buf[n:] |
| 46 | } |
| 47 | withFooBar(func() { |
| 48 | expectRead(2, "fo", nil) |
| 49 | expectRead(5, "o ", nil) |
| 50 | expectRead(5, "bar", nil) |
Russ Cox | c06cf03 | 2011-11-01 21:48:52 -0400 | [diff] [blame] | 51 | expectRead(5, "", EOF) |
Brad Fitzpatrick | 719cde2 | 2010-07-28 11:30:00 -0700 | [diff] [blame] | 52 | }) |
| 53 | withFooBar(func() { |
| 54 | expectRead(4, "foo ", nil) |
| 55 | expectRead(1, "b", nil) |
| 56 | expectRead(3, "ar", nil) |
Russ Cox | c06cf03 | 2011-11-01 21:48:52 -0400 | [diff] [blame] | 57 | expectRead(1, "", EOF) |
Brad Fitzpatrick | 719cde2 | 2010-07-28 11:30:00 -0700 | [diff] [blame] | 58 | }) |
| 59 | withFooBar(func() { |
| 60 | expectRead(5, "foo ", nil) |
| 61 | }) |
| 62 | } |
Rob Pike | 3f5966d | 2010-08-03 08:04:33 +1000 | [diff] [blame] | 63 | |
| 64 | func TestMultiWriter(t *testing.T) { |
| 65 | sha1 := sha1.New() |
| 66 | sink := new(bytes.Buffer) |
| 67 | mw := MultiWriter(sha1, sink) |
| 68 | |
| 69 | sourceString := "My input text." |
| 70 | source := strings.NewReader(sourceString) |
| 71 | written, err := Copy(mw, source) |
| 72 | |
| 73 | if written != int64(len(sourceString)) { |
| 74 | t.Errorf("short write of %d, not %d", written, len(sourceString)) |
| 75 | } |
| 76 | |
| 77 | if err != nil { |
| 78 | t.Errorf("unexpected error: %v", err) |
| 79 | } |
| 80 | |
Adam Langley | bac7bc5 | 2011-12-01 12:35:37 -0500 | [diff] [blame] | 81 | sha1hex := fmt.Sprintf("%x", sha1.Sum(nil)) |
Rob Pike | 3f5966d | 2010-08-03 08:04:33 +1000 | [diff] [blame] | 82 | if sha1hex != "01cb303fa8c30a64123067c5aa6284ba7ec2d31b" { |
| 83 | t.Error("incorrect sha1 value") |
| 84 | } |
| 85 | |
| 86 | if sink.String() != sourceString { |
Rob Pike | 1959c3a | 2010-09-23 13:48:56 +1000 | [diff] [blame] | 87 | t.Errorf("expected %q; got %q", sourceString, sink.String()) |
Rob Pike | 3f5966d | 2010-08-03 08:04:33 +1000 | [diff] [blame] | 88 | } |
| 89 | } |
Russ Cox | 211618c | 2014-05-12 23:38:35 -0400 | [diff] [blame] | 90 | |
| 91 | // Test that MultiReader copies the input slice and is insulated from future modification. |
| 92 | func TestMultiReaderCopy(t *testing.T) { |
| 93 | slice := []Reader{strings.NewReader("hello world")} |
| 94 | r := MultiReader(slice...) |
| 95 | slice[0] = nil |
| 96 | data, err := ioutil.ReadAll(r) |
| 97 | if err != nil || string(data) != "hello world" { |
| 98 | t.Errorf("ReadAll() = %q, %v, want %q, nil", data, err, "hello world") |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Test that MultiWriter copies the input slice and is insulated from future modification. |
| 103 | func TestMultiWriterCopy(t *testing.T) { |
| 104 | var buf bytes.Buffer |
| 105 | slice := []Writer{&buf} |
| 106 | w := MultiWriter(slice...) |
| 107 | slice[0] = nil |
| 108 | n, err := w.Write([]byte("hello world")) |
| 109 | if err != nil || n != 11 { |
| 110 | t.Errorf("Write(`hello world`) = %d, %v, want 11, nil", n, err) |
| 111 | } |
| 112 | if buf.String() != "hello world" { |
| 113 | t.Errorf("buf.String() = %q, want %q", buf.String(), "hello world") |
| 114 | } |
| 115 | } |