File: prefix_test.go

package info (click to toggle)
golang-github-dsnet-compress 0.0.2~git20230904.39efe44%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,724 kB
  • sloc: sh: 108; makefile: 5
file content (74 lines) | stat: -rw-r--r-- 1,720 bytes parent folder | download
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
// Copyright 2015, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

package bzip2

import (
	"reflect"
	"testing"

	"github.com/dsnet/compress/internal/prefix"
)

func TestDegenerateCodes(t *testing.T) {
	vectors := []struct {
		input  prefix.PrefixCodes
		output prefix.PrefixCodes
	}{{
		input: []prefix.PrefixCode{
			{Sym: 0, Len: 1},
		},
		output: []prefix.PrefixCode{
			{Sym: 0, Len: 1, Val: 0},   // 0
			{Sym: 258, Len: 1, Val: 1}, // 1
		},
	}, {
		input: []prefix.PrefixCode{
			{Sym: 0, Len: 1},
			{Sym: 1, Len: 1},
			{Sym: 2, Len: 1},
		},
		output: []prefix.PrefixCode{
			{Sym: 0, Len: 1, Val: 0}, // 0
			{Sym: 1, Len: 1, Val: 1}, // 1
		},
	}, {
		input: []prefix.PrefixCode{
			{Sym: 0, Len: 3},
			{Sym: 1, Len: 4},
			{Sym: 2, Len: 3},
		},
		output: []prefix.PrefixCode{
			{Sym: 0, Len: 3, Val: 0},    //  000
			{Sym: 1, Len: 4, Val: 2},    // 0010
			{Sym: 2, Len: 3, Val: 4},    //  100
			{Sym: 258, Len: 4, Val: 10}, // 1010
			{Sym: 259, Len: 3, Val: 6},  //  110
			{Sym: 260, Len: 1, Val: 1},  //    1
		},
	}, {
		input: []prefix.PrefixCode{
			{Sym: 0, Len: 1},
			{Sym: 1, Len: 3},
			{Sym: 2, Len: 4},
			{Sym: 3, Len: 3},
			{Sym: 4, Len: 2},
		},
		output: []prefix.PrefixCode{
			{Sym: 0, Len: 1, Val: 0}, //   0
			{Sym: 1, Len: 3, Val: 3}, // 011
			{Sym: 3, Len: 3, Val: 7}, // 111
			{Sym: 4, Len: 2, Val: 1}, //  01
		},
	}}

	for i, v := range vectors {
		input := append(prefix.PrefixCodes(nil), v.input...)
		output := handleDegenerateCodes(input)

		if !reflect.DeepEqual(output, v.output) {
			t.Errorf("test %d, output mismatch:\ngot  %v\nwant %v", i, output, v.output)
		}
	}
}