encoding_test.go 1.73 KB
Newer Older
obscuren's avatar
obscuren committed
1
package trie
obscuren's avatar
obscuren committed
2 3

import (
4
	"bytes"
obscuren's avatar
obscuren committed
5 6 7 8 9
	"fmt"
	"testing"
)

func TestCompactEncode(t *testing.T) {
10
	test1 := []byte{1, 2, 3, 4, 5}
obscuren's avatar
obscuren committed
11 12 13 14
	if res := CompactEncode(test1); res != "\x11\x23\x45" {
		t.Error(fmt.Sprintf("even compact encode failed. Got: %q", res))
	}

15
	test2 := []byte{0, 1, 2, 3, 4, 5}
obscuren's avatar
obscuren committed
16 17 18 19
	if res := CompactEncode(test2); res != "\x00\x01\x23\x45" {
		t.Error(fmt.Sprintf("odd compact encode failed. Got: %q", res))
	}

20
	test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
obscuren's avatar
obscuren committed
21 22 23 24
	if res := CompactEncode(test3); res != "\x20\x0f\x1c\xb8" {
		t.Error(fmt.Sprintf("odd terminated compact encode failed. Got: %q", res))
	}

25
	test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16}
obscuren's avatar
obscuren committed
26 27 28 29 30 31
	if res := CompactEncode(test4); res != "\x3f\x1c\xb8" {
		t.Error(fmt.Sprintf("even terminated compact encode failed. Got: %q", res))
	}
}

func TestCompactHexDecode(t *testing.T) {
32
	exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
obscuren's avatar
obscuren committed
33 34
	res := CompactHexDecode("verb")

35
	if !bytes.Equal(res, exp) {
obscuren's avatar
obscuren committed
36 37 38
		t.Error("Error compact hex decode. Expected", exp, "got", res)
	}
}
Joey Zhou's avatar
Joey Zhou committed
39 40

func TestCompactDecode(t *testing.T) {
41
	exp := []byte{1, 2, 3, 4, 5}
Joey Zhou's avatar
Joey Zhou committed
42 43
	res := CompactDecode("\x11\x23\x45")

44
	if !bytes.Equal(res, exp) {
Joey Zhou's avatar
Joey Zhou committed
45 46 47
		t.Error("odd compact decode. Expected", exp, "got", res)
	}

48
	exp = []byte{0, 1, 2, 3, 4, 5}
Joey Zhou's avatar
Joey Zhou committed
49 50
	res = CompactDecode("\x00\x01\x23\x45")

51
	if !bytes.Equal(res, exp) {
Joey Zhou's avatar
Joey Zhou committed
52 53 54
		t.Error("even compact decode. Expected", exp, "got", res)
	}

55
	exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
Joey Zhou's avatar
Joey Zhou committed
56 57
	res = CompactDecode("\x20\x0f\x1c\xb8")

58
	if !bytes.Equal(res, exp) {
Joey Zhou's avatar
Joey Zhou committed
59 60 61
		t.Error("even terminated compact decode. Expected", exp, "got", res)
	}

62
	exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
Joey Zhou's avatar
Joey Zhou committed
63 64
	res = CompactDecode("\x3f\x1c\xb8")

65
	if !bytes.Equal(res, exp) {
Joey Zhou's avatar
Joey Zhou committed
66 67
		t.Error("even terminated compact decode. Expected", exp, "got", res)
	}
zelig's avatar
zelig committed
68
}