Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 🧪 added missing tests for SLL, DLL and trie #23

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/tests/doubly-linked-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,59 @@ Deno.test('DoublyLinkedList - basic operations', async (t) => {
assertEquals(values, [3, 2, 1]);
});
});

Deno.test('DoublyLinkedList - complex removal operations', async (t) => {
await t.step(
'removing first element in 1-sized list using removeFirst method',
() => {
const list = new DoublyLinkedList<number>();
list.append(1);
assertEquals(list.size, 1);
const val = list.removeFirst();
assertEquals(val, 1);
assertEquals(list.size, 0);
},
);

await t.step(
'removing last element in 1-sized list using removeLast method',
() => {
const list = new DoublyLinkedList<number>();
list.append(1);
assertEquals(list.size, 1);
const val = list.removeLast();
assertEquals(val, 1);
assertEquals(list.size, 0);
},
);

await t.step('removing first and last elements using removeAt method', () => {
const list = new DoublyLinkedList<number>();
list.append(1);
list.append(2);
list.append(3);
list.append(8);
assertEquals(list.size, 4);
const stVal = list.removeAt(0);
assertEquals(stVal, 1);
assertEquals(list.size, 3);
const endVal = list.removeAt(2);
assertEquals(endVal, 8);
assertEquals(list.size, 2);
});

await t.step('removing first and last elements using remove method', () => {
const list = new DoublyLinkedList<number>();
list.append(1);
list.append(2);
list.append(3);
list.append(8);
assertEquals(list.size, 4);
const stVal = list.remove(1);
assertEquals(stVal, true);
assertEquals(list.size, 3);
const endVal = list.remove(8);
assertEquals(endVal, true);
assertEquals(list.size, 2);
});
});
73 changes: 73 additions & 0 deletions src/tests/linked-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,76 @@ Deno.test('LinkedList - basic operations', async (t) => {
assertEquals(values, [1, 2, 3]);
});
});

Deno.test('LinkedList - complex removal operations', async (t) => {
await t.step('remove some middle value of the linked list', () => {
const list = new LinkedList<number>();
list.append(2);
list.append(3);
list.append(56);
list.append(4);
assertEquals(list.size, 4);
list.remove(56);
assertEquals(list.toArray(), [2, 3, 4]);
assertEquals(list.size, 3);
});

await t.step('remove some non-existent value from the linked list', () => {
const list = new LinkedList<number>();
list.append(2);
list.append(3);
list.append(56);
list.append(4);
assertEquals(list.size, 4);
const val = list.remove(79);
assertEquals(list.toArray(), [2, 3, 56, 4]);
assertEquals(list.size, 4);
assertEquals(val, false);
});

await t.step('remove one value from a 2-sized linked list', () => {
const list = new LinkedList<number>();
list.append(3);
list.append(4);
assertEquals(list.size, 2);
const val = list.remove(4);
assertEquals(list.size, 1);
assertEquals(val, true);
});

await t.step('remove head using removeAt method', () => {
const list = new LinkedList<number>();
list.append(3);
list.append(4);
list.append(8);
assertEquals(list.size, 3);
const val = list.removeAt(0);
assertEquals(list.size, 2);
assertEquals(val, 3);
});

await t.step('remove middle indexed value using removeAt method', () => {
const list = new LinkedList<number>();
list.append(3);
list.append(4);
list.append(11);
list.append(8);
assertEquals(list.size, 4);
const val = list.removeAt(2);
assertEquals(list.size, 3);
assertEquals(val, 11);
});
});

Deno.test('LinkedList - edge case for insertAt operation', async (t) => {
await t.step('remove head using removeAt method', () => {
const list = new LinkedList<number>();
list.append(3);
list.append(4);
list.append(8);
assertEquals(list.size, 3);
list.insertAt(0, 2);
assertEquals(list.size, 4);
assertEquals(list.toArray(), [3, 4, 0, 8]);
});
});
1 change: 1 addition & 0 deletions src/tests/trie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Deno.test('Trie - basic operations', async (t) => {
const trie = new Trie<number>();
trie.insert('hello', 1);
trie.insert('help', 2);
trie.insert('', 3);
assertEquals(trie.search('hello'), 1);
assertEquals(trie.search('help'), 2);
assertEquals(trie.search('hell'), null);
Expand Down