jdk/test/java/util/concurrent/ConcurrentHashMap/MapCheck.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4347 ab0a9f495844
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
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @bug 4486658
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @compile -source 1.5 MapCheck.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/timeout=240 MapCheck
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @summary Times and checks basic map operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class MapCheck {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final int absentSize = 1 << 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final int absentMask = absentSize - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static Object[] absent = new Object[absentSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final Object MISSING = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static TestTimer timer = new TestTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static void reallyAssert(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (!b) throw new Error("Failed Assertion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Class mapClass = java.util.concurrent.ConcurrentHashMap.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int numTests = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int size = 50000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                mapClass = Class.forName(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            } catch(ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                throw new RuntimeException("Class " + args[0] + " not found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (args.length > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            numTests = Integer.parseInt(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (args.length > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            size = Integer.parseInt(args[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        boolean doSerializeTest = args.length > 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (int i = 0; i < absentSize; ++i) absent[i] = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Object[] key = new Object[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        for (int i = 0; i < size; ++i) key[i] = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        forceMem(size * 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (int rep = 0; rep < numTests; ++rep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            runTest(newMap(mapClass), key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        TestTimer.printStats();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (doSerializeTest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            stest(newMap(mapClass), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static Map newMap(Class cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            Map m = (Map)cl.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new RuntimeException("Can't instantiate " + cl + ": " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static void runTest(Map s, Object[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        shuffle(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int size = key.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        test(s, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        long time = System.currentTimeMillis() - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static void forceMem(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // force enough memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Long[] junk = new Long[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int i = 0; i < junk.length; ++i) junk[i] = new Long(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (int i = 0; i < junk.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            sum += (int)(junk[i].longValue() + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (sum == 0) System.out.println("Useless number = " + sum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        junk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        //        System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static void t1(String nm, int n, Map s, Object[] key, int expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int iters = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        timer.start(nm, n * iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        for (int j = 0; j < iters; ++j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (s.get(key[i]) != null) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        reallyAssert (sum == expect * iters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static void t2(String nm, int n, Map s, Object[] key, int expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        timer.start(nm, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (s.remove(key[i]) != null) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        reallyAssert (sum == expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static void t3(String nm, int n, Map s, Object[] key, int expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        timer.start(nm, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (s.put(key[i], absent[i & absentMask]) == null) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        reallyAssert (sum == expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static void t4(String nm, int n, Map s, Object[] key, int expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        timer.start(nm, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (s.containsKey(key[i])) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        reallyAssert (sum == expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static void t5(String nm, int n, Map s, Object[] key, int expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        timer.start(nm, n/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (int i = n-2; i >= 0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (s.remove(key[i]) != null) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        reallyAssert (sum == expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    static void t6(String nm, int n, Map s, Object[] k1, Object[] k2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        timer.start(nm, n * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (s.get(k1[i]) != null) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (s.get(k2[i & absentMask]) != null) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        reallyAssert (sum == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    static void t7(String nm, int n, Map s, Object[] k1, Object[] k2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        timer.start(nm, n * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (s.containsKey(k1[i])) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (s.containsKey(k2[i & absentMask])) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        reallyAssert (sum == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    static void t8(String nm, int n, Map s, Object[] key, int expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        timer.start(nm, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (s.get(key[i]) != null) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        reallyAssert (sum == expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    static void t9(Map s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int iters = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        timer.start("ContainsValue (/n)     ", iters * s.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int step = absentSize / iters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        for (int i = 0; i < absentSize; i += step)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (s.containsValue(absent[i])) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        reallyAssert (sum != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    static void ktest(Map s, int size, Object[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        timer.start("ContainsKey            ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Set ks = s.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (ks.contains(key[i])) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    static void ittest1(Map s, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        timer.start("Iter Key               ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if(it.next() != MISSING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    static void ittest2(Map s, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        timer.start("Iter Value             ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        for (Iterator it = s.values().iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if(it.next() != MISSING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    static void ittest3(Map s, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        timer.start("Iter Entry             ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if(it.next() != MISSING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    static void ittest4(Map s, int size, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        IdentityHashMap seen = new IdentityHashMap(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        reallyAssert (s.size() == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        timer.start("Iter XEntry            ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        Iterator it = s.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        Object k = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        Object v = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        for (int i = 0; i < size-pos; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            Map.Entry x = (Map.Entry)(it.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            k = x.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            v = x.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            seen.put(k, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (x != MISSING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        reallyAssert (s.containsKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        reallyAssert (!s.containsKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            Map.Entry x = (Map.Entry)(it.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            Object k2 = x.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            seen.put(k2, k2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (x != MISSING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        reallyAssert (s.size() == size-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        s.put(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        reallyAssert (seen.size() == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        reallyAssert (s.size() == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    static void ittest(Map s, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        ittest1(s, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        ittest2(s, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ittest3(s, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        //        for (int i = 0; i < size-1; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        //            ittest4(s, size, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    static void entest1(Hashtable ht, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        timer.start("Iter Enumeration Key   ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        for (Enumeration en = ht.keys(); en.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (en.nextElement() != MISSING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    static void entest2(Hashtable ht, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        timer.start("Iter Enumeration Value ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        for (Enumeration en = ht.elements(); en.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (en.nextElement() != MISSING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    static void entest3(Hashtable ht, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        timer.start("Iterf Enumeration Key  ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        Enumeration en = ht.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        for (int i = 0; i < size; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (en.nextElement() != MISSING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    static void entest4(Hashtable ht, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        timer.start("Iterf Enumeration Value", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        Enumeration en = ht.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        for (int i = 0; i < size; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (en.nextElement() != MISSING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    static void entest(Map s, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (s instanceof Hashtable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            Hashtable ht = (Hashtable)s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            //            entest3(ht, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            //            entest4(ht, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            entest1(ht, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            entest2(ht, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            entest1(ht, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            entest2(ht, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            entest1(ht, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            entest2(ht, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    static void rtest(Map s, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        timer.start("Remove (iterator)      ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    static void rvtest(Map s, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        timer.start("Remove (iterator)      ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        for (Iterator it = s.values().iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    static void dtest(Map s, int size, Object[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        timer.start("Put (putAll)           ", size * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        Map s2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            s2 = (Map) (s.getClass().newInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            s2.putAll(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        catch (Exception e) { e.printStackTrace(); return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        timer.start("Iter Equals            ", size * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        boolean eqt = s2.equals(s) && s.equals(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        reallyAssert (eqt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        timer.start("Iter HashCode          ", size * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int shc = s.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int s2hc = s2.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        reallyAssert (shc == s2hc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        timer.start("Put (present)          ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        s2.putAll(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        timer.start("Iter EntrySet contains ", size * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        Set es2 = s2.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        for (Iterator i1 = s.entrySet().iterator(); i1.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            Object entry = i1.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (es2.contains(entry)) ++sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        reallyAssert (sum == size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        t6("Get                    ", size, s2, key, absent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        Object hold = s2.get(key[size-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        s2.put(key[size-1], absent[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        timer.start("Iter Equals            ", size * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        eqt = s2.equals(s) && s.equals(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        reallyAssert (!eqt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        timer.start("Iter HashCode          ", size * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        int s1h = s.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        int s2h = s2.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        reallyAssert (s1h != s2h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        s2.put(key[size-1], hold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        timer.start("Remove (iterator)      ", size * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Iterator s2i = s2.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        Set es = s.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        while (s2i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            es.remove(s2i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        reallyAssert (s.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        timer.start("Clear                  ", size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        s2.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        timer.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        reallyAssert (s2.isEmpty() && s.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    static void stest(Map s, int size) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (!(s instanceof Serializable))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        System.out.print("Serialize              : ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            s.put(new Integer(i), Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        FileOutputStream fs = new FileOutputStream("MapCheck.dat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        out.writeObject(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        FileInputStream is = new FileInputStream("MapCheck.dat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        Map m = (Map)in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        long endTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        long time = endTime - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        System.out.print(time + "ms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (s instanceof IdentityHashMap) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        reallyAssert (s.equals(m));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    static void test(Map s, Object[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        int size = key.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        t3("Put (absent)           ", size, s, key, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        t3("Put (present)          ", size, s, key, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        t7("ContainsKey            ", size, s, key, absent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        t4("ContainsKey            ", size, s, key, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        ktest(s, size, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        t4("ContainsKey            ", absentSize, s, absent, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        t6("Get                    ", size, s, key, absent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        t1("Get (present)          ", size, s, key, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        t1("Get (absent)           ", absentSize, s, absent, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        t2("Remove (absent)        ", absentSize, s, absent, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        t5("Remove (present)       ", size, s, key, size / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        t3("Put (half present)     ", size, s, key, size / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        ittest(s, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        entest(s, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        t9(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        rtest(s, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        t4("ContainsKey            ", size, s, key, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        t2("Remove (absent)        ", size, s, key, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        t3("Put (presized)         ", size, s, key, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        dtest(s, size, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    static class TestTimer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        private long numOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        private long startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        private String cname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        static final java.util.TreeMap accum = new java.util.TreeMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        static void printStats() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                Map.Entry e = (Map.Entry)(it.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                Stats stats = ((Stats)(e.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                int n = stats.number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                double t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    t = stats.sum / n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    t = stats.least;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                long nano = Math.round(1000000.0 * t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                System.out.println(e.getKey() + ": " + nano);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        void start(String name, long numOps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            this.cname = classify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            this.numOps = numOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        String classify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (name.startsWith("Get"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                return "Get                    ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            else if (name.startsWith("Put"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                return "Put                    ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            else if (name.startsWith("Remove"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                return "Remove                 ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            else if (name.startsWith("Iter"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                return "Iter                   ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        void finish() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            long endTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            long time = endTime - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            double timePerOp = ((double)time)/numOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            Object st = accum.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (st == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                accum.put(name, new Stats(timePerOp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                Stats stats = (Stats) st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                stats.sum += timePerOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                stats.number++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                if (timePerOp < stats.least) stats.least = timePerOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                st = accum.get(cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                if (st == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    accum.put(cname, new Stats(timePerOp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    Stats stats = (Stats) st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    stats.sum += timePerOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    stats.number++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    if (timePerOp < stats.least) stats.least = timePerOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    static class Stats {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        double sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        double least;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        int number = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        Stats(double t) { least = t; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    static Random rng = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    static void shuffle(Object[] keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        int size = keys.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        for (int i=size; i>1; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            int r = rng.nextInt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            Object t = keys[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            keys[i-1] = keys[r];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            keys[r] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
}