TEST(IndexInfoTest, ReadFromDirCorruptRecover2)
{
    RAMDirectory dir;

    {
        ScopedPtr<OutputStream> output(dir.createFile("info_0"));
        output->writeVInt32(2);
        output->writeVInt32(2);
        output->writeVInt32(0);
        output->writeVInt32(42);
        output->writeVInt32(100);
        output->writeVInt32(123);
        output->writeVInt32(1);
        output->writeVInt32(66);
        output->writeVInt32(200);
        output->writeVInt32(456);
        output->writeVInt32(1);
        output->writeString("foo");
        output->writeString("bar");
        output->writeInt32(3656423981u);
    }

    {
        ScopedPtr<OutputStream> output(dir.createFile("info_1"));
        output->writeVInt32(3);
    }

    {
        ScopedPtr<OutputStream> output(dir.createFile("info_2"));
        output->writeVInt32(4);
    }

    IndexInfo infos;
    infos.load(&dir);

    ASSERT_EQ(0, infos.revision());
    ASSERT_EQ(2, infos.lastSegmentId());
    ASSERT_EQ(2, infos.segmentCount());
    ASSERT_EQ("segment_0", infos.segment(0).name());
    ASSERT_EQ(42, infos.segment(0).blockCount());
    ASSERT_EQ(100, infos.segment(0).lastKey());
    ASSERT_EQ(123, infos.segment(0).checksum());
    ASSERT_EQ("segment_1", infos.segment(1).name());
    ASSERT_EQ(66, infos.segment(1).blockCount());
    ASSERT_EQ(200, infos.segment(1).lastKey());
    ASSERT_EQ(456, infos.segment(1).checksum());
    ASSERT_EQ(1, infos.attributes().size());
    ASSERT_EQ("bar", infos.attribute("foo"));
}
TEST(SegmentInfoListTest, Read)
{
	RAMDirectory dir;

	ScopedPtr<OutputStream> output(dir.createFile("segments_0"));
	output->writeVInt32(3);
	output->writeVInt32(2);
	output->writeString("segment_0");
	output->writeVInt32(42);
	output->writeString("segment_2");
	output->writeVInt32(66);
	output.reset();

	SegmentInfoList infos;
	ScopedPtr<InputStream> input(dir.openFile("segments_0"));
	infos.read(input.get());
	input.reset();

	ASSERT_EQ(3, infos.lastSegmetNum());
	ASSERT_EQ(2, infos.segmentCount());
	ASSERT_EQ("segment_0", infos.info(0).name());
	ASSERT_EQ(42, infos.info(0).numDocs());
	ASSERT_EQ("segment_2", infos.info(1).name());
	ASSERT_EQ(66, infos.info(1).numDocs());
}
TEST(IndexInfoTest, ReadFromDirCorruptFail)
{
    RAMDirectory dir;

    {
        ScopedPtr<OutputStream> output(dir.createFile("info_0"));
        output->writeVInt32(2);
    }

    {
        ScopedPtr<OutputStream> output(dir.createFile("info_1"));
        output->writeVInt32(3);
    }

    IndexInfo infos;
    ASSERT_THROW(infos.load(&dir), CorruptIndexException);
}
TEST(IndexInfoTest, FindCurrentRevision)
{
    RAMDirectory dir;

    int rev = IndexInfo::findCurrentRevision(&dir);
    ASSERT_EQ(-1, rev);

    delete dir.createFile("info_0");
    rev = IndexInfo::findCurrentRevision(&dir);
    ASSERT_EQ(0, rev);

    delete dir.createFile("info_1");
    rev = IndexInfo::findCurrentRevision(&dir);
    ASSERT_EQ(1, rev);

    delete dir.createFile("info_8");
    rev = IndexInfo::findCurrentRevision(&dir);
    ASSERT_EQ(8, rev);
}
TEST(SegmentInfoListTest, FindCurrentRevision)
{
	RAMDirectory dir;

	int rev = SegmentInfoList::findCurrentRevision(&dir);
	ASSERT_EQ(-1, rev);

	delete dir.createFile("segments_0");
	rev = SegmentInfoList::findCurrentRevision(&dir);
	ASSERT_EQ(0, rev);

	delete dir.createFile("segments_1");
	rev = SegmentInfoList::findCurrentRevision(&dir);
	ASSERT_EQ(1, rev);

	delete dir.createFile("segments_8");
	rev = SegmentInfoList::findCurrentRevision(&dir);
	ASSERT_EQ(8, rev);
}
TEST(SegmentInfoListTest, Write)
{
	RAMDirectory dir;

	SegmentInfoList infos;
	infos.add(SegmentInfo("segment_0", 42));
	infos.incNextSegmentNum();
	infos.add(SegmentInfo("segment_1", 66));
	infos.incNextSegmentNum();
	ScopedPtr<OutputStream> output(dir.createFile("segments_0"));
	infos.write(output.get());
	output.reset();

	ScopedPtr<InputStream> input(dir.openFile("segments_0"));
	ASSERT_EQ(2, input->readVInt32());
	ASSERT_EQ(2, input->readVInt32());
	ASSERT_EQ("segment_0", input->readString());
	ASSERT_EQ(42, input->readVInt32());
	ASSERT_EQ("segment_1", input->readString());
	ASSERT_EQ(66, input->readVInt32());
}