2 var Yallist = require('../yallist.js')
4 var y = new Yallist(1,2,3,4,5)
5 var z = new Yallist([1,2,3,4,5])
6 t.similar(y, z, 'build from single list or args')
11 t.similar(y.map(add10).toArray(), [11, 12, 13, 14, 15])
12 t.similar(y.mapReverse(add10).toArray(), [15, 14, 13, 12, 11])
14 t.similar(y.map(add10).toArrayReverse(), [15, 14, 13, 12, 11])
15 t.isa(Yallist(1,2,3), 'Yallist')
16 t.equal(y.push(6, 7, 8), 8)
17 t.similar(y.toArray(), [1, 2, 3, 4, 5, 6, 7, 8])
22 var expect = [100, 2, 3, 4, 5, 6, 7]
23 var expectReverse = [ 7, 6, 5, 4, 3, 2, 100 ]
25 t.similar(y.toArray(), expect)
26 t.equal(y.length, y.toArray().length)
28 t.test(function forEach (t) {
30 y.forEach(function (item, i, list) {
31 t.equal(item, expect[i])
36 t.test(function forEach (t) {
39 y.forEachReverse(function (item, i, list) {
40 t.equal(item, expectReverse[n])
41 t.equal(item, expect[i])
42 t.equal(item, y.get(i))
43 t.equal(item, y.getReverse(n))
49 t.equal(y.getReverse(100), undefined)
51 t.equal(y.get(9999), undefined)
54 function sum (a, b) { return a + b }
55 t.equal(y.reduce(sum), 127)
56 t.equal(y.reduce(sum, 100), 227)
57 t.equal(y.reduceReverse(sum), 127)
58 t.equal(y.reduceReverse(sum, 100), 227)
60 t.equal(Yallist().pop(), undefined)
61 t.equal(Yallist().shift(), undefined)
66 t.similar(x.toArray(), [1])
68 // verify that y.toArray() returns an array and if we create a
69 // new Yallist from that array, we get a list matching
70 t.similar(Yallist(y.toArray()), y)
71 t.similar(Yallist.apply(null, y.toArray()), y)
73 t.throws(function () {
74 new Yallist().reduce(function () {})
75 }, {}, new TypeError('Reduce of empty list with no initial value'))
76 t.throws(function () {
77 new Yallist().reduceReverse(function () {})
78 }, {}, new TypeError('Reduce of empty list with no initial value'))
82 t.similar(y.toArray(), expectReverse)
84 t.similar(y.toArray(), expect)
86 var a = Yallist(1,2,3,4,5,6)
91 [ [1, -2], [2, 3, 4] ],
93 [ [-5, -2], [2, 3, 4] ],
98 t.test('slice', function (t) {
100 cases.forEach(function (c) {
101 t.test(JSON.stringify(c), function (t) {
102 t.similar(a.slice.apply(a, c[0]), Yallist(c[1]))
103 t.similar([].slice.apply(a.toArray(), c[0]), c[1])
109 t.test('sliceReverse', function (t) {
111 cases.forEach(function (c) {
112 var rev = c[1].slice().reverse()
113 t.test(JSON.stringify([c[0], rev]), function (t) {
114 t.similar(a.sliceReverse.apply(a, c[0]), Yallist(rev))
115 t.similar([].slice.apply(a.toArray(), c[0]).reverse(), rev)
121 var inserter = Yallist(1,2,3,4,5)
122 inserter.unshiftNode(inserter.head.next)
123 t.similar(inserter.toArray(), [2,1,3,4,5])
124 inserter.unshiftNode(inserter.tail)
125 t.similar(inserter.toArray(), [5,2,1,3,4])
126 inserter.unshiftNode(inserter.head)
127 t.similar(inserter.toArray(), [5,2,1,3,4])
129 var single = Yallist(1)
130 single.unshiftNode(single.head)
131 t.similar(single.toArray(), [1])
133 inserter = Yallist(1,2,3,4,5)
134 inserter.pushNode(inserter.tail.prev)
135 t.similar(inserter.toArray(), [1,2,3,5,4])
136 inserter.pushNode(inserter.head)
137 t.similar(inserter.toArray(), [2,3,5,4,1])
138 inserter.unshiftNode(inserter.head)
139 t.similar(inserter.toArray(), [2,3,5,4,1])
142 single.pushNode(single.tail)
143 t.similar(single.toArray(), [1])
145 var swiped = Yallist(9,8,7)
146 inserter.unshiftNode(swiped.head.next)
147 t.similar(inserter.toArray(), [8,2,3,5,4,1])
148 t.similar(swiped.toArray(), [9,7])
150 swiped = Yallist(9,8,7)
151 inserter.pushNode(swiped.head.next)
152 t.similar(inserter.toArray(), [8,2,3,5,4,1,8])
153 t.similar(swiped.toArray(), [9,7])
155 swiped.unshiftNode(Yallist.Node(99))
156 t.similar(swiped.toArray(), [99,9,7])
157 swiped.pushNode(Yallist.Node(66))
158 t.similar(swiped.toArray(), [99,9,7,66])
161 e.unshiftNode(Yallist.Node(1))
162 t.same(e.toArray(), [1])
164 e.pushNode(Yallist.Node(1))
165 t.same(e.toArray(), [1])
167 // steal them back, don't break the lists
168 swiped.unshiftNode(inserter.head)
169 t.same(swiped, Yallist(8,99,9,7,66))
170 t.same(inserter, Yallist(2,3,5,4,1,8))
171 swiped.unshiftNode(inserter.tail)
172 t.same(inserter, Yallist(2,3,5,4,1))
173 t.same(swiped, Yallist(8,8,99,9,7,66))
176 t.throws(function remove_foreign_node () {
177 e.removeNode(swiped.head)
178 }, {}, new Error('removing node which does not belong to this list'))
179 t.throws(function remove_unlisted_node () {
180 e.removeNode(Yallist.Node('nope'))
181 }, {}, new Error('removing node which does not belong to this list'))
185 t.same(e, Yallist(2))
188 t.same(e, Yallist(1))