OSDN Git Service

Fix an issue where we're adding 4x the intended offset.
[android-x86/dalvik.git] / libcore / nio / src / test / java / org / apache / harmony / nio / tests / java / nio / DirectIntBufferTest.java
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  */
16 package org.apache.harmony.nio.tests.java.nio;
17
18 import dalvik.annotation.TestLevel;
19 import dalvik.annotation.TestTargetNew;
20 import dalvik.annotation.TestTargetClass;
21
22 import java.nio.BufferOverflowException;
23 import java.nio.ByteBuffer;
24 import java.nio.ByteOrder;
25 import java.nio.IntBuffer;
26
27 @TestTargetClass(java.nio.IntBuffer.class)
28 public class DirectIntBufferTest extends IntBufferTest {
29     public void setUp(){
30         capacity = BUFFER_LENGTH;
31         buf = ByteBuffer.allocateDirect(BUFFER_LENGTH*4).asIntBuffer();
32         loadTestData1(buf);
33         baseBuf = buf;
34     }
35     
36     public void tearDown(){
37         buf = null;
38         baseBuf = null;
39     }
40
41     /**
42      * Regression for http://code.google.com/p/android/issues/detail?id=3279
43      */
44     @TestTargetNew(
45             level = TestLevel.PARTIAL_COMPLETE,
46             notes = "",
47             method = "put",
48             args = {int[].class, int.class, int.class}
49     )
50     public void testPutWhenOffsetIsNonZero() {
51         ByteBuffer byteBuffer = ByteBuffer.allocateDirect(40);
52         byteBuffer.order(ByteOrder.nativeOrder());
53         IntBuffer intBuffer = byteBuffer.asIntBuffer();
54
55         int[] source = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
56
57         intBuffer.put(source, 2, 2);
58         intBuffer.put(source, 4, 2);
59         assertEquals(4, intBuffer.get(0));
60         assertEquals(5, intBuffer.get(1));
61         assertEquals(6, intBuffer.get(2));
62         assertEquals(7, intBuffer.get(3));
63     }
64
65     @TestTargetNew(
66         level = TestLevel.PARTIAL_COMPLETE,
67         notes = "Verifies hasArray method for direct IntBuffer.",
68         method = "hasArray",
69         args = {}
70     )
71     public void testHasArray() {
72         assertFalse(buf.hasArray());
73     }
74
75     @TestTargetNew(
76         level = TestLevel.PARTIAL_COMPLETE,
77         notes = "Verifies array method for direct IntBuffer.",
78         method = "array",
79         args = {}
80     )
81     public void testArray() {
82         try {
83             buf.array();
84             fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$
85         } catch (UnsupportedOperationException e) {
86         }
87     }
88
89     @TestTargetNew(
90         level = TestLevel.PARTIAL_COMPLETE,
91         notes = "Verifies arrayOffset method for direct IntBuffer.",
92         method = "arrayOffset",
93         args = {}
94     )
95     public void testArrayOffset() {
96         try {
97             buf.arrayOffset();
98             fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$
99         } catch (UnsupportedOperationException e) {
100             //expected
101         }
102     }
103
104     @TestTargetNew(
105         level = TestLevel.PARTIAL_COMPLETE,
106         notes = "Verifies isDirect method for direct IntBuffer.",
107         method = "isDirect",
108         args = {}
109     )
110     public void testIsDirect() {
111         assertTrue(buf.isDirect());
112     }
113
114     @TestTargetNew(
115         level = TestLevel.PARTIAL_COMPLETE,
116         notes = "Verifies order method for direct IntBuffer.",
117         method = "order",
118         args = {}
119     )
120     public void testOrder() {
121         assertEquals(ByteOrder.BIG_ENDIAN, buf.order());
122     }
123
124     @TestTargetNew(
125         level = TestLevel.PARTIAL_COMPLETE,
126         notes = "Regression test for IntToByteBufferAdapter",
127         clazz = ByteBuffer.class,
128         method = "asIntBuffer",
129         args = {}
130     )
131     public void testRangeChecks() {
132         int[] myInts = new int[BUFFER_LENGTH];
133
134         for (int i = 0; i < BUFFER_LENGTH; i++) {
135             myInts[i] = 1000 + i;
136         }
137
138         buf.position(0);
139         buf.put(myInts, 0, BUFFER_LENGTH);
140         buf.position(0);
141         buf.put(myInts, 0, BUFFER_LENGTH);
142
143         try {
144             buf.put(myInts, 0, 1); // should fail
145             fail("BufferOverflowException expected but not thrown");
146         } catch (BufferOverflowException boe) {
147             // expected
148         }
149
150         try {
151             buf.position(0);
152             buf.put(myInts, 0, BUFFER_LENGTH + 1); // should fail
153             fail("BufferOverflowException expected but not thrown");
154         } catch (IndexOutOfBoundsException ioobe) {
155             // expected
156         }
157
158         try {
159             buf.position(BUFFER_LENGTH - 1);
160             buf.put(myInts, 0, 2); // should fail
161             fail("BufferOverflowException expected but not thrown");
162         } catch (BufferOverflowException boe) {
163             // expected
164         }
165     }
166 }