package it.unimi.dsi.mg4j.search;
import it.unimi.dsi.mg4j.index.BitStreamIndex;
import it.unimi.dsi.mg4j.index.DiskBasedIndex;
import it.unimi.dsi.mg4j.query.nodes.Query;
import it.unimi.dsi.mg4j.query.nodes.QueryBuilderVisitorException;
import it.unimi.dsi.mg4j.query.parser.QueryParserException;
import it.unimi.dsi.mg4j.query.parser.SimpleParser;
import it.unimi.dsi.mg4j.search.DocumentIterator;
import it.unimi.dsi.mg4j.search.DocumentIteratorBuilderVisitor;
import it.unimi.dsi.mg4j.search.IntervalIterators;
import it.unimi.dsi.mg4j.search.OrDocumentIterator;
import it.unimi.dsi.mg4j.tool.IndexBuilder;
import it.unimi.dsi.util.Interval;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import junit.framework.TestCase;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import it.unimi.dsi.mg4j.document.StringArrayDocumentCollection;
public class OrDocumentIteratorTest extends TestCase {
private BitStreamIndex index;
private SimpleParser simpleParser;
private String basename;
@Override
public void setUp() throws ConfigurationException, SecurityException, IOException, URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
basename = File.createTempFile( getClass().getSimpleName(), "test" ).getCanonicalPath();
new IndexBuilder( basename, new StringArrayDocumentCollection( "a", "b", "c" ) ).run();
index = DiskBasedIndex.getInstance( basename + "-text", true, true );
simpleParser = new SimpleParser( index.termProcessor );
}
@Override
public void tearDown() {
for( File f: new File( basename ).getParentFile().listFiles( (FileFilter)new PrefixFileFilter( new File( basename ).getName() ) ) ) f.delete();
}
public void testSkipBug() throws QueryParserException, QueryBuilderVisitorException, IOException {
Query query = simpleParser.parse( "a | b | c" );
DocumentIteratorBuilderVisitor documentIteratorBuilderVisitor = new DocumentIteratorBuilderVisitor( null, index, Integer.MAX_VALUE );
DocumentIterator documentIterator = query.accept( documentIteratorBuilderVisitor );
assertEquals( 2, documentIterator.skipTo( 2 ) );
documentIterator.dispose();
}
public void testOr() throws IOException {
DocumentIterator i0 = new IntArrayDocumentIterator( new int[] { 0, 2, 3, 4, 5, 6, 7 },
new int[][][] {
{ { 0, 1 } },
{ { 0, 1 }, { 1, 2 } },
{ { 0, 1 }, { 1, 2 } },
{ {} },
{ {} },
{},
{},
} );
DocumentIterator i1 = new IntArrayDocumentIterator( new int[] { 1, 2, 3, 4, 5, 6, 7 },
new int[][][] {
{ { 1 } },
{ { 1, 3 }, { 3, 4 } },
{ {} },
{ {} },
{},
{ {} },
{},
} );
OrDocumentIterator orDocumentIterator = (OrDocumentIterator)OrDocumentIterator.getInstance( i0, i1 );
assertTrue( orDocumentIterator.hasNext() );
assertTrue( orDocumentIterator.hasNext() ); // To increase coverage
assertEquals( 0, orDocumentIterator.nextDocument() );
assertTrue( orDocumentIterator.intervalIterator().hasNext() );
assertTrue( orDocumentIterator.intervalIterator().hasNext() ); // To increase coverage
assertEquals( Interval.valueOf( 0, 1 ), orDocumentIterator.intervalIterator().nextInterval() );
assertFalse( orDocumentIterator.intervalIterator().hasNext() );
assertFalse( orDocumentIterator.intervalIterator().hasNext() ); // To increase coverage
assertEquals( 1, orDocumentIterator.nextDocument() );
assertEquals( Interval.valueOf( 1, 1 ), orDocumentIterator.intervalIterator().nextInterval() );
assertFalse( orDocumentIterator.intervalIterator().hasNext() );
assertEquals( 2, orDocumentIterator.nextDocument() );
assertEquals( Interval.valueOf( 0, 1 ), orDocumentIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 1, 2 ), orDocumentIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 3, 4 ), orDocumentIterator.intervalIterator().nextInterval() );
assertFalse( orDocumentIterator.intervalIterator().hasNext() );
assertEquals( 3, orDocumentIterator.nextDocument() );
assertEquals( Interval.valueOf( 0, 1 ), orDocumentIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 1, 2 ), orDocumentIterator.intervalIterator().nextInterval() );
assertFalse( orDocumentIterator.intervalIterator().hasNext() );
assertEquals( 4, orDocumentIterator.nextDocument() );
assertEquals( IntervalIterators.TRUE, orDocumentIterator.intervalIterator() );
assertEquals( 5, orDocumentIterator.nextDocument() );
assertEquals( IntervalIterators.TRUE, orDocumentIterator.intervalIterator() );
assertEquals( 6, orDocumentIterator.nextDocument() );
assertEquals( IntervalIterators.TRUE, orDocumentIterator.intervalIterator() );
assertEquals( 7, orDocumentIterator.nextDocument() );
assertEquals( IntervalIterators.FALSE, orDocumentIterator.intervalIterator() );
}
}