Skip to content

Commit 4383c70

Browse files
committed
Add tests
1 parent 01367c7 commit 4383c70

File tree

3 files changed

+141
-4
lines changed

3 files changed

+141
-4
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.lucene.util.fst;
18+
19+
import org.apache.lucene.store.OutputStreamDataOutput;
20+
import org.apache.lucene.tests.util.LuceneTestCase;
21+
import org.apache.lucene.tests.util.TestUtil;
22+
23+
import java.io.ByteArrayOutputStream;
24+
import java.util.Arrays;
25+
26+
public class TestDataOutputFSTWriter extends LuceneTestCase {
27+
28+
public void testRandom() throws Exception {
29+
30+
final int iters = atLeast(10);
31+
final int maxBytes = TEST_NIGHTLY ? 200000 : 20000;
32+
for (int iter = 0; iter < iters; iter++) {
33+
final int numBytes = TestUtil.nextInt(random(), 1, maxBytes);
34+
final byte[] expected = new byte[numBytes];
35+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
36+
final DataOutputFSTWriter bytes = new DataOutputFSTWriter(new OutputStreamDataOutput(baos), null);
37+
if (VERBOSE) {
38+
System.out.println("TEST: iter=" + iter + " numBytes=" + numBytes);
39+
}
40+
41+
int pos = 0;
42+
while (pos < numBytes) {
43+
int op = random().nextInt(2);
44+
if (VERBOSE) {
45+
System.out.println(" cycle pos=" + pos);
46+
}
47+
switch (op) {
48+
case 0:
49+
{
50+
// write random byte
51+
byte b = (byte) random().nextInt(256);
52+
if (VERBOSE) {
53+
System.out.println(" writeByte b=" + b);
54+
}
55+
56+
expected[pos++] = b;
57+
bytes.writeByte(b);
58+
}
59+
break;
60+
61+
case 1:
62+
{
63+
// write random byte[]
64+
int len = random().nextInt(Math.min(numBytes - pos, 100));
65+
byte[] temp = new byte[len];
66+
random().nextBytes(temp);
67+
if (VERBOSE) {
68+
System.out.println(" writeBytes len=" + len + " bytes=" + Arrays.toString(temp));
69+
}
70+
System.arraycopy(temp, 0, expected, pos, temp.length);
71+
bytes.writeBytes(temp, 0, temp.length);
72+
pos += len;
73+
}
74+
break;
75+
}
76+
77+
assertEquals(pos, bytes.size());
78+
}
79+
for (int i = 0; i < numBytes; i++) {
80+
assertEquals("byte @ index=" + i, expected[i], baos.toByteArray()[i]);
81+
}
82+
}
83+
}
84+
}

lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222

2323
import java.io.BufferedReader;
2424
import java.io.ByteArrayOutputStream;
25+
import java.io.File;
2526
import java.io.IOException;
27+
import java.io.RandomAccessFile;
2628
import java.io.StringWriter;
2729
import java.io.Writer;
30+
import java.nio.channels.FileChannel;
2831
import java.nio.charset.StandardCharsets;
2932
import java.nio.file.Files;
3033
import java.nio.file.Path;
@@ -104,6 +107,55 @@ public void tearDown() throws Exception {
104107
super.tearDown();
105108
}
106109

110+
public void testFSAWithFileChannel() throws IOException {
111+
File file = createTempFile().toFile();
112+
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
113+
FileChannel fileChannel = raf.getChannel();
114+
String[] strings2 =
115+
new String[] {
116+
"station", "commotion", "elation", "elastic", "plastic", "stop", "ftop", "ftation"
117+
};
118+
IntsRef[] terms2 = new IntsRef[strings2.length];
119+
for (int inputMode = 0; inputMode < 2; inputMode++) {
120+
if (VERBOSE) {
121+
System.out.println("TEST: inputMode=" + inputModeToString(inputMode));
122+
}
123+
124+
for (int idx = 0; idx < strings2.length; idx++) {
125+
terms2[idx] = toIntsRef(strings2[idx], inputMode);
126+
}
127+
Arrays.sort(terms2);
128+
129+
// Test pre-determined FST sizes to make sure we haven't lost minimality (at least on this
130+
// trivial set of terms):
131+
132+
// FSA
133+
{
134+
final Outputs<Object> outputs = NoOutputs.getSingleton();
135+
final Object NO_OUTPUT = outputs.getNoOutput();
136+
final List<FSTTester.InputOutput<Object>> pairs = new ArrayList<>(terms2.length);
137+
for (IntsRef term : terms2) {
138+
pairs.add(new FSTTester.InputOutput<>(term, NO_OUTPUT));
139+
}
140+
FSTTester<Object> tester = new FSTTester<>(random(), dir, inputMode, pairs, outputs) {
141+
142+
@Override
143+
protected FSTCompiler.Builder<Object> getFSTBuilder() {
144+
return super.getFSTBuilder().fstWriter(new FileChannelFSTWriter(fileChannel, 1024));
145+
}
146+
};
147+
FST<Object> fst = tester.doTest();
148+
assertNotNull(fst);
149+
assertEquals(22, tester.nodeCount);
150+
assertEquals(27, tester.arcCount);
151+
for (IntsRef intsRef : terms2) {
152+
assertNotNull(Util.get(fst, intsRef));
153+
}
154+
}
155+
}
156+
}
157+
}
158+
107159
public void testBasicFSA() throws IOException {
108160
String[] strings =
109161
new String[] {

lucene/test-framework/src/java/org/apache/lucene/tests/util/fst/FSTTester.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,7 @@ private T randomAcceptedWord(FST<T> fst, IntsRefBuilder in) throws IOException {
255255

256256
public FST<T> doTest() throws IOException {
257257

258-
final FSTCompiler<T> fstCompiler =
259-
new FSTCompiler.Builder<>(
260-
inputMode == 0 ? FST.INPUT_TYPE.BYTE1 : FST.INPUT_TYPE.BYTE4, outputs)
261-
.build();
258+
final FSTCompiler<T> fstCompiler = getFSTBuilder().build();
262259

263260
for (InputOutput<T> pair : pairs) {
264261
if (pair.output instanceof List) {
@@ -316,6 +313,10 @@ public FST<T> doTest() throws IOException {
316313
return fst;
317314
}
318315

316+
protected FSTCompiler.Builder<T> getFSTBuilder() {
317+
return new FSTCompiler.Builder<>(inputMode == 0 ? FST.INPUT_TYPE.BYTE1 : FST.INPUT_TYPE.BYTE4, outputs);
318+
}
319+
319320
protected boolean outputsEqual(T a, T b) {
320321
return a.equals(b);
321322
}

0 commit comments

Comments
 (0)