Check-in [2bcd5307ea]
Overview
Comment:Added package ioutil with io.Writer and io.Reader function wrappers
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2bcd5307ea2611963e6cc816d899979d0980b1dd
User & Date: spaskalev on 2014-12-23 07:52:13
Other Links: manifest | tags
Context
2014-12-23
08:15
Removed the decompressor alias type from predictor, use ioutil.ReaderFunc check-in: 2b049247ed user: spaskalev tags: trunk
07:52
Added package ioutil with io.Writer and io.Reader function wrappers check-in: 2bcd5307ea user: spaskalev tags: trunk
2014-12-22
19:52
Added documentation for the decompressor check-in: 89bfe97384 user: spaskalev tags: trunk
Changes

Added src/0dev.org/ioutil/ioutil.go version [404db9969d].

            1  +// Package ioutil contains various constructs for io operations
            2  +package ioutil
            3  +
            4  +// An function alias type that implements io.Writer
            5  +type WriterFunc func([]byte) (int, error)
            6  +
            7  +// Delegates the call to the WriterFunc while implementing io.Writer
            8  +func (w WriterFunc) Write(b []byte) (int, error) {
            9  +	return w(b)
           10  +}
           11  +
           12  +// An function alias type that implements io.Reader
           13  +type ReaderFunc func([]byte) (int, error)
           14  +
           15  +// Delegates the call to the WriterFunc while implementing io.Reader
           16  +func (r ReaderFunc) Read(b []byte) (int, error) {
           17  +	return r(b)
           18  +}

Added src/0dev.org/ioutil/ioutil_test.go version [e21bc31120].

            1  +package ioutil
            2  +
            3  +import (
            4  +	diff "0dev.org/diff"
            5  +	"bytes"
            6  +	"testing"
            7  +)
            8  +
            9  +func TestWriter(t *testing.T) {
           10  +	var (
           11  +		input  []byte = []byte{0, 1, 2, 3, 4, 5, 6, 7}
           12  +		output []byte
           13  +
           14  +		reader *bytes.Reader = bytes.NewReader(input)
           15  +		buffer bytes.Buffer
           16  +	)
           17  +
           18  +	reader.WriteTo(WriterFunc(buffer.Write))
           19  +	output = buffer.Bytes()
           20  +
           21  +	// Diff the result against the initial input
           22  +	delta := diff.Diff(diff.D{len(input), len(output),
           23  +		func(i, j int) bool { return input[i] == output[j] }})
           24  +	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
           25  +		t.Error("Differences detected ", delta)
           26  +	}
           27  +}
           28  +
           29  +func TestReader(t *testing.T) {
           30  +	var (
           31  +		input  []byte = []byte{0, 1, 2, 3, 4, 5, 6, 7}
           32  +		output []byte
           33  +
           34  +		reader *bytes.Reader = bytes.NewReader(input)
           35  +		buffer bytes.Buffer
           36  +	)
           37  +
           38  +	buffer.ReadFrom(ReaderFunc(reader.Read))
           39  +	output = buffer.Bytes()
           40  +
           41  +	// Diff the result against the initial input
           42  +	delta := diff.Diff(diff.D{len(input), len(output),
           43  +		func(i, j int) bool { return input[i] == output[j] }})
           44  +	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
           45  +		t.Error("Differences detected ", delta)
           46  +	}
           47  +}