David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 1 | // Copyright 2011 The Go Authors. All rights reserved. |
| 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 norm |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | var bufSizes = []int{1, 2, 3, 4, 5, 6, 7, 8, 100, 101, 102, 103, 4000, 4001, 4002, 4003} |
| 14 | |
| 15 | func readFunc(size int) appendFunc { |
| 16 | return func(f Form, out []byte, s string) []byte { |
| 17 | out = append(out, s...) |
| 18 | r := f.Reader(bytes.NewBuffer(out)) |
| 19 | buf := make([]byte, size) |
| 20 | result := []byte{} |
| 21 | for n, err := 0, error(nil); err == nil; { |
| 22 | n, err = r.Read(buf) |
| 23 | result = append(result, buf[:n]...) |
| 24 | } |
| 25 | return result |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestReader(t *testing.T) { |
| 30 | for _, s := range bufSizes { |
| 31 | name := fmt.Sprintf("TestReader%d", s) |
| 32 | runNormTests(t, name, readFunc(s)) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func writeFunc(size int) appendFunc { |
| 37 | return func(f Form, out []byte, s string) []byte { |
| 38 | in := append(out, s...) |
| 39 | result := new(bytes.Buffer) |
| 40 | w := f.Writer(result) |
| 41 | buf := make([]byte, size) |
| 42 | for n := 0; len(in) > 0; in = in[n:] { |
| 43 | n = copy(buf, in) |
| 44 | _, _ = w.Write(buf[:n]) |
| 45 | } |
| 46 | w.Close() |
| 47 | return result.Bytes() |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestWriter(t *testing.T) { |
| 52 | for _, s := range bufSizes { |
| 53 | name := fmt.Sprintf("TestWriter%d", s) |
| 54 | runNormTests(t, name, writeFunc(s)) |
| 55 | } |
| 56 | } |