summaryrefslogtreecommitdiffstats
path: root/luni/src/test
diff options
context:
space:
mode:
authorMathieu Chartier <mathieuc@google.com>2014-07-22 15:11:03 -0700
committerMathieu Chartier <mathieuc@google.com>2014-07-22 16:02:12 -0700
commita20d8a3e850694e80dfc1cf7bcd440a812a811d0 (patch)
tree8b8cb4422c125f282e0c78d39b181c88818c7d5c /luni/src/test
parenta0eabca4c56e5d9b9e2803dece2a3855375c6b4f (diff)
downloadlibcore-a20d8a3e850694e80dfc1cf7bcd440a812a811d0.zip
libcore-a20d8a3e850694e80dfc1cf7bcd440a812a811d0.tar.gz
libcore-a20d8a3e850694e80dfc1cf7bcd440a812a811d0.tar.bz2
Fix broken nio BufferTest and add alignment test.
Bug: 16486921 Bug: 16449607 (cherry picked from commit 04a5e523e9b13fbc02a62af55b102d093b5cd054) Change-Id: Ia1049decde8f5960712ab78954077e78d07f47a0
Diffstat (limited to 'luni/src/test')
-rw-r--r--luni/src/test/java/libcore/java/nio/BufferTest.java32
1 files changed, 23 insertions, 9 deletions
diff --git a/luni/src/test/java/libcore/java/nio/BufferTest.java b/luni/src/test/java/libcore/java/nio/BufferTest.java
index 613c6fa..c936cdf 100644
--- a/luni/src/test/java/libcore/java/nio/BufferTest.java
+++ b/luni/src/test/java/libcore/java/nio/BufferTest.java
@@ -20,6 +20,8 @@ import junit.framework.TestCase;
import java.io.File;
import java.io.RandomAccessFile;
import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.nio.Buffer;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
@@ -600,17 +602,31 @@ public class BufferTest extends TestCase {
assertTrue(b.isDirect());
// Check the buffer has an array of the right size.
assertTrue(b.hasArray());
- assertEquals(0, b.arrayOffset());
byte[] array = b.array();
- assertEquals(10, array.length);
+ assertTrue(array.length >= b.capacity());
+ assertEquals(10, b.capacity());
// Check that writes to the array show up in the buffer.
assertEquals(0, b.get(0));
- array[0] = 1;
+ array[b.arrayOffset()] = 1;
assertEquals(1, b.get(0));
// Check that writes to the buffer show up in the array.
- assertEquals(1, array[0]);
+ assertEquals(1, array[b.arrayOffset()]);
b.put(0, (byte) 0);
- assertEquals(0, array[0]);
+ assertEquals(0, array[b.arrayOffset()]);
+ }
+
+ // Test that direct byte buffers are 8 byte aligned.
+ // http://b/16449607
+ public void testDirectByteBufferAlignment() throws Exception {
+ ByteBuffer b = ByteBuffer.allocateDirect(10);
+ Field addressField = Buffer.class.getDeclaredField("effectiveDirectAddress");
+ assertTrue(addressField != null);
+ addressField.setAccessible(true);
+ long address = addressField.getLong(b);
+ // Check that the address field is aligned by 8.
+ // Normally reading this field happens in native code by calling
+ // GetDirectBufferAddress.
+ assertEquals(0, address % 8);
}
public void testSliceOffset() throws Exception {
@@ -618,14 +634,12 @@ public class BufferTest extends TestCase {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.get();
ByteBuffer slice = buffer.slice();
- assertEquals(0, buffer.arrayOffset());
- assertEquals(1, slice.arrayOffset());
+ assertEquals(buffer.arrayOffset() + 1, slice.arrayOffset());
ByteBuffer directBuffer = ByteBuffer.allocateDirect(10);
directBuffer.get();
ByteBuffer directSlice = directBuffer.slice();
- assertEquals(0, directBuffer.arrayOffset());
- assertEquals(1, directSlice.arrayOffset());
+ assertEquals(directBuffer.arrayOffset() + 1, directSlice.arrayOffset());
}
// http://code.google.com/p/android/issues/detail?id=16184