001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.unit;
007:
008: import java.sql.SQLException;
009: import java.util.Comparator;
010: import java.util.HashMap;
011: import java.util.Random;
012:
013: import org.h2.constant.SysProperties;
014: import org.h2.store.DataHandler;
015: import org.h2.store.FileStore;
016: import org.h2.test.TestBase;
017: import org.h2.util.ObjectArray;
018: import org.h2.util.SmallLRUCache;
019: import org.h2.util.ValueHashMap;
020: import org.h2.value.CompareMode;
021: import org.h2.value.Value;
022: import org.h2.value.ValueInt;
023:
024: /**
025: * Tests the value hash map.
026: */
027: public class TestValueHashMap extends TestBase implements DataHandler {
028:
029: CompareMode compareMode = new CompareMode(null, null, 0);
030:
031: public void test() throws Exception {
032: ValueHashMap map = new ValueHashMap(this );
033: HashMap hash = new HashMap();
034: Random random = new Random(1);
035: Comparator vc = new Comparator() {
036: public int compare(Object o1, Object o2) {
037: Value v1 = (Value) o1;
038: Value v2 = (Value) o2;
039: try {
040: return v1.compareTo(v2, compareMode);
041: } catch (SQLException e) {
042: throw new Error(e);
043: }
044: }
045: };
046: for (int i = 0; i < 10000; i++) {
047: int op = random.nextInt(10);
048: Value key = ValueInt.get(random.nextInt(100));
049: Value value = ValueInt.get(random.nextInt(100));
050: switch (op) {
051: case 0:
052: map.put(key, value);
053: hash.put(key, value);
054: break;
055: case 1:
056: map.remove(key);
057: hash.remove(key);
058: break;
059: case 2:
060: Value v1 = (Value) map.get(key);
061: Value v2 = (Value) hash.get(key);
062: check((v1 == null && v2 == null) || v1.compareEqual(v2));
063: break;
064: case 3: {
065: ObjectArray a1 = map.keys();
066: ObjectArray a2 = new ObjectArray(hash.keySet());
067: check(a1.size(), a2.size());
068: a1.sort(vc);
069: a2.sort(vc);
070: for (int j = 0; j < a1.size(); j++) {
071: check(((Value) a1.get(j)).compareEqual((Value) a2
072: .get(j)));
073: }
074: break;
075: }
076: case 4:
077: ObjectArray a1 = map.values();
078: ObjectArray a2 = new ObjectArray(hash.values());
079: check(a1.size(), a2.size());
080: a1.sort(vc);
081: a2.sort(vc);
082: for (int j = 0; j < a1.size(); j++) {
083: check(((Value) a1.get(j)).compareEqual((Value) a2
084: .get(j)));
085: }
086: break;
087: }
088: }
089: }
090:
091: public boolean getTextStorage() {
092: return false;
093: }
094:
095: public String getDatabasePath() {
096: return null;
097: }
098:
099: public FileStore openFile(String name, String mode,
100: boolean mustExist) throws SQLException {
101: return null;
102: }
103:
104: public int getChecksum(byte[] data, int start, int end) {
105: return 0;
106: }
107:
108: public void checkPowerOff() throws SQLException {
109: }
110:
111: public void checkWritingAllowed() throws SQLException {
112: }
113:
114: public void freeUpDiskSpace() throws SQLException {
115: }
116:
117: public void handleInvalidChecksum() throws SQLException {
118: }
119:
120: public int compareTypeSave(Value a, Value b) throws SQLException {
121: return a.compareTo(b, compareMode);
122: }
123:
124: public int getMaxLengthInplaceLob() {
125: return 0;
126: }
127:
128: public int allocateObjectId(boolean b, boolean c) {
129: return 0;
130: }
131:
132: public String createTempFile() throws SQLException {
133: return null;
134: }
135:
136: public String getLobCompressionAlgorithm(int type) {
137: return null;
138: }
139:
140: public Object getLobSyncObject() {
141: return this ;
142: }
143:
144: public boolean getLobFilesInDirectories() {
145: return SysProperties.LOB_FILES_IN_DIRECTORIES;
146: }
147:
148: public SmallLRUCache getLobFileListCache() {
149: return null;
150: }
151:
152: }
|