jdk/test/java/util/Collections/CheckedMapBash.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4904067 5023830
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Unit test for Collections.checkedMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class CheckedMapBash {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static Object nil = new Integer(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        int numItr = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        int mapSize = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        // Linked List test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        for (int i=0; i<numItr; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            Map m = newMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            Object head = nil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            for (int j=0; j<mapSize; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                Object newHead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                    newHead = new Integer(rnd.nextInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                } while (m.containsKey(newHead));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                m.put(newHead, head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                head = newHead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (m.size() != mapSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                fail("Size not as expected.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                HashMap hm = new HashMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                if (! (hm.hashCode() == m.hashCode() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                       hm.entrySet().hashCode() == m.entrySet().hashCode() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                       hm.keySet().hashCode() == m.keySet().hashCode()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    fail("Incorrect hashCode computation.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                if (! (hm.equals(m) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                       hm.entrySet().equals(m.entrySet()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                       hm.keySet().equals(m.keySet()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                       m.equals(hm) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                       m.entrySet().equals(hm.entrySet()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                       m.keySet().equals(hm.keySet())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    fail("Incorrect equals computation.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            Map m2 = newMap(); m2.putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            m2.values().removeAll(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (m2.size()!= 1 || !m2.containsValue(nil))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                fail("Collection views test failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            int j=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            while (head != nil) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                if (!m.containsKey(head))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    fail("Linked list doesn't contain a link.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                Object newHead = m.get(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                if (newHead == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    fail("Could not retrieve a link.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                m.remove(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                head = newHead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (!m.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                fail("Map nonempty after removing all links.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (j != mapSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                fail("Linked list size not as expected.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Map m = newMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        for (int i=0; i<mapSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (m.put(new Integer(i), new Integer(2*i)) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                fail("put returns a non-null value erroenously.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i=0; i<2*mapSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (m.containsValue(new Integer(i)) != (i%2==0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                fail("contains value "+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (m.put(nil, nil) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            fail("put returns a null value erroenously.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Map m2 = newMap(); m2.putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (!m.equals(m2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            fail("Clone not equal to original. (1)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (!m2.equals(m))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            fail("Clone not equal to original. (2)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Set s = m.entrySet(), s2 = m2.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (!s.equals(s2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            fail("Clone not equal to original. (3)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (!s2.equals(s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            fail("Clone not equal to original. (4)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (!s.containsAll(s2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            fail("Original doesn't contain clone!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (!s2.containsAll(s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            fail("Clone doesn't contain original!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        s2.removeAll(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (!m2.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            fail("entrySet().removeAll failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        m2.putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        m2.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (!m2.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            fail("clear failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Iterator i = m.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        while(i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (!m.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            fail("Iterator.remove() failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static Map newMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Map m = Collections.checkedMap(new HashMap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                       Integer.class, Integer.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (!m.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            fail("New instance non empty.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    static void fail(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        throw new RuntimeException(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}