trie.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { createTrie } from './trie';
  2. // eslint-disable-next-line import-x/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')).toBeTrue();
  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. console.log({ trie });
  62. expect(trie.find('roman')).toEqual(['roman', 'esqueroman', 'sesqueroman']);
  63. expect(trie.find('man')).toEqual(['roman', 'esqueroman', 'sesqueroman']);
  64. expect(trie.find('esqueroman')).toEqual(['esqueroman', 'sesqueroman']);
  65. expect(trie.find('eek')).toEqual(['greek']);
  66. expect(trie.find('hello')).toEqual([]);
  67. expect(trie.find('')).toEqual(['greek', 'roman', 'esqueroman', 'sesqueroman']);
  68. });
  69. it('should be possible to create a trie from an arbitrary iterable.', () => {
  70. const words = ['roman', 'esqueroman'];
  71. const trie = createTrie(words);
  72. expect(trie.size).toBe(2);
  73. expect(trie.has('roman')).toBe(true);
  74. });
  75. });
  76. describe.each([
  77. ['hostname mode off', false],
  78. ['hostname mode on', true]
  79. ])('surge domainset dedupe %s', (_, hostnameMode) => {
  80. it('should not remove same entry', () => {
  81. const trie = createTrie(['.skk.moe', 'noc.one'], hostnameMode);
  82. console.log(trie);
  83. expect(trie.find('.skk.moe')).toStrictEqual(['.skk.moe']);
  84. expect(trie.find('noc.one')).toStrictEqual(['noc.one']);
  85. });
  86. it('should match subdomain - 1', () => {
  87. const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], hostnameMode);
  88. console.log(trie);
  89. expect(trie.find('.skk.moe')).toStrictEqual(['image.cdn.skk.moe', 'blog.skk.moe']);
  90. expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']);
  91. });
  92. it('should match subdomain - 2', () => {
  93. const trie = createTrie(['www.noc.one', 'www.sukkaw.com', '.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], hostnameMode);
  94. console.log(trie);
  95. expect(trie.find('.skk.moe')).toStrictEqual(['.skk.moe', 'image.cdn.skk.moe', 'blog.skk.moe']);
  96. expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']);
  97. });
  98. it('should not remove non-subdomain', () => {
  99. const trie = createTrie(['skk.moe', 'sukkaskk.moe'], hostnameMode);
  100. expect(trie.find('.skk.moe')).toStrictEqual([]);
  101. });
  102. });
  103. describe('smol tree', () => {
  104. it('should create simple tree - 1', () => {
  105. const trie = createTrie([
  106. '.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe',
  107. 'www.noc.one', 'cdn.noc.one',
  108. '.blog.sub.example.com', 'sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
  109. ], true, true);
  110. console.log(trie);
  111. expect(trie.dump()).toStrictEqual([
  112. '.sub.example.com',
  113. 'cdn.noc.one', 'www.noc.one',
  114. '.skk.moe'
  115. ]);
  116. });
  117. it.only('should create simple tree - 2', () => {
  118. const trie = createTrie([
  119. '.skk.moe', 'blog.skk.moe', '.cdn.skk.moe', 'skk.moe'
  120. ], true, true);
  121. console.log({ trie });
  122. expect(trie.dump()).toStrictEqual([
  123. '.skk.moe'
  124. ]);
  125. });
  126. it('should create simple tree - 2', () => {
  127. const trie = createTrie([
  128. '.blog.sub.example.com', 'cdn.sub.example.com', '.sub.example.com'
  129. ], true, true);
  130. console.log(trie);
  131. expect(trie.dump()).toStrictEqual([
  132. '.sub.example.com'
  133. ]);
  134. trie.add('.sub.example.com');
  135. expect(trie.dump()).toStrictEqual([
  136. '.sub.example.com'
  137. ]);
  138. });
  139. it('should create simple tree - 3', () => {
  140. const trie = createTrie([
  141. 'commercial.shouji.360.cn',
  142. 'act.commercial.shouji.360.cn',
  143. 'cdn.creative.medialytics.com',
  144. 'px.cdn.creative.medialytics.com'
  145. ], true, true);
  146. expect(trie.dump()).toStrictEqual([
  147. 'cdn.creative.medialytics.com',
  148. 'px.cdn.creative.medialytics.com',
  149. 'commercial.shouji.360.cn',
  150. 'act.commercial.shouji.360.cn'
  151. ]);
  152. });
  153. });