trie.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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')).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. 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 be possible to create a trie from an arbitrary iterable.', () => {
  69. const words = ['roman', 'esqueroman'];
  70. const trie = createTrie(words);
  71. expect(trie.size).toBe(2);
  72. expect(trie.has('roman')).toBe(true);
  73. });
  74. });
  75. describe('surge domainset dedupe', () => {
  76. it('should not remove same entry', () => {
  77. const trie = createTrie(['.skk.moe', 'noc.one']);
  78. expect(trie.find('.skk.moe')).toStrictEqual(['.skk.moe']);
  79. expect(trie.find('noc.one')).toStrictEqual(['noc.one']);
  80. });
  81. it('should remove subdomain', () => {
  82. const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net']);
  83. console.log(trie);
  84. expect(trie.find('.skk.moe')).toStrictEqual(['image.cdn.skk.moe', 'blog.skk.moe']);
  85. expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']);
  86. });
  87. it('should not remove non-subdomain', () => {
  88. const trie = createTrie(['skk.moe', 'sukkaskk.moe']);
  89. expect(trie.find('.skk.moe')).toStrictEqual([]);
  90. });
  91. });