View Javadoc
1   /*
2    * Copyright (C) 2010 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.testing.testers;
18  
19  import static com.google.common.collect.testing.Helpers.assertEqualInOrder;
20  import static com.google.common.collect.testing.features.CollectionSize.ONE;
21  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
22  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.collect.testing.AbstractMapTester;
26  import com.google.common.collect.testing.Helpers;
27  import com.google.common.collect.testing.features.CollectionSize;
28  import java.util.Collections;
29  import java.util.Comparator;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map.Entry;
33  import java.util.NoSuchElementException;
34  import java.util.SortedMap;
35  
36  /**
37   * A generic JUnit test which tests operations on a SortedMap. Can't be
38   * invoked directly; please see {@code SortedMapTestSuiteBuilder}.
39   *
40   * @author Jesse Wilson
41   * @author Louis Wasserman
42   */
43  @GwtCompatible
44  public class SortedMapNavigationTester<K, V> extends AbstractMapTester<K, V> {
45  
46    private SortedMap<K, V> navigableMap;
47    private Entry<K, V> a;
48    private Entry<K, V> c;
49  
50    @Override
51    public void setUp() throws Exception {
52      super.setUp();
53      navigableMap = (SortedMap<K, V>) getMap();
54      List<Entry<K, V>> entries =
55          Helpers.copyToList(
56              getSubjectGenerator()
57                  .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
58      Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
59  
60      // some tests assume SEVERAL == 3
61      if (entries.size() >= 1) {
62        a = entries.get(0);
63        if (entries.size() >= 3) {
64          c = entries.get(2);
65        }
66      }
67    }
68  
69    @CollectionSize.Require(ZERO)
70    public void testEmptyMapFirst() {
71      try {
72        navigableMap.firstKey();
73        fail();
74      } catch (NoSuchElementException e) {
75      }
76    }
77  
78    @CollectionSize.Require(ZERO)
79    public void testEmptyMapLast() {
80      try {
81        assertNull(navigableMap.lastKey());
82        fail();
83      } catch (NoSuchElementException e) {
84      }
85    }
86  
87    @CollectionSize.Require(ONE)
88    public void testSingletonMapFirst() {
89      assertEquals(a.getKey(), navigableMap.firstKey());
90    }
91  
92    @CollectionSize.Require(ONE)
93    public void testSingletonMapLast() {
94      assertEquals(a.getKey(), navigableMap.lastKey());
95    }
96  
97    @CollectionSize.Require(SEVERAL)
98    public void testFirst() {
99      assertEquals(a.getKey(), navigableMap.firstKey());
100   }
101 
102   @CollectionSize.Require(SEVERAL)
103   public void testLast() {
104     assertEquals(c.getKey(), navigableMap.lastKey());
105   }
106 
107   @CollectionSize.Require(absent = ZERO)
108   public void testHeadMapExclusive() {
109     assertFalse(navigableMap.headMap(a.getKey()).containsKey(a.getKey()));
110   }
111 
112   @CollectionSize.Require(absent = ZERO)
113   public void testTailMapInclusive() {
114     assertTrue(navigableMap.tailMap(a.getKey()).containsKey(a.getKey()));
115   }
116 
117   public void testHeadMap() {
118     List<Entry<K, V>> entries =
119         Helpers.copyToList(
120             getSubjectGenerator()
121                 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
122     Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
123     for (int i = 0; i < entries.size(); i++) {
124       assertEqualInOrder(
125           entries.subList(0, i), navigableMap.headMap(entries.get(i).getKey()).entrySet());
126     }
127   }
128 
129   public void testTailMap() {
130     List<Entry<K, V>> entries =
131         Helpers.copyToList(
132             getSubjectGenerator()
133                 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
134     Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
135     for (int i = 0; i < entries.size(); i++) {
136       assertEqualInOrder(
137           entries.subList(i, entries.size()),
138           navigableMap.tailMap(entries.get(i).getKey()).entrySet());
139     }
140   }
141 
142   public void testSubMap() {
143     List<Entry<K, V>> entries =
144         Helpers.copyToList(
145             getSubjectGenerator()
146                 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
147     Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator()));
148     for (int i = 0; i < entries.size(); i++) {
149       for (int j = i + 1; j < entries.size(); j++) {
150         assertEqualInOrder(
151             entries.subList(i, j),
152             navigableMap.subMap(entries.get(i).getKey(), entries.get(j).getKey()).entrySet());
153       }
154     }
155   }
156 
157   @CollectionSize.Require(SEVERAL)
158   public void testSubMapIllegal() {
159     try {
160       navigableMap.subMap(c.getKey(), a.getKey());
161       fail("Expected IllegalArgumentException");
162     } catch (IllegalArgumentException expected) {
163     }
164   }
165 
166   @CollectionSize.Require(absent = ZERO)
167   public void testOrderedByComparator() {
168     @SuppressWarnings("unchecked")
169     Comparator<? super K> comparator = navigableMap.comparator();
170     if (comparator == null) {
171       comparator =
172           new Comparator<K>() {
173             @SuppressWarnings("unchecked")
174             @Override
175             public int compare(K o1, K o2) {
176               return ((Comparable) o1).compareTo(o2);
177             }
178           };
179     }
180     Iterator<Entry<K, V>> entryItr = navigableMap.entrySet().iterator();
181     Entry<K, V> prevEntry = entryItr.next();
182     while (entryItr.hasNext()) {
183       Entry<K, V> nextEntry = entryItr.next();
184       assertTrue(comparator.compare(prevEntry.getKey(), nextEntry.getKey()) < 0);
185       prevEntry = nextEntry;
186     }
187   }
188 }