linked-list-typed 1.34.4 → 1.34.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/coverage/clover.xml +7 -3
- package/coverage/coverage-final.json +2 -1
- package/coverage/coverage-summary.json +2 -1
- package/coverage/lcov-report/index.html +25 -10
- package/coverage/lcov-report/index.ts.html +109 -0
- package/coverage/lcov.info +18 -0
- package/package.json +2 -2
- package/test/index.test.ts +429 -3
package/coverage/clover.xml
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<coverage generated="
|
|
3
|
-
<project timestamp="
|
|
4
|
-
<metrics statements="
|
|
2
|
+
<coverage generated="1696760575194" clover="3.2.0">
|
|
3
|
+
<project timestamp="1696760575194" name="All files">
|
|
4
|
+
<metrics statements="1" coveredstatements="1" conditionals="0" coveredconditionals="0" methods="4" coveredmethods="1" elements="5" coveredelements="2" complexity="0" loc="1" ncloc="1" packages="1" files="1" classes="1"/>
|
|
5
|
+
<file name="index.ts" path="/Users/revone/projects/data-structure-typed-individuals/linked-list-typed/src/index.ts">
|
|
6
|
+
<metrics statements="1" coveredstatements="1" conditionals="0" coveredconditionals="0" methods="4" coveredmethods="1"/>
|
|
7
|
+
<line num="8" count="88" type="stmt"/>
|
|
8
|
+
</file>
|
|
5
9
|
</project>
|
|
6
10
|
</coverage>
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
{}
|
|
1
|
+
{"/Users/revone/projects/data-structure-typed-individuals/linked-list-typed/src/index.ts": {"path":"/Users/revone/projects/data-structure-typed-individuals/linked-list-typed/src/index.ts","statementMap":{"0":{"start":{"line":8,"column":0},"end":{"line":8,"column":9}},"1":{"start":{"line":8,"column":9},"end":{"line":8,"column":31}},"2":{"start":{"line":8,"column":31},"end":{"line":8,"column":49}},"3":{"start":{"line":8,"column":49},"end":{"line":8,"column":71}},"4":{"start":{"line":8,"column":71},"end":{"line":8,"column":118}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":8,"column":9},"end":{"line":8,"column":29}},"loc":{"start":{"line":8,"column":9},"end":{"line":8,"column":31}}},"1":{"name":"(anonymous_1)","decl":{"start":{"line":8,"column":31},"end":{"line":8,"column":47}},"loc":{"start":{"line":8,"column":31},"end":{"line":8,"column":49}}},"2":{"name":"(anonymous_2)","decl":{"start":{"line":8,"column":49},"end":{"line":8,"column":69}},"loc":{"start":{"line":8,"column":49},"end":{"line":8,"column":71}}},"3":{"name":"(anonymous_3)","decl":{"start":{"line":8,"column":71},"end":{"line":8,"column":87}},"loc":{"start":{"line":8,"column":71},"end":{"line":8,"column":118}}}},"branchMap":{},"s":{"0":1,"1":1,"2":1,"3":1,"4":88},"f":{"0":0,"1":0,"2":0,"3":87},"b":{}}
|
|
2
|
+
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
{"total": {"lines":{"total":
|
|
1
|
+
{"total": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":5,"covered":5,"skipped":0,"pct":100},"functions":{"total":4,"covered":1,"skipped":0,"pct":25},"branches":{"total":0,"covered":0,"skipped":0,"pct":100},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"}}
|
|
2
|
+
,"/Users/revone/projects/data-structure-typed-individuals/linked-list-typed/src/index.ts": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":4,"covered":1,"skipped":0,"pct":25},"statements":{"total":5,"covered":5,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
|
|
2
3
|
}
|
|
@@ -23,30 +23,30 @@
|
|
|
23
23
|
<div class='clearfix'>
|
|
24
24
|
|
|
25
25
|
<div class='fl pad1y space-right2'>
|
|
26
|
-
<span class="strong">
|
|
26
|
+
<span class="strong">100% </span>
|
|
27
27
|
<span class="quiet">Statements</span>
|
|
28
|
-
<span class='fraction'>
|
|
28
|
+
<span class='fraction'>5/5</span>
|
|
29
29
|
</div>
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
<div class='fl pad1y space-right2'>
|
|
33
|
-
<span class="strong">
|
|
33
|
+
<span class="strong">100% </span>
|
|
34
34
|
<span class="quiet">Branches</span>
|
|
35
35
|
<span class='fraction'>0/0</span>
|
|
36
36
|
</div>
|
|
37
37
|
|
|
38
38
|
|
|
39
39
|
<div class='fl pad1y space-right2'>
|
|
40
|
-
<span class="strong">
|
|
40
|
+
<span class="strong">25% </span>
|
|
41
41
|
<span class="quiet">Functions</span>
|
|
42
|
-
<span class='fraction'>
|
|
42
|
+
<span class='fraction'>1/4</span>
|
|
43
43
|
</div>
|
|
44
44
|
|
|
45
45
|
|
|
46
46
|
<div class='fl pad1y space-right2'>
|
|
47
|
-
<span class="strong">
|
|
47
|
+
<span class="strong">100% </span>
|
|
48
48
|
<span class="quiet">Lines</span>
|
|
49
|
-
<span class='fraction'>
|
|
49
|
+
<span class='fraction'>1/1</span>
|
|
50
50
|
</div>
|
|
51
51
|
|
|
52
52
|
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
</div>
|
|
62
62
|
</template>
|
|
63
63
|
</div>
|
|
64
|
-
<div class='status-line
|
|
64
|
+
<div class='status-line high'></div>
|
|
65
65
|
<div class="pad1">
|
|
66
66
|
<table class="coverage-summary">
|
|
67
67
|
<thead>
|
|
@@ -78,7 +78,22 @@
|
|
|
78
78
|
<th data-col="lines_raw" data-type="number" data-fmt="html" class="abs"></th>
|
|
79
79
|
</tr>
|
|
80
80
|
</thead>
|
|
81
|
-
<tbody
|
|
81
|
+
<tbody><tr>
|
|
82
|
+
<td class="file high" data-value="index.ts"><a href="index.ts.html">index.ts</a></td>
|
|
83
|
+
<td data-value="100" class="pic high">
|
|
84
|
+
<div class="chart"><div class="cover-fill cover-full" style="width: 100%"></div><div class="cover-empty" style="width: 0%"></div></div>
|
|
85
|
+
</td>
|
|
86
|
+
<td data-value="100" class="pct high">100%</td>
|
|
87
|
+
<td data-value="5" class="abs high">5/5</td>
|
|
88
|
+
<td data-value="100" class="pct high">100%</td>
|
|
89
|
+
<td data-value="0" class="abs high">0/0</td>
|
|
90
|
+
<td data-value="25" class="pct low">25%</td>
|
|
91
|
+
<td data-value="4" class="abs low">1/4</td>
|
|
92
|
+
<td data-value="100" class="pct high">100%</td>
|
|
93
|
+
<td data-value="1" class="abs high">1/1</td>
|
|
94
|
+
</tr>
|
|
95
|
+
|
|
96
|
+
</tbody>
|
|
82
97
|
</table>
|
|
83
98
|
</div>
|
|
84
99
|
<div class='push'></div><!-- for sticky footer -->
|
|
@@ -86,7 +101,7 @@
|
|
|
86
101
|
<div class='footer quiet pad2 space-top1 center small'>
|
|
87
102
|
Code coverage generated by
|
|
88
103
|
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
|
|
89
|
-
at 2023-10-
|
|
104
|
+
at 2023-10-08T10:22:55.211Z
|
|
90
105
|
</div>
|
|
91
106
|
<script src="prettify.js"></script>
|
|
92
107
|
<script>
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
|
|
2
|
+
<!doctype html>
|
|
3
|
+
<html lang="en">
|
|
4
|
+
|
|
5
|
+
<head>
|
|
6
|
+
<title>Code coverage report for index.ts</title>
|
|
7
|
+
<meta charset="utf-8" />
|
|
8
|
+
<link rel="stylesheet" href="prettify.css" />
|
|
9
|
+
<link rel="stylesheet" href="base.css" />
|
|
10
|
+
<link rel="shortcut icon" type="image/x-icon" href="favicon.png" />
|
|
11
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
12
|
+
<style type='text/css'>
|
|
13
|
+
.coverage-summary .sorter {
|
|
14
|
+
background-image: url(sort-arrow-sprite.png);
|
|
15
|
+
}
|
|
16
|
+
</style>
|
|
17
|
+
</head>
|
|
18
|
+
|
|
19
|
+
<body>
|
|
20
|
+
<div class='wrapper'>
|
|
21
|
+
<div class='pad1'>
|
|
22
|
+
<h1><a href="index.html">All files</a> index.ts</h1>
|
|
23
|
+
<div class='clearfix'>
|
|
24
|
+
|
|
25
|
+
<div class='fl pad1y space-right2'>
|
|
26
|
+
<span class="strong">100% </span>
|
|
27
|
+
<span class="quiet">Statements</span>
|
|
28
|
+
<span class='fraction'>5/5</span>
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
<div class='fl pad1y space-right2'>
|
|
33
|
+
<span class="strong">100% </span>
|
|
34
|
+
<span class="quiet">Branches</span>
|
|
35
|
+
<span class='fraction'>0/0</span>
|
|
36
|
+
</div>
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
<div class='fl pad1y space-right2'>
|
|
40
|
+
<span class="strong">25% </span>
|
|
41
|
+
<span class="quiet">Functions</span>
|
|
42
|
+
<span class='fraction'>1/4</span>
|
|
43
|
+
</div>
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
<div class='fl pad1y space-right2'>
|
|
47
|
+
<span class="strong">100% </span>
|
|
48
|
+
<span class="quiet">Lines</span>
|
|
49
|
+
<span class='fraction'>1/1</span>
|
|
50
|
+
</div>
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
</div>
|
|
54
|
+
<p class="quiet">
|
|
55
|
+
Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for the previous block.
|
|
56
|
+
</p>
|
|
57
|
+
<template id="filterTemplate">
|
|
58
|
+
<div class="quiet">
|
|
59
|
+
Filter:
|
|
60
|
+
<input oninput="onInput()" type="search" id="fileSearch">
|
|
61
|
+
</div>
|
|
62
|
+
</template>
|
|
63
|
+
</div>
|
|
64
|
+
<div class='status-line high'></div>
|
|
65
|
+
<pre><table class="coverage">
|
|
66
|
+
<tr><td class="line-count quiet"><a name='L1'></a><a href='#L1'>1</a>
|
|
67
|
+
<a name='L2'></a><a href='#L2'>2</a>
|
|
68
|
+
<a name='L3'></a><a href='#L3'>3</a>
|
|
69
|
+
<a name='L4'></a><a href='#L4'>4</a>
|
|
70
|
+
<a name='L5'></a><a href='#L5'>5</a>
|
|
71
|
+
<a name='L6'></a><a href='#L6'>6</a>
|
|
72
|
+
<a name='L7'></a><a href='#L7'>7</a>
|
|
73
|
+
<a name='L8'></a><a href='#L8'>8</a>
|
|
74
|
+
<a name='L9'></a><a href='#L9'>9</a></td><td class="line-coverage quiet"><span class="cline-any cline-neutral"> </span>
|
|
75
|
+
<span class="cline-any cline-neutral"> </span>
|
|
76
|
+
<span class="cline-any cline-neutral"> </span>
|
|
77
|
+
<span class="cline-any cline-neutral"> </span>
|
|
78
|
+
<span class="cline-any cline-neutral"> </span>
|
|
79
|
+
<span class="cline-any cline-neutral"> </span>
|
|
80
|
+
<span class="cline-any cline-neutral"> </span>
|
|
81
|
+
<span class="cline-any cline-yes">88x</span>
|
|
82
|
+
<span class="cline-any cline-neutral"> </span></td><td class="text"><pre class="prettyprint lang-js">/**
|
|
83
|
+
* data-structure-typed
|
|
84
|
+
*
|
|
85
|
+
* @author Tyler Zeng
|
|
86
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
87
|
+
* @license MIT License
|
|
88
|
+
*/
|
|
89
|
+
export { <span class="fstat-no" title="function not covered" >DoublyLinkedListNode,</span> <span class="fstat-no" title="function not covered" >DoublyLinkedList,</span> <span class="fstat-no" title="function not covered" >SinglyLinkedListNode,</span> SinglyLinkedList } from 'data-structure-typed';
|
|
90
|
+
</pre></td></tr></table></pre>
|
|
91
|
+
|
|
92
|
+
<div class='push'></div><!-- for sticky footer -->
|
|
93
|
+
</div><!-- /wrapper -->
|
|
94
|
+
<div class='footer quiet pad2 space-top1 center small'>
|
|
95
|
+
Code coverage generated by
|
|
96
|
+
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
|
|
97
|
+
at 2023-10-08T10:22:55.211Z
|
|
98
|
+
</div>
|
|
99
|
+
<script src="prettify.js"></script>
|
|
100
|
+
<script>
|
|
101
|
+
window.onload = function () {
|
|
102
|
+
prettyPrint();
|
|
103
|
+
};
|
|
104
|
+
</script>
|
|
105
|
+
<script src="sorter.js"></script>
|
|
106
|
+
<script src="block-navigation.js"></script>
|
|
107
|
+
</body>
|
|
108
|
+
</html>
|
|
109
|
+
|
package/coverage/lcov.info
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
TN:
|
|
2
|
+
SF:src/index.ts
|
|
3
|
+
FN:8,(anonymous_0)
|
|
4
|
+
FN:8,(anonymous_1)
|
|
5
|
+
FN:8,(anonymous_2)
|
|
6
|
+
FN:8,(anonymous_3)
|
|
7
|
+
FNF:4
|
|
8
|
+
FNH:1
|
|
9
|
+
FNDA:0,(anonymous_0)
|
|
10
|
+
FNDA:0,(anonymous_1)
|
|
11
|
+
FNDA:0,(anonymous_2)
|
|
12
|
+
FNDA:87,(anonymous_3)
|
|
13
|
+
DA:8,88
|
|
14
|
+
LF:1
|
|
15
|
+
LH:1
|
|
16
|
+
BRF:0
|
|
17
|
+
BRH:0
|
|
18
|
+
end_of_record
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linked-list-typed",
|
|
3
|
-
"version": "1.34.
|
|
3
|
+
"version": "1.34.6",
|
|
4
4
|
"description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"typescript": "^4.9.5"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"data-structure-typed": "^1.34.
|
|
69
|
+
"data-structure-typed": "^1.34.6"
|
|
70
70
|
}
|
|
71
71
|
}
|
package/test/index.test.ts
CHANGED
|
@@ -1,5 +1,431 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {SinglyLinkedList} from '../src';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
describe('SinglyLinkedList Operation Test', () => {
|
|
5
|
+
let list: SinglyLinkedList<number>;
|
|
6
|
+
let objectList: SinglyLinkedList<{ keyA: number }>;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
list = new SinglyLinkedList<number>();
|
|
9
|
+
objectList = new SinglyLinkedList<{ keyA: number }>();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('push', () => {
|
|
13
|
+
it('should add elements to the end of the list', () => {
|
|
14
|
+
list.push(1);
|
|
15
|
+
list.push(2);
|
|
16
|
+
expect(list.toArray()).toEqual([1, 2]);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('pop', () => {
|
|
21
|
+
it('should remove and return the last element of the list', () => {
|
|
22
|
+
list.push(1);
|
|
23
|
+
list.push(2);
|
|
24
|
+
const popped = list.pop();
|
|
25
|
+
expect(popped).toBe(2);
|
|
26
|
+
expect(list.toArray()).toEqual([1]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should return undefined if the list is empty', () => {
|
|
30
|
+
const popped = list.pop();
|
|
31
|
+
expect(popped).toBeUndefined();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('shift', () => {
|
|
36
|
+
it('should remove and return the first element of the list', () => {
|
|
37
|
+
list.push(1);
|
|
38
|
+
list.push(2);
|
|
39
|
+
const shifted = list.shift();
|
|
40
|
+
expect(shifted).toBe(1);
|
|
41
|
+
expect(list.toArray()).toEqual([2]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return undefined if the list is empty', () => {
|
|
45
|
+
const shifted = list.shift();
|
|
46
|
+
expect(shifted).toBeUndefined();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('unshift', () => {
|
|
51
|
+
it('should add elements to the beginning of the list', () => {
|
|
52
|
+
list.unshift(1);
|
|
53
|
+
list.unshift(2);
|
|
54
|
+
expect(list.toArray()).toEqual([2, 1]);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('get', () => {
|
|
59
|
+
it('should return the element at the specified index', () => {
|
|
60
|
+
list.push(1);
|
|
61
|
+
list.push(2);
|
|
62
|
+
list.push(3);
|
|
63
|
+
const element = list.getAt(1);
|
|
64
|
+
expect(element).toBe(2);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should return undefined for an out-of-bounds index', () => {
|
|
68
|
+
list.push(1);
|
|
69
|
+
const element = list.getAt(1);
|
|
70
|
+
expect(element).toBeUndefined();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('insertAfter', () => {
|
|
75
|
+
it('should insert an element after an existing value', () => {
|
|
76
|
+
list.push(1);
|
|
77
|
+
list.push(2);
|
|
78
|
+
list.push(3);
|
|
79
|
+
list.insertAfter(2, 4);
|
|
80
|
+
expect(list.toArray()).toEqual([1, 2, 4, 3]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should return false if the existing value is not found', () => {
|
|
84
|
+
list.push(1);
|
|
85
|
+
list.push(2);
|
|
86
|
+
list.push(3);
|
|
87
|
+
const result = list.insertAfter(5, 4);
|
|
88
|
+
expect(result).toBe(false);
|
|
89
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('countOccurrences', () => {
|
|
94
|
+
it('should count occurrences of a value in the list', () => {
|
|
95
|
+
list.push(1);
|
|
96
|
+
list.push(2);
|
|
97
|
+
list.push(2);
|
|
98
|
+
list.push(3);
|
|
99
|
+
const count = list.countOccurrences(2);
|
|
100
|
+
expect(count).toBe(2);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should return 0 if the value is not found', () => {
|
|
104
|
+
list.push(1);
|
|
105
|
+
list.push(2);
|
|
106
|
+
const count = list.countOccurrences(3);
|
|
107
|
+
expect(count).toBe(0);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('removeValue', () => {
|
|
112
|
+
it('should remove the first occurrence of a value from the list', () => {
|
|
113
|
+
list.push(1);
|
|
114
|
+
list.push(2);
|
|
115
|
+
list.push(3);
|
|
116
|
+
const removed = list.delete(2);
|
|
117
|
+
expect(removed).toBe(true);
|
|
118
|
+
expect(list.toArray()).toEqual([1, 3]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should return false if the value is not found', () => {
|
|
122
|
+
list.push(1);
|
|
123
|
+
list.push(2);
|
|
124
|
+
list.push(3);
|
|
125
|
+
const removed = list.delete(4);
|
|
126
|
+
expect(removed).toBe(false);
|
|
127
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('isEmpty', () => {
|
|
132
|
+
it('should return true for an empty list', () => {
|
|
133
|
+
expect(list.isEmpty()).toBe(true);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should return false for a non-empty list', () => {
|
|
137
|
+
list.push(1);
|
|
138
|
+
expect(list.isEmpty()).toBe(false);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe('clear', () => {
|
|
143
|
+
it('should clear all elements from the list', () => {
|
|
144
|
+
list.push(1);
|
|
145
|
+
list.push(2);
|
|
146
|
+
list.push(3);
|
|
147
|
+
list.clear();
|
|
148
|
+
expect(list.toArray()).toEqual([]);
|
|
149
|
+
expect(list.length).toBe(0);
|
|
150
|
+
expect(list.isEmpty()).toBe(true);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe('reverse', () => {
|
|
155
|
+
it('should reverse the order of elements in the list', () => {
|
|
156
|
+
list.push(1);
|
|
157
|
+
list.push(2);
|
|
158
|
+
list.push(3);
|
|
159
|
+
list.reverse();
|
|
160
|
+
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should handle an empty list', () => {
|
|
164
|
+
list.reverse();
|
|
165
|
+
expect(list.toArray()).toEqual([]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('should handle a list with a single element', () => {
|
|
169
|
+
list.push(1);
|
|
170
|
+
list.reverse();
|
|
171
|
+
expect(list.toArray()).toEqual([1]);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('indexOf', () => {
|
|
176
|
+
it('should return the index of the first occurrence of a value', () => {
|
|
177
|
+
list.push(1);
|
|
178
|
+
list.push(2);
|
|
179
|
+
list.push(3);
|
|
180
|
+
const index = list.indexOf(2);
|
|
181
|
+
expect(index).toBe(1);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should return -1 if the value is not found', () => {
|
|
185
|
+
list.push(1);
|
|
186
|
+
list.push(2);
|
|
187
|
+
list.push(3);
|
|
188
|
+
const index = list.indexOf(4);
|
|
189
|
+
expect(index).toBe(-1);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe('toArray', () => {
|
|
194
|
+
it('should convert the list to an array', () => {
|
|
195
|
+
list.push(1);
|
|
196
|
+
list.push(2);
|
|
197
|
+
list.push(3);
|
|
198
|
+
const array = list.toArray();
|
|
199
|
+
expect(array).toEqual([1, 2, 3]);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('should return an empty array for an empty list', () => {
|
|
203
|
+
const array = list.toArray();
|
|
204
|
+
expect(array).toEqual([]);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe('insertBefore', () => {
|
|
209
|
+
it('should insert an element before an existing value', () => {
|
|
210
|
+
list.push(1);
|
|
211
|
+
list.push(2);
|
|
212
|
+
list.push(3);
|
|
213
|
+
list.insertBefore(2, 4);
|
|
214
|
+
expect(list.toArray()).toEqual([1, 4, 2, 3]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should insert an element at the beginning', () => {
|
|
218
|
+
list.push(1);
|
|
219
|
+
list.push(2);
|
|
220
|
+
list.insertBefore(1, 3);
|
|
221
|
+
expect(list.toArray()).toEqual([3, 1, 2]);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should return false if the existing value is not found', () => {
|
|
225
|
+
list.push(1);
|
|
226
|
+
list.push(2);
|
|
227
|
+
list.push(3);
|
|
228
|
+
const result = list.insertBefore(5, 4);
|
|
229
|
+
expect(result).toBe(false);
|
|
230
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe('getLength', () => {
|
|
235
|
+
it('should return the correct length of the list', () => {
|
|
236
|
+
expect(list.length).toBe(0);
|
|
237
|
+
list.push(1);
|
|
238
|
+
list.push(2);
|
|
239
|
+
expect(list.length).toBe(2);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe('remove', () => {
|
|
244
|
+
it('should remove and return the element at the specified index', () => {
|
|
245
|
+
list.push(1);
|
|
246
|
+
list.push(2);
|
|
247
|
+
list.push(3);
|
|
248
|
+
const removed = list.deleteAt(1);
|
|
249
|
+
expect(removed).toBe(2);
|
|
250
|
+
expect(list.toArray()).toEqual([1, 3]);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('should return undefined for an out-of-bounds index', () => {
|
|
254
|
+
list.push(1);
|
|
255
|
+
const removed = list.deleteAt(1);
|
|
256
|
+
expect(removed).toBeUndefined();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('should remove and return the first element', () => {
|
|
260
|
+
list.push(1);
|
|
261
|
+
list.push(2);
|
|
262
|
+
const removed = list.deleteAt(0);
|
|
263
|
+
expect(removed).toBe(1);
|
|
264
|
+
expect(list.toArray()).toEqual([2]);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('should remove and return the last element', () => {
|
|
268
|
+
list.push(1);
|
|
269
|
+
list.push(2);
|
|
270
|
+
const removed = list.deleteAt(1);
|
|
271
|
+
expect(removed).toBe(2);
|
|
272
|
+
expect(list.toArray()).toEqual([1]);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
describe('push and pop', () => {
|
|
277
|
+
it('should push and pop elements correctly', () => {
|
|
278
|
+
list.push(1);
|
|
279
|
+
list.push(2);
|
|
280
|
+
expect(list.pop()).toBe(2);
|
|
281
|
+
expect(list.pop()).toBe(1);
|
|
282
|
+
expect(list.pop()).toBeUndefined();
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
describe('shift and unshift', () => {
|
|
287
|
+
it('should shift and unshift elements correctly', () => {
|
|
288
|
+
list.unshift(1);
|
|
289
|
+
list.unshift(2);
|
|
290
|
+
expect(list.shift()).toBe(2);
|
|
291
|
+
expect(list.shift()).toBe(1);
|
|
292
|
+
expect(list.shift()).toBeUndefined();
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
describe('insert and toArray', () => {
|
|
297
|
+
it('should insert elements and return array correctly', () => {
|
|
298
|
+
list.insertAt(0, 1);
|
|
299
|
+
list.insertAt(1, 3);
|
|
300
|
+
list.insertAt(1, 2);
|
|
301
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
describe('find', () => {
|
|
306
|
+
it('should find elements using a callback function', () => {
|
|
307
|
+
list.push(1);
|
|
308
|
+
list.push(2);
|
|
309
|
+
list.push(3);
|
|
310
|
+
const result = list.find(data => data % 2 === 0);
|
|
311
|
+
expect(result).toBe(2);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('should return undefined if element is not found', () => {
|
|
315
|
+
list.push(1);
|
|
316
|
+
list.push(3);
|
|
317
|
+
const result = list.find(data => data % 2 === 0);
|
|
318
|
+
expect(result).toBeNull();
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe('reverse', () => {
|
|
323
|
+
it('should reverse the order of elements', () => {
|
|
324
|
+
list.push(1);
|
|
325
|
+
list.push(2);
|
|
326
|
+
list.push(3);
|
|
327
|
+
list.reverse();
|
|
328
|
+
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
describe('countOccurrences', () => {
|
|
333
|
+
it('should count occurrences of a value', () => {
|
|
334
|
+
list.push(1);
|
|
335
|
+
list.push(2);
|
|
336
|
+
list.push(2);
|
|
337
|
+
list.push(3);
|
|
338
|
+
const count = list.countOccurrences(2);
|
|
339
|
+
expect(count).toBe(2);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('should return 0 if value is not found', () => {
|
|
343
|
+
list.push(1);
|
|
344
|
+
list.push(2);
|
|
345
|
+
const count = list.countOccurrences(3);
|
|
346
|
+
expect(count).toBe(0);
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('should insert and manipulate objects with numeric properties', () => {
|
|
351
|
+
const obj1 = {keyA: 1};
|
|
352
|
+
const obj2 = {keyA: 2};
|
|
353
|
+
const obj3 = {keyA: 3};
|
|
354
|
+
|
|
355
|
+
objectList.push(obj1);
|
|
356
|
+
objectList.push(obj2);
|
|
357
|
+
objectList.push(obj3);
|
|
358
|
+
|
|
359
|
+
expect(objectList.toArray()).toEqual([obj1, obj2, obj3]);
|
|
360
|
+
|
|
361
|
+
const newObj = {keyA: 2.5}; // Corrected newObj value
|
|
362
|
+
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
363
|
+
expect(insertSuccess).toBe(true);
|
|
364
|
+
|
|
365
|
+
const findNode = objectList.findNode(newObj); // Use newObj instead of obj2
|
|
366
|
+
expect(findNode?.val).toEqual(newObj);
|
|
367
|
+
|
|
368
|
+
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
369
|
+
expect(deleted).toBe(true);
|
|
370
|
+
|
|
371
|
+
const poppedObj = objectList.pop();
|
|
372
|
+
expect(poppedObj).toBe(obj3);
|
|
373
|
+
|
|
374
|
+
const shiftedObj = objectList.shift();
|
|
375
|
+
expect(shiftedObj).toBe(obj1);
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
describe('SinglyLinkedList', () => {
|
|
380
|
+
let list: SinglyLinkedList<number>;
|
|
381
|
+
|
|
382
|
+
beforeEach(() => {
|
|
383
|
+
list = new SinglyLinkedList<number>();
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it('should initialize an empty list', () => {
|
|
387
|
+
expect(list.head).toBeNull();
|
|
388
|
+
expect(list.tail).toBeNull();
|
|
389
|
+
expect(list.length).toBe(0);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it('should push elements to the end of the list', () => {
|
|
393
|
+
list.push(1);
|
|
394
|
+
list.push(2);
|
|
395
|
+
expect(list.head!.val).toBe(1);
|
|
396
|
+
expect(list.tail!.val).toBe(2);
|
|
397
|
+
expect(list.length).toBe(2);
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it('should pop elements from the end of the list', () => {
|
|
401
|
+
list.push(1);
|
|
402
|
+
list.push(2);
|
|
403
|
+
const popped = list.pop();
|
|
404
|
+
expect(popped).toBe(2);
|
|
405
|
+
expect(list.head!.val).toBe(1);
|
|
406
|
+
expect(list.tail!.val).toBe(1);
|
|
407
|
+
expect(list.length).toBe(1);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
// Add more test cases for other methods like shift, unshift, getAt, deleteAt, and more.
|
|
411
|
+
|
|
412
|
+
it('should reverse the list', () => {
|
|
413
|
+
list.push(1);
|
|
414
|
+
list.push(2);
|
|
415
|
+
list.push(3);
|
|
416
|
+
list.reverse();
|
|
417
|
+
expect(list.head!.val).toBe(3);
|
|
418
|
+
expect(list.tail!.val).toBe(1);
|
|
419
|
+
// Add more assertions for reversed order.
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
// Add more test cases for other methods like find, indexOf, and more.
|
|
423
|
+
|
|
424
|
+
it('should convert the list to an array', () => {
|
|
425
|
+
list.push(1);
|
|
426
|
+
list.push(2);
|
|
427
|
+
list.push(3);
|
|
428
|
+
const array = list.toArray();
|
|
429
|
+
expect(array).toEqual([1, 2, 3]);
|
|
4
430
|
});
|
|
5
431
|
});
|