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