Repository navigation
Expand file tree
/
Copy pathUtilsTest.java
More file actions
138 lines (102 loc) · 3.79 KB
/
Copy pathUtilsTest.java
File metadata and controls
138 lines (102 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.mapdb;
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.Set;
import static java.util.Arrays.asList;
import static org.junit.Assert.*;
public class UtilsTest {
@Test public void testPackInt() throws Exception {
DataOutput2 out = new DataOutput2();
DataInput2 in = new DataInput2(ByteBuffer.wrap(out.buf,0, out.pos),0);
for(int i = 0;i>-1; i = i + 1 + i/1111){ //overflow is expected
out.pos = 0;
DataOutput2.packInt(out, i);
in.pos = 0;
in.buf.clear();
int i2 = DataInput2.unpackInt(in);
Assert.assertEquals(i, i2);
}
}
@Test public void testPackLong() throws Exception {
DataOutput2 out = new DataOutput2();
DataInput2 in = new DataInput2(ByteBuffer.wrap(out.buf,0, out.pos),0);
for(long i = 0;i>-1L ; i=i+1 + i/111){ //overflow is expected
out.pos = 0;
DataOutput2.packLong(out, i);
in.pos = 0;
in.buf.clear();
long i2 = DataInput2.unpackLong(in);
Assert.assertEquals(i, i2);
}
}
@Test public void testArrayPut(){
assertEquals(asList(1,2,3,4,5), asList(BTreeMap.arrayPut(new Integer[]{1, 2, 4, 5}, 2, 3)));
assertEquals(asList(1,2,3,4,5), asList(BTreeMap.arrayPut(new Integer[]{2, 3, 4, 5}, 0, 1)));
assertEquals(asList(1,2,3,4,5), asList(BTreeMap.arrayPut(new Integer[]{1, 2, 3, 4}, 4, 5)));
}
@Test
public void testNextPowTwo() throws Exception {
int val=9;
assertEquals(16, 1 << (32 - Integer.numberOfLeadingZeros(val - 1)));
val = 8;
assertEquals(8, 1 << (32 - Integer.numberOfLeadingZeros(val - 1)));
}
/** clone value using serialization */
public static <E> E clone(E value, Serializer<E> serializer){
try{
DataOutput2 out = new DataOutput2();
serializer.serialize(out,value);
DataInput2 in = new DataInput2(ByteBuffer.wrap(out.copyBytes()), 0);
return serializer.deserialize(in,out.pos);
}catch(IOException ee){
throw new IOError(ee);
}
}
public static Serializer FAIL = new Serializer() {
@Override
public void serialize(DataOutput out, Object value) throws IOException {
throw new RuntimeException();
}
@Override
public Object deserialize(DataInput in, int available) throws IOException {
throw new RuntimeException();
}
@Override
public int fixedSize() {
return -1;
}
};
@Test public void testHexaConversion(){
byte[] b = new byte[]{11,112,11,0,39,90};
assertArrayEquals(b, DBMaker.fromHexa(DBMaker.toHexa(b)));
}
/**
* Create temporary file in temp folder. All associated db files will be deleted on JVM exit.
*/
public static File tempDbFile() {
try{
File index = File.createTempFile("mapdb","db");
index.deleteOnExit();
new File(index.getPath()+ StoreDirect.DATA_FILE_EXT).deleteOnExit();
new File(index.getPath()+ StoreWAL.TRANS_LOG_FILE_EXT).deleteOnExit();
return index;
}catch(IOException e){
throw new IOError(e);
}
}
public static String randomString(int size) {
String chars = "0123456789abcdefghijklmnopqrstuvwxyz !@#$%^&*()_+=-{}[]:\",./<>?|\\";
StringBuilder b = new StringBuilder(size);
Random r = new Random();
for(int i=0;i<size;i++){
b.append(chars.charAt(r.nextInt(chars.length())));
}
return b.toString();
}
public static int randomInt() {
return new Random().nextInt();
}
}