1 | import DoubleLinkListCycle from "../DoubleLinkListCycle";
|
2 |
|
3 | describe("DoubleLinkListCycle Gen test", () => {
|
4 | test("should create an empty linkDoubleCycle", () => {
|
5 | const linkDoubleCycle = new DoubleLinkListCycle();
|
6 | expect(linkDoubleCycle).toBeDefined();
|
7 | expect(linkDoubleCycle.isEmpty()).toBe(true);
|
8 | });
|
9 | test("should append items to the linkDoubleCycle", () => {
|
10 | const linkDoubleCycle = new DoubleLinkListCycle();
|
11 | linkDoubleCycle.append(1);
|
12 | const headNode = linkDoubleCycle.getHeadNode();
|
13 | const tailNode = linkDoubleCycle.getTailNode();
|
14 | expect(linkDoubleCycle.isEmpty()).toBe(false);
|
15 | expect(headNode).toBe(tailNode);
|
16 |
|
17 | linkDoubleCycle.append(5);
|
18 | const headNodeTwo = linkDoubleCycle.getHeadNode();
|
19 | const tailNodeTwo = linkDoubleCycle.getTailNode();
|
20 | expect(tailNodeTwo.value).toEqual(5);
|
21 | expect(tailNodeTwo.Prev).toBe(headNodeTwo);
|
22 | expect(headNodeTwo.Next.value).toEqual(5);
|
23 | });
|
24 | test("should prepend items in linkDoubleCycle", () => {
|
25 | const linkDoubleCycle = new DoubleLinkListCycle();
|
26 | linkDoubleCycle.prepend(1);
|
27 | const headNode = linkDoubleCycle.getHeadNode();
|
28 | const tailNode = linkDoubleCycle.getTailNode();
|
29 | expect(linkDoubleCycle.isEmpty()).toBe(false);
|
30 | expect(headNode).toBe(tailNode);
|
31 | expect(headNode.Prev).toBe(tailNode);
|
32 | expect(tailNode.Next).toBe(headNode);
|
33 |
|
34 | linkDoubleCycle.prepend(5);
|
35 | const headNodeTwo = linkDoubleCycle.getHeadNode();
|
36 | const tailNodeTwo = linkDoubleCycle.getTailNode();
|
37 | expect(headNodeTwo.value).toEqual(5);
|
38 | expect(headNodeTwo.Prev).toBe(tailNodeTwo);
|
39 | expect(tailNodeTwo.Next).toBe(headNodeTwo);
|
40 | });
|
41 | test("should shift items in linkDoubleCycle", () => {
|
42 | const linkDoubleCycle = new DoubleLinkListCycle();
|
43 | linkDoubleCycle.shift();
|
44 | expect(linkDoubleCycle.getHeadNode()).toEqual(null);
|
45 | linkDoubleCycle.append(1);
|
46 | linkDoubleCycle.append(5);
|
47 | linkDoubleCycle.shift();
|
48 | expect(linkDoubleCycle.getHeadNode().value).toEqual(5);
|
49 | linkDoubleCycle.shift();
|
50 | expect(linkDoubleCycle.getHeadNode()).toEqual(null);
|
51 | expect(linkDoubleCycle.getTailNode()).toEqual(null);
|
52 | });
|
53 | test("should pop items in linkDoubleCycle", () => {
|
54 | const linkDoubleCycle = new DoubleLinkListCycle();
|
55 | linkDoubleCycle.pop();
|
56 | expect(linkDoubleCycle.getTailNode()).toEqual(null);
|
57 |
|
58 | linkDoubleCycle.append(1);
|
59 | const data = linkDoubleCycle.pop();
|
60 | expect(data.value).toEqual(1);
|
61 | linkDoubleCycle.append(1);
|
62 | linkDoubleCycle.append(5);
|
63 |
|
64 | linkDoubleCycle.pop();
|
65 | expect(linkDoubleCycle.getTailNode().value).toEqual(1);
|
66 |
|
67 | linkDoubleCycle.pop();
|
68 | expect(linkDoubleCycle.getHeadNode()).toEqual(null);
|
69 | expect(linkDoubleCycle.getTailNode()).toEqual(null);
|
70 | });
|
71 | test("should deleteNode items in linkDoubleCycle", () => {
|
72 | const linkDoubleCycle = new DoubleLinkListCycle();
|
73 | linkDoubleCycle.deleteNode(5);
|
74 | linkDoubleCycle.append(1);
|
75 | linkDoubleCycle.append(5);
|
76 | linkDoubleCycle.append(7);
|
77 | linkDoubleCycle.append(1);
|
78 | linkDoubleCycle.deleteNode((item: any) => item === 5);
|
79 | expect(linkDoubleCycle.toString()).toEqual("1,7,1");
|
80 | linkDoubleCycle.deleteNode(1);
|
81 | expect(linkDoubleCycle.toString()).toEqual("7");
|
82 | linkDoubleCycle.deleteNode(7);
|
83 | expect(linkDoubleCycle.getHeadNode()).toBe(null);
|
84 | });
|
85 | test("should findNode items in linkDoubleCycle", () => {
|
86 | const linkDoubleCycle = new DoubleLinkListCycle();
|
87 | linkDoubleCycle.append(1);
|
88 | linkDoubleCycle.append(7);
|
89 | linkDoubleCycle.append(1);
|
90 | linkDoubleCycle.append(5);
|
91 | const res = linkDoubleCycle.findNode(2);
|
92 | expect(res).toBe(null);
|
93 | const findRes = linkDoubleCycle.findNode(item => item === 1);
|
94 | expect(findRes.Next.value).toEqual(7);
|
95 |
|
96 | const findTwoRes = linkDoubleCycle.findNode(5);
|
97 | expect(findTwoRes.Next).toBe(linkDoubleCycle.getHeadNode());
|
98 | });
|
99 | test("should isEmpty items in linkDoubleCycle", () => {
|
100 | const linkDoubleCycle = new DoubleLinkListCycle();
|
101 | expect(linkDoubleCycle.isEmpty()).toBe(true);
|
102 | linkDoubleCycle.append(1);
|
103 | expect(linkDoubleCycle.isEmpty()).toBe(false);
|
104 | });
|
105 | test("should getEnumerator items in linkDoubleCycle", () => {
|
106 | const linkDoubleList = new DoubleLinkListCycle<number>();
|
107 | linkDoubleList.append(1);
|
108 | linkDoubleList.append(3);
|
109 | const iterator = linkDoubleList.getEnumerator();
|
110 | const arr: Array<number> = [];
|
111 | while (arr.length < 10){
|
112 | arr.push(iterator.Current.value);
|
113 | iterator.next();
|
114 | }
|
115 | });
|
116 | });
|