readwriter_test.go 1.2 KB

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