001/* 002 * Copyright (C) 2010 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.Helpers.assertEqualInOrder; 020import static com.google.common.collect.testing.features.CollectionSize.ONE; 021import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 022import static com.google.common.collect.testing.features.CollectionSize.ZERO; 023 024import com.google.common.annotations.GwtCompatible; 025import com.google.common.collect.testing.AbstractMapTester; 026import com.google.common.collect.testing.Helpers; 027import com.google.common.collect.testing.features.CollectionSize; 028import java.util.Collections; 029import java.util.Comparator; 030import java.util.Iterator; 031import java.util.List; 032import java.util.Map.Entry; 033import java.util.NoSuchElementException; 034import java.util.SortedMap; 035import org.junit.Ignore; 036 037/** 038 * A generic JUnit test which tests operations on a SortedMap. Can't be invoked directly; please see 039 * {@code SortedMapTestSuiteBuilder}. 040 * 041 * @author Jesse Wilson 042 * @author Louis Wasserman 043 */ 044@GwtCompatible 045@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 046@SuppressWarnings("JUnit4ClassUsedInJUnit3") 047public class SortedMapNavigationTester<K, V> extends AbstractMapTester<K, V> { 048 049 private SortedMap<K, V> navigableMap; 050 private Entry<K, V> a; 051 private Entry<K, V> c; 052 053 @Override 054 public void setUp() throws Exception { 055 super.setUp(); 056 navigableMap = (SortedMap<K, V>) getMap(); 057 List<Entry<K, V>> entries = 058 Helpers.copyToList( 059 getSubjectGenerator() 060 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); 061 Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator())); 062 063 // some tests assume SEVERAL == 3 064 if (entries.size() >= 1) { 065 a = entries.get(0); 066 if (entries.size() >= 3) { 067 c = entries.get(2); 068 } 069 } 070 } 071 072 @CollectionSize.Require(ZERO) 073 public void testEmptyMapFirst() { 074 try { 075 navigableMap.firstKey(); 076 fail(); 077 } catch (NoSuchElementException e) { 078 } 079 } 080 081 @CollectionSize.Require(ZERO) 082 public void testEmptyMapLast() { 083 try { 084 assertNull(navigableMap.lastKey()); 085 fail(); 086 } catch (NoSuchElementException e) { 087 } 088 } 089 090 @CollectionSize.Require(ONE) 091 public void testSingletonMapFirst() { 092 assertEquals(a.getKey(), navigableMap.firstKey()); 093 } 094 095 @CollectionSize.Require(ONE) 096 public void testSingletonMapLast() { 097 assertEquals(a.getKey(), navigableMap.lastKey()); 098 } 099 100 @CollectionSize.Require(SEVERAL) 101 public void testFirst() { 102 assertEquals(a.getKey(), navigableMap.firstKey()); 103 } 104 105 @CollectionSize.Require(SEVERAL) 106 public void testLast() { 107 assertEquals(c.getKey(), navigableMap.lastKey()); 108 } 109 110 @CollectionSize.Require(absent = ZERO) 111 public void testHeadMapExclusive() { 112 assertFalse(navigableMap.headMap(a.getKey()).containsKey(a.getKey())); 113 } 114 115 @CollectionSize.Require(absent = ZERO) 116 public void testTailMapInclusive() { 117 assertTrue(navigableMap.tailMap(a.getKey()).containsKey(a.getKey())); 118 } 119 120 public void testHeadMap() { 121 List<Entry<K, V>> entries = 122 Helpers.copyToList( 123 getSubjectGenerator() 124 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); 125 Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator())); 126 for (int i = 0; i < entries.size(); i++) { 127 assertEqualInOrder( 128 entries.subList(0, i), navigableMap.headMap(entries.get(i).getKey()).entrySet()); 129 } 130 } 131 132 public void testTailMap() { 133 List<Entry<K, V>> entries = 134 Helpers.copyToList( 135 getSubjectGenerator() 136 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); 137 Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator())); 138 for (int i = 0; i < entries.size(); i++) { 139 assertEqualInOrder( 140 entries.subList(i, entries.size()), 141 navigableMap.tailMap(entries.get(i).getKey()).entrySet()); 142 } 143 } 144 145 public void testSubMap() { 146 List<Entry<K, V>> entries = 147 Helpers.copyToList( 148 getSubjectGenerator() 149 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); 150 Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator())); 151 for (int i = 0; i < entries.size(); i++) { 152 for (int j = i + 1; j < entries.size(); j++) { 153 assertEqualInOrder( 154 entries.subList(i, j), 155 navigableMap.subMap(entries.get(i).getKey(), entries.get(j).getKey()).entrySet()); 156 } 157 } 158 } 159 160 @CollectionSize.Require(SEVERAL) 161 public void testSubMapIllegal() { 162 try { 163 navigableMap.subMap(c.getKey(), a.getKey()); 164 fail("Expected IllegalArgumentException"); 165 } catch (IllegalArgumentException expected) { 166 } 167 } 168 169 @CollectionSize.Require(absent = ZERO) 170 public void testOrderedByComparator() { 171 Comparator<? super K> comparator = navigableMap.comparator(); 172 if (comparator == null) { 173 comparator = 174 new Comparator<K>() { 175 @SuppressWarnings("unchecked") 176 @Override 177 public int compare(K o1, K o2) { 178 return ((Comparable) o1).compareTo(o2); 179 } 180 }; 181 } 182 Iterator<Entry<K, V>> entryItr = navigableMap.entrySet().iterator(); 183 Entry<K, V> prevEntry = entryItr.next(); 184 while (entryItr.hasNext()) { 185 Entry<K, V> nextEntry = entryItr.next(); 186 assertTrue(comparator.compare(prevEntry.getKey(), nextEntry.getKey()) < 0); 187 prevEntry = nextEntry; 188 } 189 } 190}