1 5 package org.h2.test.unit; 6 7 import java.util.Random ; 8 9 import org.h2.test.TestBase; 10 import org.h2.util.IntIntHashMap; 11 12 public class TestIntIntHashMap extends TestBase { 13 14 Random rand = new Random (); 15 16 public void test() throws Exception { 17 rand.setSeed(10); 18 test(true); 19 test(false); 20 } 21 22 public void test(boolean random) throws Exception { 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 ("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 ("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 ("get " + x[i] + " = " + map.get(i) + " should be " + i); 52 } 53 } 54 } 55 } 56 | Popular Tags |