Enable trie hostname mode & extend hostname mode test cases

This commit is contained in:
SukkaW
2024-05-27 01:56:08 +08:00
parent d137bdb8a3
commit eb0623c1a9
5 changed files with 110 additions and 31 deletions

View File

@@ -20,6 +20,23 @@ describe('Trie', () => {
expect(trie.has('sukkaw')).toBeFalse();
});
it('should be possible to add domains to a Trie (hostname).', () => {
const trie = createTrie(null, true);
trie.add('a.skk.moe');
trie.add('skk.moe');
trie.add('anotherskk.moe');
expect(trie.size).toBe(3);
expect(trie.has('a.skk.moe')).toBeTrue();
expect(trie.has('skk.moe')).toBeTrue();
expect(trie.has('anotherskk.moe')).toBeTrue();
expect(trie.has('example.com')).toBeFalse();
expect(trie.has('skk.mo')).toBeFalse();
expect(trie.has('another.skk.moe')).toBeFalse();
});
it('adding the same item several times should not increase size.', () => {
const trie = createTrie();
@@ -31,9 +48,24 @@ describe('Trie', () => {
expect(trie.has('rat')).toBeTrue();
});
it('should be possible to set the null sequence.', () => {
const trie = createTrie();
it('adding the same item several times should not increase size (hostname).', () => {
const trie = createTrie(null, true);
trie.add('skk.moe');
trie.add('blog.skk.moe');
trie.add('skk.moe');
expect(trie.size).toBe(2);
expect(trie.has('skk.moe')).toBeTrue();
});
it('should be possible to set the null sequence.', () => {
let trie = createTrie();
trie.add('');
expect(trie.has('')).toBeTrue();
trie = createTrie(null, true);
trie.add('');
expect(trie.has('')).toBeTrue();
});
@@ -61,6 +93,29 @@ describe('Trie', () => {
expect(trie.size).toBe(0);
});
it('should be possible to delete items (hostname).', () => {
const trie = createTrie(null, true);
trie.add('skk.moe');
trie.add('example.com');
trie.add('moe.sb');
expect(trie.delete('')).toBeFalse();
expect(trie.delete('')).toBeFalse();
expect(trie.delete('example.org')).toBeFalse();
expect(trie.delete('skk.moe')).toBeTrue();
expect(trie.has('skk.moe')).toBeFalse();
expect(trie.has('moe.sb')).toBeTrue();
expect(trie.size).toBe(2);
expect(trie.delete('example.com')).toBeTrue();
expect(trie.size).toBe(1);
expect(trie.delete('moe.sb')).toBeTrue();
expect(trie.size).toBe(0);
});
it('should be possible to check the existence of a sequence in the Trie.', () => {
const trie = createTrie();
@@ -68,6 +123,18 @@ describe('Trie', () => {
expect(trie.has('romanesque')).toBe(true);
expect(trie.has('roman')).toBe(false);
expect(trie.has('esque')).toBe(false);
expect(trie.has('')).toBe(false);
});
it('should be possible to check the existence of a sequence in the Trie (hostname).', () => {
const trie = createTrie(null, true);
trie.add('example.org.skk.moe');
expect(trie.has('example.org.skk.moe')).toBe(true);
expect(trie.has('skk.moe')).toBe(false);
expect(trie.has('example.org')).toBe(false);
expect(trie.has('')).toBe(false);
});
@@ -79,8 +146,6 @@ describe('Trie', () => {
trie.add('sesqueroman');
trie.add('greek');
console.log({ trie });
expect(trie.find('roman')).toEqual(['roman', 'esqueroman', 'sesqueroman']);
expect(trie.find('man')).toEqual(['roman', 'esqueroman', 'sesqueroman']);
expect(trie.find('esqueroman')).toEqual(['esqueroman', 'sesqueroman']);
@@ -89,13 +154,31 @@ describe('Trie', () => {
expect(trie.find('')).toEqual(['greek', 'roman', 'esqueroman', 'sesqueroman']);
});
it('should be possible to create a trie from an arbitrary iterable.', () => {
const words = ['roman', 'esqueroman'];
it('should be possible to retrieve items matching the given prefix (hostname).', () => {
const trie = createTrie(null, true);
const trie = createTrie(words);
trie.add('example.com');
trie.add('blog.example.com');
trie.add('cdn.example.com');
trie.add('example.org');
expect(trie.find('example.com')).toEqual(['example.com', 'cdn.example.com', 'blog.example.com']);
expect(trie.find('com')).toEqual(['example.com', 'cdn.example.com', 'blog.example.com']);
expect(trie.find('.example.com')).toEqual(['cdn.example.com', 'blog.example.com']);
expect(trie.find('org')).toEqual(['example.org']);
expect(trie.find('example.net')).toEqual([]);
expect(trie.find('')).toEqual(['example.org', 'example.com', 'cdn.example.com', 'blog.example.com']);
});
it('should be possible to create a trie from an arbitrary iterable.', () => {
let trie = createTrie(['roman', 'esqueroman']);
expect(trie.size).toBe(2);
expect(trie.has('roman')).toBe(true);
trie = createTrie(new Set(['skk.moe', 'example.com']), true);
expect(trie.size).toBe(2);
expect(trie.has('skk.moe')).toBe(true);
});
});
@@ -106,8 +189,6 @@ describe.each([
it('should not remove same entry', () => {
const trie = createTrie(['.skk.moe', 'noc.one'], hostnameMode);
console.log(trie);
expect(trie.find('.skk.moe')).toStrictEqual(['.skk.moe']);
expect(trie.find('noc.one')).toStrictEqual(['noc.one']);
});
@@ -115,8 +196,6 @@ describe.each([
it('should match subdomain - 1', () => {
const trie = createTrie(['www.noc.one', 'www.sukkaw.com', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], hostnameMode);
console.log(trie);
expect(trie.find('.skk.moe')).toStrictEqual(['image.cdn.skk.moe', 'blog.skk.moe']);
expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']);
});
@@ -124,8 +203,6 @@ describe.each([
it('should match subdomain - 2', () => {
const trie = createTrie(['www.noc.one', 'www.sukkaw.com', '.skk.moe', 'blog.skk.moe', 'image.cdn.skk.moe', 'cdn.sukkaw.net'], hostnameMode);
console.log(trie);
expect(trie.find('.skk.moe')).toStrictEqual(['.skk.moe', 'image.cdn.skk.moe', 'blog.skk.moe']);
expect(trie.find('.sukkaw.com')).toStrictEqual(['www.sukkaw.com']);
});