1// Copyright 2014-2019 Ulrich Kunitz. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package lzma
6
7import (
8	"fmt"
9	"testing"
10)
11
12func TestHashTable(t *testing.T) {
13	ht, err := newHashTable(32, 2)
14	if err != nil {
15		t.Fatalf("newHashTable: error %s", err)
16	}
17	//    01234567890123456
18	s := "abcabcdefghijklmn"
19	n, err := ht.Write([]byte(s))
20	if err != nil {
21		t.Fatalf("ht.Write: error %s", err)
22	}
23	if n != len(s) {
24		t.Fatalf("ht.Write returned %d; want %d", n, len(s))
25	}
26	tests := []struct {
27		s string
28		w string
29	}{
30		{"ab", "[3 0]"},
31		{"bc", "[4 1]"},
32		{"ca", "[2]"},
33		{"xx", "[]"},
34		{"gh", "[9]"},
35		{"mn", "[15]"},
36	}
37	distances := make([]int64, 20)
38	for _, c := range tests {
39		distances := distances[:20]
40		k := ht.Matches([]byte(c.s), distances)
41		distances = distances[:k]
42		o := fmt.Sprintf("%v", distances)
43		if o != c.w {
44			t.Errorf("%s: offsets %s; want %s", c.s, o, c.w)
45		}
46	}
47}
48