2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.util;
21 import gov.nasa.jpf.util.test.TestJPF;
22 import java.util.Iterator;
23 import java.util.NoSuchElementException;
24 import org.junit.Test;
29 public class ArrayObjectQueueTest extends TestJPF {
31 <E> void printLogicalOrder (ArrayObjectQueue<E> q){
33 System.out.print('{');
36 System.out.print(',');
41 System.out.println('}');
44 <E> void printPhysicalOrder (ArrayObjectQueue<E> q){
46 System.out.print('{');
47 for (Iterator<E> it = q.storageIterator(); it.hasNext(); ){
50 System.out.print(',');
55 System.out.println('}');
60 public void testBasic(){
61 ArrayObjectQueue<String> q = new ArrayObjectQueue<String>(4);
63 assertTrue( q.isEmpty());
64 assertTrue( q.size() == 0);
67 assertFalse( q.isEmpty());
68 assertTrue( q.size() == 1);
78 // now we need to grow
83 assertTrue( "1".equals( q.remove()));
84 assertTrue(q.size() == 4);
87 assertTrue( "2".equals( q.remove()));
88 assertTrue( "3".equals( q.remove()));
89 assertTrue( "4".equals( q.remove()));
90 assertTrue( "5".equals( q.remove()));
93 assertTrue(q.isEmpty());
97 fail("should never get here");
98 } catch (NoSuchElementException x){
104 public void testTailChasing(){
105 ArrayObjectQueue<String> q = new ArrayObjectQueue<String>(4);
111 q.remove(); // should remove "1"
112 q.add("5"); // should make use of the free space
114 printLogicalOrder(q);
115 assertTrue( q.getCurrentCapacity() == 4);
117 q.remove(); // should remove "2"
118 q.remove(); // should remove "3"
121 printLogicalOrder(q);
122 printPhysicalOrder(q);
124 String s = q.remove();
125 assertTrue( "4".equals(s));
126 // next remove should wrap around
127 assertTrue( "5".equals(q.remove()));
129 printLogicalOrder(q);
130 printPhysicalOrder(q);
135 public void testMidGrow(){
136 ArrayObjectQueue<String> q = new ArrayObjectQueue<String>(4);
143 q.add("5"); // that shouldn't grow yet
144 int len = q.getCurrentCapacity();
145 assertTrue( len == 4);
146 printPhysicalOrder(q);
148 q.add("6"); // that should grow
149 assertTrue(q.getCurrentCapacity() > len);
151 printLogicalOrder(q);
152 printPhysicalOrder(q);
155 //--- queue processing
162 E (String name){ this.name = name; }
165 static class EProcessor implements Processor<E> {
166 ArrayObjectQueue queue;
169 EProcessor (ArrayObjectQueue<E> queue){
174 public void process(E e){
177 System.out.println("processed: " + e.name);
180 if (ref != null && !ref.visited) {
186 if (ref != null && !ref.visited) {
194 public void testProcessing(){
206 d.left = a; d.right = e;
207 e.left = f; e.right = g;
210 ArrayObjectQueue<E> q = new ArrayObjectQueue<E>();
214 EProcessor proc = new EProcessor(q);
217 assertTrue(a.visited);
218 assertTrue(b.visited);
219 assertTrue(c.visited);
220 assertTrue(d.visited);
221 assertTrue(e.visited);
222 assertTrue(f.visited);
223 assertTrue(g.visited);
225 assertTrue( proc.processed == 7);