KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > unit > TestIntIntHashMap


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.unit;
6
7 import java.util.Random JavaDoc;
8
9 import org.h2.test.TestBase;
10 import org.h2.util.IntIntHashMap;
11
12 public class TestIntIntHashMap extends TestBase {
13
14     Random JavaDoc rand = new Random JavaDoc();
15
16     public void test() throws Exception JavaDoc {
17         rand.setSeed(10);
18         test(true);
19         test(false);
20     }
21
22     public void test(boolean random) throws Exception JavaDoc {
23         int len = 2000;
24         int[] x = new int[len];
25         for (int i = 0; i < len; i++) {
26             int key = random ? rand.nextInt() : i;
27             x[i] = key;
28         }
29         IntIntHashMap map = new IntIntHashMap();
30         for (int i = 0; i < len; i++) {
31             map.put(x[i], i);
32         }
33         for (int i = 0; i < len; i ++) {
34             if (map.get(x[i]) != i) {
35                 throw new Error JavaDoc("get " + x[i] + " = " + map.get(i) + " should be " + i);
36             }
37         }
38         for (int i = 1; i < len; i += 2) {
39             map.remove(x[i]);
40         }
41         for (int i = 1; i < len; i += 2) {
42             if (map.get(x[i]) != -1) {
43                 throw new Error JavaDoc("get " + x[i] + " = " + map.get(i) + " should be <=0");
44             }
45         }
46         for (int i = 1; i < len; i += 2) {
47             map.put(x[i], i);
48         }
49         for (int i = 0; i < len; i ++) {
50             if (map.get(x[i]) != i) {
51                 throw new Error JavaDoc("get " + x[i] + " = " + map.get(i) + " should be " + i);
52             }
53         }
54     }
55 }
56
Popular Tags