mtf_test.go at [93fcb281a1]

File src/0dev.org/mtf/mtf_test.go artifact efb3f6ccdb part of check-in 93fcb281a1


package mtf

import (
	diff "0dev.org/diff"
	"bytes"
	"io"
	"testing"
)

func TestEncoder(t *testing.T) {
	var (
		input    []byte = []byte{1, 1, 0, 0}
		expected []byte = []byte{1, 0, 1, 0}

		buffer  bytes.Buffer
		encoder io.Writer = Encoder(&buffer)
	)

	count, err := encoder.Write(input)
	if count != len(input) {
		t.Error("Unexpected write count from encoder", count)
	}
	if err != nil {
		t.Error("Unexpected write error from encoder", err)
	}

	output := buffer.Bytes()

	// Diff the output against the expected result
	delta := diff.Diff(diff.D{Len1: len(expected), Len2: len(output),
		EqualFunc: func(i, j int) bool { return expected[i] == output[j] }})
	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
		t.Error("Differences detected ", delta)
	}
}

func TestDecoder(t *testing.T) {
	var (
		input    []byte = []byte{1, 0, 1, 0}
		expected []byte = []byte{1, 1, 0, 0}
		output   []byte = make([]byte, 4)

		reader  *bytes.Reader = bytes.NewReader(input)
		decoder io.Reader     = Decoder(reader)
	)

	count, err := decoder.Read(output)
	if count != len(output) {
		t.Error("Unexpected read count from decoder", count)
	}
	if err != nil {
		t.Error("Unexpected read error from decoder", err)
	}

	// Diff the output against the expected result
	delta := diff.Diff(diff.D{Len1: len(expected), Len2: len(output),
		EqualFunc: func(i, j int) bool { return expected[i] == output[j] }})
	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
		t.Error("Differences detected ", delta)
	}
}