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