1// Copyright (c) 2014 Couchbase, 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 analysis 16 17import ( 18 "reflect" 19 "testing" 20) 21 22func TestTokenFrequency(t *testing.T) { 23 tokens := TokenStream{ 24 &Token{ 25 Term: []byte("water"), 26 Position: 1, 27 Start: 0, 28 End: 5, 29 }, 30 &Token{ 31 Term: []byte("water"), 32 Position: 2, 33 Start: 6, 34 End: 11, 35 }, 36 } 37 expectedResult := TokenFrequencies{ 38 "water": &TokenFreq{ 39 Term: []byte("water"), 40 Locations: []*TokenLocation{ 41 { 42 Position: 1, 43 Start: 0, 44 End: 5, 45 }, 46 { 47 Position: 2, 48 Start: 6, 49 End: 11, 50 }, 51 }, 52 frequency: 2, 53 }, 54 } 55 result := TokenFrequency(tokens, nil, true) 56 if !reflect.DeepEqual(result, expectedResult) { 57 t.Errorf("expected %#v, got %#v", expectedResult, result) 58 } 59} 60 61func TestTokenFrequenciesMergeAll(t *testing.T) { 62 tf1 := TokenFrequencies{ 63 "water": &TokenFreq{ 64 Term: []byte("water"), 65 Locations: []*TokenLocation{ 66 { 67 Position: 1, 68 Start: 0, 69 End: 5, 70 }, 71 { 72 Position: 2, 73 Start: 6, 74 End: 11, 75 }, 76 }, 77 }, 78 } 79 tf2 := TokenFrequencies{ 80 "water": &TokenFreq{ 81 Term: []byte("water"), 82 Locations: []*TokenLocation{ 83 { 84 Position: 1, 85 Start: 0, 86 End: 5, 87 }, 88 { 89 Position: 2, 90 Start: 6, 91 End: 11, 92 }, 93 }, 94 }, 95 } 96 expectedResult := TokenFrequencies{ 97 "water": &TokenFreq{ 98 Term: []byte("water"), 99 Locations: []*TokenLocation{ 100 { 101 Position: 1, 102 Start: 0, 103 End: 5, 104 }, 105 { 106 Position: 2, 107 Start: 6, 108 End: 11, 109 }, 110 { 111 Field: "tf2", 112 Position: 1, 113 Start: 0, 114 End: 5, 115 }, 116 { 117 Field: "tf2", 118 Position: 2, 119 Start: 6, 120 End: 11, 121 }, 122 }, 123 }, 124 } 125 tf1.MergeAll("tf2", tf2) 126 if !reflect.DeepEqual(tf1, expectedResult) { 127 t.Errorf("expected %#v, got %#v", expectedResult, tf1) 128 } 129} 130 131func TestTokenFrequenciesMergeAllLeftEmpty(t *testing.T) { 132 tf1 := TokenFrequencies{} 133 tf2 := TokenFrequencies{ 134 "water": &TokenFreq{ 135 Term: []byte("water"), 136 Locations: []*TokenLocation{ 137 { 138 Position: 1, 139 Start: 0, 140 End: 5, 141 }, 142 { 143 Position: 2, 144 Start: 6, 145 End: 11, 146 }, 147 }, 148 }, 149 } 150 expectedResult := TokenFrequencies{ 151 "water": &TokenFreq{ 152 Term: []byte("water"), 153 Locations: []*TokenLocation{ 154 { 155 Field: "tf2", 156 Position: 1, 157 Start: 0, 158 End: 5, 159 }, 160 { 161 Field: "tf2", 162 Position: 2, 163 Start: 6, 164 End: 11, 165 }, 166 }, 167 }, 168 } 169 tf1.MergeAll("tf2", tf2) 170 if !reflect.DeepEqual(tf1, expectedResult) { 171 t.Errorf("expected %#v, got %#v", expectedResult, tf1) 172 } 173} 174