package predictor

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

func TestRFC(t *testing.T) {
	input := []byte{0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x0a,
		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x0a,
		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x0a,
		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x0a,
		0x41, 0x42, 0x41, 0x42, 0x41, 0x42, 0x41, 0x0a,
		0x42, 0x41, 0x42, 0x41, 0x42, 0x41, 0x42, 0x0a,
		0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x0a}

	output := []byte{0x60, 0x41, 0x41, 0x41, 0x41, 0x41, 0x0a, 0x60,
		0x41, 0x41, 0x41, 0x41, 0x41, 0x0a, 0x6f, 0x41,
		0x0a, 0x6f, 0x41, 0x0a, 0x41, 0x42, 0x41, 0x42,
		0x41, 0x42, 0x0a, 0x60, 0x42, 0x41, 0x42, 0x41,
		0x42, 0x0a, 0x60, 0x78, 0x78, 0x78, 0x78, 0x78, 0x0a}

	var buf bytes.Buffer
	var err error

	out := Compressor(&buf)
	err = out(input)
	if err != nil {
		t.Error(err)
	}

	err = out([]byte{})
	if err != nil {
		t.Error(err)
	}

	result := buf.Bytes()
	delta := diff.Diff(diff.D{len(result), len(output), func(i, j int) bool { return result[i] == output[i] }})

	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
		t.Error("Unexpected compressed output", delta)
	}
}
