Diff

Differences From Artifact [05441cad24]:

To Artifact [0cbbd433d4]:


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
40
41
42
43
44
45
46
47
48
49
50











51
52
53

54
55































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
40
41
42
43
44
45
46
47
48






49
50
51
52
53
54
55
56
57
58
59
60
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95









-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+

+
-
-
-
-
-
+
+
+
+
+

+
+
-
-
+
+
+







-
+










-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+


-
+


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
package predictor

import (
	diff "0dev.org/diff"
	"bytes"
	"io/ioutil"
	"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}
// Sample input from RFC1978 - PPP Predictor Compression Protocol
var 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}

// Sample output from RFC1978 - PPP Predictor Compression Protocol
	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 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}

func TestCompressor(t *testing.T) {
	var (
	var buf bytes.Buffer
	var err error
		buf bytes.Buffer
		err error
	)

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

	err = out([]byte{})
	err = out(nil)
	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[j] }})

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

	data := bytes.NewBuffer(result)
	in := Decompressor(data)

	result, err = ioutil.ReadAll(in)
	delta = diff.Diff(diff.D{len(result), len(input), func(i, j int) bool { return result[i] == input[j] }})
}

func TestDecompressor(t *testing.T) {
	in := Decompressor(bytes.NewReader(output))
	result, err := ioutil.ReadAll(in)
	if err != nil {
		t.Error("Unexpected error while decompressing", err)
	}

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

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

func TestPartial(t *testing.T) {
	var (
		input []byte = []byte{0, 1, 2, 3, 4, 5, 6}
		buf   bytes.Buffer
		err   error
	)

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

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

	compressed := buf.Bytes()
	decompressed, err := ioutil.ReadAll(Decompressor(bytes.NewReader(compressed)))

	delta := diff.Diff(diff.D{len(input), len(decompressed),
		func(i, j int) bool { return input[i] == decompressed[j] }})

	if len(delta.Added) > 0 || len(delta.Removed) > 0 {
		t.Error("Unexpected decompressed output", delta)
		t.Errorf("%#x", input)
		t.Errorf("%#x", decompressed)
	}
}