1// Copyright 2020 Google Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package classifier 16 17import ( 18 "fmt" 19 "testing" 20) 21 22func TestDictionary(t *testing.T) { 23 d := newDictionary() 24 if len(d.words) > 0 { 25 t.Errorf("new dictionary should not have words populated") 26 } 27 if len(d.indices) > 0 { 28 t.Errorf("new dictionary should not have indices populated") 29 } 30 31 // Add a word to the dictionary 32 d.add("hello") 33 // verify internal contents 34 if got := len(d.words); got != 1 { 35 t.Errorf("dictionary has %d words, expected 1", got) 36 } 37 if got := len(d.indices); got != 1 { 38 t.Errorf("dictionary has %d indices, expected 1", got) 39 } 40 if got := d.getIndex("hello"); got != 1 { 41 t.Errorf("dictionary index: got %d, want 1", got) 42 } 43 if got := d.getWord(1); got != "hello" { 44 t.Errorf("dictionary word: got %q, want %q", got, "hello") 45 } 46 47 // Adding the same word to the dictionary doesn't change the dictionary 48 d.add("hello") 49 // verify internal contents 50 if got := len(d.words); got != 1 { 51 t.Errorf("dictionary has %d words, expected 1", got) 52 } 53 if got := len(d.indices); got != 1 { 54 t.Errorf("dictionary has %d indices, expected 1", got) 55 } 56 if got := d.getIndex("hello"); got != 1 { 57 t.Errorf("dictionary index: got %d, want 1", got) 58 } 59 if got := d.getWord(1); got != "hello" { 60 t.Errorf("dictionary word: got %q, want %q", got, "hello") 61 } 62 63 // Fetching an unknown index returns the special value 64 if got := d.getWord(2); got != unknownWord { 65 t.Errorf("dictionary word: got %q, want %q", got, unknownWord) 66 } 67 68 // Fetching an unknown word returns the special value 69 if got := d.getIndex("unknown"); got != unknownIndex { 70 t.Errorf("dictionary word: got %d, want %d", got, unknownIndex) 71 } 72} 73 74func TestComputeQ(t *testing.T) { 75 tests := []struct { 76 threshold float64 77 expected int 78 }{ 79 { 80 threshold: .9, 81 expected: 9, 82 }, 83 { 84 threshold: .8, 85 expected: 4, 86 }, 87 { 88 threshold: .67, 89 expected: 2, 90 }, 91 { 92 threshold: .5, 93 expected: 1, 94 }, 95 { 96 threshold: 0.0, 97 expected: 1, 98 }, 99 { 100 threshold: 1.0, 101 expected: 10, 102 }, 103 } 104 105 for i, test := range tests { 106 t.Run(fmt.Sprintf("threshold test %d", i), func(t *testing.T) { 107 if actual := computeQ(test.threshold); actual != test.expected { 108 t.Errorf("got %v want %v", actual, test.expected) 109 } 110 }) 111 } 112} 113