@Test
/**
* Tests seekNear and getNext work correctly.
*/
public void testSeek() throws IOException, InterruptedException {
IndexedStorage storage = new IndexedStorage("\t","0,1");
Configuration conf = new Configuration();
conf.set("fs.default.name", "file:///");
LocalFileSystem fs = FileSystem.getLocal(conf);
TaskAttemptID taskId = HadoopShims.createTaskAttemptID("jt", 2, true, 2, 2);
conf.set("mapred.task.id", taskId.toString());
conf.set("mapred.input.dir","out");
storage.initialize(conf);
TupleFactory tupleFactory = TupleFactory.getInstance();
Tuple seek = tupleFactory.newTuple(2);
Integer key;
Tuple read;
//Seek to 1,1 (not in index). getNext should return 2
seek.set(0, new Integer(1));
seek.set(1, new Integer(1));
storage.seekNear(seek);
read = storage.getNext();
key = Integer.decode(((DataByteArray)read.get(0)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(2));
key = Integer.decode(((DataByteArray)read.get(1)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(2));
//Seek to 3,2. getNext should return 3,2
seek.set(0, new Integer(3));
seek.set(1, new Integer(2));
storage.seekNear(seek);
read = storage.getNext();
key = Integer.decode(((DataByteArray)read.get(0)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(3));
key = Integer.decode(((DataByteArray)read.get(1)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(2));
//getNext should return 3,3
read = storage.getNext();
key = Integer.decode(((DataByteArray)read.get(0)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(3));
key = Integer.decode(((DataByteArray)read.get(1)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(3));
//Seek to 6,6. getNext should return 6,6
seek.set(0, new Integer(6));
seek.set(1, new Integer(6));
storage.seekNear(seek);
read = storage.getNext();
key = Integer.decode(((DataByteArray)read.get(0)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(6));
key = Integer.decode(((DataByteArray)read.get(1)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(6));
//getNext should return 7,7
read = storage.getNext();
key = Integer.decode(((DataByteArray)read.get(0)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(7));
key = Integer.decode(((DataByteArray)read.get(1)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(7));
//Seek to 9,9. getNext should return 9,9
seek.set(0, new Integer(9));
seek.set(1, new Integer(9));
storage.seekNear(seek);
read = storage.getNext();
key = Integer.decode(((DataByteArray)read.get(0)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(9));
key = Integer.decode(((DataByteArray)read.get(1)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(9));
//getNext should return 10,10
read = storage.getNext();
key = Integer.decode(((DataByteArray)read.get(0)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(10));
key = Integer.decode(((DataByteArray)read.get(1)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(10));
//Seek to 13,12 (Not in index). getNext should return 13,13
seek.set(0, new Integer(13));
seek.set(1, new Integer(12));
storage.seekNear(seek);
read = storage.getNext();
key = Integer.decode(((DataByteArray)read.get(0)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(13));
key = Integer.decode(((DataByteArray)read.get(1)).toString());
Assert.assertTrue("GetNext did not return the correct value", key.equals(13));
//Seek to 20 (Not in index). getNext should return null
seek.set(0, new Integer(20));
seek.set(1, new Integer(20));
storage.seekNear(seek);
read = storage.getNext();
Assert.assertTrue("GetNext did not return the correct value", (read == null));
}