aboutsummaryrefslogtreecommitdiff
path: root/v2/document_test.go
blob: e06d1494e5e99a7bb03a3349ee6b26b01d699a3b (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package classifier

import (
	"fmt"
	"testing"
)

func TestDictionary(t *testing.T) {
	d := newDictionary()
	if len(d.words) > 0 {
		t.Errorf("new dictionary should not have words populated")
	}
	if len(d.indices) > 0 {
		t.Errorf("new dictionary should not have indices populated")
	}

	// Add a word to the dictionary
	d.add("hello")
	// verify internal contents
	if got := len(d.words); got != 1 {
		t.Errorf("dictionary has %d words, expected 1", got)
	}
	if got := len(d.indices); got != 1 {
		t.Errorf("dictionary has %d indices, expected 1", got)
	}
	if got := d.getIndex("hello"); got != 1 {
		t.Errorf("dictionary index: got %d, want 1", got)
	}
	if got := d.getWord(1); got != "hello" {
		t.Errorf("dictionary word: got %q, want %q", got, "hello")
	}

	// Adding the same word to the dictionary doesn't change the dictionary
	d.add("hello")
	// verify internal contents
	if got := len(d.words); got != 1 {
		t.Errorf("dictionary has %d words, expected 1", got)
	}
	if got := len(d.indices); got != 1 {
		t.Errorf("dictionary has %d indices, expected 1", got)
	}
	if got := d.getIndex("hello"); got != 1 {
		t.Errorf("dictionary index: got %d, want 1", got)
	}
	if got := d.getWord(1); got != "hello" {
		t.Errorf("dictionary word: got %q, want %q", got, "hello")
	}

	// Fetching an unknown index returns the special value
	if got := d.getWord(2); got != unknownWord {
		t.Errorf("dictionary word: got %q, want %q", got, unknownWord)
	}

	// Fetching an unknown word returns the special value
	if got := d.getIndex("unknown"); got != unknownIndex {
		t.Errorf("dictionary word: got %d, want %d", got, unknownIndex)
	}
}

func TestComputeQ(t *testing.T) {
	tests := []struct {
		threshold float64
		expected  int
	}{
		{
			threshold: .9,
			expected:  9,
		},
		{
			threshold: .8,
			expected:  4,
		},
		{
			threshold: .67,
			expected:  2,
		},
		{
			threshold: .5,
			expected:  1,
		},
		{
			threshold: 0.0,
			expected:  1,
		},
		{
			threshold: 1.0,
			expected:  10,
		},
	}

	for i, test := range tests {
		t.Run(fmt.Sprintf("threshold test %d", i), func(t *testing.T) {
			if actual := computeQ(test.threshold); actual != test.expected {
				t.Errorf("got %v want %v", actual, test.expected)
			}
		})
	}
}