OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #library("QueueTest.dart"); | |
6 #import("dart:coreimpl"); | |
7 | |
8 class QueueTest { | |
9 | |
10 static testMain() { | |
11 Queue queue = new Queue(); | |
12 checkQueue(queue, 0, 0); | |
13 | |
14 queue.addFirst(1); | |
15 checkQueue(queue, 1, 1); | |
16 | |
17 queue.addLast(10); | |
18 checkQueue(queue, 2, 11); | |
19 | |
20 Expect.equals(10, queue.removeLast()); | |
21 checkQueue(queue, 1, 1); | |
22 | |
23 queue.addLast(10); | |
24 Expect.equals(1, queue.removeFirst()); | |
25 checkQueue(queue, 1, 10); | |
26 | |
27 queue.addFirst(1); | |
28 queue.addLast(100); | |
29 queue.addLast(1000); | |
30 Expect.equals(1000, queue.removeLast()); | |
31 queue.addLast(1000); | |
32 checkQueue(queue, 4, 1111); | |
33 | |
34 queue.removeFirst(); | |
35 checkQueue(queue, 3, 1110); | |
36 | |
37 int mapTest(int value) { | |
38 return value ~/ 10; | |
39 } | |
40 | |
41 bool is10(int value) { | |
42 return (value == 10); | |
43 } | |
44 | |
45 Queue mapped = queue.map(mapTest); | |
46 checkQueue(mapped, 3, 111); | |
47 checkQueue(queue, 3, 1110); | |
48 Expect.equals(1, mapped.removeFirst()); | |
49 Expect.equals(100, mapped.removeLast()); | |
50 Expect.equals(10, mapped.removeFirst()); | |
51 | |
52 Queue other = queue.filter(is10); | |
53 checkQueue(other, 1, 10); | |
54 | |
55 Expect.equals(true, queue.some(is10)); | |
56 | |
57 bool isInstanceOfInt(int value) { | |
58 return (value is int); | |
59 } | |
60 | |
61 Expect.equals(true, queue.every(isInstanceOfInt)); | |
62 | |
63 Expect.equals(false, queue.every(is10)); | |
64 | |
65 bool is1(int value) { | |
66 return (value == 1); | |
67 } | |
68 Expect.equals(false, queue.some(is1)); | |
69 | |
70 queue.clear(); | |
71 Expect.equals(0, queue.length); | |
72 | |
73 var exception = null; | |
74 try { | |
75 queue.removeFirst(); | |
76 } catch (EmptyQueueException e) { | |
77 exception = e; | |
78 } | |
79 Expect.equals(true, exception != null); | |
80 Expect.equals(0, queue.length); | |
81 | |
82 exception = null; | |
83 try { | |
84 queue.removeLast(); | |
85 } catch (EmptyQueueException e) { | |
86 exception = e; | |
87 } | |
88 Expect.equals(true, exception != null); | |
89 Expect.equals(0, queue.length); | |
90 | |
91 queue.addFirst(1); | |
92 queue.addFirst(2); | |
93 Expect.equals(2, queue.first()); | |
94 Expect.equals(1, queue.last()); | |
95 | |
96 queue.addLast(3); | |
97 Expect.equals(3, queue.last()); | |
98 bool isGreaterThanOne(int value) { | |
99 return (value > 1); | |
100 } | |
101 | |
102 other = queue.filter(isGreaterThanOne); | |
103 checkQueue(other, 2, 5); | |
104 | |
105 testAddAll(); | |
106 } | |
107 | |
108 static void checkQueue(Queue queue, int expectedSize, int expectedSum) { | |
109 Expect.equals(expectedSize, queue.length); | |
110 int sum = 0; | |
111 void sumElements(int value) { | |
112 sum += value; | |
113 } | |
114 queue.forEach(sumElements); | |
115 Expect.equals(expectedSum, sum); | |
116 } | |
117 | |
118 static testAddAll() { | |
119 Set<int> set = new Set<int>.from([1, 2, 4]); | |
120 | |
121 Queue<int> queue1 = new Queue<int>.from(set); | |
122 Queue<int> queue2 = new Queue<int>(); | |
123 Queue<int> queue3 = new Queue<int>(); | |
124 | |
125 queue2.addAll(set); | |
126 queue3.addAll(queue1); | |
127 | |
128 Expect.equals(3, set.length); | |
129 Expect.equals(3, queue1.length); | |
130 Expect.equals(3, queue2.length); | |
131 Expect.equals(3, queue3.length); | |
132 | |
133 int sum = 0; | |
134 void f(e) { sum += e; }; | |
135 | |
136 set.forEach(f); | |
137 Expect.equals(7, sum); | |
138 sum = 0; | |
139 | |
140 queue1.forEach(f); | |
141 Expect.equals(7, sum); | |
142 sum = 0; | |
143 | |
144 queue2.forEach(f); | |
145 Expect.equals(7, sum); | |
146 sum = 0; | |
147 | |
148 queue3.forEach(f); | |
149 Expect.equals(7, sum); | |
150 sum = 0; | |
151 | |
152 set = new Set<int>.from([]); | |
153 queue1 = new Queue<int>.from(set); | |
154 queue2 = new Queue<int>(); | |
155 queue3 = new Queue<int>(); | |
156 | |
157 queue2.addAll(set); | |
158 queue3.addAll(queue1); | |
159 | |
160 Expect.equals(0, set.length); | |
161 Expect.equals(0, queue1.length); | |
162 Expect.equals(0, queue2.length); | |
163 Expect.equals(0, queue3.length); | |
164 | |
165 testQueueElements(); | |
166 } | |
167 | |
168 static testQueueElements() { | |
169 DoubleLinkedQueue<int> queue1 = new DoubleLinkedQueue<int>.from([1, 2, 4]); | |
170 DoubleLinkedQueue<int> queue2 = new DoubleLinkedQueue<int>(); | |
171 queue2.addAll(queue1); | |
172 | |
173 Expect.equals(queue1.length, queue2.length); | |
174 DoubleLinkedQueueEntry<int> entry1 = queue1.firstEntry(); | |
175 DoubleLinkedQueueEntry<int> entry2 = queue2.firstEntry(); | |
176 while (entry1 != null) { | |
177 Expect.equals(true, entry1 !== entry2); | |
178 entry1 = entry1.nextEntry(); | |
179 entry2 = entry2.nextEntry(); | |
180 } | |
181 Expect.equals(null, entry2); | |
182 } | |
183 } | |
184 | |
185 main() { | |
186 QueueTest.testMain(); | |
187 } | |
OLD | NEW |