1 /*
2  * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3  * Use of this file is governed by the BSD 3-clause license that
4  * can be found in the LICENSE.txt file in the project root.
5  */
6 package org.antlr.v4.test.tool;
7 
8 import org.antlr.runtime.misc.FastQueue;
9 import org.junit.Test;
10 
11 import java.util.NoSuchElementException;
12 
13 import static org.junit.Assert.assertEquals;
14 
15 public class TestFastQueue {
testQueueNoRemove()16     @Test public void testQueueNoRemove() throws Exception {
17         FastQueue<String> q = new FastQueue<String>();
18         q.add("a");
19         q.add("b");
20         q.add("c");
21         q.add("d");
22         q.add("e");
23         String expecting = "a b c d e";
24         String found = q.toString();
25         assertEquals(expecting, found);
26     }
27 
testQueueThenRemoveAll()28     @Test public void testQueueThenRemoveAll() throws Exception {
29         FastQueue<String> q = new FastQueue<String>();
30         q.add("a");
31         q.add("b");
32         q.add("c");
33         q.add("d");
34         q.add("e");
35         StringBuilder buf = new StringBuilder();
36         while ( q.size()>0 ) {
37             String o = q.remove();
38             buf.append(o);
39             if ( q.size()>0 ) buf.append(" ");
40         }
41         assertEquals("queue should be empty", 0, q.size());
42         String expecting = "a b c d e";
43         String found = buf.toString();
44         assertEquals(expecting, found);
45     }
46 
testQueueThenRemoveOneByOne()47     @Test public void testQueueThenRemoveOneByOne() throws Exception {
48         StringBuilder buf = new StringBuilder();
49         FastQueue<String> q = new FastQueue<String>();
50         q.add("a");
51         buf.append(q.remove());
52         q.add("b");
53         buf.append(q.remove());
54         q.add("c");
55         buf.append(q.remove());
56         q.add("d");
57         buf.append(q.remove());
58         q.add("e");
59         buf.append(q.remove());
60         assertEquals("queue should be empty", 0, q.size());
61         String expecting = "abcde";
62         String found = buf.toString();
63         assertEquals(expecting, found);
64     }
65 
66     // E r r o r s
67 
testGetFromEmptyQueue()68     @Test public void testGetFromEmptyQueue() throws Exception {
69         FastQueue<String> q = new FastQueue<String>();
70         String msg = null;
71         try { q.remove(); }
72         catch (NoSuchElementException nsee) {
73             msg = nsee.getMessage();
74         }
75         String expecting = "queue index 0 > last index -1";
76         String found = msg;
77         assertEquals(expecting, found);
78     }
79 
testGetFromEmptyQueueAfterSomeAdds()80     @Test public void testGetFromEmptyQueueAfterSomeAdds() throws Exception {
81         FastQueue<String> q = new FastQueue<String>();
82         q.add("a");
83         q.add("b");
84         q.remove();
85         q.remove();
86         String msg = null;
87         try { q.remove(); }
88         catch (NoSuchElementException nsee) {
89             msg = nsee.getMessage();
90         }
91         String expecting = "queue index 0 > last index -1";
92         String found = msg;
93         assertEquals(expecting, found);
94     }
95 
testGetFromEmptyQueueAfterClear()96     @Test public void testGetFromEmptyQueueAfterClear() throws Exception {
97         FastQueue<String> q = new FastQueue<String>();
98         q.add("a");
99         q.add("b");
100         q.clear();
101         String msg = null;
102         try { q.remove(); }
103         catch (NoSuchElementException nsee) {
104             msg = nsee.getMessage();
105         }
106         String expecting = "queue index 0 > last index -1";
107         String found = msg;
108         assertEquals(expecting, found);
109     }
110 }
111