vector_test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. suite('lunr.Vector', function () {
  2. var vectorFromArgs = function () {
  3. var vector = new lunr.Vector
  4. Array.prototype.slice.call(arguments)
  5. .forEach(function (el, i) {
  6. vector.insert(i, el)
  7. })
  8. return vector
  9. }
  10. suite('#magnitude', function () {
  11. test('calculates magnitude of a vector', function () {
  12. var vector = vectorFromArgs(4,5,6)
  13. assert.equal(Math.sqrt(77), vector.magnitude())
  14. })
  15. })
  16. suite('#dot', function () {
  17. test('calculates dot product of two vectors', function () {
  18. var v1 = vectorFromArgs(1, 3, -5),
  19. v2 = vectorFromArgs(4, -2, -1)
  20. assert.equal(3, v1.dot(v2))
  21. })
  22. })
  23. suite('#similarity', function () {
  24. test('calculates the similarity between two vectors', function () {
  25. var v1 = vectorFromArgs(1, 3, -5),
  26. v2 = vectorFromArgs(4, -2, -1)
  27. assert.approximately(v1.similarity(v2), 0.5, 0.01)
  28. })
  29. test('empty vector', function () {
  30. var vEmpty = new lunr.Vector,
  31. v1 = vectorFromArgs(1)
  32. assert.equal(0, vEmpty.similarity(v1))
  33. assert.equal(0, v1.similarity(vEmpty))
  34. })
  35. test('non-overlapping vector', function () {
  36. var v1 = new lunr.Vector([1, 1]),
  37. v2 = new lunr.Vector([2, 1])
  38. assert.equal(0, v1.similarity(v2))
  39. assert.equal(0, v2.similarity(v1))
  40. })
  41. })
  42. suite('#insert', function () {
  43. test('invalidates magnitude cache', function () {
  44. var vector = vectorFromArgs(4,5,6)
  45. assert.equal(Math.sqrt(77), vector.magnitude())
  46. vector.insert(3, 7)
  47. assert.equal(Math.sqrt(126), vector.magnitude())
  48. })
  49. test('keeps items in index specified order', function () {
  50. var vector = new lunr.Vector
  51. vector.insert(2, 4)
  52. vector.insert(1, 5)
  53. vector.insert(0, 6)
  54. assert.deepEqual([6,5,4], vector.toArray())
  55. })
  56. test('fails when duplicate entry', function () {
  57. var vector = vectorFromArgs(4, 5, 6)
  58. assert.throws(function () { vector.insert(0, 44) })
  59. })
  60. })
  61. suite('#upsert', function () {
  62. test('invalidates magnitude cache', function () {
  63. var vector = vectorFromArgs(4,5,6)
  64. assert.equal(Math.sqrt(77), vector.magnitude())
  65. vector.upsert(3, 7)
  66. assert.equal(Math.sqrt(126), vector.magnitude())
  67. })
  68. test('keeps items in index specified order', function () {
  69. var vector = new lunr.Vector
  70. vector.upsert(2, 4)
  71. vector.upsert(1, 5)
  72. vector.upsert(0, 6)
  73. assert.deepEqual([6,5,4], vector.toArray())
  74. })
  75. test('calls fn for value on duplicate', function () {
  76. var vector = vectorFromArgs(4, 5, 6)
  77. vector.upsert(0, 4, function (current, passed) { return current + passed })
  78. assert.deepEqual([8, 5, 6], vector.toArray())
  79. })
  80. })
  81. suite('#positionForIndex', function () {
  82. var vector = new lunr.Vector ([
  83. 1, 'a',
  84. 2, 'b',
  85. 4, 'c',
  86. 7, 'd',
  87. 11, 'e'
  88. ])
  89. test('at the beginning', function () {
  90. assert.equal(0, vector.positionForIndex(0))
  91. })
  92. test('at the end', function () {
  93. assert.equal(10, vector.positionForIndex(20))
  94. })
  95. test('consecutive', function () {
  96. assert.equal(4, vector.positionForIndex(3))
  97. })
  98. test('non-consecutive gap after', function () {
  99. assert.equal(6, vector.positionForIndex(5))
  100. })
  101. test('non-consecutive gap before', function () {
  102. assert.equal(6, vector.positionForIndex(6))
  103. })
  104. test('non-consecutive gave before and after', function () {
  105. assert.equal(8, vector.positionForIndex(9))
  106. })
  107. test('duplicate at the beginning', function () {
  108. assert.equal(0, vector.positionForIndex(1))
  109. })
  110. test('duplicate at the end', function () {
  111. assert.equal(8, vector.positionForIndex(11))
  112. })
  113. test('duplicate consecutive', function () {
  114. assert.equal(4, vector.positionForIndex(4))
  115. })
  116. })
  117. })