summaryrefslogtreecommitdiffhomepage
path: root/types/iox/io_test.go
blob: 9fba39605d28dfacdaa157136919c81462fb585e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

package iox

import (
	"bytes"
	"io"
	"testing"
	"testing/iotest"

	"tailscale.com/util/must"
)

func TestCopy(t *testing.T) {
	const testdata = "the quick brown fox jumped over the lazy dog"
	src := testdata
	bb := new(bytes.Buffer)
	if got := must.Get(io.Copy(bb, ReaderFunc(func(b []byte) (n int, err error) {
		n = copy(b[:min(len(b), 7)], src)
		src = src[n:]
		if len(src) == 0 {
			err = io.EOF
		}
		return n, err
	}))); int(got) != len(testdata) {
		t.Errorf("copy = %d, want %d", got, len(testdata))
	}
	var dst []byte
	if got := must.Get(io.Copy(WriterFunc(func(b []byte) (n int, err error) {
		dst = append(dst, b...)
		return len(b), nil
	}), iotest.OneByteReader(bb))); int(got) != len(testdata) {
		t.Errorf("copy = %d, want %d", got, len(testdata))
	}
	if string(dst) != testdata {
		t.Errorf("copy = %q, want %q", dst, testdata)
	}
}