001    package aima.test.search;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import junit.framework.TestCase;
007    import aima.util.AbstractQueue;
008    
009    /**
010     * @author Ravi Mohan
011     * 
012     */
013    public class QueueTest extends TestCase {
014    
015            private AbstractQueue queue;
016    
017            @Override
018            public void setUp() {
019                    queue = new AbstractQueue();
020            }
021    
022            public void testInitialization() {
023                    assertEquals(0, queue.size());
024                    assertTrue(queue.isEmpty());
025            }
026    
027            public void testGetFirst() {
028                    queue.addToFront("Hello");
029                    assertEquals(1, queue.size());
030                    assertFalse(queue.isEmpty());
031    
032                    String s = (String) queue.getFirst();
033                    assertEquals("Hello", s);
034                    assertFalse(queue.isEmpty());
035            }
036    
037            public void testRemoveFirst() {
038                    queue.addToFront("Hello");
039                    assertEquals(1, queue.size());
040                    assertFalse(queue.isEmpty());
041    
042                    String s = (String) queue.removeFirst();
043                    assertEquals("Hello", s);
044                    assertTrue(queue.isEmpty());
045            }
046    
047            public void testGetLast() {
048                    queue.addToFront("Hello");
049                    assertEquals(1, queue.size());
050                    assertFalse(queue.isEmpty());
051    
052                    String s = (String) queue.getLast();
053                    assertEquals("Hello", s);
054                    assertFalse(queue.isEmpty());
055            }
056    
057            public void testRemoveLast() {
058                    queue.addToFront("Hello");
059                    assertEquals(1, queue.size());
060                    assertFalse(queue.isEmpty());
061    
062                    String s = (String) queue.removeLast();
063                    assertEquals("Hello", s);
064                    assertTrue(queue.isEmpty());
065            }
066    
067            public void testAddToFront() {
068                    queue.addToFront("Hello");
069                    assertEquals(1, queue.size());
070                    assertFalse(queue.isEmpty());
071    
072                    queue.addToFront("Hi");
073                    assertEquals(2, queue.size());
074                    assertFalse(queue.isEmpty());
075    
076                    String s = (String) queue.removeFirst();
077                    assertEquals("Hi", s);
078                    assertEquals(1, queue.size());
079                    assertEquals("Hello", queue.getFirst());
080    
081            }
082    
083            public void testAddToBack() {
084                    queue.addToBack("Hello");
085                    assertEquals(1, queue.size());
086                    assertFalse(queue.isEmpty());
087    
088                    queue.addToBack("Hi");
089                    assertEquals(2, queue.size());
090                    assertFalse(queue.isEmpty());
091    
092                    String s = (String) queue.removeFirst();
093                    assertEquals("Hello", s);
094                    assertEquals(1, queue.size());
095                    assertEquals("Hi", queue.getFirst());
096    
097            }
098    
099            public void testMultipleAddToFront() {
100                    queue.addToFront("Hello");
101                    assertEquals(1, queue.size());
102                    assertFalse(queue.isEmpty());
103    
104                    List<String> l = new ArrayList<String>();
105                    l.add("Hi");
106                    l.add("Bye");
107                    queue.addToFront(l);
108                    assertEquals(3, queue.size());
109    
110                    String s = (String) queue.removeFirst();
111                    assertEquals("Hi", s);
112                    assertEquals(2, queue.size());
113                    assertEquals("Bye", queue.getFirst());
114    
115            }
116    
117            public void testMultipleAddToBack() {
118                    queue.addToFront("Hello");
119                    assertEquals(1, queue.size());
120                    assertFalse(queue.isEmpty());
121    
122                    List<String> l = new ArrayList<String>();
123                    l.add("Hi");
124                    l.add("Bye");
125                    queue.addToBack(l);
126                    assertEquals(3, queue.size());
127    
128                    String s = (String) queue.removeFirst();
129                    assertEquals("Hello", s);
130                    assertEquals(2, queue.size());
131                    assertEquals("Hi", queue.getFirst());
132    
133            }
134    
135            public void testAsList() {
136                    queue.addToFront("Hello");
137                    List<String> l = new ArrayList<String>();
138                    l.add("Hi");
139                    l.add("Bye");
140                    queue.addToBack(l);
141                    assertEquals(3, queue.size());
142                    List list = queue.asList();
143                    assertEquals(3, list.size());
144    
145            }
146    
147            public void testExceptions() {
148                    try {
149                            queue.add("Hello");
150                            fail("Runtime Exception should have been thrown");
151                    } catch (RuntimeException e) {
152                            // Test passed
153                    }
154    
155                    try {
156                            queue.remove();
157                            fail("Runtime Exception should have been thrown");
158                    } catch (RuntimeException e) {
159                            // Test passed
160                    }
161                    try {
162                            queue.get();
163                            fail("Runtime Exception should have been thrown");
164                    } catch (RuntimeException e) {
165                            // Test passed
166                    }
167                    List l = new ArrayList();
168                    try {
169                            queue.add(l);
170                            fail("Runtime Exception should have been thrown");
171                    } catch (RuntimeException e) {
172                            // Test passed
173                    }
174    
175            }
176    }