Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edf84c5

Browse files
Fixed a bug in the ArrayQueue iterator
1 parent b982482 commit edf84c5

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

‎src/com/jwetherell/algorithms/data_structures/Queue.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ public boolean hasNext() {
563563
public T next() {
564564
if (queue.firstIndex+index < queue.lastIndex) {
565565
last = queue.firstIndex+index;
566-
return queue.array[queue.firstIndex+index++];
566+
return queue.array[(queue.firstIndex + index++) % queue.array.length];
567567
}
568568
return null;
569569
}

‎test/com/jwetherell/algorithms/data_structures/test/QueueTests.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertTrue;
44

55
import java.util.Collection;
6+
import java.util.Iterator;
67

78
import org.junit.Test;
89

@@ -26,6 +27,17 @@ public void testArrayQueue() {
2627
data.unsorted, data.invalid));
2728
assertTrue(JavaCollectionTest.testCollection(aCollection, Integer.class, aName,
2829
data.unsorted, data.sorted, data.invalid));
30+
31+
// Specific test based on bug
32+
aQueue = new Queue.ArrayQueue<Integer>();
33+
for (int i = 0; i < 1024; i++) {
34+
aQueue.offer(i);
35+
}
36+
aQueue.poll();
37+
aQueue.offer(1024);
38+
Iterator it = aQueue.toQueue().iterator();
39+
while (it.hasNext())
40+
it.next();
2941
}
3042

3143
@Test
@@ -40,5 +52,15 @@ public void testLinkedQueue() {
4052
data.unsorted, data.invalid));
4153
assertTrue(JavaCollectionTest.testCollection(lCollection, Integer.class, lName,
4254
data.unsorted, data.sorted, data.invalid));
55+
56+
lQueue = new Queue.LinkedQueue<Integer>();
57+
for (int i = 0; i < 1024; i++) {
58+
lQueue.offer(i);
59+
}
60+
lQueue.poll();
61+
lQueue.offer(1024);
62+
Iterator it = lQueue.toQueue().iterator();
63+
while (it.hasNext())
64+
it.next();
4365
}
4466
}

‎test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ private static <T extends Comparable<T>> boolean addAndRemoveInOrder(Collection<
7878
return false;
7979
}
8080

81+
if (!IteratorTest.testIterator(collection.iterator())) {
82+
System.err.println(name+" addAndRemoveInOrder iterator failed.");
83+
Utils.handleError(data,collection);
84+
return false;
85+
}
86+
8187
for (int i = 0; i < data.length; i++) {
8288
Integer value = data[i];
8389
T item = Utils.parseT(value, type);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /