01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.unit;
07:
08: import java.util.Random;
09:
10: import org.h2.test.TestBase;
11: import org.h2.util.IntIntHashMap;
12:
13: /**
14: * Tests the IntHashMap class.
15: */
16: public class TestIntIntHashMap extends TestBase {
17:
18: Random rand = new Random();
19:
20: public void test() throws Exception {
21: rand.setSeed(10);
22: test(true);
23: test(false);
24: }
25:
26: public void test(boolean random) throws Exception {
27: int len = 2000;
28: int[] x = new int[len];
29: for (int i = 0; i < len; i++) {
30: int key = random ? rand.nextInt() : i;
31: x[i] = key;
32: }
33: IntIntHashMap map = new IntIntHashMap();
34: for (int i = 0; i < len; i++) {
35: map.put(x[i], i);
36: }
37: for (int i = 0; i < len; i++) {
38: if (map.get(x[i]) != i) {
39: throw new Error("get " + x[i] + " = " + map.get(i)
40: + " should be " + i);
41: }
42: }
43: for (int i = 1; i < len; i += 2) {
44: map.remove(x[i]);
45: }
46: for (int i = 1; i < len; i += 2) {
47: if (map.get(x[i]) != -1) {
48: throw new Error("get " + x[i] + " = " + map.get(i)
49: + " should be <=0");
50: }
51: }
52: for (int i = 1; i < len; i += 2) {
53: map.put(x[i], i);
54: }
55: for (int i = 0; i < len; i++) {
56: if (map.get(x[i]) != i) {
57: throw new Error("get " + x[i] + " = " + map.get(i)
58: + " should be " + i);
59: }
60: }
61: }
62: }
|