@Test
  public void testIterator() {
    ThriftyList<Integer> list = new ThriftyList<Integer>();

    ListIterator<Integer> i = list.listIterator();
    try {
      i.next();
      Assert.fail();
    } catch (NoSuchElementException e) {

    }
    try {
      i.remove();
      Assert.fail();
    } catch (IllegalStateException e) {

    }

    int testCount = 100;
    i = list.listIterator();
    for (int j = 0; j < testCount; j++) {
      i.add(j);
    }

    for (int j = testCount - 1; j >= 0; j--) {
      Assert.assertEquals(j, i.previousIndex());
      Assert.assertEquals(Integer.valueOf(j), i.previous());
    }

    for (int j = 0; j < testCount; j++) {
      Assert.assertTrue(i.hasNext());
      Integer o = list.get(j);
      Assert.assertEquals(j, i.nextIndex());
      Assert.assertEquals(o, i.next());
      i.remove();
      Assert.assertEquals(testCount - 1, list.size());
      i.add(o);
      Assert.assertEquals(testCount, list.size());
      Assert.assertEquals(o, i.previous());
      i.next();
    }

    int arbitraryIndex = testCount / 2;
    Integer arbitraryValue = -1;
    i = list.listIterator(arbitraryIndex);
    i.next();
    i.set(arbitraryValue);
    for (int j = 0; j < testCount; j++) {
      if (j == arbitraryIndex) {
        Assert.assertEquals(arbitraryValue, list.get(j));
        continue;
      }
      Assert.assertEquals(Integer.valueOf(j), list.get(j));
    }

    i = list.listIterator(list.size());
    for (int j = list.size() - 1; i.hasPrevious(); j--) {
      Integer o = list.get(j);
      Assert.assertEquals(j, i.previousIndex());
      Assert.assertEquals(o, i.previous());
      i.remove();
      Assert.assertEquals(testCount - 1, list.size());
      i.add(o);
      Assert.assertEquals(testCount, list.size());
      Assert.assertEquals(o, i.previous());
    }
  }