| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class QueueIteratorTest { | 5 class QueueIteratorTest { |
| 6 static testMain() { | 6 static testMain() { |
| 7 testSmallQueue(); | 7 testSmallQueue(); |
| 8 testLargeQueue(); | 8 testLargeQueue(); |
| 9 testEmptyQueue(); | 9 testEmptyQueue(); |
| 10 } | 10 } |
| 11 | 11 |
| 12 static void testThrows(Iterator<int> it) { | 12 static void testThrows(Iterator<int> it) { |
| 13 Expect.equals(false, it.hasNext); | 13 Expect.isFalse(it.moveNext()); |
| 14 var exception = null; | 14 Expect.throws(() => it.current, (e) => e is StateError); |
| 15 try { | |
| 16 it.next(); | |
| 17 } on StateError catch (e) { | |
| 18 exception = e; | |
| 19 } | |
| 20 Expect.equals(true, exception != null); | |
| 21 } | 15 } |
| 22 | 16 |
| 23 static int sum(int expected, Iterator<int> it) { | 17 static int sum(int expected, Iterator<int> it) { |
| 24 int count = 0; | 18 int count = 0; |
| 25 while (it.hasNext) { | 19 while (it.moveNext()) { |
| 26 count += it.next(); | 20 count += it.current; |
| 27 } | 21 } |
| 28 Expect.equals(expected, count); | 22 Expect.equals(expected, count); |
| 29 } | 23 } |
| 30 | 24 |
| 31 static void testSmallQueue() { | 25 static void testSmallQueue() { |
| 32 Queue<int> queue = new Queue<int>(); | 26 Queue<int> queue = new Queue<int>(); |
| 33 queue.addLast(1); | 27 queue.addLast(1); |
| 34 queue.addLast(2); | 28 queue.addLast(2); |
| 35 queue.addLast(3); | 29 queue.addLast(3); |
| 36 | 30 |
| 37 Iterator<int> it = queue.iterator(); | 31 Iterator<int> it = queue.iterator; |
| 38 Expect.equals(true, it.hasNext); | |
| 39 sum(6, it); | 32 sum(6, it); |
| 40 testThrows(it); | 33 testThrows(it); |
| 41 } | 34 } |
| 42 | 35 |
| 43 static void testLargeQueue() { | 36 static void testLargeQueue() { |
| 44 Queue<int> queue = new Queue<int>(); | 37 Queue<int> queue = new Queue<int>(); |
| 45 int count = 0; | 38 int count = 0; |
| 46 for (int i = 0; i < 100; i++) { | 39 for (int i = 0; i < 100; i++) { |
| 47 count += i; | 40 count += i; |
| 48 queue.addLast(i); | 41 queue.addLast(i); |
| 49 } | 42 } |
| 50 Iterator<int> it = queue.iterator(); | 43 Iterator<int> it = queue.iterator; |
| 51 Expect.equals(true, it.hasNext); | |
| 52 sum(count, it); | 44 sum(count, it); |
| 53 testThrows(it); | 45 testThrows(it); |
| 54 } | 46 } |
| 55 | 47 |
| 56 static void testEmptyQueue() { | 48 static void testEmptyQueue() { |
| 57 Queue<int> queue = new Queue<int>(); | 49 Queue<int> queue = new Queue<int>(); |
| 58 Iterator<int> it = queue.iterator(); | 50 Iterator<int> it = queue.iterator; |
| 59 Expect.equals(false, it.hasNext); | |
| 60 sum(0, it); | 51 sum(0, it); |
| 61 testThrows(it); | 52 testThrows(it); |
| 62 } | 53 } |
| 63 } | 54 } |
| 64 | 55 |
| 65 main() { | 56 main() { |
| 66 QueueIteratorTest.testMain(); | 57 QueueIteratorTest.testMain(); |
| 67 } | 58 } |
| OLD | NEW |