trie.test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. require('chai').should();
  2. const Trie = require('./trie');
  3. const assert = require('assert');
  4. describe('Trie', () => {
  5. it('should be possible to add items to a Trie.', () => {
  6. const trie = new Trie();
  7. trie.add('sukka');
  8. trie.add('ukka');
  9. trie.add('akku');
  10. trie.size.should.eq(3);
  11. trie.has('sukka').should.eq(true);
  12. trie.has('ukka').should.eq(true);
  13. trie.has('akku').should.eq(true);
  14. trie.has('noc').should.eq(false);
  15. trie.has('suk').should.eq(false);
  16. trie.has('sukkaw').should.eq(false);
  17. });
  18. it('adding the same item several times should not increase size.', () => {
  19. const trie = new Trie();
  20. trie.add('rat');
  21. trie.add('erat');
  22. trie.add('rat');
  23. assert.strictEqual(trie.size, 2);
  24. assert.strictEqual(trie.has('rat'), true);
  25. });
  26. it('should be possible to set the null sequence.', () => {
  27. let trie = new Trie();
  28. trie.add('');
  29. trie.size.should.eq(1);
  30. trie.has('').should.eq(true);
  31. trie = new Trie(Array);
  32. trie.add([]);
  33. trie.size.should.eq(1);
  34. trie.has([]).should.eq(true);
  35. });
  36. it('should be possible to delete items.', () => {
  37. const trie = new Trie();
  38. trie.add('rat');
  39. trie.add('rate');
  40. trie.add('tar');
  41. assert.strictEqual(trie.delete(''), false);
  42. trie.delete('').should.eq(false);
  43. trie.delete('hello').should.eq(false);
  44. trie.delete('rat').should.eq(true);
  45. trie.has('rat').should.eq(false);
  46. trie.has('rate').should.eq(true);
  47. trie.size.should.eq(2);
  48. assert.strictEqual(trie.delete('rate'), true);
  49. assert.strictEqual(trie.size, 1);
  50. assert.strictEqual(trie.delete('tar'), true);
  51. assert.strictEqual(trie.size, 0);
  52. });
  53. it('should be possible to check the existence of a sequence in the Trie.', () => {
  54. const trie = new Trie();
  55. trie.add('romanesque');
  56. assert.strictEqual(trie.has('romanesque'), true);
  57. assert.strictEqual(trie.has('roman'), false);
  58. assert.strictEqual(trie.has(''), false);
  59. });
  60. it('should be possible to retrieve items matching the given prefix.', () => {
  61. const trie = new Trie();
  62. trie.add('roman');
  63. trie.add('esqueroman');
  64. trie.add('sesqueroman');
  65. trie.add('greek');
  66. assert.deepStrictEqual(trie.find('roman'), ['roman', 'esqueroman', 'sesqueroman']);
  67. assert.deepStrictEqual(trie.find('man'), ['roman', 'esqueroman', 'sesqueroman']);
  68. assert.deepStrictEqual(trie.find('esqueroman'), ['esqueroman', 'sesqueroman']);
  69. assert.deepStrictEqual(trie.find('eek'), ['greek']);
  70. assert.deepStrictEqual(trie.find('hello'), []);
  71. assert.deepStrictEqual(trie.find(''), ['greek', 'roman', 'esqueroman', 'sesqueroman']);
  72. });
  73. // it('should work with custom tokens.', () => {
  74. // const trie = new Trie(Array);
  75. // trie.add(['the', 'cat', 'eats', 'the', 'mouse']);
  76. // trie.add(['the', 'mouse', 'eats', 'cheese']);
  77. // trie.add(['hello', 'world']);
  78. // assert.strictEqual(trie.size, 3);
  79. // assert.strictEqual(trie.has(['the', 'mouse', 'eats', 'cheese']), true);
  80. // assert.strictEqual(trie.has(['the', 'mouse', 'eats']), false);
  81. // assert.strictEqual(trie.delete(['hello']), false);
  82. // assert.strictEqual(trie.delete(['hello', 'world']), true);
  83. // assert.strictEqual(trie.size, 2);
  84. // });
  85. // it('should be possible to iterate over the trie\'s prefixes.', () => {
  86. // const trie = new Trie();
  87. // trie.add('rat');
  88. // trie.add('rate');
  89. // let prefixes = take(trie.prefixes());
  90. // assert.deepStrictEqual(prefixes, ['rat', 'rate']);
  91. // trie.add('rater');
  92. // trie.add('rates');
  93. // prefixes = take(trie.keys('rate'));
  94. // assert.deepStrictEqual(prefixes, ['rate', 'rates', 'rater']);
  95. // });
  96. // it('should be possible to iterate over the trie\'s prefixes using for...of.', () => {
  97. // const trie = new Trie();
  98. // trie.add('rat');
  99. // trie.add('rate');
  100. // const tests = [
  101. // 'rat',
  102. // 'rate'
  103. // ];
  104. // let i = 0;
  105. // for (const prefix of trie)
  106. // assert.deepStrictEqual(prefix, tests[i++]);
  107. // });
  108. it('should be possible to create a trie from an arbitrary iterable.', () => {
  109. const words = ['roman', 'esqueroman'];
  110. const trie = Trie.from(words);
  111. assert.strictEqual(trie.size, 2);
  112. assert.deepStrictEqual(trie.has('roman'), true);
  113. });
  114. });
  115. describe('surge domainset dedupe', () => {
  116. it('should not remove same entry', () => {
  117. const trie = Trie.from(['.skk.moe', 'noc.one']);
  118. trie.find('.skk.moe').should.eql(['.skk.moe']);
  119. trie.find('noc.one').should.eql(['noc.one']);
  120. });
  121. it('should remove subdomain', () => {
  122. const trie = Trie.from(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net']);
  123. // trie.find('noc.one').should.eql(['www.noc.one']);
  124. trie.find('.skk.moe').should.eql(['image.cdn.skk.moe', 'blog.skk.moe']);
  125. // trie.find('sukkaw.net').should.eql(['cdn.sukkaw.net']);
  126. trie.find('.sukkaw.com').should.eql(['www.sukkaw.com']);
  127. });
  128. it('should not remove non-subdomain', () => {
  129. const trie = Trie.from(['skk.moe', 'sukkaskk.moe']);
  130. trie.find('.skk.moe').should.eql([]);
  131. });
  132. });