trie.test.js 5.0 KB

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