trie.test.ts 4.9 KB

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