Home | History | Annotate | Download | only in collect
      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 static java.util.Arrays.asList;
     20 
     21 import com.google.common.testing.NullPointerTester;
     22 import com.google.common.testing.SerializableTester;
     23 
     24 import junit.framework.TestCase;
     25 
     26 import java.util.NoSuchElementException;
     27 
     28 /**
     29  * Tests for {@link EvictingQueue}.
     30  *
     31  * @author Kurt Alfred Kluever
     32  */
     33 public class EvictingQueueTest extends TestCase {
     34 
     35   public void testCreateWithNegativeSize() throws Exception {
     36     try {
     37       EvictingQueue.create(-1);
     38       fail();
     39     } catch (IllegalArgumentException expected) {
     40     }
     41   }
     42 
     43   public void testCreateWithZeroSize() throws Exception {
     44     EvictingQueue<String> queue = EvictingQueue.create(0);
     45     assertEquals(0, queue.size());
     46 
     47     assertTrue(queue.add("hi"));
     48     assertEquals(0, queue.size());
     49 
     50     assertTrue(queue.offer("hi"));
     51     assertEquals(0, queue.size());
     52 
     53     assertFalse(queue.remove("hi"));
     54     assertEquals(0, queue.size());
     55 
     56     try {
     57       queue.element();
     58       fail();
     59     } catch (NoSuchElementException expected) {}
     60 
     61     assertNull(queue.peek());
     62     assertNull(queue.poll());
     63     try {
     64       queue.remove();
     65       fail();
     66     } catch (NoSuchElementException expected) {}
     67   }
     68 
     69   public void testRemainingCapacity_maxSize0() {
     70     EvictingQueue<String> queue = EvictingQueue.create(0);
     71     assertEquals(0, queue.remainingCapacity());
     72   }
     73 
     74   public void testRemainingCapacity_maxSize1() {
     75     EvictingQueue<String> queue = EvictingQueue.create(1);
     76     assertEquals(1, queue.remainingCapacity());
     77     queue.add("hi");
     78     assertEquals(0, queue.remainingCapacity());
     79   }
     80 
     81   public void testRemainingCapacity_maxSize3() {
     82     EvictingQueue<String> queue = EvictingQueue.create(3);
     83     assertEquals(3, queue.remainingCapacity());
     84     queue.add("hi");
     85     assertEquals(2, queue.remainingCapacity());
     86     queue.add("hi");
     87     assertEquals(1, queue.remainingCapacity());
     88     queue.add("hi");
     89     assertEquals(0, queue.remainingCapacity());
     90   }
     91 
     92   public void testEvictingAfterOne() throws Exception {
     93     EvictingQueue<String> queue = EvictingQueue.create(1);
     94     assertEquals(0, queue.size());
     95     assertEquals(1, queue.remainingCapacity());
     96 
     97     assertTrue(queue.add("hi"));
     98     assertEquals("hi", queue.element());
     99     assertEquals("hi", queue.peek());
    100     assertEquals(1, queue.size());
    101     assertEquals(0, queue.remainingCapacity());
    102 
    103     assertTrue(queue.add("there"));
    104     assertEquals("there", queue.element());
    105     assertEquals("there", queue.peek());
    106     assertEquals(1, queue.size());
    107     assertEquals(0, queue.remainingCapacity());
    108 
    109     assertEquals("there", queue.remove());
    110     assertEquals(0, queue.size());
    111     assertEquals(1, queue.remainingCapacity());
    112   }
    113 
    114   public void testEvictingAfterThree() throws Exception {
    115     EvictingQueue<String> queue = EvictingQueue.create(3);
    116     assertEquals(0, queue.size());
    117     assertEquals(3, queue.remainingCapacity());
    118 
    119     assertTrue(queue.add("one"));
    120     assertTrue(queue.add("two"));
    121     assertTrue(queue.add("three"));
    122     assertEquals("one", queue.element());
    123     assertEquals("one", queue.peek());
    124     assertEquals(3, queue.size());
    125     assertEquals(0, queue.remainingCapacity());
    126 
    127     assertTrue(queue.add("four"));
    128     assertEquals("two", queue.element());
    129     assertEquals("two", queue.peek());
    130     assertEquals(3, queue.size());
    131     assertEquals(0, queue.remainingCapacity());
    132 
    133     assertEquals("two", queue.remove());
    134     assertEquals(2, queue.size());
    135     assertEquals(1, queue.remainingCapacity());
    136   }
    137 
    138   public void testAddAll() throws Exception {
    139     EvictingQueue<String> queue = EvictingQueue.create(3);
    140     assertEquals(0, queue.size());
    141     assertEquals(3, queue.remainingCapacity());
    142 
    143     assertTrue(queue.addAll(asList("one", "two", "three")));
    144     assertEquals("one", queue.element());
    145     assertEquals("one", queue.peek());
    146     assertEquals(3, queue.size());
    147     assertEquals(0, queue.remainingCapacity());
    148 
    149     assertTrue(queue.addAll(asList("four")));
    150     assertEquals("two", queue.element());
    151     assertEquals("two", queue.peek());
    152     assertEquals(3, queue.size());
    153     assertEquals(0, queue.remainingCapacity());
    154 
    155     assertEquals("two", queue.remove());
    156     assertEquals(2, queue.size());
    157     assertEquals(1, queue.remainingCapacity());
    158   }
    159 
    160   public void testNullPointerExceptions() {
    161     NullPointerTester tester = new NullPointerTester();
    162     tester.testAllPublicStaticMethods(EvictingQueue.class);
    163     tester.testAllPublicConstructors(EvictingQueue.class);
    164     EvictingQueue<String> queue = EvictingQueue.create(5);
    165     // The queue must be non-empty so it throws a NPE correctly
    166     queue.add("one");
    167     tester.testAllPublicInstanceMethods(queue);
    168   }
    169 
    170   public void testSerialization() {
    171     EvictingQueue<String> original = EvictingQueue.create(5);
    172     original.add("one");
    173     original.add("two");
    174     original.add("three");
    175 
    176     EvictingQueue<String> copy = SerializableTester.reserialize(original);
    177     assertEquals(copy.maxSize, original.maxSize);
    178     assertEquals("one", copy.remove());
    179     assertEquals("two", copy.remove());
    180     assertEquals("three", copy.remove());
    181     assertTrue(copy.isEmpty());
    182   }
    183 }
    184