freq_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  3. // except in compliance with the License. You may obtain a copy of the License at
  4. // http://www.apache.org/licenses/LICENSE-2.0
  5. // Unless required by applicable law or agreed to in writing, software distributed under the
  6. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  7. // either express or implied. See the License for the specific language governing permissions
  8. // and limitations under the License.
  9. package analysis
  10. import (
  11. "reflect"
  12. "testing"
  13. )
  14. func TestTokenFrequency(t *testing.T) {
  15. tokens := TokenStream{
  16. &Token{
  17. Term: []byte("water"),
  18. Position: 1,
  19. Start: 0,
  20. End: 5,
  21. },
  22. &Token{
  23. Term: []byte("water"),
  24. Position: 2,
  25. Start: 6,
  26. End: 11,
  27. },
  28. }
  29. expectedResult := TokenFrequencies{
  30. &TokenFreq{
  31. Term: []byte("water"),
  32. Locations: []*TokenLocation{
  33. &TokenLocation{
  34. Position: 1,
  35. Start: 0,
  36. End: 5,
  37. },
  38. &TokenLocation{
  39. Position: 2,
  40. Start: 6,
  41. End: 11,
  42. },
  43. },
  44. },
  45. }
  46. result := TokenFrequency(tokens)
  47. if !reflect.DeepEqual(result, expectedResult) {
  48. t.Errorf("expected %#v, got %#v", expectedResult, result)
  49. }
  50. }
  51. func TestTokenFrequenciesMergeAll(t *testing.T) {
  52. tf1 := TokenFrequencies{
  53. &TokenFreq{
  54. Term: []byte("water"),
  55. Locations: []*TokenLocation{
  56. &TokenLocation{
  57. Position: 1,
  58. Start: 0,
  59. End: 5,
  60. },
  61. &TokenLocation{
  62. Position: 2,
  63. Start: 6,
  64. End: 11,
  65. },
  66. },
  67. },
  68. }
  69. tf2 := TokenFrequencies{
  70. &TokenFreq{
  71. Term: []byte("water"),
  72. Locations: []*TokenLocation{
  73. &TokenLocation{
  74. Position: 1,
  75. Start: 0,
  76. End: 5,
  77. },
  78. &TokenLocation{
  79. Position: 2,
  80. Start: 6,
  81. End: 11,
  82. },
  83. },
  84. },
  85. }
  86. expectedResult := TokenFrequencies{
  87. &TokenFreq{
  88. Term: []byte("water"),
  89. Locations: []*TokenLocation{
  90. &TokenLocation{
  91. Position: 1,
  92. Start: 0,
  93. End: 5,
  94. },
  95. &TokenLocation{
  96. Position: 2,
  97. Start: 6,
  98. End: 11,
  99. },
  100. &TokenLocation{
  101. Field: "tf2",
  102. Position: 1,
  103. Start: 0,
  104. End: 5,
  105. },
  106. &TokenLocation{
  107. Field: "tf2",
  108. Position: 2,
  109. Start: 6,
  110. End: 11,
  111. },
  112. },
  113. },
  114. }
  115. tf1.MergeAll("tf2", tf2)
  116. if !reflect.DeepEqual(tf1, expectedResult) {
  117. t.Errorf("expected %#v, got %#v", expectedResult, tf1)
  118. }
  119. }
  120. func TestTokenFrequenciesMergeAllLeftEmpty(t *testing.T) {
  121. tf1 := TokenFrequencies{}
  122. tf2 := TokenFrequencies{
  123. &TokenFreq{
  124. Term: []byte("water"),
  125. Locations: []*TokenLocation{
  126. &TokenLocation{
  127. Position: 1,
  128. Start: 0,
  129. End: 5,
  130. },
  131. &TokenLocation{
  132. Position: 2,
  133. Start: 6,
  134. End: 11,
  135. },
  136. },
  137. },
  138. }
  139. expectedResult := TokenFrequencies{
  140. &TokenFreq{
  141. Term: []byte("water"),
  142. Locations: []*TokenLocation{
  143. &TokenLocation{
  144. Field: "tf2",
  145. Position: 1,
  146. Start: 0,
  147. End: 5,
  148. },
  149. &TokenLocation{
  150. Field: "tf2",
  151. Position: 2,
  152. Start: 6,
  153. End: 11,
  154. },
  155. },
  156. },
  157. }
  158. result := tf1.MergeAll("tf2", tf2)
  159. if !reflect.DeepEqual(result, expectedResult) {
  160. t.Errorf("expected %#v, got %#v", expectedResult, result)
  161. }
  162. }