Search in sources :

Example 11 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project j2objc by google.

the class TreeMapTest method test_values.

/**
     * @tests java.util.TreeMap#values()
     */
public void test_values() {
    // Test for method java.util.Collection java.util.TreeMap.values()
    Collection 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));
    }
    TreeMap myTreeMap = new TreeMap();
    for (int i = 0; i < 100; i++) {
        myTreeMap.put(objArray[i], objArray[i]);
    }
    Collection values = myTreeMap.values();
    new Support_UnmodifiableCollectionTest("Test Returned Collection From TreeMap.values()", values).runTest();
    values.remove(new Integer(0));
    assertTrue("Removing from the values collection should remove from the original map", !myTreeMap.containsValue(new Integer(0)));
}
Also used : Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest) Collection(java.util.Collection) TreeMap(java.util.TreeMap)

Example 12 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project robovm by robovm.

the class CollectionsTest method test_unmodifiableSetLjava_util_Set.

/**
     * java.util.Collections#unmodifiableSet(java.util.Set)
     */
public void test_unmodifiableSetLjava_util_Set() {
    // Test for method java.util.Set
    // java.util.Collections.unmodifiableSet(java.util.Set)
    boolean exception = false;
    Set c = Collections.unmodifiableSet(s);
    assertTrue("Returned set is of incorrect size", c.size() == s.size());
    Iterator i = ll.iterator();
    while (i.hasNext()) assertTrue("Returned set missing elements", c.contains(i.next()));
    try {
        c.add(new Object());
    } catch (UnsupportedOperationException e) {
        exception = true;
    // Correct
    }
    if (!exception) {
        fail("Allowed modification of set");
    }
    try {
        c.remove(new Object());
        fail("Allowed modification of set");
    } catch (UnsupportedOperationException e) {
    // Correct
    }
    Set mySet = Collections.unmodifiableSet(new HashSet());
    assertTrue("Should not contain null", !mySet.contains(null));
    mySet = Collections.unmodifiableSet(Collections.singleton(null));
    assertTrue("Should contain null", mySet.contains(null));
    mySet = new TreeSet();
    for (int counter = 0; counter < 100; counter++) {
        mySet.add(objArray[counter]);
    }
    new Support_UnmodifiableCollectionTest("", Collections.unmodifiableSet(mySet)).runTest();
}
Also used : SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) TreeSet(java.util.TreeSet) Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 13 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project robovm by robovm.

the class CollectionsTest method test_unmodifiableListLjava_util_List.

/**
     * java.util.Collections#unmodifiableList(java.util.List)
     */
public void test_unmodifiableListLjava_util_List() {
    // Test for method java.util.List
    // java.util.Collections.unmodifiableList(java.util.List)
    // test with a Sequential Access List
    boolean exception = false;
    List c = Collections.unmodifiableList(ll);
    // Ensure a NPE is thrown if the list is NULL
    try {
        Collections.unmodifiableList(null);
        fail("Expected NullPointerException for null list parameter");
    } catch (NullPointerException e) {
    }
    assertTrue("Returned list is of incorrect size", c.size() == ll.size());
    assertTrue("Returned List should not implement Random Access interface", !(c instanceof RandomAccess));
    Iterator i = ll.iterator();
    while (i.hasNext()) assertTrue("Returned list missing elements", c.contains(i.next()));
    try {
        c.add(new Object());
    } catch (UnsupportedOperationException e) {
        exception = true;
    // Correct
    }
    if (!exception) {
        fail("Allowed modification of list");
    }
    try {
        c.remove(new Object());
        fail("Allowed modification of list");
    } catch (UnsupportedOperationException e) {
    // Correct
    }
    // test with a Random Access List
    List smallList = new ArrayList();
    smallList.add(null);
    smallList.add("yoink");
    c = Collections.unmodifiableList(smallList);
    assertNull("First element should be null", c.get(0));
    assertTrue("List should contain null", c.contains(null));
    assertTrue("T1. Returned List should implement Random Access interface", c instanceof RandomAccess);
    smallList = new ArrayList();
    for (int counter = 0; counter < 100; counter++) {
        smallList.add(objArray[counter]);
    }
    List myList = Collections.unmodifiableList(smallList);
    assertTrue("List should not contain null", !myList.contains(null));
    assertTrue("T2. Returned List should implement Random Access interface", myList instanceof RandomAccess);
    assertTrue("get failed on unmodifiable list", myList.get(50).equals(new Integer(50)));
    ListIterator listIterator = myList.listIterator();
    for (int counter = 0; listIterator.hasNext(); counter++) {
        assertTrue("List has wrong elements", ((Integer) listIterator.next()).intValue() == counter);
    }
    new Support_UnmodifiableCollectionTest("", smallList).runTest();
}
Also used : Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) RandomAccess(java.util.RandomAccess) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ListIterator(java.util.ListIterator)

Example 14 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project robovm by robovm.

the class HashMapTest method test_values.

/**
     * java.util.HashMap#values()
     */
public void test_values() {
    // Test for method java.util.Collection java.util.HashMap.values()
    Collection c = hm.values();
    assertTrue("Returned collection of incorrect size()", c.size() == hm.size());
    for (int i = 0; i < objArray.length; i++) assertTrue("Returned collection does not contain all keys", c.contains(objArray[i]));
    HashMap myHashMap = new HashMap();
    for (int i = 0; i < 100; i++) myHashMap.put(objArray2[i], objArray[i]);
    Collection values = myHashMap.values();
    new Support_UnmodifiableCollectionTest("Test Returned Collection From HashMap.values()", values).runTest();
    values.remove(new Integer(0));
    assertTrue("Removing from the values collection should remove from the original map", !myHashMap.containsValue(new Integer(0)));
}
Also used : Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest)

Example 15 with Support_UnmodifiableCollectionTest

use of tests.support.Support_UnmodifiableCollectionTest in project robovm by robovm.

the class LinkedHashMapTest method test_values.

/**
     * java.util.LinkedHashMap#values()
     */
public void test_values() {
    // Test for method java.util.Collection java.util.LinkedHashMap.values()
    Collection c = hm.values();
    assertTrue("Returned collection of incorrect size()", c.size() == hm.size());
    for (int i = 0; i < objArray.length; i++) assertTrue("Returned collection does not contain all keys", c.contains(objArray[i]));
    LinkedHashMap myLinkedHashMap = new LinkedHashMap();
    for (int i = 0; i < 100; i++) myLinkedHashMap.put(objArray2[i], objArray[i]);
    Collection values = myLinkedHashMap.values();
    new Support_UnmodifiableCollectionTest("Test Returned Collection From LinkedHashMap.values()", values).runTest();
    values.remove(new Integer(0));
    assertTrue("Removing from the values collection should remove from the original map", !myLinkedHashMap.containsValue(new Integer(0)));
}
Also used : Support_UnmodifiableCollectionTest(tests.support.Support_UnmodifiableCollectionTest) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Support_UnmodifiableCollectionTest (tests.support.Support_UnmodifiableCollectionTest)15 Collection (java.util.Collection)8 Iterator (java.util.Iterator)6 ListIterator (java.util.ListIterator)6 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 Enumeration (java.util.Enumeration)2 HashSet (java.util.HashSet)2 Hashtable (java.util.Hashtable)2 LinkedHashMap (java.util.LinkedHashMap)2 RandomAccess (java.util.RandomAccess)2 Set (java.util.Set)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1