Repository navigation
Expand file tree
/
Copy pathLinkedList.test.js
More file actions
131 lines (116 loc) · 3.58 KB
/
Copy pathLinkedList.test.js
File metadata and controls
131 lines (116 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const LinkedList = require("./LinkedList");
describe("#insertAtHead", () => {
test("it adds the element to the beginning of the list", () => {
const ll = new LinkedList();
ll.insertAtHead(10);
const oldHead = ll.head;
ll.insertAtHead(20);
expect(ll.head.value).toBe(20);
expect(ll.head.next).toBe(oldHead);
expect(ll.length).toBe(2);
});
});
describe("#getByIndex", () => {
describe("with index less than 0", () => {
test("it will return null", () => {
const ll = LinkedList.fromValues(10, 20);
expect(ll.getByIndex(-1)).toBeNull();
});
});
describe("with index greater than list length", () => {
test("it will return null", () => {
const ll = LinkedList.fromValues(10, 20);
expect(ll.getByIndex(5)).toBeNull();
});
});
describe("with index 0", () => {
test("it returns the head", () => {
const ll = LinkedList.fromValues(10, 20);
expect(ll.getByIndex(0).value).toBe(10);
});
});
describe("with index in the middle", () => {
test("it returns the element at that index", () => {
const ll = LinkedList.fromValues(10, 20, 30, 40);
expect(ll.getByIndex(2).value).toBe(30);
});
});
});
describe("#insertAtIndex", () => {
describe("with index less than 0", () => {
test("it does not insert any thing", () => {
const ll = LinkedList.fromValues(10, 20);
ll.insertAtIndex(-1, 30);
expect(ll.length).toBe(2);
});
});
describe("with index greater than list length", () => {
test("it does not insert any thing", () => {
const ll = LinkedList.fromValues(10, 20);
ll.insertAtIndex(5, 30);
expect(ll.length).toBe(2);
});
});
describe("with index 0", () => {
test("it insert at the head", () => {
const ll = LinkedList.fromValues(10, 20);
ll.insertAtIndex(0, 30);
expect(ll.length).toBe(3);
expect(ll.head.value).toBe(30);
expect(ll.head.next.value).toBe(10);
});
});
describe("with index in the middle", () => {
test("it insert at the given index", () => {
const ll = LinkedList.fromValues(10, 20, 40);
ll.insertAtIndex(2, 30);
const node = ll.getByIndex(2);
expect(ll.length).toBe(4);
expect(node.value).toBe(30);
expect(node.next.value).toBe(40);
});
});
});
describe("#removeHead", () => {
test("removes the head", () => {
const ll = LinkedList.fromValues(10, 20, 30);
ll.removeHead();
expect(ll.head.value).toBe(20);
expect(ll.length).toBe(2);
});
});
describe("#removeAtIndex", () => {
describe("with index less than 0", () => {
test("it does not delete any thing", () => {
const ll = LinkedList.fromValues(10, 20);
ll.removeAtIndex(-1);
expect(ll.length).toBe(2);
});
});
describe("with index greater than list length", () => {
test("it does not remove any thing", () => {
const ll = LinkedList.fromValues(10, 20);
ll.removeAtIndex(5);
expect(ll.length).toBe(2);
});
});
describe("with index 0", () => {
test("it removes the head", () => {
const ll = LinkedList.fromValues(10, 20, 30);
ll.removeAtIndex(0);
expect(ll.length).toBe(2);
expect(ll.head.value).toBe(20);
expect(ll.head.next.value).toBe(30);
});
});
describe("with index in the middle", () => {
test("it remove at the given index", () => {
const ll = LinkedList.fromValues(10, 20, 30, 40, 50);
ll.removeAtIndex(2);
const node = ll.getByIndex(2);
expect(ll.length).toBe(4);
expect(node.value).toBe(40);
expect(node.next.value).toBe(50);
});
});
});