package it.unimi.dsi.mg4j.search;
import it.unimi.dsi.mg4j.search.AndDocumentIterator;
import it.unimi.dsi.mg4j.search.DifferenceDocumentIterator;
import it.unimi.dsi.mg4j.search.DocumentIterator;
import it.unimi.dsi.mg4j.search.IntervalIterators;
import it.unimi.dsi.util.Interval;
import java.io.IOException;
import junit.framework.TestCase;
public class DifferenceDocumentIteratorTest extends TestCase {
public void testdifference() throws IOException {
DocumentIterator minuendIterator = new IntArrayDocumentIterator( new int[] { 0, 1, 2, 3 },
new int[][][] {
{ { 0, 1 } },
{ { 0, 1 }, { 1, 2 }, { 2, 3 } },
{ { 0, 1 }, { 1, 2 }, { 2, 3 } },
{ { 0 } }
} );
DocumentIterator subtrahendIterator = new IntArrayDocumentIterator( new int[] { 1, 2 },
new int[][][] {
{ { 1 } },
{ { 1, 3 } }
} );
DocumentIterator differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator );
assertTrue( differenceIterator.hasNext() );
assertTrue( differenceIterator.hasNext() ); // To increase coverage
assertEquals( 0, differenceIterator.nextDocument() );
assertEquals( 0, differenceIterator.document() );
assertEquals( Interval.valueOf( 0, 1 ), differenceIterator.intervalIterator().nextInterval() );
assertFalse( differenceIterator.intervalIterator().hasNext() );
assertEquals( 1, differenceIterator.nextDocument() );
assertEquals( 1, differenceIterator.document() );
assertEquals( Interval.valueOf( 2, 3 ), differenceIterator.intervalIterator().nextInterval() );
assertFalse( differenceIterator.intervalIterator().hasNext() );
assertEquals( 2, differenceIterator.nextDocument() );
assertEquals( 2, differenceIterator.document() );
assertEquals( Interval.valueOf( 0, 1 ), differenceIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 1, 2 ), differenceIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 2, 3 ), differenceIterator.intervalIterator().nextInterval() );
assertFalse( differenceIterator.intervalIterator().hasNext() );
assertEquals( 3, differenceIterator.nextDocument() );
assertEquals( 3, differenceIterator.document() );
assertEquals( Interval.valueOf( 0 ), differenceIterator.intervalIterator().nextInterval() );
assertFalse( differenceIterator.intervalIterator().hasNext() );
assertFalse( differenceIterator.hasNext() ); // To increase coverage
}
public void testSubtrahendExhaustion() throws IOException {
DocumentIterator minuendIterator = new IntArrayDocumentIterator( new int[] { 0 },
new int[][][] {
{ { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 } }
} );
DocumentIterator subtrahendIterator = new IntArrayDocumentIterator( new int[] { 0 },
new int[][][] {
{ { 1 } },
} );
DocumentIterator differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator );
assertTrue( differenceIterator.hasNext() );
assertEquals( 0, differenceIterator.nextDocument() );
assertEquals( 0, differenceIterator.document() );
assertEquals( Interval.valueOf( 2, 3 ), differenceIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 3, 4 ), differenceIterator.intervalIterator().nextInterval() );
assertFalse( differenceIterator.intervalIterator().hasNext() );
assertFalse( differenceIterator.hasNext() ); // To increase coverage
}
public void testskipTo() throws IOException {
IntArrayDocumentIterator minuendIterator = new IntArrayDocumentIterator( new int[] { 0, 1, 2, 3, 4 },
new int[][][] {
{ { 0, 1 } },
{ { 0, 1 }, { 1, 2 } },
{ { 0, 1 }, { 1, 2 }, { 2, 3 } },
{ { 0 } },
{ { 0 } }
} );
IntArrayDocumentIterator subtrahendIterator = new IntArrayDocumentIterator( new int[] { 1, 2, 3, 4 },
new int[][][] {
{ { 1 } },
{ { 1, 3 } },
{ {} },
{ {} },
} );
DocumentIterator differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator );
assertEquals( 2, differenceIterator.skipTo( 1 ) );
assertEquals( 2, differenceIterator.skipTo( 1 ) ); // To increase coverage
assertEquals( Integer.MAX_VALUE, differenceIterator.skipTo( 4 ) );
minuendIterator.reset();
subtrahendIterator.reset();
differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator );
assertEquals( 2, differenceIterator.skipTo( 2 ) );
minuendIterator.reset();
subtrahendIterator.reset();
differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator );
assertEquals( Integer.MAX_VALUE, differenceIterator.skipTo( 10 ) );
assertEquals( Integer.MAX_VALUE, differenceIterator.skipTo( 10 ) );
assertEquals( -1, differenceIterator.nextDocument() );
}
public void testtrueFalseDifference() throws IOException {
DocumentIterator minuendIterator = new IntArrayDocumentIterator( new int[] { 0, 1, 2, 3 },
new int[][][] {
{ {} },
{},
{ {} },
{},
} );
DocumentIterator subtrahendIterator = new IntArrayDocumentIterator( new int[] { 0, 1, 2, 3 },
new int[][][] {
{ {} },
{ {} },
{},
{},
} );
DocumentIterator differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator );
assertTrue( differenceIterator.hasNext() );
assertEquals( 2, differenceIterator.nextDocument() );
assertEquals( IntervalIterators.TRUE, differenceIterator.intervalIterator() );
assertFalse( differenceIterator.hasNext() );
}
public void testTrueFalseOtherDifference() throws IOException {
DocumentIterator minuendIterator = new IntArrayDocumentIterator( new int[] { 0, 1, 2, 3 },
new int[][][] {
{ { 0, 1 }, { 1, 2 } },
{ { 0, 1 }, { 1, 2 } },
{ {} },
{},
} );
DocumentIterator subtrahendIterator = new IntArrayDocumentIterator( new int[] { 0, 1, 2, 3 },
new int[][][] {
{ {} },
{},
{ { 0, 1 }, { 1, 2 } },
{ { 0, 1 }, { 1, 2 } },
} );
DocumentIterator differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator );
assertTrue( differenceIterator.hasNext() );
assertEquals( 1, differenceIterator.nextDocument() );
assertEquals( Interval.valueOf( 0, 1 ), differenceIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 1, 2 ), differenceIterator.intervalIterator().nextInterval() );
assertFalse( differenceIterator.intervalIterator().hasNext() );
assertEquals( 2, differenceIterator.nextDocument() );
assertEquals( IntervalIterators.TRUE, differenceIterator.intervalIterator() );
assertFalse( differenceIterator.hasNext() );
}
public void testvoidDifference() {
DocumentIterator minuendIterator = new IntArrayDocumentIterator( new int[] { 0 },
new int[][][] {
{ { 0, 1 }, { 1, 2 } },
} );
DocumentIterator subtrahendIterator = new IntArrayDocumentIterator( new int[] {},
new int[][][] {} );
DocumentIterator differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator );
assertEquals( minuendIterator, differenceIterator );
}
public void testSpuriousReset() throws IOException {
DocumentIterator minuendIterator = AndDocumentIterator.getInstance(
new IntArrayDocumentIterator( new int[] { 0 },
new int[][][] {
{ { 0 } },
} ),
new IntArrayDocumentIterator( new int[] { 0 },
new int[][][] {
{ { 1 } },
} )
);
DocumentIterator subtrahendIterator = new IntArrayDocumentIterator( new int[] { 1 }, new int[][][] { {} } );
DocumentIterator differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator );
assertTrue( differenceIterator.hasNext() );
assertEquals( 0, differenceIterator.nextDocument() );
assertEquals( Interval.valueOf( 0, 1 ), differenceIterator.intervalIterator().nextInterval() );
}
public void testEnlargment() throws IOException {
DocumentIterator minuendIterator = new IntArrayDocumentIterator( new int[] { 0, 1, 2, 3 },
new int[][][] {
{ { 0, 1 }, { 1, 2 } },
{ { 0, 1 }, { 1, 2 } },
{ {} },
{ { 0, 1 }, { 1, 2 }, { 3, 5 }, { 4, 6 } },
} );
DocumentIterator subtrahendIterator = new IntArrayDocumentIterator( new int[] { 0, 1, 2, 3 },
new int[][][] {
{ {} },
{},
{ { 0, 1 }, { 1, 2 } },
{ { 0 }, { 1 }, { 4 } },
} );
DocumentIterator differenceIterator = DifferenceDocumentIterator.getInstance( minuendIterator, subtrahendIterator, 1, 1 );
assertTrue( differenceIterator.hasNext() );
assertEquals( 1, differenceIterator.nextDocument() );
assertEquals( Interval.valueOf( 0, 1 ), differenceIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 1, 2 ), differenceIterator.intervalIterator().nextInterval() );
assertFalse( differenceIterator.intervalIterator().hasNext() );
assertEquals( 2, differenceIterator.nextDocument() );
assertEquals( IntervalIterators.TRUE, differenceIterator.intervalIterator() );
assertEquals( 3, differenceIterator.nextDocument() );
assertEquals( Interval.valueOf( 0, 1 ), differenceIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 1, 2 ), differenceIterator.intervalIterator().nextInterval() );
assertEquals( Interval.valueOf( 4, 6 ), differenceIterator.intervalIterator().nextInterval() );
assertFalse( differenceIterator.intervalIterator().hasNext() );
assertFalse( differenceIterator.hasNext() );
}
}