• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23
24// Flags: --expose-internals
25
26require('../common');
27const assert = require('assert');
28const L = require('internal/linkedlist');
29
30const list = { name: 'list' };
31const A = { name: 'A' };
32const B = { name: 'B' };
33const C = { name: 'C' };
34const D = { name: 'D' };
35
36
37L.init(list);
38L.init(A);
39L.init(B);
40L.init(C);
41L.init(D);
42
43assert.ok(L.isEmpty(list));
44assert.strictEqual(L.peek(list), null);
45
46L.append(list, A);
47// list -> A
48assert.strictEqual(L.peek(list), A);
49
50L.append(list, B);
51// list -> A -> B
52assert.strictEqual(L.peek(list), A);
53
54L.append(list, C);
55// list -> A -> B -> C
56assert.strictEqual(L.peek(list), A);
57
58L.append(list, D);
59// list -> A -> B -> C -> D
60assert.strictEqual(L.peek(list), A);
61
62L.remove(A);
63L.remove(B);
64// B is already removed, so removing it again shouldn't hurt.
65L.remove(B);
66// list -> C -> D
67assert.strictEqual(L.peek(list), C);
68
69// Put B back on the list
70L.append(list, B);
71// list -> C -> D -> B
72assert.strictEqual(L.peek(list), C);
73
74L.remove(C);
75// list -> D -> B
76assert.strictEqual(L.peek(list), D);
77
78L.remove(B);
79// list -> D
80assert.strictEqual(L.peek(list), D);
81
82L.remove(D);
83// list
84assert.strictEqual(L.peek(list), null);
85
86
87assert.ok(L.isEmpty(list));
88
89
90L.append(list, D);
91// list -> D
92assert.strictEqual(L.peek(list), D);
93
94L.append(list, C);
95L.append(list, B);
96L.append(list, A);
97// list -> D -> C -> B -> A
98
99// Append should REMOVE C from the list and append it to the end.
100L.append(list, C);
101// list -> D -> B -> A -> C
102
103assert.strictEqual(L.peek(list), D);
104assert.strictEqual(L.peek(D), B);
105assert.strictEqual(L.peek(B), A);
106assert.strictEqual(L.peek(A), C);
107assert.strictEqual(L.peek(C), list);
108