Home | History | Annotate | Download | only in nio
      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 
     18 package org.apache.harmony.tests.java.nio;
     19 
     20 import java.nio.BufferOverflowException;
     21 import java.nio.BufferUnderflowException;
     22 import java.nio.ByteOrder;
     23 import java.nio.InvalidMarkException;
     24 import java.nio.LongBuffer;
     25 
     26 /**
     27  * Tests java.nio.LongBuffer
     28  *
     29  */
     30 public class LongBufferTest 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 LongBuffer buf;
     38 
     39     protected void setUp() throws Exception {
     40         buf = LongBuffer.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         long 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         long 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         LongBuffer 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         LongBuffer 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         LongBuffer other = LongBuffer.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         LongBuffer 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         LongBuffer readonly = buf.asReadOnlyBuffer();
    218         assertTrue(buf.equals(readonly));
    219         LongBuffer 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 long 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.LongBuffer get(long[])
    255      */
    256     public void testGetlongArray() {
    257         long array[] = new long[1];
    258         buf.clear();
    259         for (int i = 0; i < buf.capacity(); i++) {
    260             assertEquals(buf.position(), i);
    261             LongBuffer 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.position(buf.limit());
    273             buf.get((long[])null);
    274             fail("Should throw Exception"); //$NON-NLS-1$
    275         } catch (NullPointerException e) {
    276             // expected
    277         }
    278     }
    279 
    280     /*
    281      * Class under test for java.nio.LongBuffer get(long[], int, int)
    282      */
    283     public void testGetlongArrayintint() {
    284         buf.clear();
    285         long array[] = new long[buf.capacity()];
    286 
    287         try {
    288             buf.get(new long[buf.capacity() + 1], 0, buf.capacity() + 1);
    289             fail("Should throw Exception"); //$NON-NLS-1$
    290         } catch (BufferUnderflowException e) {
    291             // expected
    292         }
    293         assertEquals(buf.position(), 0);
    294         try {
    295             buf.get(array, -1, array.length);
    296             fail("Should throw Exception"); //$NON-NLS-1$
    297         } catch (IndexOutOfBoundsException e) {
    298             // expected
    299         }
    300         buf.get(array, array.length, 0);
    301         try {
    302             buf.get(array, array.length + 1, 1);
    303             fail("Should throw Exception"); //$NON-NLS-1$
    304         } catch (IndexOutOfBoundsException e) {
    305             // expected
    306         }
    307         assertEquals(buf.position(), 0);
    308         try {
    309             buf.get(array, 2, -1);
    310             fail("Should throw Exception"); //$NON-NLS-1$
    311         } catch (IndexOutOfBoundsException e) {
    312             // expected
    313         }
    314         try {
    315             buf.get((long[])null, 2, -1);
    316             fail("Should throw Exception"); //$NON-NLS-1$
    317         } catch (NullPointerException e) {
    318             // expected
    319         }
    320         try {
    321             buf.get(array, 2, array.length);
    322             fail("Should throw Exception"); //$NON-NLS-1$
    323         } catch (IndexOutOfBoundsException e) {
    324             // expected
    325         }
    326         try {
    327             buf.get(array, 1, Integer.MAX_VALUE);
    328             fail("Should throw Exception"); //$NON-NLS-1$
    329         } catch (BufferUnderflowException expected) {
    330         } catch (IndexOutOfBoundsException expected) {
    331         }
    332         try {
    333             buf.get(array, Integer.MAX_VALUE, 1);
    334             fail("Should throw Exception"); //$NON-NLS-1$
    335         } catch (IndexOutOfBoundsException e) {
    336             // expected
    337         }
    338         assertEquals(buf.position(), 0);
    339 
    340         buf.clear();
    341         LongBuffer ret = buf.get(array, 0, array.length);
    342         assertEquals(buf.position(), buf.capacity());
    343         assertContentEquals(buf, array, 0, array.length);
    344         assertSame(ret, buf);
    345     }
    346 
    347     /*
    348      * Class under test for long get(int)
    349      */
    350     public void testGetint() {
    351         buf.clear();
    352         for (int i = 0; i < buf.capacity(); i++) {
    353             assertEquals(buf.position(), i);
    354             assertEquals(buf.get(), buf.get(i));
    355         }
    356         try {
    357             buf.get(-1);
    358             fail("Should throw Exception"); //$NON-NLS-1$
    359         } catch (IndexOutOfBoundsException e) {
    360             // expected
    361         }
    362         try {
    363             buf.get(buf.limit());
    364             fail("Should throw Exception"); //$NON-NLS-1$
    365         } catch (IndexOutOfBoundsException e) {
    366             // expected
    367         }
    368     }
    369 
    370     public void testHasArray() {
    371         assertNotNull(buf.array());
    372     }
    373 
    374     public void testHashCode() {
    375         buf.clear();
    376         LongBuffer readonly = buf.asReadOnlyBuffer();
    377         LongBuffer duplicate = buf.duplicate();
    378         assertTrue(buf.hashCode() == readonly.hashCode());
    379 
    380         assertTrue(buf.capacity() > 5);
    381         duplicate.position(buf.capacity() / 2);
    382         assertTrue(buf.hashCode() != duplicate.hashCode());
    383     }
    384 
    385     public void testIsDirect() {
    386         assertFalse(buf.isDirect());
    387     }
    388 
    389     public void testOrder() {
    390         buf.order();
    391         assertEquals(ByteOrder.nativeOrder(), buf.order());
    392     }
    393 
    394     /*
    395      * Class under test for java.nio.LongBuffer put(long)
    396      */
    397     public void testPutlong() {
    398          buf.clear();
    399         for (int i = 0; i < buf.capacity(); i++) {
    400             assertEquals(buf.position(), i);
    401             LongBuffer ret = buf.put((long) i);
    402             assertEquals(buf.get(i), (long) i);
    403             assertSame(ret, buf);
    404         }
    405         try {
    406             buf.put(0);
    407             fail("Should throw Exception"); //$NON-NLS-1$
    408         } catch (BufferOverflowException e) {
    409             // expected
    410         }
    411     }
    412 
    413     /*
    414      * Class under test for java.nio.LongBuffer put(long[])
    415      */
    416     public void testPutlongArray() {
    417         long array[] = new long[1];
    418         buf.clear();
    419         for (int i = 0; i < buf.capacity(); i++) {
    420             assertEquals(buf.position(), i);
    421             array[0] = (long) i;
    422             LongBuffer ret = buf.put(array);
    423             assertEquals(buf.get(i), (long) i);
    424             assertSame(ret, buf);
    425         }
    426         try {
    427             buf.put(array);
    428             fail("Should throw Exception"); //$NON-NLS-1$
    429         } catch (BufferOverflowException e) {
    430             // expected
    431         }
    432         try {
    433             buf.position(buf.limit());
    434             buf.put((long[])null);
    435             fail("Should throw Exception"); //$NON-NLS-1$
    436         } catch (NullPointerException e) {
    437             // expected
    438         }
    439     }
    440 
    441     /*
    442      * Class under test for java.nio.LongBuffer put(long[], int, int)
    443      */
    444     public void testPutlongArrayintint() {
    445         buf.clear();
    446         long array[] = new long[buf.capacity()];
    447         try {
    448             buf.put(new long[buf.capacity() + 1], 0, buf.capacity() + 1);
    449             fail("Should throw Exception"); //$NON-NLS-1$
    450         } catch (BufferOverflowException e) {
    451             // expected
    452         }
    453         assertEquals(buf.position(), 0);
    454         try {
    455             buf.put(array, -1, array.length);
    456             fail("Should throw Exception"); //$NON-NLS-1$
    457         } catch (IndexOutOfBoundsException e) {
    458             // expected
    459         }
    460         try {
    461             buf.put(array, array.length + 1, 0);
    462             fail("Should throw Exception"); //$NON-NLS-1$
    463         } catch (IndexOutOfBoundsException e) {
    464             // expected
    465         }
    466         buf.put(array, array.length, 0);
    467         assertEquals(buf.position(), 0);
    468         try {
    469             buf.put(array, 0, -1);
    470             fail("Should throw Exception"); //$NON-NLS-1$
    471         } catch (IndexOutOfBoundsException e) {
    472             // expected
    473         }
    474         try {
    475             buf.put(array, 2, array.length);
    476             fail("Should throw Exception"); //$NON-NLS-1$
    477         } catch (IndexOutOfBoundsException e) {
    478             // expected
    479         }
    480         try {
    481             buf.put((long[])null, 0, -1);
    482             fail("Should throw Exception"); //$NON-NLS-1$
    483         } catch (NullPointerException e) {
    484             // expected
    485         }
    486         try {
    487             buf.put(array, 2, array.length);
    488             fail("Should throw Exception"); //$NON-NLS-1$
    489         } catch (IndexOutOfBoundsException e) {
    490             // expected
    491         }
    492         try {
    493             buf.put(array, Integer.MAX_VALUE, 1);
    494             fail("Should throw Exception"); //$NON-NLS-1$
    495         } catch (IndexOutOfBoundsException e) {
    496             // expected
    497         }
    498         try {
    499             buf.put(array, 1, Integer.MAX_VALUE);
    500             fail("Should throw Exception"); //$NON-NLS-1$
    501         } catch (BufferOverflowException expected) {
    502         } catch (IndexOutOfBoundsException expected) {
    503         }
    504         assertEquals(buf.position(), 0);
    505 
    506         loadTestData2(array, 0, array.length);
    507         LongBuffer ret = buf.put(array, 0, array.length);
    508         assertEquals(buf.position(), buf.capacity());
    509         assertContentEquals(buf, array, 0, array.length);
    510         assertSame(ret, buf);
    511     }
    512 
    513     /*
    514      * Class under test for java.nio.LongBuffer put(java.nio.LongBuffer)
    515      */
    516     public void testPutLongBuffer() {
    517         LongBuffer other = LongBuffer.allocate(buf.capacity());
    518         try {
    519             buf.put(buf);
    520             fail("Should throw Exception"); //$NON-NLS-1$
    521         } catch (IllegalArgumentException e) {
    522             // expected
    523         }
    524         try {
    525             buf.put(LongBuffer.allocate(buf.capacity() + 1));
    526             fail("Should throw Exception"); //$NON-NLS-1$
    527         } catch (BufferOverflowException e) {
    528             // expected
    529         }
    530         try {
    531             buf.flip();
    532             buf.put((LongBuffer)null);
    533             fail("Should throw Exception"); //$NON-NLS-1$
    534         } catch (NullPointerException e) {
    535             // expected
    536         }
    537 
    538         loadTestData2(other);
    539         other.clear();
    540         buf.clear();
    541         LongBuffer ret = buf.put(other);
    542         assertEquals(other.position(), other.capacity());
    543         assertEquals(buf.position(), buf.capacity());
    544         assertContentEquals(other, buf);
    545         assertSame(ret, buf);
    546     }
    547 
    548     /*
    549      * Class under test for java.nio.LongBuffer put(int, long)
    550      */
    551     public void testPutintlong() {
    552         buf.clear();
    553         for (int i = 0; i < buf.capacity(); i++) {
    554             assertEquals(buf.position(), 0);
    555             LongBuffer ret = buf.put(i, (long) i);
    556             assertEquals(buf.get(i), (long) i);
    557             assertSame(ret, buf);
    558         }
    559         try {
    560             buf.put(-1, 0);
    561             fail("Should throw Exception"); //$NON-NLS-1$
    562         } catch (IndexOutOfBoundsException e) {
    563             // expected
    564         }
    565         try {
    566             buf.put(buf.limit(), 0);
    567             fail("Should throw Exception"); //$NON-NLS-1$
    568         } catch (IndexOutOfBoundsException e) {
    569             // expected
    570         }
    571     }
    572 
    573     public void testSlice() {
    574         assertTrue(buf.capacity() > 5);
    575         buf.position(1);
    576         buf.limit(buf.capacity() - 1);
    577 
    578         LongBuffer slice = buf.slice();
    579         assertEquals(buf.isReadOnly(), slice.isReadOnly());
    580         assertEquals(buf.isDirect(), slice.isDirect());
    581         assertEquals(buf.order(), slice.order());
    582         assertEquals(slice.position(), 0);
    583         assertEquals(slice.limit(), buf.remaining());
    584         assertEquals(slice.capacity(), buf.remaining());
    585         try {
    586             slice.reset();
    587             fail("Should throw Exception"); //$NON-NLS-1$
    588         } catch (InvalidMarkException e) {
    589             // expected
    590         }
    591 
    592         // slice share the same content with buf
    593         if (!slice.isReadOnly()) {
    594             loadTestData1(slice);
    595             assertContentLikeTestData1(buf, 1, 0, slice.capacity());
    596             buf.put(2, 500);
    597             assertEquals(slice.get(1), 500);
    598         }
    599     }
    600 
    601     public void testToString() {
    602         String str = buf.toString();
    603         assertTrue(str.indexOf("Long") >= 0 || str.indexOf("long") >= 0);
    604         assertTrue(str.indexOf("" + buf.position()) >= 0);
    605         assertTrue(str.indexOf("" + buf.limit()) >= 0);
    606         assertTrue(str.indexOf("" + buf.capacity()) >= 0);
    607     }
    608 
    609     void loadTestData1(long array[], int offset, int length) {
    610         for (int i = 0; i < length; i++) {
    611             array[offset + i] = (long) i;
    612         }
    613     }
    614 
    615     void loadTestData2(long array[], int offset, int length) {
    616         for (int i = 0; i < length; i++) {
    617             array[offset + i] = (long) length - i;
    618         }
    619     }
    620 
    621     void loadTestData1(LongBuffer buf) {
    622         buf.clear();
    623         for (int i = 0; i < buf.capacity(); i++) {
    624             buf.put(i, (long) i);
    625         }
    626     }
    627 
    628     void loadTestData2(LongBuffer buf) {
    629         buf.clear();
    630         for (int i = 0; i < buf.capacity(); i++) {
    631             buf.put(i, (long) buf.capacity() - i);
    632         }
    633     }
    634 
    635     void assertContentEquals(LongBuffer buf, long array[],
    636             int offset, int length) {
    637         for (int i = 0; i < length; i++) {
    638             assertEquals(buf.get(i), array[offset + i]);
    639         }
    640     }
    641 
    642     void assertContentEquals(LongBuffer buf, LongBuffer other) {
    643         assertEquals(buf.capacity(), other.capacity());
    644         for (int i = 0; i < buf.capacity(); i++) {
    645             assertEquals(buf.get(i), other.get(i));
    646         }
    647     }
    648 
    649     void assertContentLikeTestData1(LongBuffer buf,
    650             int startIndex, long startValue, int length) {
    651         long value = startValue;
    652         for (int i = 0; i < length; i++) {
    653             assertEquals(buf.get(startIndex + i), value);
    654             value = value + 1;
    655         }
    656     }
    657 }
    658