/
SuccinctIndexedFileBufferTest.java
135 lines (113 loc) · 4.04 KB
/
SuccinctIndexedFileBufferTest.java
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
package edu.berkeley.cs.succinct.buffers;
import edu.berkeley.cs.succinct.StorageMode;
import edu.berkeley.cs.succinct.SuccinctIndexedFile;
import edu.berkeley.cs.succinct.SuccinctIndexedFileTest;
import edu.berkeley.cs.succinct.util.Source;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class SuccinctIndexedFileBufferTest extends SuccinctIndexedFileTest {
byte[] data;
private String testFileRaw = this.getClass().getResource("/test_file").getFile();
private String testFileSuccinct =
this.getClass().getResource("/test_file").getFile() + ".idx.succinct";
private String testFileSuccinctMin =
this.getClass().getResource("/test_file").getFile() + ".idx.min.succinct";
private String[] queryStrings = {"int", "include", "random", "random int"};
@Override public String getQueryString(int i) {
return queryStrings[i];
}
@Override public int numQueryStrings() {
return queryStrings.length;
}
@Override public String getData() {
return new String(data);
}
/**
* Set up test.
*
* @throws Exception
*/
public void setUp() throws Exception {
super.setUp();
File inputFile = new File(testFileRaw);
data = new byte[(int) inputFile.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(inputFile));
dis.readFully(data);
fileData = new Source() {
@Override public int length() {
return data.length;
}
@Override public int get(int i) {
return data[i];
}
};
ArrayList<Integer> positions = new ArrayList<>();
positions.add(0);
for (int i = 0; i < fileData.length(); i++) {
if (fileData.get(i) == '\n') {
positions.add(i + 1);
}
}
offsets = new int[positions.size()];
for (int i = 0; i < offsets.length; i++) {
offsets[i] = positions.get(i);
}
sIFile = new SuccinctIndexedFileBuffer(data, offsets);
}
/**
* Test method: void readObject(ObjectInputStream ois)
* Test method: void writeObject(ObjectOutputStream oos)
*
* @throws Exception
*/
public void testSerializeDeserialize() throws Exception {
// Serialize data
FileOutputStream fOut = new FileOutputStream(testFileSuccinct);
ObjectOutputStream oos = new ObjectOutputStream(fOut);
oos.writeObject(sIFile);
oos.close();
// Deserialize data
FileInputStream fIn = new FileInputStream(testFileSuccinct);
ObjectInputStream ois = new ObjectInputStream(fIn);
SuccinctIndexedFile sIFileRead = (SuccinctIndexedFileBuffer) ois.readObject();
ois.close();
assertNotNull(sIFileRead);
assertEquals(sIFile.getNumRecords(), sIFileRead.getNumRecords());
for (int i = 0; i < sIFile.getNumRecords(); i++) {
assertTrue(Arrays.equals(sIFile.getRecordBytes(i), sIFileRead.getRecordBytes(i)));
}
}
/**
* Test method: void writeToFile(String path)
* Test method: void memoryMap(String path)
*
* @throws Exception
*/
public void testMemoryMap() throws Exception {
((SuccinctIndexedFileBuffer) sIFile).writeToFile(testFileSuccinctMin);
SuccinctIndexedFile sIFileRead =
new SuccinctIndexedFileBuffer(testFileSuccinctMin, StorageMode.MEMORY_MAPPED);
assertNotNull(sIFileRead);
assertEquals(sIFile.getNumRecords(), sIFileRead.getNumRecords());
for (int i = 0; i < sIFile.getNumRecords(); i++) {
assertTrue(Arrays.equals(sIFile.getRecordBytes(i), sIFileRead.getRecordBytes(i)));
}
}
/**
* Test method: void writeToFile(String path)
* Test method: void readFromFile(String path)
*
* @throws Exception
*/
public void testReadFromFile() throws Exception {
((SuccinctIndexedFileBuffer) sIFile).writeToFile(testFileSuccinctMin);
SuccinctIndexedFile sIFileRead =
new SuccinctIndexedFileBuffer(testFileSuccinctMin, StorageMode.MEMORY_ONLY);
assertNotNull(sIFileRead);
assertEquals(sIFile.getNumRecords(), sIFileRead.getNumRecords());
for (int i = 0; i < sIFile.getNumRecords(); i++) {
assertTrue(Arrays.equals(sIFile.getRecordBytes(i), sIFileRead.getRecordBytes(i)));
}
}
}