trie.test.js 5.1 KB

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