public void testDuplicate() {
    buf.clear();
    buf.mark();
    buf.position(buf.limit());

    // duplicate's contents should be the same as buf
    DoubleBuffer duplicate = buf.duplicate();
    assertNotSame(buf, duplicate);
    assertEquals(buf.position(), duplicate.position());
    assertEquals(buf.limit(), duplicate.limit());
    assertEquals(buf.isReadOnly(), duplicate.isReadOnly());
    assertEquals(buf.isDirect(), duplicate.isDirect());
    assertEquals(buf.order(), duplicate.order());
    assertContentEquals(buf, duplicate);

    // duplicate's position, mark, and limit should be independent to buf
    duplicate.reset();
    assertEquals(duplicate.position(), 0);
    duplicate.clear();
    assertEquals(buf.position(), buf.limit());
    buf.reset();
    assertEquals(buf.position(), 0);

    // duplicate share the same content with buf
    // FIXME
    if (!duplicate.isReadOnly()) {
      loadTestData1(buf);
      assertContentEquals(buf, duplicate);
      loadTestData2(duplicate);
      assertContentEquals(buf, duplicate);
    }
  }
  @TestTargetNew(
      level = TestLevel.PARTIAL_COMPLETE,
      notes = "",
      method = "compact",
      args = {})
  @AndroidOnly("Fails on RI. See comment below")
  public void testCompact() {
    // case: buffer is full
    buf.clear();
    buf.mark();
    loadTestData1(buf);
    DoubleBuffer ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), buf.capacity());
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0.0, buf.capacity());
    try {
      buf.reset();
      fail("Should throw Exception"); // $NON-NLS-1$
    } catch (InvalidMarkException e) {
      // expected
    }

    // case: buffer is empty
    buf.position(0);
    buf.limit(0);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 0);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0.0, buf.capacity());
    try {
      // Fails on RI. Spec doesn't specify the behavior if
      // actually nothing to be done by compact(). So RI doesn't reset
      // mark position
      buf.reset();
      fail("Should throw Exception"); // $NON-NLS-1$
    } catch (InvalidMarkException e) {
      // expected
    }

    // case: normal
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(5);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 4);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 1.0, 4);
    try {
      buf.reset();
      fail("Should throw Exception"); // $NON-NLS-1$
    } catch (InvalidMarkException e) {
      // expected
    }
  }
  public void testCompact() {
    // case: buffer is full
    buf.clear();
    buf.mark();
    loadTestData1(buf);
    DoubleBuffer ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), buf.capacity());
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0.0, buf.capacity());
    try {
      buf.reset();
      fail("Should throw Exception"); // $NON-NLS-1$
    } catch (InvalidMarkException e) {
      // expected
    }

    // case: buffer is empty
    buf.position(0);
    buf.limit(0);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 0);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0.0, buf.capacity());
    try {
      buf.reset();
      fail("Should throw Exception"); // $NON-NLS-1$
    } catch (InvalidMarkException e) {
      // expected
    }

    // case: normal
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(5);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 4);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 1.0, 4);
    try {
      buf.reset();
      fail("Should throw Exception"); // $NON-NLS-1$
    } catch (InvalidMarkException e) {
      // expected
    }
  }
  public void testSlice() {
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(buf.capacity() - 1);

    DoubleBuffer slice = buf.slice();
    assertEquals(buf.isReadOnly(), slice.isReadOnly());
    assertEquals(buf.isDirect(), slice.isDirect());
    assertEquals(buf.order(), slice.order());
    assertEquals(slice.position(), 0);
    assertEquals(slice.limit(), buf.remaining());
    assertEquals(slice.capacity(), buf.remaining());
    try {
      slice.reset();
      fail("Should throw Exception"); // $NON-NLS-1$
    } catch (InvalidMarkException e) {
      // expected
    }

    // slice share the same content with buf
    // FIXME:
    if (!slice.isReadOnly()) {
      loadTestData1(slice);
      assertContentLikeTestData1(buf, 1, 0, slice.capacity());
      buf.put(2, 500);
      assertEquals(slice.get(1), 500, 0.0);
    }
  }
  @TestTargetNew(
      level = TestLevel.PARTIAL_COMPLETE,
      notes = "",
      method = "asReadOnlyBuffer",
      args = {})
  public void testAsReadOnlyBuffer() {
    buf.clear();
    buf.mark();
    buf.position(buf.limit());

    // readonly's contents should be the same as buf
    DoubleBuffer readonly = buf.asReadOnlyBuffer();
    assertNotSame(buf, readonly);
    assertTrue(readonly.isReadOnly());
    assertEquals(buf.position(), readonly.position());
    assertEquals(buf.limit(), readonly.limit());
    assertEquals(buf.isDirect(), readonly.isDirect());
    assertEquals(buf.order(), readonly.order());
    assertContentEquals(buf, readonly);

    // readonly's position, mark, and limit should be independent to buf
    readonly.reset();
    assertEquals(readonly.position(), 0);
    readonly.clear();
    assertEquals(buf.position(), buf.limit());
    buf.reset();
    assertEquals(buf.position(), 0);

    // BEGIN android-added
    // copied from a newer version of Harmony
    DoubleBuffer dbuffer1 = DoubleBuffer.wrap(new double[] {Double.NaN});
    DoubleBuffer dbuffer2 = DoubleBuffer.wrap(new double[] {Double.NaN});
    DoubleBuffer dbuffer3 = DoubleBuffer.wrap(new double[] {42d});

    assertEquals("Failed equal comparison with NaN entry", 0, dbuffer1.compareTo(dbuffer2));
    assertEquals("Failed greater than comparison with NaN entry", 1, dbuffer3.compareTo(dbuffer1));
    assertEquals("Failed greater than comparison with NaN entry", 1, dbuffer1.compareTo(dbuffer3));
    // END android-added
  }
  public void testAsReadOnlyBuffer() {
    buf.clear();
    buf.mark();
    buf.position(buf.limit());

    // readonly's contents should be the same as buf
    DoubleBuffer readonly = buf.asReadOnlyBuffer();
    assertNotSame(buf, readonly);
    assertTrue(readonly.isReadOnly());
    assertEquals(buf.position(), readonly.position());
    assertEquals(buf.limit(), readonly.limit());
    assertEquals(buf.isDirect(), readonly.isDirect());
    assertEquals(buf.order(), readonly.order());
    assertContentEquals(buf, readonly);

    // readonly's position, mark, and limit should be independent to buf
    readonly.reset();
    assertEquals(readonly.position(), 0);
    readonly.clear();
    assertEquals(buf.position(), buf.limit());
    buf.reset();
    assertEquals(buf.position(), 0);
  }