@Test
  public void testExplain() {
    DBCollection c = _db.getCollection("explain1");
    c.drop();

    for (int i = 0; i < 100; i++) c.save(new BasicDBObject("x", i));

    DBObject q = BasicDBObjectBuilder.start().push("x").add("$gt", 50).get();

    assertEquals(49, c.find(q).count());
    assertEquals(49, c.find(q).itcount());
    assertEquals(49, c.find(q).toArray().size());
    assertEquals(49, c.find(q).itcount());
    assertEquals(20, c.find(q).limit(20).itcount());
    assertEquals(20, c.find(q).limit(-20).itcount());

    c.ensureIndex(new BasicDBObject("x", 1));

    assertEquals(49, c.find(q).count());
    assertEquals(49, c.find(q).toArray().size());
    assertEquals(49, c.find(q).itcount());
    assertEquals(20, c.find(q).limit(20).itcount());
    assertEquals(20, c.find(q).limit(-20).itcount());

    assertEquals(49, c.find(q).explain().get("n"));

    assertEquals(20, c.find(q).limit(20).explain().get("n"));
    assertEquals(20, c.find(q).limit(-20).explain().get("n"));
  }
  @Test
  public void testBig() {
    DBCollection c = _db.getCollection("big1");
    c.drop();

    String bigString;
    {
      StringBuilder buf = new StringBuilder(16000);
      for (int i = 0; i < 16000; i++) buf.append("x");
      bigString = buf.toString();
    }

    int numToInsert = (15 * 1024 * 1024) / bigString.length();

    for (int i = 0; i < numToInsert; i++)
      c.save(BasicDBObjectBuilder.start().add("x", i).add("s", bigString).get());

    assert (800 < numToInsert);

    assertEquals(numToInsert, c.find().count());
    assertEquals(numToInsert, c.find().toArray().size());
    assertEquals(numToInsert, c.find().limit(800).count());
    assertEquals(800, c.find().limit(800).toArray().size());

    // negative limit works like negative batchsize, for legacy reason
    int x = c.find().limit(-800).toArray().size();
    assertLess(x, 800);

    DBCursor a = c.find();
    assertEquals(numToInsert, a.itcount());

    DBCursor b = c.find().batchSize(10);
    assertEquals(numToInsert, b.itcount());
    assertEquals(10, b.getSizes().get(0).intValue());

    assertLess(a.numGetMores(), b.numGetMores());

    assertEquals(numToInsert, c.find().batchSize(2).itcount());
    assertEquals(numToInsert, c.find().batchSize(1).itcount());

    assertEquals(numToInsert, _count(c.find(null, null).skip(0).batchSize(5)));
    assertEquals(5, _count(c.find(null, null).skip(0).batchSize(-5)));
  }
  @Test
  public void testLimitAndBatchSize() {
    DBCollection c = _db.getCollection("LimitAndBatchSize");
    c.drop();

    for (int i = 0; i < 1000; i++) c.save(new BasicDBObject("x", i));

    DBObject q = BasicDBObjectBuilder.start().push("x").add("$lt", 200).get();

    DBCursor cur = c.find(q);
    assertEquals(0, cur.getCursorId());
    assertEquals(200, cur.itcount());

    cur = c.find(q).limit(50);
    assertEquals(0, cur.getCursorId());
    assertEquals(50, cur.itcount());

    cur = c.find(q).batchSize(50);
    assertEquals(0, cur.getCursorId());
    assertEquals(200, cur.itcount());

    cur = c.find(q).batchSize(100).limit(50);
    assertEquals(0, cur.getCursorId());
    assertEquals(50, cur.itcount());

    cur = c.find(q).batchSize(-40);
    assertEquals(0, cur.getCursorId());
    assertEquals(40, cur.itcount());

    cur = c.find(q).limit(-100);
    assertEquals(0, cur.getCursorId());
    assertEquals(100, cur.itcount());

    cur = c.find(q).batchSize(-40).limit(20);
    assertEquals(0, cur.getCursorId());
    assertEquals(20, cur.itcount());

    cur = c.find(q).batchSize(-20).limit(100);
    assertEquals(0, cur.getCursorId());
    assertEquals(20, cur.itcount());
  }