Fixes some tests on java.util.TreeMap. Fixes tests code style

This commit is contained in:
konsoletyper 2014-03-21 22:53:11 +04:00
parent 56012d1a9f
commit 6c599c2886
3 changed files with 87 additions and 369 deletions

View File

@ -75,7 +75,7 @@ public class SystemNativeGenerator implements Generator, DependencyPlugin {
String destPos = context.getParameterName(4);
String length = context.getParameterName(5);
writer.append("for (var i = 0; i < " + length + "; i = (i + 1) | 0) {").indent().softNewLine();
writer.append(dest + ".data[" + srcPos + "++] = " + src + ".data[" + destPos + "++];").softNewLine();
writer.append(dest + ".data[" + destPos + "++] = " + src + ".data[" + srcPos + "++];").softNewLine();
writer.outdent().append("}").softNewLine();
}

View File

@ -34,15 +34,6 @@ package org.teavm.classlib.java.util;
import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.*;
/**
* TreeMap is an implementation of SortedMap. All optional operations (adding
* and removing) are supported. The values can be any objects. The keys can be
* any objects which are comparable to each other either using their natural
* order or a specified Comparator.
*
* @since 1.2
*/
public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K, V>, TCloneable, TSerializable {
transient int size;
private TComparator<? super K> comparator;
@ -79,8 +70,7 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
Entry<?, ?> entry = (Entry<?, ?>) object;
V value = getValue();
return (key == null ? entry.getKey() == null : key.equals(entry.getKey()))
&& (value == null ? entry.getValue() == null : value
.equals(entry.getValue()));
&& (value == null ? entry.getValue() == null : value.equals(entry.getValue()));
}
return false;
}
@ -513,8 +503,7 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
if (firstKeyModCount == backingMap.modCount) {
return;
}
TComparable<K> object = backingMap.comparator == null ?
toComparable(startKey) : null;
TComparable<K> object = backingMap.comparator == null ? toComparable(startKey) : null;
K key = startKey;
Node<K, V> node = backingMap.root;
Node<K, V> foundNode = null;
@ -972,45 +961,17 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
}
}
/**
* Constructs a new empty {@code TreeMap} instance.
*/
public TTreeMap() {
}
/**
* Constructs a new empty {@code TreeMap} instance with the specified
* comparator.
*
* @param comparator
* the comparator to compare keys with.
*/
public TTreeMap(TComparator<? super K> comparator) {
this.comparator = comparator;
}
/**
* Constructs a new {@code TreeMap} instance containing the mappings from
* the specified map and using natural ordering.
*
* @param map
* the mappings to add.
* @throws ClassCastException
* if a key in the specified map does not implement the
* Comparable interface, or if the keys in the map cannot be
* compared.
*/
public TTreeMap(TMap<? extends K, ? extends V> map) {
putAll(map);
}
/**
* Constructs a new {@code TreeMap} instance containing the mappings from
* the specified SortedMap and using the same comparator.
*
* @param map
* the mappings to add.
*/
public TTreeMap(TSortedMap<K, ? extends V> map) {
this(map.comparator());
Node<K, V> lastNode = null;
@ -1038,12 +999,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return last;
}
/**
* Removes all mappings from this TreeMap, leaving it empty.
*
* @see Map#isEmpty()
* @see #size()
*/
@Override
public void clear() {
root = null;
@ -1051,13 +1006,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
modCount++;
}
/**
* Returns a new {@code TreeMap} with the same mappings, size and comparator
* as this instance.
*
* @return a shallow copy of this instance.
* @see java.lang.Cloneable
*/
@SuppressWarnings("unchecked")
@Override
public TObject clone() {
@ -1095,30 +1043,11 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return y;
}
/**
* Returns the comparator used to compare elements in this map.
*
* @return the comparator or {@code null} if the natural ordering is used.
*/
@Override
public TComparator<? super K> comparator() {
return comparator;
}
/**
* Returns whether this map contains the specified key.
*
* @param key
* the key to search for.
* @return {@code true} if this map contains the specified key,
* {@code false} otherwise.
* @throws ClassCastException
* if the specified key cannot be compared with the keys in this
* map.
* @throws NullPointerException
* if the specified key is {@code null} and the comparator
* cannot handle {@code null} keys.
*/
@Override
public boolean containsKey(Object key) {
@SuppressWarnings("unchecked")
@ -1163,14 +1092,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return false;
}
/**
* Returns whether this map contains the specified value.
*
* @param value
* the value to search for.
* @return {@code true} if this map contains the specified value,
* {@code false} otherwise.
*/
@Override
public boolean containsValue(Object value) {
if (root == null) {
@ -1203,14 +1124,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return false;
}
/**
* Returns a set containing all of the mappings in this map. Each mapping is
* an instance of {@link Map.Entry}. As the set is backed by this map,
* changes in one will be reflected in the other. It does not support adding
* operations.
*
* @return a set of the mappings.
*/
@Override
public TSet<Entry<K, V>> entrySet() {
if (entrySet == null) {
@ -1258,13 +1171,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return entrySet;
}
/**
* Returns the first key in this map.
*
* @return the first key in this map.
* @throws NoSuchElementException
* if this map is empty.
*/
@Override
public K firstKey() {
if (root != null) {
@ -1275,18 +1181,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
}
/**
* Returns the value of the mapping with the specified key.
*
* @param key
* the key.
* @return the value of the mapping with the specified key.
* @throws ClassCastException
* if the key cannot be compared with the keys in this map.
* @throws NullPointerException
* if the key is {@code null} and the comparator cannot handle
* {@code null}.
*/
@Override
public V get(Object key) {
@SuppressWarnings("unchecked")
@ -1332,31 +1226,9 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
}
private int cmp(TComparable<K> object, K key1, K key2) {
return object != null ?
object.compareTo(key2) : comparator.compare(key1, key2);
return object != null ? object.compareTo(key2) : comparator.compare(key1, key2);
}
/**
* Returns a sorted map over a range of this sorted map with all keys that
* are less than the specified {@code endKey}. Changes to the returned
* sorted map are reflected in this sorted map and vice versa.
* <p>
* Note: The returned map will not allow an insertion of a key outside the
* specified range.
*
* @param endKey
* the high boundary of the range specified.
* @return a sorted map where the keys are less than {@code endKey}.
* @throws ClassCastException
* if the specified key cannot be compared with the keys in this
* map.
* @throws NullPointerException
* if the specified key is {@code null} and the comparator
* cannot handle {@code null} keys.
* @throws IllegalArgumentException
* if this map is itself a sorted map over a range of another
* map and the specified key is outside of its range.
*/
@Override
public TSortedMap<K, V> headMap(K endKey) {
// Check for errors
@ -1368,13 +1240,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return new SubMap<>(this, endKey);
}
/**
* Returns a set of the keys contained in this map. The set is backed by
* this map so changes to one are reflected by the other. The set does not
* support adding.
*
* @return a set of the keys.
*/
@Override
public TSet<K> keySet() {
if (cachedKeySet == null) {
@ -1412,13 +1277,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return cachedKeySet;
}
/**
* Returns the last key in this map.
*
* @return the last key in this map.
* @throws NoSuchElementException
* if this map is empty.
*/
@Override
public K lastKey() {
if (root != null) {
@ -1448,22 +1306,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return x;
}
/**
* Maps the specified key to the specified value.
*
* @param key
* the key.
* @param value
* the value.
* @return the value of any previous mapping with the specified key or
* {@code null} if there was no mapping.
* @throws ClassCastException
* if the specified key cannot be compared with the keys in this
* map.
* @throws NullPointerException
* if the specified key is {@code null} and the comparator
* cannot handle {@code null} keys.
*/
@Override
public V put(K key, V value) {
if (root == null) {
@ -1827,40 +1669,11 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
x.parent = y;
}
/**
* Copies all the mappings in the given map to this map. These mappings will
* replace all mappings that this map had for any of the keys currently in
* the given map.
*
* @param map
* the map to copy mappings from.
* @throws ClassCastException
* if a key in the specified map cannot be compared with the
* keys in this map.
* @throws NullPointerException
* if a key in the specified map is {@code null} and the
* comparator cannot handle {@code null} keys.
*/
@Override
public void putAll(TMap<? extends K, ? extends V> map) {
super.putAll(map);
}
/**
* Removes the mapping with the specified key from this map.
*
* @param key
* the key of the mapping to remove.
* @return the value of the removed mapping or {@code null} if no mapping
* for the specified key was found.
* @throws ClassCastException
* if the specified key cannot be compared with the keys in this
* map.
* @throws NullPointerException
* if the specified key is {@code null} and the comparator
* cannot handle {@code null} keys.
*/
@Override
public V remove(Object key) {
if (size == 0) {
@ -2275,42 +2088,11 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
x.color = false;
}
/**
* Returns the number of mappings in this map.
*
* @return the number of mappings in this map.
*/
@Override
public int size() {
return size;
}
/**
* Returns a sorted map over a range of this sorted map with all keys
* greater than or equal to the specified {@code startKey} and less than the
* specified {@code endKey}. Changes to the returned sorted map are
* reflected in this sorted map and vice versa.
* <p>
* Note: The returned map will not allow an insertion of a key outside the
* specified range.
*
* @param startKey
* the low boundary of the range (inclusive).
* @param endKey
* the high boundary of the range (exclusive),
* @return a sorted map with the key from the specified range.
* @throws ClassCastException
* if the start or end key cannot be compared with the keys in
* this map.
* @throws NullPointerException
* if the start or end key is {@code null} and the comparator
* cannot handle {@code null} keys.
* @throws IllegalArgumentException
* if the start key is greater than the end key, or if this map
* is itself a sorted map over a range of another sorted map and
* the specified range is outside of its range.
*/
@Override
public TSortedMap<K, V> subMap(K startKey, K endKey) {
if (comparator == null) {
@ -2325,28 +2107,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
throw new TIllegalArgumentException();
}
/**
* Returns a sorted map over a range of this sorted map with all keys that
* are greater than or equal to the specified {@code startKey}. Changes to
* the returned sorted map are reflected in this sorted map and vice versa.
* <p>
* Note: The returned map will not allow an insertion of a key outside the
* specified range.
*
* @param startKey
* the low boundary of the range specified.
* @return a sorted map where the keys are greater or equal to
* {@code startKey}.
* @throws ClassCastException
* if the specified key cannot be compared with the keys in this
* map.
* @throws NullPointerException
* if the specified key is {@code null} and the comparator
* cannot handle {@code null} keys.
* @throws IllegalArgumentException
* if this map itself a sorted map over a range of another map
* and the specified key is outside of its range.
*/
@Override
public TSortedMap<K, V> tailMap(K startKey) {
// Check for errors
@ -2358,25 +2118,6 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return new SubMap<>(startKey, this);
}
/**
* Returns a collection of the values contained in this map. The collection
* is backed by this map so changes to one are reflected by the other. The
* collection supports remove, removeAll, retainAll and clear operations,
* and it does not support add or addAll operations.
* <p>
* This method returns a collection which is the subclass of
* AbstractCollection. The iterator method of this subclass returns a
* "wrapper object" over the iterator of map's entrySet(). The {@code size}
* method wraps the map's size method and the {@code contains} method wraps
* the map's containsValue method.
* <p>
* The collection is created when this method is called for the first time
* and returned in response to all subsequent calls. This method may return
* different collections when multiple concurrent calls occur, since no
* synchronization is performed.
*
* @return a collection of the values contained in this map.
*/
@Override
public TCollection<V> values() {
if (cachedValues == null) {
@ -2405,4 +2146,3 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
return cachedValues;
}
}

View File

@ -35,33 +35,27 @@ import static org.junit.Assert.*;
import java.io.Serializable;
import java.text.CollationKey;
import java.text.Collator;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.*;
import org.junit.Test;
public class TreeMapTest {
public static class ReversedComparator implements Comparator {
public static class ReversedComparator implements Comparator<Object> {
@SuppressWarnings("unchecked")
@Override
public int compare(Object o1, Object o2) {
return -(((Comparable) o1).compareTo(o2));
return -(((Comparable<Object>) o1).compareTo(o2));
}
@SuppressWarnings("unchecked")
public boolean equals(Object o1, Object o2) {
return (((Comparable) o1).compareTo(o2)) == 0;
return (((Comparable<Object>) o1).compareTo(o2)) == 0;
}
}
// Regression for Harmony-1026
public static class MockComparator<T extends Comparable<T>> implements
Comparator<T>, Serializable {
public static class MockComparator<T extends Comparable<T>> implements Comparator<T> {
@Override
public int compare(T o1, T o2) {
if (o1 == o2) {
return 0;
@ -77,7 +71,7 @@ public class TreeMapTest {
// Regression for Harmony-1161
class MockComparatorNullTolerable implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
if (o1 == o2) {
return 0;
@ -92,12 +86,12 @@ public class TreeMapTest {
}
}
TreeMap tm;
TreeMap<Object, Object> tm;
Object objArray[] = new Object[1000];
public TreeMapTest() {
tm = new TreeMap();
tm = new TreeMap<>();
for (int i = 0; i < objArray.length; i++) {
Object x = objArray[i] = new Integer(i);
tm.put(x.toString(), x);
@ -108,8 +102,8 @@ public class TreeMapTest {
@Test
public void test_ConstructorLjava_util_Comparator() {
// Test for method java.util.TreeMap(java.util.Comparator)
Comparator comp = new ReversedComparator();
TreeMap reversedTreeMap = new TreeMap(comp);
Comparator<Object> comp = new ReversedComparator();
TreeMap<Object, Object> reversedTreeMap = new TreeMap<>(comp);
assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
.comparator() == comp);
reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
@ -124,22 +118,21 @@ public class TreeMapTest {
@Test
public void test_ConstructorLjava_util_Map() {
// Test for method java.util.TreeMap(java.util.Map)
TreeMap myTreeMap = new TreeMap(new HashMap(tm));
TreeMap<Object, Object> myTreeMap = new TreeMap<>(new HashMap<>(tm));
assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length);
for (Object element : objArray) {
assertTrue("Map has incorrect mappings", myTreeMap.get(
element.toString()).equals(element));
assertTrue("Map has incorrect mappings", myTreeMap.get(element.toString()).equals(element));
}
}
@Test
public void test_ConstructorLjava_util_SortedMap() {
// Test for method java.util.TreeMap(java.util.SortedMap)
Comparator comp = new ReversedComparator();
TreeMap reversedTreeMap = new TreeMap(comp);
Comparator<Object> comp = new ReversedComparator();
TreeMap<Object, Object> reversedTreeMap = new TreeMap<>(comp);
reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
TreeMap anotherTreeMap = new TreeMap(reversedTreeMap);
TreeMap<Object, Object> anotherTreeMap = new TreeMap<>(reversedTreeMap);
assertTrue("New tree map does not answer correct comparator",
anotherTreeMap.comparator() == comp);
assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
@ -159,34 +152,34 @@ public class TreeMapTest {
@Test
public void test_clone() {
// Test for method java.lang.Object java.util.TreeMap.clone()
TreeMap clonedMap = (TreeMap) tm.clone();
assertTrue("Cloned map does not equal the original map", clonedMap
.equals(tm));
assertTrue("Cloned map is the same reference as the original map",
clonedMap != tm);
@SuppressWarnings("unchecked")
TreeMap<Object, Object> clonedMap = (TreeMap<Object, Object>) tm.clone();
assertTrue("Cloned map does not equal the original map", clonedMap.equals(tm));
assertTrue("Cloned map is the same reference as the original map", clonedMap != tm);
for (Object element : objArray) {
assertTrue("Cloned map contains incorrect elements", clonedMap
.get(element.toString()) == tm.get(element.toString()));
}
TreeMap map = new TreeMap();
TreeMap<Object, Object> map = new TreeMap<>();
map.put("key", "value");
// get the keySet() and values() on the original Map
Set keys = map.keySet();
Collection values = map.values();
Set<Object> keys = map.keySet();
Collection<Object> values = map.values();
assertEquals("values() does not work", "value", values.iterator()
.next());
assertEquals("keySet() does not work", "key", keys.iterator().next());
AbstractMap map2 = (AbstractMap) map.clone();
@SuppressWarnings("unchecked")
Map<Object, Object> map2 = (Map<Object, Object>) map.clone();
map2.put("key", "value2");
Collection values2 = map2.values();
Collection<Object> values2 = map2.values();
assertTrue("values() is identical", values2 != values);
// values() and keySet() on the cloned() map should be different
assertEquals("values() was not cloned", "value2", values2.iterator()
.next());
map2.clear();
map2.put("key2", "value3");
Set key2 = map2.keySet();
Set<Object> key2 = map2.keySet();
assertTrue("keySet() is identical", key2 != keys);
assertEquals("keySet() was not cloned", "key2", key2.iterator().next());
}
@ -194,10 +187,9 @@ public class TreeMapTest {
@Test
public void test_comparator() {
// Test for method java.util.Comparator java.util.TreeMap.comparator()\
Comparator comp = new ReversedComparator();
TreeMap reversedTreeMap = new TreeMap(comp);
assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
.comparator() == comp);
Comparator<Object> comp = new ReversedComparator();
TreeMap<Object, Object> reversedTreeMap = new TreeMap<>(comp);
assertTrue("TreeMap answered incorrect comparator", reversedTreeMap.comparator() == comp);
reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
@ -227,15 +219,13 @@ public class TreeMapTest {
@Test
public void test_entrySet() {
// Test for method java.util.Set java.util.TreeMap.entrySet()
Set anEntrySet = tm.entrySet();
Iterator entrySetIterator = anEntrySet.iterator();
assertTrue("EntrySet is incorrect size",
anEntrySet.size() == objArray.length);
Map.Entry entry;
Set<Map.Entry<Object, Object>> anEntrySet = tm.entrySet();
Iterator<Map.Entry<Object, Object>> entrySetIterator = anEntrySet.iterator();
assertTrue("EntrySet is incorrect size", anEntrySet.size() == objArray.length);
Map.Entry<Object, Object> entry;
while (entrySetIterator.hasNext()) {
entry = (Map.Entry) entrySetIterator.next();
assertTrue("EntrySet does not contain correct mappings", tm
.get(entry.getKey()) == entry.getValue());
entry = entrySetIterator.next();
assertTrue("EntrySet does not contain correct mappings", tm.get(entry.getKey()) == entry.getValue());
}
}
@ -259,15 +249,14 @@ public class TreeMapTest {
public void test_headMapLjava_lang_Object() {
// Test for method java.util.SortedMap
// java.util.TreeMap.headMap(java.lang.Object)
Map head = tm.headMap("100");
Map<Object, Object> head = tm.headMap("100");
assertEquals("Returned map of incorrect size", 3, head.size());
assertTrue("Returned incorrect elements", head.containsKey("0")
&& head.containsValue(new Integer("1"))
&& head.containsKey("10"));
// Regression for Harmony-1026
TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(
new MockComparator());
TreeMap<Integer, Double> map = new TreeMap<>(new MockComparator<Integer>());
map.put(1, 2.1);
map.put(2, 3.1);
map.put(3, 4.5);
@ -310,16 +299,16 @@ public class TreeMapTest {
}
};
TreeMap<String, String> treemap = new TreeMap<String, String>(c);
TreeMap<String, String> treemap = new TreeMap<>(c);
assertEquals(0, treemap.headMap(null).size());
treemap = new TreeMap();
SortedMap<String, String> headMap = treemap.headMap("100");
headMap.headMap("100");
treemap = new TreeMap<>();
SortedMap<String, String> headMap = treemap.headMap("100");
headMap.headMap("100");
SortedMap<Integer,Integer> intMap,sub;
SortedMap<Integer,Integer> intMap,sub;
int size = 16;
intMap = new TreeMap<Integer,Integer>();
intMap = new TreeMap<>();
for(int i=0; i<size; i++) {
intMap.put(i,i);
}
@ -339,7 +328,7 @@ public class TreeMapTest {
}
size = 256;
intMap = new TreeMap<Integer,Integer>();
intMap = new TreeMap<>();
for(int i=0; i<size; i++) {
intMap.put(i,i);
}
@ -363,20 +352,17 @@ public class TreeMapTest {
@Test
public void test_keySet() {
// Test for method java.util.Set java.util.TreeMap.keySet()
Set ks = tm.keySet();
assertTrue("Returned set of incorrect size",
ks.size() == objArray.length);
Set<Object> ks = tm.keySet();
assertTrue("Returned set of incorrect size",ks.size() == objArray.length);
for (int i = 0; i < tm.size(); i++) {
assertTrue("Returned set is missing keys", ks.contains(new Integer(
i).toString()));
assertTrue("Returned set is missing keys", ks.contains(new Integer(i).toString()));
}
}
@Test
public void test_lastKey() {
// Test for method java.lang.Object java.util.TreeMap.lastKey()
assertTrue("Returned incorrect last key", tm.lastKey().equals(
objArray[objArray.length - 1].toString()));
assertTrue("Returned incorrect last key", tm.lastKey().equals(objArray[objArray.length - 1].toString()));
}
@Test
@ -394,19 +380,18 @@ public class TreeMapTest {
// expected
}
tm = new TreeMap();
tm = new TreeMap<>();
assertNull(tm.put(new Integer(1), new Object()));
}
@Test
public void test_putAllLjava_util_Map() {
// Test for method void java.util.TreeMap.putAll(java.util.Map)
TreeMap x = new TreeMap();
TreeMap<Object, Object> x = new TreeMap<>();
x.putAll(tm);
assertTrue("Map incorrect size after put", x.size() == tm.size());
for (Object element : objArray) {
assertTrue("Failed to put all elements", x.get(element.toString())
.equals(element));
assertTrue("Failed to put all elements", x.get(element.toString()).equals(element));
}
}
@ -429,8 +414,7 @@ public class TreeMapTest {
public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
// Test for method java.util.SortedMap
// java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
.toString());
SortedMap<Object, Object> subMap = tm.subMap(objArray[100].toString(), objArray[109].toString());
assertEquals("subMap is of incorrect size", 9, subMap.size());
for (int counter = 100; counter < 109; counter++) {
assertTrue("SubMap contains incorrect elements", subMap.get(
@ -445,8 +429,7 @@ public class TreeMapTest {
}
// Regression for Harmony-1161
TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
new MockComparatorNullTolerable());
TreeMap<String, String> treeMapWithNull = new TreeMap<>(new MockComparatorNullTolerable());
treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
@ -454,7 +437,7 @@ public class TreeMapTest {
assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$
// Regression test for typo in lastKey method
SortedMap<String, String> map = new TreeMap<String, String>();
SortedMap<String, String> map = new TreeMap<>();
map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$
map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$
map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$
@ -466,7 +449,7 @@ public class TreeMapTest {
@Test
public void test_subMap_Iterator() {
TreeMap<String, String> map = new TreeMap<String, String>();
TreeMap<String, String> map = new TreeMap<>();
String[] keys = { "1", "2", "3" };
String[] values = { "one", "two", "three" };
@ -476,15 +459,14 @@ public class TreeMapTest {
assertEquals(3, map.size());
Map subMap = map.subMap("", "test");
Map<String, String> subMap = map.subMap("", "test");
assertEquals(3, subMap.size());
Set entrySet = subMap.entrySet();
Iterator iter = entrySet.iterator();
Set<Map.Entry<String, String>> entrySet = subMap.entrySet();
Iterator<Map.Entry<String, String>> iter = entrySet.iterator();
int size = 0;
while (iter.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
.next();
Map.Entry<String, String> entry = iter.next();
assertTrue(map.containsKey(entry.getKey()));
assertTrue(map.containsValue(entry.getValue()));
size++;
@ -492,10 +474,10 @@ public class TreeMapTest {
assertEquals(map.size(), size);
Set<String> keySet = subMap.keySet();
iter = keySet.iterator();
Iterator<String> keyIter = keySet.iterator();
size = 0;
while (iter.hasNext()) {
String key = (String) iter.next();
while (keyIter.hasNext()) {
String key = keyIter.next();
assertTrue(map.containsKey(key));
size++;
}
@ -507,12 +489,10 @@ public class TreeMapTest {
public void test_tailMapLjava_lang_Object() {
// Test for method java.util.SortedMap
// java.util.TreeMap.tailMap(java.lang.Object)
Map tail = tm.tailMap(objArray[900].toString());
assertTrue("Returned map of incorrect size : " + tail.size(), tail
.size() == (objArray.length - 900) + 9);
Map<Object, Object> tail = tm.tailMap(objArray[900].toString());
assertTrue("Returned map of incorrect size : " + tail.size(), tail.size() == (objArray.length - 900) + 9);
for (int i = 900; i < objArray.length; i++) {
assertTrue("Map contains incorrect entries", tail
.containsValue(objArray[i]));
assertTrue("Map contains incorrect entries", tail.containsValue(objArray[i]));
}
// Regression for Harmony-1066
@ -520,7 +500,7 @@ public class TreeMapTest {
SortedMap<Integer,Integer> intMap,sub;
int size = 16;
intMap = new TreeMap<Integer,Integer>();
intMap = new TreeMap<>();
for(int i=0; i<size; i++) {
intMap.put(i,i);
}
@ -540,7 +520,7 @@ public class TreeMapTest {
}
size = 256;
intMap = new TreeMap<Integer,Integer>();
intMap = new TreeMap<>();
for(int i=0; i<size; i++) {
intMap.put(i,i);
}
@ -564,31 +544,29 @@ public class TreeMapTest {
@Test
public void test_values() {
// Test for method java.util.Collection java.util.TreeMap.values()
Collection vals = tm.values();
Collection<Object> vals = tm.values();
vals.iterator();
assertTrue("Returned collection of incorrect size",
vals.size() == objArray.length);
for (Object element : objArray) {
assertTrue("Collection contains incorrect elements", vals
.contains(element));
assertTrue("Collection contains incorrect elements", vals.contains(element));
}
TreeMap myTreeMap = new TreeMap();
TreeMap<Object, Object> myTreeMap = new TreeMap<>();
for (int i = 0; i < 100; i++) {
myTreeMap.put(objArray[i], objArray[i]);
}
Collection values = myTreeMap.values();
Collection<Object> values = myTreeMap.values();
values.remove(new Integer(0));
assertTrue(
"Removing from the values collection should remove from the original map",
assertTrue("Removing from the values collection should remove from the original map",
!myTreeMap.containsValue(new Integer(0)));
}
@Test
public void test_equals() throws Exception {
// comparing TreeMaps with different object types
Map m1 = new TreeMap();
Map m2 = new TreeMap();
Map<Object, Object> m1 = new TreeMap<>();
Map<Object, Object> m2 = new TreeMap<>();
m1.put("key1", "val1");
m1.put("key2", "val2");
m2.put(new Integer(1), "val1");
@ -597,8 +575,8 @@ public class TreeMapTest {
assertFalse("Maps should not be equal 2", m2.equals(m1));
// comparing TreeMap with HashMap
m1 = new TreeMap();
m2 = new HashMap();
m1 = new TreeMap<>();
m2 = new HashMap<>();
m1.put("key", "val");
m2.put(new Object(), "val");
assertFalse("Maps should not be equal 3", m1.equals(m2));
@ -612,8 +590,8 @@ public class TreeMapTest {
*/
@Test
public void test_entrySet_contains() throws Exception {
TreeMap master = new TreeMap<String, String>();
TreeMap test_map = new TreeMap<String, String>();
TreeMap<String, String> master = new TreeMap<>();
TreeMap<String, String> test_map = new TreeMap<>();
master.put("null", null);
Object[] entry = master.entrySet().toArray();
@ -630,7 +608,7 @@ public class TreeMapTest {
test_map.entrySet().containsAll(master.entrySet()));
master.clear();
master.put("null", '0');
master.put("null", "0");
entry = master.entrySet().toArray();
assertFalse("Null-valued entry should not equal non-null-valued entry",
test_map.entrySet().contains(entry[0]));