TestFastQueue.java revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2 * [The "BSD license"]
3 *  Copyright (c) 2010 Terence Parr
4 *  All rights reserved.
5 *
6 *  Redistribution and use in source and binary forms, with or without
7 *  modification, are permitted provided that the following conditions
8 *  are met:
9 *  1. Redistributions of source code must retain the above copyright
10 *      notice, this list of conditions and the following disclaimer.
11 *  2. Redistributions in binary form must reproduce the above copyright
12 *      notice, this list of conditions and the following disclaimer in the
13 *      documentation and/or other materials provided with the distribution.
14 *  3. The name of the author may not be used to endorse or promote products
15 *      derived from this software without specific prior written permission.
16 *
17 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28package org.antlr.test;
29
30import org.antlr.runtime.misc.FastQueue;
31import org.junit.Test;
32
33import java.util.NoSuchElementException;
34
35import static org.junit.Assert.assertEquals;
36
37public class TestFastQueue {
38    @Test public void testQueueNoRemove() throws Exception {
39        FastQueue<String> q = new FastQueue<String>();
40        q.add("a");
41        q.add("b");
42        q.add("c");
43        q.add("d");
44        q.add("e");
45        String expecting = "a b c d e";
46        String found = q.toString();
47        assertEquals(expecting, found);
48    }
49
50    @Test public void testQueueThenRemoveAll() throws Exception {
51        FastQueue<String> q = new FastQueue<String>();
52        q.add("a");
53        q.add("b");
54        q.add("c");
55        q.add("d");
56        q.add("e");
57        StringBuffer buf = new StringBuffer();
58        while ( q.size()>0 ) {
59            String o = q.remove();
60            buf.append(o);
61            if ( q.size()>0 ) buf.append(" ");
62        }
63        assertEquals("queue should be empty", 0, q.size());
64        String expecting = "a b c d e";
65        String found = buf.toString();
66        assertEquals(expecting, found);
67    }
68
69    @Test public void testQueueThenRemoveOneByOne() throws Exception {
70        StringBuffer buf = new StringBuffer();
71        FastQueue<String> q = new FastQueue<String>();
72        q.add("a");
73        buf.append(q.remove());
74        q.add("b");
75        buf.append(q.remove());
76        q.add("c");
77        buf.append(q.remove());
78        q.add("d");
79        buf.append(q.remove());
80        q.add("e");
81        buf.append(q.remove());
82        assertEquals("queue should be empty", 0, q.size());
83        String expecting = "abcde";
84        String found = buf.toString();
85        assertEquals(expecting, found);
86    }
87
88    // E r r o r s
89
90    @Test public void testGetFromEmptyQueue() throws Exception {
91        FastQueue<String> q = new FastQueue<String>();
92        String msg = null;
93        try { q.remove(); }
94        catch (NoSuchElementException nsee) {
95            msg = nsee.getMessage();
96        }
97        String expecting = "queue index 0 > last index -1";
98        String found = msg;
99        assertEquals(expecting, found);
100    }
101
102    @Test public void testGetFromEmptyQueueAfterSomeAdds() throws Exception {
103        FastQueue<String> q = new FastQueue<String>();
104        q.add("a");
105        q.add("b");
106        q.remove();
107        q.remove();
108        String msg = null;
109        try { q.remove(); }
110        catch (NoSuchElementException nsee) {
111            msg = nsee.getMessage();
112        }
113        String expecting = "queue index 0 > last index -1";
114        String found = msg;
115        assertEquals(expecting, found);
116    }
117
118    @Test public void testGetFromEmptyQueueAfterClear() throws Exception {
119        FastQueue<String> q = new FastQueue<String>();
120        q.add("a");
121        q.add("b");
122        q.clear();
123        String msg = null;
124        try { q.remove(); }
125        catch (NoSuchElementException nsee) {
126            msg = nsee.getMessage();
127        }
128        String expecting = "queue index 0 > last index -1";
129        String found = msg;
130        assertEquals(expecting, found);
131    }
132}
133