• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.annotations.J2ktIncompatible;
22 import com.google.common.testing.NullPointerTester;
23 import com.google.common.testing.SerializableTester;
24 import java.util.AbstractList;
25 import java.util.List;
26 import java.util.NoSuchElementException;
27 import junit.framework.TestCase;
28 
29 /**
30  * Tests for {@link EvictingQueue}.
31  *
32  * @author Kurt Alfred Kluever
33  */
34 @GwtCompatible(emulated = true)
35 @ElementTypesAreNonnullByDefault
36 public class EvictingQueueTest extends TestCase {
37 
testCreateWithNegativeSize()38   public void testCreateWithNegativeSize() throws Exception {
39     try {
40       EvictingQueue.create(-1);
41       fail();
42     } catch (IllegalArgumentException expected) {
43     }
44   }
45 
testCreateWithZeroSize()46   public void testCreateWithZeroSize() throws Exception {
47     EvictingQueue<String> queue = EvictingQueue.create(0);
48     assertEquals(0, queue.size());
49 
50     assertTrue(queue.add("hi"));
51     assertEquals(0, queue.size());
52 
53     assertTrue(queue.offer("hi"));
54     assertEquals(0, queue.size());
55 
56     assertFalse(queue.remove("hi"));
57     assertEquals(0, queue.size());
58 
59     try {
60       queue.element();
61       fail();
62     } catch (NoSuchElementException expected) {
63     }
64 
65     assertNull(queue.peek());
66     assertNull(queue.poll());
67     try {
68       queue.remove();
69       fail();
70     } catch (NoSuchElementException expected) {
71     }
72   }
73 
testRemainingCapacity_maxSize0()74   public void testRemainingCapacity_maxSize0() {
75     EvictingQueue<String> queue = EvictingQueue.create(0);
76     assertEquals(0, queue.remainingCapacity());
77   }
78 
testRemainingCapacity_maxSize1()79   public void testRemainingCapacity_maxSize1() {
80     EvictingQueue<String> queue = EvictingQueue.create(1);
81     assertEquals(1, queue.remainingCapacity());
82     queue.add("hi");
83     assertEquals(0, queue.remainingCapacity());
84   }
85 
testRemainingCapacity_maxSize3()86   public void testRemainingCapacity_maxSize3() {
87     EvictingQueue<String> queue = EvictingQueue.create(3);
88     assertEquals(3, queue.remainingCapacity());
89     queue.add("hi");
90     assertEquals(2, queue.remainingCapacity());
91     queue.add("hi");
92     assertEquals(1, queue.remainingCapacity());
93     queue.add("hi");
94     assertEquals(0, queue.remainingCapacity());
95   }
96 
testEvictingAfterOne()97   public void testEvictingAfterOne() throws Exception {
98     EvictingQueue<String> queue = EvictingQueue.create(1);
99     assertEquals(0, queue.size());
100     assertEquals(1, queue.remainingCapacity());
101 
102     assertTrue(queue.add("hi"));
103     assertEquals("hi", queue.element());
104     assertEquals("hi", queue.peek());
105     assertEquals(1, queue.size());
106     assertEquals(0, queue.remainingCapacity());
107 
108     assertTrue(queue.add("there"));
109     assertEquals("there", queue.element());
110     assertEquals("there", queue.peek());
111     assertEquals(1, queue.size());
112     assertEquals(0, queue.remainingCapacity());
113 
114     assertEquals("there", queue.remove());
115     assertEquals(0, queue.size());
116     assertEquals(1, queue.remainingCapacity());
117   }
118 
testEvictingAfterThree()119   public void testEvictingAfterThree() throws Exception {
120     EvictingQueue<String> queue = EvictingQueue.create(3);
121     assertEquals(0, queue.size());
122     assertEquals(3, queue.remainingCapacity());
123 
124     assertTrue(queue.add("one"));
125     assertTrue(queue.add("two"));
126     assertTrue(queue.add("three"));
127     assertEquals("one", queue.element());
128     assertEquals("one", queue.peek());
129     assertEquals(3, queue.size());
130     assertEquals(0, queue.remainingCapacity());
131 
132     assertTrue(queue.add("four"));
133     assertEquals("two", queue.element());
134     assertEquals("two", queue.peek());
135     assertEquals(3, queue.size());
136     assertEquals(0, queue.remainingCapacity());
137 
138     assertEquals("two", queue.remove());
139     assertEquals(2, queue.size());
140     assertEquals(1, queue.remainingCapacity());
141   }
142 
testAddAll()143   public void testAddAll() throws Exception {
144     EvictingQueue<String> queue = EvictingQueue.create(3);
145     assertEquals(0, queue.size());
146     assertEquals(3, queue.remainingCapacity());
147 
148     assertTrue(queue.addAll(ImmutableList.of("one", "two", "three")));
149     assertEquals("one", queue.element());
150     assertEquals("one", queue.peek());
151     assertEquals(3, queue.size());
152     assertEquals(0, queue.remainingCapacity());
153 
154     assertTrue(queue.addAll(ImmutableList.of("four")));
155     assertEquals("two", queue.element());
156     assertEquals("two", queue.peek());
157     assertEquals(3, queue.size());
158     assertEquals(0, queue.remainingCapacity());
159 
160     assertEquals("two", queue.remove());
161     assertEquals(2, queue.size());
162     assertEquals(1, queue.remainingCapacity());
163   }
164 
testAddAll_largeList()165   public void testAddAll_largeList() {
166     final List<String> list = ImmutableList.of("one", "two", "three", "four", "five");
167     List<String> misbehavingList =
168         new AbstractList<String>() {
169           @Override
170           public int size() {
171             return list.size();
172           }
173 
174           @Override
175           public String get(int index) {
176             if (index < 2) {
177               throw new AssertionError();
178             }
179             return list.get(index);
180           }
181         };
182 
183     EvictingQueue<String> queue = EvictingQueue.create(3);
184     assertTrue(queue.addAll(misbehavingList));
185 
186     assertEquals("three", queue.remove());
187     assertEquals("four", queue.remove());
188     assertEquals("five", queue.remove());
189     assertTrue(queue.isEmpty());
190   }
191 
192   @J2ktIncompatible
193   @GwtIncompatible // NullPointerTester
testNullPointerExceptions()194   public void testNullPointerExceptions() {
195     NullPointerTester tester = new NullPointerTester();
196     tester.testAllPublicStaticMethods(EvictingQueue.class);
197     tester.testAllPublicConstructors(EvictingQueue.class);
198     EvictingQueue<String> queue = EvictingQueue.create(5);
199     // The queue must be non-empty so it throws a NPE correctly
200     queue.add("one");
201     tester.testAllPublicInstanceMethods(queue);
202   }
203 
testSerialization()204   public void testSerialization() {
205     EvictingQueue<String> original = EvictingQueue.create(5);
206     original.add("one");
207     original.add("two");
208     original.add("three");
209 
210     EvictingQueue<String> copy = SerializableTester.reserialize(original);
211     assertEquals(copy.maxSize, original.maxSize);
212     assertEquals("one", copy.remove());
213     assertEquals("two", copy.remove());
214     assertEquals("three", copy.remove());
215     assertTrue(copy.isEmpty());
216   }
217 }
218