use of java.util.EnumMap in project robovm by robovm.
the class EnumMapTest method test_equalsLjava_lang_Object.
/**
* java.util.EnumMap#equals(Object)
*/
@SuppressWarnings({ "unchecked", "boxing" })
public void test_equalsLjava_lang_Object() {
EnumMap enumMap = new EnumMap(Size.class);
enumMap.put(Size.Small, 1);
EnumMap enumSizeMap = new EnumMap(Size.class);
assertFalse("Returned true for unequal EnumMap", enumSizeMap.equals(enumMap));
enumSizeMap.put(Size.Small, 1);
assertTrue("Returned false for equal EnumMap", enumSizeMap.equals(enumMap));
enumSizeMap.put(Size.Big, null);
assertFalse("Returned true for unequal EnumMap", enumSizeMap.equals(enumMap));
enumMap.put(Size.Middle, null);
assertFalse("Returned true for unequal EnumMap", enumSizeMap.equals(enumMap));
enumMap.remove(Size.Middle);
enumMap.put(Size.Big, 3);
assertFalse("Returned true for unequal EnumMap", enumSizeMap.equals(enumMap));
enumMap.put(Size.Big, null);
assertTrue("Returned false for equal EnumMap", enumSizeMap.equals(enumMap));
HashMap hashMap = new HashMap();
hashMap.put(Size.Small, 1);
assertFalse("Returned true for unequal EnumMap", hashMap.equals(enumMap));
hashMap.put(Size.Big, null);
assertTrue("Returned false for equal EnumMap", enumMap.equals(hashMap));
assertFalse("Should return false", enumSizeMap.equals(new Integer(1)));
}
use of java.util.EnumMap in project robovm by robovm.
the class EnumMapTest method testSerializationSelf.
/**
* serialization/deserialization.
*/
@SuppressWarnings({ "unchecked", "boxing" })
public void testSerializationSelf() throws Exception {
EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class);
enumColorMap.put(Color.Blue, 3);
SerializationTest.verifySelf(enumColorMap);
}
use of java.util.EnumMap in project robovm by robovm.
the class EnumMapTest method test_entrySet.
/**
* java.util.EnumMap#entrySet()
*/
@AndroidOnly("Map.Entry is indirectly modified on RI when Iterator.next() is invoked")
@SuppressWarnings({ "unchecked", "boxing" })
public void test_entrySet() {
EnumMap enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
MockEntry mockEntry = new MockEntry(Size.Middle, 1);
Set set = enumSizeMap.entrySet();
Set set1 = enumSizeMap.entrySet();
assertSame("Should be same", set1, set);
try {
set.add(mockEntry);
fail("Should throw UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected
}
assertTrue("Returned false for contained object", set.contains(mockEntry));
mockEntry = new MockEntry(Size.Middle, null);
assertFalse("Returned true for uncontained object", set.contains(mockEntry));
assertFalse("Returned true for uncontained object", set.contains(Size.Small));
mockEntry = new MockEntry(new Integer(1), 1);
assertFalse("Returned true for uncontained object", set.contains(mockEntry));
assertFalse("Returned true for uncontained object", set.contains(new Integer(1)));
mockEntry = new MockEntry(Size.Big, null);
assertTrue("Returned false for contained object", set.contains(mockEntry));
assertTrue("Returned false when the object can be removed", set.remove(mockEntry));
assertFalse("Returned true for uncontained object", set.contains(mockEntry));
assertFalse("Returned true when the object can not be removed", set.remove(mockEntry));
mockEntry = new MockEntry(new Integer(1), 1);
assertFalse("Returned true when the object can not be removed", set.remove(mockEntry));
assertFalse("Returned true when the object can not be removed", set.remove(new Integer(1)));
// The set is backed by the map so changes to one are reflected by the
// other.
enumSizeMap.put(Size.Big, 3);
mockEntry = new MockEntry(Size.Big, 3);
assertTrue("Returned false for contained object", set.contains(mockEntry));
enumSizeMap.remove(Size.Big);
assertFalse("Returned true for uncontained object", set.contains(mockEntry));
assertEquals("Wrong size", 1, set.size());
set.clear();
assertEquals("Wrong size", 0, set.size());
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.entrySet();
Collection c = new ArrayList();
c.add(new MockEntry(Size.Middle, 1));
assertTrue("Return wrong value", set.containsAll(c));
assertTrue("Remove does not success", set.removeAll(c));
enumSizeMap.put(Size.Middle, 1);
c.add(new MockEntry(Size.Big, 3));
assertTrue("Remove does not success", set.removeAll(c));
assertFalse("Should return false", set.removeAll(c));
assertEquals("Wrong size", 1, set.size());
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.entrySet();
c = new ArrayList();
c.add(new MockEntry(Size.Middle, 1));
c.add(new MockEntry(Size.Big, 3));
assertTrue("Retain does not success", set.retainAll(c));
assertEquals("Wrong size", 1, set.size());
assertFalse("Should return false", set.retainAll(c));
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.entrySet();
Object[] array = set.toArray();
assertEquals("Wrong length", 2, array.length);
Map.Entry entry = (Map.Entry) array[0];
assertEquals("Wrong key", Size.Middle, entry.getKey());
assertEquals("Wrong value", 1, entry.getValue());
Object[] array1 = new Object[10];
array1 = set.toArray();
assertEquals("Wrong length", 2, array1.length);
entry = (Map.Entry) array[0];
assertEquals("Wrong key", Size.Middle, entry.getKey());
assertEquals("Wrong value", 1, entry.getValue());
array1 = new Object[10];
array1 = set.toArray(array1);
assertEquals("Wrong length", 10, array1.length);
entry = (Map.Entry) array[1];
assertEquals("Wrong key", Size.Big, entry.getKey());
assertNull("Should be null", array1[2]);
set = enumSizeMap.entrySet();
Integer integer = new Integer("1");
assertFalse("Returned true when the object can not be removed", set.remove(integer));
assertTrue("Returned false when the object can be removed", set.remove(entry));
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.entrySet();
Iterator iter = set.iterator();
entry = (Map.Entry) iter.next();
assertTrue("Returned false for contained object", set.contains(entry));
mockEntry = new MockEntry(Size.Middle, 2);
assertFalse("Returned true for uncontained object", set.contains(mockEntry));
mockEntry = new MockEntry(new Integer(2), 2);
assertFalse("Returned true for uncontained object", set.contains(mockEntry));
entry = (Map.Entry) iter.next();
assertTrue("Returned false for contained object", set.contains(entry));
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.remove(Size.Big);
mockEntry = new MockEntry(Size.Big, null);
assertEquals("Wrong size", 1, set.size());
assertFalse("Returned true for uncontained object", set.contains(mockEntry));
enumSizeMap.put(Size.Big, 2);
mockEntry = new MockEntry(Size.Big, 2);
assertTrue("Returned false for contained object", set.contains(mockEntry));
iter.remove();
try {
iter.remove();
fail("Should throw IllegalStateException");
} catch (IllegalStateException e) {
// Expected
}
try {
entry.setValue(2);
fail("Should throw IllegalStateException");
} catch (IllegalStateException e) {
// Expected
}
try {
set.contains(entry);
fail("Should throw IllegalStateException");
} catch (IllegalStateException e) {
// Expected
}
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.entrySet();
iter = set.iterator();
entry = (Map.Entry) iter.next();
assertEquals("Wrong key", Size.Middle, entry.getKey());
assertTrue("Returned false for contained object", set.contains(entry));
enumSizeMap.put(Size.Middle, 3);
assertTrue("Returned false for contained object", set.contains(entry));
entry.setValue(2);
assertTrue("Returned false for contained object", set.contains(entry));
assertFalse("Returned true for uncontained object", set.remove(new Integer(1)));
iter.next();
assertEquals("Wrong key", Size.Middle, entry.getKey());
set.clear();
assertEquals("Wrong size", 0, set.size());
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.entrySet();
iter = set.iterator();
mockEntry = new MockEntry(Size.Middle, 1);
assertFalse("Wrong result", entry.equals(mockEntry));
try {
iter.remove();
fail("Should throw IllegalStateException");
} catch (IllegalStateException e) {
// Expected
}
entry = (Map.Entry) iter.next();
assertEquals("Wrong key", Size.Middle, entry.getKey());
assertTrue("Should return true", entry.equals(mockEntry));
assertEquals("Should be equal", mockEntry.hashCode(), entry.hashCode());
mockEntry = new MockEntry(Size.Big, 1);
assertFalse("Wrong result", entry.equals(mockEntry));
entry = (Map.Entry) iter.next();
assertFalse("Wrong result", entry.equals(mockEntry));
assertEquals("Wrong key", Size.Big, entry.getKey());
iter.remove();
assertFalse("Wrong result", entry.equals(mockEntry));
assertEquals("Wrong size", 1, set.size());
try {
iter.remove();
fail("Should throw IllegalStateException");
} catch (IllegalStateException e) {
// Expected
}
try {
iter.next();
fail("Should throw NoSuchElementException");
} catch (NoSuchElementException e) {
// Expected
}
}
use of java.util.EnumMap in project robovm by robovm.
the class EnumMapTest method test_keySet.
/**
* java.util.EnumMap#keySet()
*/
@SuppressWarnings({ "unchecked", "boxing" })
public void test_keySet() {
EnumMap enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 2);
enumSizeMap.put(Size.Big, null);
Set set = enumSizeMap.keySet();
Set set1 = enumSizeMap.keySet();
assertSame("Should be same", set1, set);
try {
set.add(Size.Big);
fail("Should throw UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected
}
assertTrue("Returned false for contained object", set.contains(Size.Middle));
assertTrue("Returned false for contained object", set.contains(Size.Big));
assertFalse("Returned true for uncontained object", set.contains(Size.Small));
assertFalse("Returned true for uncontained object", set.contains(new Integer(1)));
assertTrue("Returned false when the object can be removed", set.remove(Size.Big));
assertFalse("Returned true for uncontained object", set.contains(Size.Big));
assertFalse("Returned true when the object can not be removed", set.remove(Size.Big));
assertFalse("Returned true when the object can not be removed", set.remove(new Integer(1)));
// The set is backed by the map so changes to one are reflected by the
// other.
enumSizeMap.put(Size.Big, 3);
assertTrue("Returned false for contained object", set.contains(Size.Big));
enumSizeMap.remove(Size.Big);
assertFalse("Returned true for uncontained object", set.contains(Size.Big));
assertEquals("Wrong size", 1, set.size());
set.clear();
assertEquals("Wrong size", 0, set.size());
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.keySet();
Collection c = new ArrayList();
c.add(Size.Big);
assertTrue("Should return true", set.containsAll(c));
c.add(Size.Small);
assertFalse("Should return false", set.containsAll(c));
assertTrue("Should return true", set.removeAll(c));
assertEquals("Wrong size", 1, set.size());
assertFalse("Should return false", set.removeAll(c));
assertEquals("Wrong size", 1, set.size());
try {
set.addAll(c);
fail("Should throw UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected
}
enumSizeMap.put(Size.Big, null);
assertEquals("Wrong size", 2, set.size());
assertTrue("Should return true", set.retainAll(c));
assertEquals("Wrong size", 1, set.size());
assertFalse("Should return false", set.retainAll(c));
assertEquals(1, set.size());
Object[] array = set.toArray();
assertEquals("Wrong length", 1, array.length);
assertEquals("Wrong key", Size.Big, array[0]);
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.keySet();
c = new ArrayList();
c.add(Color.Blue);
assertFalse("Should return false", set.remove(c));
assertEquals("Wrong size", 2, set.size());
assertTrue("Should return true", set.retainAll(c));
assertEquals("Wrong size", 0, set.size());
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.keySet();
Iterator iter = set.iterator();
Enum enumKey = (Enum) iter.next();
assertTrue("Returned false for contained object", set.contains(enumKey));
enumKey = (Enum) iter.next();
assertTrue("Returned false for contained object", set.contains(enumKey));
enumSizeMap.remove(Size.Big);
assertFalse("Returned true for uncontained object", set.contains(enumKey));
iter.remove();
try {
iter.remove();
fail("Should throw IllegalStateException");
} catch (IllegalStateException e) {
// Expected
}
assertFalse("Returned true for uncontained object", set.contains(enumKey));
iter = set.iterator();
enumKey = (Enum) iter.next();
assertTrue("Returned false for contained object", set.contains(enumKey));
enumSizeMap.put(Size.Middle, 3);
assertTrue("Returned false for contained object", set.contains(enumKey));
enumSizeMap = new EnumMap(Size.class);
enumSizeMap.put(Size.Middle, 1);
enumSizeMap.put(Size.Big, null);
set = enumSizeMap.keySet();
iter = set.iterator();
try {
iter.remove();
fail("Should throw IllegalStateException");
} catch (IllegalStateException e) {
// Expected
}
enumKey = (Enum) iter.next();
assertEquals("Wrong key", Size.Middle, enumKey);
assertSame("Wrong key", Size.Middle, enumKey);
assertFalse("Returned true for unequal object", iter.equals(enumKey));
iter.remove();
assertFalse("Returned true for uncontained object", set.contains(enumKey));
try {
iter.remove();
fail("Should throw IllegalStateException");
} catch (IllegalStateException e) {
// Expected
}
assertEquals("Wrong size", 1, set.size());
enumKey = (Enum) iter.next();
assertEquals("Wrong key", Size.Big, enumKey);
iter.remove();
try {
iter.next();
fail("Should throw NoSuchElementException");
} catch (NoSuchElementException e) {
// Expected
}
}
use of java.util.EnumMap in project robovm by robovm.
the class EnumMapTest method test_size.
/**
* java.util.EnumMap#size()
*/
@SuppressWarnings({ "unchecked", "boxing" })
public void test_size() {
EnumMap enumSizeMap = new EnumMap(Size.class);
assertEquals("Wrong size", 0, enumSizeMap.size());
enumSizeMap.put(Size.Small, 1);
assertEquals("Wrong size", 1, enumSizeMap.size());
enumSizeMap.put(Size.Small, 0);
assertEquals("Wrong size", 1, enumSizeMap.size());
try {
enumSizeMap.put(Color.Red, 2);
fail("Expected ClassCastException");
} catch (ClassCastException e) {
// Expected
}
assertEquals("Wrong size", 1, enumSizeMap.size());
enumSizeMap.put(Size.Middle, null);
assertEquals("Wrong size", 2, enumSizeMap.size());
enumSizeMap.remove(Size.Big);
assertEquals("Wrong size", 2, enumSizeMap.size());
enumSizeMap.remove(Size.Middle);
assertEquals("Wrong size", 1, enumSizeMap.size());
enumSizeMap.remove(Color.Green);
assertEquals("Wrong size", 1, enumSizeMap.size());
EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class);
enumColorMap.put(Color.Green, 2);
assertEquals("Wrong size", 1, enumColorMap.size());
enumColorMap.remove(Color.Green);
assertEquals("Wrong size", 0, enumColorMap.size());
EnumMap enumEmptyMap = new EnumMap<Empty, Double>(Empty.class);
assertEquals("Wrong size", 0, enumEmptyMap.size());
}
Aggregations