TEST(IndexInfoTest, WriteIntoDir)
{
    RAMDirectory dir;

    IndexInfo infos;
    infos.addSegment(SegmentInfo(0, 42, 100, 123));
    infos.incLastSegmentId();
    infos.addSegment(SegmentInfo(1, 66, 200, 456));
    infos.incLastSegmentId();
    infos.setAttribute("foo", "bar");
    infos.save(&dir);

    ScopedPtr<InputStream> input(dir.openFile("info_0"));
    ASSERT_EQ(2, input->readVInt32());
    ASSERT_EQ(2, input->readVInt32());
    ASSERT_EQ(0, input->readVInt32());
    ASSERT_EQ(42, input->readVInt32());
    ASSERT_EQ(100, input->readVInt32());
    ASSERT_EQ(123, input->readVInt32());
    ASSERT_EQ(1, input->readVInt32());
    ASSERT_EQ(66, input->readVInt32());
    ASSERT_EQ(200, input->readVInt32());
    ASSERT_EQ(456, input->readVInt32());
    ASSERT_EQ(1, input->readVInt32());
    ASSERT_EQ("foo", input->readString());
    ASSERT_EQ("bar", input->readString());
    ASSERT_EQ(3656423981u, input->readInt32());
}
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(IndexWriterTest, AddDocument)
{
	RAMDirectory dir;
	IndexWriter writer(&dir, true);
	ASSERT_TRUE(dir.fileExists("segments_0"));
	ASSERT_EQ(0, writer.revision());
	ASSERT_EQ(0, writer.segmentInfoList().segmentCount());

	uint32_t fp[] = { 7, 9, 12 };
	writer.addDocument(1, fp, 3);
	writer.commit();
	ASSERT_TRUE(dir.fileExists("segments_1"));
	ASSERT_TRUE(dir.fileExists("segment_0.fii"));
	ASSERT_TRUE(dir.fileExists("segment_0.fid"));
	ASSERT_EQ(1, writer.revision());
	ASSERT_EQ(1, writer.segmentInfoList().segmentCount());
	ASSERT_EQ("segment_0", writer.segmentInfoList().info(0).name());
	ASSERT_EQ(1, writer.segmentInfoList().info(0).numDocs());

	{
		ScopedPtr<InputStream> input(dir.openFile("segment_0.fii"));
		ASSERT_EQ(512, input->readInt32());
		ASSERT_EQ(1, input->readInt32());
		ASSERT_EQ(7, input->readVInt32());
	}

	{
		ScopedPtr<InputStream> input(dir.openFile("segment_0.fid"));
		ASSERT_EQ(3, input->readVInt32());
		ASSERT_EQ(1, input->readVInt32());
		ASSERT_EQ(2, input->readVInt32());
		ASSERT_EQ(1, input->readVInt32());
		ASSERT_EQ(3, input->readVInt32());
		ASSERT_EQ(1, input->readVInt32());
	}
}
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());
}