sorted_set_test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. module('lunr.SortedSet')
  2. test('adding an element that doesn\'t exist into the set', function () {
  3. var set = new lunr.SortedSet
  4. equal(set.length, 0)
  5. set.add('foo')
  6. equal(set.length, 1)
  7. })
  8. test('adding an element that does exist into the set', function () {
  9. var set = new lunr.SortedSet
  10. set.add('foo')
  11. equal(set.length, 1)
  12. set.add('foo')
  13. equal(set.length, 1)
  14. })
  15. test('sort is maintained when adding elements to the set', function () {
  16. var set = new lunr.SortedSet
  17. set.add('b')
  18. set.add('d')
  19. set.add('a')
  20. set.add('c')
  21. deepEqual(set.elements, ['a', 'b', 'c', 'd'])
  22. })
  23. test('adding more than one element to the set in one go', function () {
  24. var set = new lunr.SortedSet
  25. set.add('foo', 'bar', 'baz', 'foo')
  26. equal(set.length, 3)
  27. })
  28. test('converting to an array', function () {
  29. var set = new lunr.SortedSet
  30. set.add('foo', 'bar', 'baz')
  31. deepEqual(set.toArray(), ['bar', 'baz', 'foo'])
  32. })
  33. test('mapping the set', function () {
  34. var set = new lunr.SortedSet, a = []
  35. set.add('foo', 'bar')
  36. set.forEach(function (t) { a.push(t) })
  37. deepEqual(a, ['bar', 'foo'])
  38. })
  39. test('getting the index of an item in the set', function () {
  40. var set = new lunr.SortedSet
  41. equal(set.indexOf('non member'), -1)
  42. set.add('foo')
  43. equal(set.indexOf('foo'), 0)
  44. equal(set.indexOf('non member'), -1)
  45. set.add('bar')
  46. equal(set.indexOf('foo'), 1)
  47. equal(set.indexOf('bar'), 0)
  48. equal(set.indexOf('non member'), -1)
  49. })
  50. test('intersecting this set with another set', function () {
  51. var set1 = new lunr.SortedSet,
  52. set2 = new lunr.SortedSet,
  53. setIntersect
  54. set1.add('foo', 'bar')
  55. set2.add('baz', 'foo')
  56. setIntersect = set1.intersect(set2)
  57. ok(setIntersect.indexOf('foo') > -1)
  58. ok(setIntersect.indexOf('bar') == -1)
  59. ok(setIntersect.indexOf('baz') == -1)
  60. })
  61. test('unioning this set with another set', function () {
  62. var set1 = new lunr.SortedSet,
  63. set2 = new lunr.SortedSet,
  64. setUnion
  65. set1.add('foo', 'bar')
  66. set2.add('baz', 'foo')
  67. setUnion = set1.union(set2)
  68. ok(setUnion.indexOf('foo') > -1)
  69. ok(setUnion.indexOf('bar') > -1)
  70. ok(setUnion.indexOf('baz') > -1)
  71. equal(setUnion.length ,3)
  72. })
  73. test('serialising', function () {
  74. var emptySet = new lunr.SortedSet,
  75. nonEmptySet = new lunr.SortedSet
  76. nonEmptySet.add(1,2,3,4)
  77. deepEqual(emptySet.toJSON(), [])
  78. deepEqual(nonEmptySet.toJSON(), [1,2,3,4])
  79. })
  80. test('loading serialised dump', function () {
  81. var serialisedData = [1,2,3,4],
  82. set = lunr.SortedSet.load(serialisedData)
  83. equal(set.length, 4)
  84. deepEqual(set.elements, [1,2,3,4])
  85. })