trie.test.ts 4.9 KB

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