1 5 package org.h2.test.unit; 6 7 import java.sql.SQLException ; 8 import java.util.Comparator ; 9 import java.util.HashMap ; 10 import java.util.Random ; 11 12 import org.h2.store.DataHandler; 13 import org.h2.store.FileStore; 14 import org.h2.test.TestBase; 15 import org.h2.util.ObjectArray; 16 import org.h2.util.ValueHashMap; 17 import org.h2.value.CompareMode; 18 import org.h2.value.Value; 19 import org.h2.value.ValueInt; 20 21 public class TestValueHashMap extends TestBase implements DataHandler { 22 23 CompareMode compareMode = new CompareMode(null, null); 24 25 public void test() throws Exception { 26 ValueHashMap map = new ValueHashMap(this); 27 HashMap hash = new HashMap (); 28 Random random = new Random (1); 29 Comparator vc = new Comparator () { 30 public int compare(Object o1, Object o2) { 31 Value v1 = (Value)o1; 32 Value v2 = (Value)o2; 33 try { 34 return v1.compareTo(v2, compareMode); 35 } catch(SQLException e) { 36 throw new Error (e); 37 } 38 } 39 }; 40 for(int i=0; i<10000; i++) { 41 int op = random.nextInt(10); 42 Value key = ValueInt.get(random.nextInt(100)); 43 Value value = ValueInt.get(random.nextInt(100)); 44 switch(op) { 45 case 0: 46 map.put(key, value); 47 hash.put(key, value); 48 break; 49 case 1: 50 map.remove(key); 51 hash.remove(key); 52 break; 53 case 2: 54 Value v1 = (Value) map.get(key); 55 Value v2 = (Value) hash.get(key); 56 check((v1==null && v2==null) || v1.compareEqual(v2)); 57 break; 58 case 3: { 59 ObjectArray a1 = map.keys(); 60 ObjectArray a2 = new ObjectArray(hash.keySet()); 61 check(a1.size(), a2.size()); 62 a1.sort(vc); 63 a2.sort(vc); 64 for(int j=0; j<a1.size(); j++) { 65 check(((Value)a1.get(j)).compareEqual((Value)a2.get(j))); 66 } 67 break; 68 } 69 case 4: 70 ObjectArray a1 = map.values(); 71 ObjectArray a2 = new ObjectArray(hash.values()); 72 check(a1.size(), a2.size()); 73 a1.sort(vc); 74 a2.sort(vc); 75 for(int j=0; j<a1.size(); j++) { 76 check(((Value)a1.get(j)).compareEqual((Value)a2.get(j))); 77 } 78 break; 79 } 80 } 81 } 82 83 public boolean getTextStorage() { 84 return false; 85 } 86 87 public String getDatabasePath() { 88 return null; 89 } 90 91 public FileStore openFile(String name, boolean mustExist) throws SQLException { 92 return null; 93 } 94 95 public int getChecksum(byte[] data, int start, int end) { 96 return 0; 97 } 98 99 public void checkPowerOff() throws SQLException { 100 } 101 102 public void checkWritingAllowed() throws SQLException { 103 } 104 105 public void freeUpDiskSpace() throws SQLException { 106 } 107 108 public void handleInvalidChecksum() throws SQLException { 109 } 110 111 public int compareTypeSave(Value a, Value b) throws SQLException { 112 return a.compareTo(b, compareMode); 113 } 114 115 public int getMaxLengthInplaceLob() { 116 return 0; 117 } 118 119 public int allocateObjectId(boolean b, boolean c) { 120 return 0; 121 } 122 123 public String createTempFile() throws SQLException { 124 return null; 125 } 126 127 public String getLobCompressionAlgorithm(int type) { 128 return null; 129 } 130 131 } 132 | Popular Tags |