1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.nio;
19
20import java.nio.BufferOverflowException;
21import java.nio.BufferUnderflowException;
22import java.nio.ByteOrder;
23import java.nio.IntBuffer;
24import java.nio.InvalidMarkException;
25
26/**
27 * Tests java.nio.IntBuffer
28 *
29 */
30public class IntBufferTest extends AbstractBufferTest {
31
32
33    protected static final int SMALL_TEST_LENGTH = 5;
34
35    protected static final int BUFFER_LENGTH = 20;
36
37    protected IntBuffer buf;
38
39    protected void setUp() throws Exception {
40        buf = IntBuffer.allocate(BUFFER_LENGTH);
41        loadTestData1(buf);
42        baseBuf = buf;
43    }
44
45    protected void tearDown() throws Exception {
46        buf = null;
47        baseBuf = null;
48    }
49
50    public void testArray() {
51        int array[] = buf.array();
52        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
53
54        loadTestData1(array, buf.arrayOffset(), buf.capacity());
55        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
56
57        loadTestData2(array, buf.arrayOffset(), buf.capacity());
58        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
59
60        loadTestData1(buf);
61        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
62
63        loadTestData2(buf);
64        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
65    }
66
67    public  void testArrayOffset() {
68        int array[] = buf.array();
69        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
70
71        loadTestData1(array, buf.arrayOffset(), buf.capacity());
72        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
73
74        loadTestData2(array, buf.arrayOffset(), buf.capacity());
75        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
76
77        loadTestData1(buf);
78        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
79
80        loadTestData2(buf);
81        assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
82     }
83
84    public  void testAsReadOnlyBuffer() {
85        buf.clear();
86        buf.mark();
87        buf.position(buf.limit());
88
89        // readonly's contents should be the same as buf
90        IntBuffer readonly = buf.asReadOnlyBuffer();
91        assertNotSame(buf, readonly);
92        assertTrue(readonly.isReadOnly());
93        assertEquals(buf.position(), readonly.position());
94        assertEquals(buf.limit(), readonly.limit());
95        assertEquals(buf.isDirect(), readonly.isDirect());
96        assertEquals(buf.order(), readonly.order());
97        assertContentEquals(buf, readonly);
98
99        // readonly's position, mark, and limit should be independent to buf
100        readonly.reset();
101        assertEquals(readonly.position(), 0);
102        readonly.clear();
103        assertEquals(buf.position(), buf.limit());
104        buf.reset();
105        assertEquals(buf.position(), 0);
106    }
107
108    public  void testCompact() {
109        // case: buffer is full
110        buf.clear();
111        buf.mark();
112        loadTestData1(buf);
113        IntBuffer ret = buf.compact();
114        assertSame(ret, buf);
115        assertEquals(buf.position(), buf.capacity());
116        assertEquals(buf.limit(), buf.capacity());
117        assertContentLikeTestData1(buf, 0, 0, buf.capacity());
118        try {
119            buf.reset();
120            fail("Should throw Exception"); //$NON-NLS-1$
121        } catch (InvalidMarkException e) {
122            // expected
123        }
124
125        // case: buffer is empty
126        buf.position(0);
127        buf.limit(0);
128        buf.mark();
129        ret = buf.compact();
130        assertSame(ret, buf);
131        assertEquals(buf.position(), 0);
132        assertEquals(buf.limit(), buf.capacity());
133        assertContentLikeTestData1(buf, 0, 0, buf.capacity());
134        try {
135            buf.reset();
136            fail("Should throw Exception"); //$NON-NLS-1$
137        } catch (InvalidMarkException e) {
138            // expected
139        }
140
141        // case: normal
142        assertTrue(buf.capacity() > 5);
143        buf.position(1);
144        buf.limit(5);
145        buf.mark();
146        ret = buf.compact();
147        assertSame(ret, buf);
148        assertEquals(buf.position(), 4);
149        assertEquals(buf.limit(), buf.capacity());
150        assertContentLikeTestData1(buf, 0, 1, 4);
151        try {
152            buf.reset();
153            fail("Should throw Exception"); //$NON-NLS-1$
154        } catch (InvalidMarkException e) {
155            // expected
156        }
157    }
158
159    public  void testCompareTo() {
160        // compare to self
161        assertEquals(0, buf.compareTo(buf));
162
163        // normal cases
164        assertTrue(buf.capacity() > 5);
165        buf.clear();
166        IntBuffer other = IntBuffer.allocate(buf.capacity());
167        loadTestData1(other);
168        assertEquals(0, buf.compareTo(other));
169        assertEquals(0, other.compareTo(buf));
170        buf.position(1);
171        assertTrue(buf.compareTo(other) > 0);
172        assertTrue(other.compareTo(buf) < 0);
173        other.position(2);
174        assertTrue(buf.compareTo(other) < 0);
175        assertTrue(other.compareTo(buf) > 0);
176        buf.position(2);
177        other.limit(5);
178        assertTrue(buf.compareTo(other) > 0);
179        assertTrue(other.compareTo(buf) < 0);
180    }
181
182    public  void testDuplicate() {
183        buf.clear();
184        buf.mark();
185        buf.position(buf.limit());
186
187        // duplicate's contents should be the same as buf
188        IntBuffer duplicate = buf.duplicate();
189        assertNotSame(buf, duplicate);
190        assertEquals(buf.position(), duplicate.position());
191        assertEquals(buf.limit(), duplicate.limit());
192        assertEquals(buf.isReadOnly(), duplicate.isReadOnly());
193        assertEquals(buf.isDirect(), duplicate.isDirect());
194        assertEquals(buf.order(), duplicate.order());
195        assertContentEquals(buf, duplicate);
196
197        // duplicate's position, mark, and limit should be independent to buf
198        duplicate.reset();
199        assertEquals(duplicate.position(), 0);
200        duplicate.clear();
201        assertEquals(buf.position(), buf.limit());
202        buf.reset();
203        assertEquals(buf.position(), 0);
204
205        // duplicate share the same content with buf
206        if (!duplicate.isReadOnly()) {
207            loadTestData1(buf);
208            assertContentEquals(buf, duplicate);
209            loadTestData2(duplicate);
210            assertContentEquals(buf, duplicate);
211        }
212    }
213
214    public  void testEquals() {
215        // equal to self
216        assertTrue(buf.equals(buf));
217        IntBuffer readonly = buf.asReadOnlyBuffer();
218        assertTrue(buf.equals(readonly));
219        IntBuffer duplicate = buf.duplicate();
220        assertTrue(buf.equals(duplicate));
221
222        // always false, if type mismatch
223        assertFalse(buf.equals(Boolean.TRUE));
224
225        assertTrue(buf.capacity() > 5);
226
227        buf.limit(buf.capacity()).position(0);
228        readonly.limit(readonly.capacity()).position(1);
229        assertFalse(buf.equals(readonly));
230
231        buf.limit(buf.capacity() - 1).position(0);
232        duplicate.limit(duplicate.capacity()).position(0);
233        assertFalse(buf.equals(duplicate));
234    }
235
236    /*
237     * Class under test for int get()
238     */
239    public  void testGet() {
240        buf.clear();
241        for (int i = 0; i < buf.capacity(); i++) {
242            assertEquals(buf.position(), i);
243            assertEquals(buf.get(), buf.get(i));
244        }
245        try {
246            buf.get();
247            fail("Should throw Exception"); //$NON-NLS-1$
248        } catch (BufferUnderflowException e) {
249            // expected
250        }
251    }
252
253    /*
254     * Class under test for java.nio.IntBuffer get(int[])
255     */
256    public  void testGetintArray() {
257        int array[] = new int[1];
258        buf.clear();
259        for (int i = 0; i < buf.capacity(); i++) {
260            assertEquals(buf.position(), i);
261            IntBuffer ret = buf.get(array);
262            assertEquals(array[0], buf.get(i));
263            assertSame(ret, buf);
264        }
265        try {
266            buf.get(array);
267            fail("Should throw Exception"); //$NON-NLS-1$
268        } catch (BufferUnderflowException e) {
269            // expected
270        }
271        try {
272            buf.get((int[])null);
273            fail("Should throw NPE"); //$NON-NLS-1$
274        } catch (NullPointerException e) {
275            // expected
276        }
277    }
278
279    /*
280     * Class under test for java.nio.IntBuffer get(int[], int, int)
281     */
282    public  void testGetintArrayintint() {
283        buf.clear();
284        int array[] = new int[buf.capacity()];
285
286        try {
287            buf.get(new int[buf.capacity() + 1], 0, buf.capacity() + 1);
288            fail("Should throw Exception"); //$NON-NLS-1$
289        } catch (BufferUnderflowException e) {
290            // expected
291        }
292        assertEquals(buf.position(), 0);
293        try {
294            buf.get(array, -1, array.length);
295            fail("Should throw Exception"); //$NON-NLS-1$
296        } catch (IndexOutOfBoundsException e) {
297            // expected
298        }
299        buf.get(array, array.length, 0);
300        try {
301            buf.get(array, array.length + 1, 1);
302            fail("Should throw Exception"); //$NON-NLS-1$
303        } catch (IndexOutOfBoundsException e) {
304            // expected
305        }
306        assertEquals(buf.position(), 0);
307        try {
308            buf.get(array, 2, -1);
309            fail("Should throw Exception"); //$NON-NLS-1$
310        } catch (IndexOutOfBoundsException e) {
311            // expected
312        }
313        try {
314            buf.get((int[])null, 2, -1);
315            fail("Should throw Exception"); //$NON-NLS-1$
316        } catch (NullPointerException e) {
317            // expected
318        }
319        try {
320            buf.get(array, 2, array.length);
321            fail("Should throw Exception"); //$NON-NLS-1$
322        } catch (IndexOutOfBoundsException e) {
323            // expected
324        }
325        try {
326            buf.get(array, 1, Integer.MAX_VALUE);
327            fail("Should throw Exception"); //$NON-NLS-1$
328        } catch (BufferUnderflowException expected) {
329        } catch (IndexOutOfBoundsException expected) {
330        }
331        try {
332            buf.get(array, Integer.MAX_VALUE, 1);
333            fail("Should throw Exception"); //$NON-NLS-1$
334        } catch (IndexOutOfBoundsException e) {
335            // expected
336        }
337        assertEquals(buf.position(), 0);
338
339        buf.clear();
340        IntBuffer ret = buf.get(array, 0, array.length);
341        assertEquals(buf.position(), buf.capacity());
342        assertContentEquals(buf, array, 0, array.length);
343        assertSame(ret, buf);
344    }
345
346    /*
347     * Class under test for int get(int)
348     */
349    public  void testGetint() {
350        buf.clear();
351        for (int i = 0; i < buf.capacity(); i++) {
352            assertEquals(buf.position(), i);
353            assertEquals(buf.get(), buf.get(i));
354        }
355        try {
356            buf.get(-1);
357            fail("Should throw Exception"); //$NON-NLS-1$
358        } catch (IndexOutOfBoundsException e) {
359            // expected
360        }
361        try {
362            buf.get(buf.limit());
363            fail("Should throw Exception"); //$NON-NLS-1$
364        } catch (IndexOutOfBoundsException e) {
365            // expected
366        }
367    }
368
369    public  void testHasArray() {
370        assertNotNull(buf.array());
371    }
372
373    public  void testHashCode() {
374        buf.clear();
375        IntBuffer readonly = buf.asReadOnlyBuffer();
376        IntBuffer duplicate = buf.duplicate();
377        assertTrue(buf.hashCode() == readonly.hashCode());
378
379        assertTrue(buf.capacity() > 5);
380        duplicate.position(buf.capacity() / 2);
381        assertTrue(buf.hashCode() != duplicate.hashCode());
382    }
383
384    public  void testIsDirect() {
385        assertFalse(buf.isDirect());
386    }
387
388    public  void testOrder() {
389        buf.order();
390        assertEquals(ByteOrder.nativeOrder(), buf.order());
391    }
392
393    /*
394     * Class under test for java.nio.IntBuffer put(int)
395     */
396    public  void testPutint() {
397        buf.clear();
398        for (int i = 0; i < buf.capacity(); i++) {
399            assertEquals(buf.position(), i);
400            IntBuffer ret = buf.put((int) i);
401            assertEquals(buf.get(i), (int) i);
402            assertSame(ret, buf);
403        }
404        try {
405            buf.put(0);
406            fail("Should throw Exception"); //$NON-NLS-1$
407        } catch (BufferOverflowException e) {
408            // expected
409        }
410    }
411
412    /*
413     * Class under test for java.nio.IntBuffer put(int[])
414     */
415    public  void testPutintArray() {
416        int array[] = new int[1];
417        buf.clear();
418        for (int i = 0; i < buf.capacity(); i++) {
419            assertEquals(buf.position(), i);
420            array[0] = (int) i;
421            IntBuffer ret = buf.put(array);
422            assertEquals(buf.get(i), (int) i);
423            assertSame(ret, buf);
424        }
425        try {
426            buf.put(array);
427            fail("Should throw Exception"); //$NON-NLS-1$
428        } catch (BufferOverflowException e) {
429            // expected
430        }
431        try {
432            buf.position(buf.limit());
433            buf.put((int[])null);
434            fail("Should throw Exception"); //$NON-NLS-1$
435        } catch (NullPointerException e) {
436            // expected
437        }
438    }
439
440    /*
441     * Class under test for java.nio.IntBuffer put(int[], int, int)
442     */
443    public  void testPutintArrayintint() {
444        buf.clear();
445        int array[] = new int[buf.capacity()];
446        try {
447            buf.put(new int[buf.capacity() + 1], 0, buf.capacity() + 1);
448            fail("Should throw Exception"); //$NON-NLS-1$
449        } catch (BufferOverflowException e) {
450            // expected
451        }
452        assertEquals(buf.position(), 0);
453        try {
454            buf.put(array, -1, array.length);
455            fail("Should throw Exception"); //$NON-NLS-1$
456        } catch (IndexOutOfBoundsException e) {
457            // expected
458        }
459        try {
460            buf.put(array, array.length + 1, 0);
461            fail("Should throw Exception"); //$NON-NLS-1$
462        } catch (IndexOutOfBoundsException e) {
463            // expected
464        }
465        buf.put(array, array.length, 0);
466        assertEquals(buf.position(), 0);
467        try {
468            buf.put(array, 0, -1);
469            fail("Should throw Exception"); //$NON-NLS-1$
470        } catch (IndexOutOfBoundsException e) {
471            // expected
472        }
473        try {
474            buf.put((int[])null, 0, -1);
475            fail("Should throw Exception"); //$NON-NLS-1$
476        } catch (NullPointerException e) {
477            // expected
478        }
479        try {
480            buf.put(array, 2, array.length);
481            fail("Should throw Exception"); //$NON-NLS-1$
482        } catch (IndexOutOfBoundsException e) {
483            // expected
484        }
485        try {
486            buf.put(array, Integer.MAX_VALUE, 1);
487            fail("Should throw Exception"); //$NON-NLS-1$
488        } catch (IndexOutOfBoundsException e) {
489            // expected
490        }
491        try {
492            buf.put(array, 1, Integer.MAX_VALUE);
493            fail("Should throw Exception"); //$NON-NLS-1$
494        } catch (BufferOverflowException expected) {
495        } catch (IndexOutOfBoundsException expected) {
496        }
497        assertEquals(buf.position(), 0);
498
499        loadTestData2(array, 0, array.length);
500        IntBuffer ret = buf.put(array, 0, array.length);
501        assertEquals(buf.position(), buf.capacity());
502        assertContentEquals(buf, array, 0, array.length);
503        assertSame(ret, buf);
504    }
505
506    /*
507     * Class under test for java.nio.IntBuffer put(java.nio.IntBuffer)
508     */
509    public  void testPutIntBuffer() {
510        IntBuffer other = IntBuffer.allocate(buf.capacity());
511        try {
512            buf.put(buf);
513            fail("Should throw Exception"); //$NON-NLS-1$
514        } catch (IllegalArgumentException e) {
515            // expected
516        }
517        try {
518            buf.put(IntBuffer.allocate(buf.capacity() + 1));
519            fail("Should throw Exception"); //$NON-NLS-1$
520        } catch (BufferOverflowException e) {
521            // expected
522        }
523        try {
524            buf.flip();
525            buf.put((IntBuffer)null);
526            fail("Should throw Exception"); //$NON-NLS-1$
527        } catch (NullPointerException e) {
528            // expected
529        }
530
531        loadTestData2(other);
532        other.clear();
533        buf.clear();
534        IntBuffer ret = buf.put(other);
535        assertEquals(other.position(), other.capacity());
536        assertEquals(buf.position(), buf.capacity());
537        assertContentEquals(other, buf);
538        assertSame(ret, buf);
539    }
540
541    /*
542     * Class under test for java.nio.IntBuffer put(int, int)
543     */
544    public  void testPutintint() {
545        buf.clear();
546        for (int i = 0; i < buf.capacity(); i++) {
547            assertEquals(buf.position(), 0);
548            IntBuffer ret = buf.put(i, (int) i);
549            assertEquals(buf.get(i), (int) i);
550            assertSame(ret, buf);
551        }
552        try {
553            buf.put(-1, 0);
554            fail("Should throw Exception"); //$NON-NLS-1$
555        } catch (IndexOutOfBoundsException e) {
556            // expected
557        }
558        try {
559            buf.put(buf.limit(), 0);
560            fail("Should throw Exception"); //$NON-NLS-1$
561        } catch (IndexOutOfBoundsException e) {
562            // expected
563        }
564    }
565
566    public  void testSlice() {
567        assertTrue(buf.capacity() > 5);
568        buf.position(1);
569        buf.limit(buf.capacity() - 1);
570
571        IntBuffer slice = buf.slice();
572        assertEquals(buf.isReadOnly(), slice.isReadOnly());
573        assertEquals(buf.isDirect(), slice.isDirect());
574        assertEquals(buf.order(), slice.order());
575        assertEquals(slice.position(), 0);
576        assertEquals(slice.limit(), buf.remaining());
577        assertEquals(slice.capacity(), buf.remaining());
578        try {
579            slice.reset();
580            fail("Should throw Exception"); //$NON-NLS-1$
581        } catch (InvalidMarkException e) {
582            // expected
583        }
584
585        // slice share the same content with buf
586        if (!slice.isReadOnly()) {
587            loadTestData1(slice);
588            assertContentLikeTestData1(buf, 1, 0, slice.capacity());
589            buf.put(2, 500);
590            assertEquals(slice.get(1), 500);
591        }
592    }
593
594    public  void testToString() {
595        String str = buf.toString();
596        assertTrue(str.indexOf("Int") >= 0 || str.indexOf("int") >= 0);
597        assertTrue(str.indexOf("" + buf.position()) >= 0);
598        assertTrue(str.indexOf("" + buf.limit()) >= 0);
599        assertTrue(str.indexOf("" + buf.capacity()) >= 0);
600    }
601
602      void loadTestData1(int array[], int offset, int length) {
603        for (int i = 0; i < length; i++) {
604            array[offset + i] = (int) i;
605        }
606    }
607
608      void loadTestData2(int array[], int offset, int length) {
609        for (int i = 0; i < length; i++) {
610            array[offset + i] = (int) length - i;
611        }
612    }
613
614      void loadTestData1(IntBuffer buf) {
615        buf.clear();
616        for (int i = 0; i < buf.capacity(); i++) {
617            buf.put(i, (int) i);
618        }
619    }
620
621      void loadTestData2(IntBuffer buf) {
622        buf.clear();
623        for (int i = 0; i < buf.capacity(); i++) {
624            buf.put(i, (int) buf.capacity() - i);
625        }
626    }
627
628      void assertContentEquals(IntBuffer buf, int array[],
629            int offset, int length) {
630        for (int i = 0; i < length; i++) {
631            assertEquals(buf.get(i), array[offset + i]);
632        }
633    }
634
635      void assertContentEquals(IntBuffer buf, IntBuffer other) {
636        assertEquals(buf.capacity(), other.capacity());
637        for (int i = 0; i < buf.capacity(); i++) {
638            assertEquals(buf.get(i), other.get(i));
639        }
640    }
641
642      void assertContentLikeTestData1(IntBuffer buf,
643            int startIndex, int startValue, int length) {
644        int value = startValue;
645        for (int i = 0; i < length; i++) {
646            assertEquals(buf.get(startIndex + i), value);
647            value = value + 1;
648        }
649    }
650}
651