/**
   * This test makes sure that Humble Video sets the byte order of the returned ByteBuffer to native
   * order
   */
  @Test
  public void testByteOrderIsCorrect() {
    Buffer buf = Buffer.make(null, 1024 * 1024); // 1 MB
    assertNotNull(buf);

    assertEquals(1, buf.getCurrentRefCount());

    java.nio.ByteBuffer jbuf = buf.getByteBuffer(0, buf.getBufferSize());
    assertNotNull(buf);

    // Set 4 bytes that have a pattern that is reversible.  On a big
    // endian machine this is FOO and on a little endian it's BAR
    jbuf.put(0, (byte) 0xFF);
    jbuf.put(1, (byte) 0);
    jbuf.put(2, (byte) 0xFF);
    jbuf.put(3, (byte) 0);

    int bigOrderVal = 0xFF00FF00;
    int littleOrderVal = 0x00FF00FF;
    int expectedVal;
    if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) expectedVal = bigOrderVal;
    else expectedVal = littleOrderVal;
    java.nio.IntBuffer ibuf = jbuf.asIntBuffer();
    assertNotNull(ibuf);

    int val = ibuf.get(0);
    assertEquals(expectedVal, val);

    // now let's try changing byte orders
    jbuf.order(ByteOrder.BIG_ENDIAN);
    ibuf = jbuf.asIntBuffer();
    assertNotNull(ibuf);

    val = ibuf.get(0);
    assertEquals(bigOrderVal, val);

    jbuf.order(ByteOrder.LITTLE_ENDIAN);
    ibuf = jbuf.asIntBuffer();
    assertNotNull(ibuf);

    val = ibuf.get(0);
    assertEquals(littleOrderVal, val);
  }
  /**
   * This is a crazy test to make sure that a direct byte buffer will still be accessible even if
   * the Buffer it came from goes out of scope and is collected.
   */
  @Test(timeout = 60 * 1000)
  public void testDirectByteBufferCanBeAccessedAfterBufferDisappears() {
    Buffer buf = Buffer.make(null, 1024 * 1024); // 1 MB
    assertNotNull(buf);

    assertEquals(1, buf.getCurrentRefCount());

    java.nio.ByteBuffer jbuf = buf.getByteBuffer(0, buf.getBufferSize());
    assertNotNull(buf);

    // now release the reference
    buf.delete();
    buf = null;

    // in versions prior to 1.22, this would have caused a hard
    // crash, but with 1.22 the getByteBuffer should have incremented
    // the native ref count until this java ByteBuffer gets collected
    // and we do a JNIMemoryManager gc.
    jbuf.put(0, (byte) 0xFF);
  }