1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package org.apache.harmony.tests.java.nio; 17 18import java.nio.ByteBuffer; 19import java.nio.ByteOrder; 20import java.nio.DirectByteBuffer; 21import java.nio.FloatBuffer; 22import java.nio.NIOAccess; 23import libcore.io.SizeOf; 24 25public class DirectFloatBufferTest extends FloatBufferTest { 26 public void setUp(){ 27 buf = ByteBuffer.allocateDirect(BUFFER_LENGTH*4).asFloatBuffer(); 28 loadTestData1(buf); 29 baseBuf = buf; 30 } 31 32 public void tearDown(){ 33 buf = null; 34 baseBuf = null; 35 } 36 37 public void testHasArray() { 38 assertFalse(buf.hasArray()); 39 } 40 41 public void testArray() { 42 try { 43 buf.array(); 44 fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$ 45 } catch (UnsupportedOperationException e) { 46 } 47 } 48 49 public void testArrayOffset() { 50 try { 51 buf.arrayOffset(); 52 fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$ 53 } catch (UnsupportedOperationException e) { 54 //expected 55 } 56 } 57 58 // http://b/28964300 59 public void testJNIAccessByAddress() throws Exception { 60 DirectByteBuffer directByteBuffer = (DirectByteBuffer) ByteBuffer.allocateDirect(10); 61 directByteBuffer.put((byte)'a'); 62 FloatBuffer floatBuffer = directByteBuffer.asFloatBuffer(); 63 long byteBufferBasePointer = NIOAccess.getBasePointer(directByteBuffer); 64 long floatBufferBasePointer = NIOAccess.getBasePointer(floatBuffer); 65 assertEquals(byteBufferBasePointer, floatBufferBasePointer); 66 67 // Check if the NIOAccess method adds up the current position value. 68 floatBuffer.put((float)1); 69 assertEquals(floatBufferBasePointer + SizeOf.FLOAT, NIOAccess.getBasePointer(floatBuffer)); 70 } 71 72 public void testIsDirect() { 73 assertTrue(buf.isDirect()); 74 } 75 76 public void testOrder() { 77 assertEquals(ByteOrder.BIG_ENDIAN, buf.order()); 78 } 79} 80