Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * 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 com.android.tradefed.util;
     17 
     18 import junit.framework.TestCase;
     19 
     20 /**
     21  * Unit test suite to verify the behavior of the {@link ByteArrayList}
     22  */
     23 public class ByteArrayListTest extends TestCase {
     24     private ByteArrayList mList = null;
     25 
     26     @Override
     27     public void setUp() throws Exception {
     28         super.setUp();
     29         mList = new ByteArrayList();
     30     }
     31 
     32     public void testAdd() {
     33         mList.add((byte) 1);
     34         mList.add((byte) 2);
     35         assertEquals(2, mList.size());
     36     }
     37 
     38     public void testAddAll() {
     39         byte[] byteAry = new byte[] {0, 1, 2, 3, 4, 5};
     40         mList.addAll(byteAry);
     41         assertEquals(byteAry.length, mList.size());
     42         assertEquals(0, mList.get(0));
     43         assertEquals(5, mList.get(5));
     44     }
     45 
     46     public void testAddAll_append() {
     47         byte[] byteAry1 = new byte[] {0, 1, 2};
     48         byte[] byteAry2 = new byte[] {3, 4, 5};
     49         mList.add((byte) 0xa);
     50         mList.addAll(byteAry1);
     51         mList.add((byte) 0xb);
     52         mList.addAll(byteAry2);
     53         assertEquals(byteAry1.length + byteAry2.length + 2, mList.size());
     54         assertEquals(0xa, mList.get(0));
     55         assertEquals(0xb, mList.get(1 + byteAry1.length));
     56         assertEquals(5, mList.get(mList.size()-1));
     57     }
     58 
     59     public void testAddAll_limits() {
     60         byte[] byteAry1 = new byte[] {0, 1, 2};
     61         byte[] byteAry2 = new byte[] {3, 4, 5};
     62         mList.add((byte) 0xa);
     63         mList.addAll(byteAry1, 1, 2);
     64         mList.add((byte) 0xb);
     65         mList.addAll(byteAry2, 3, 0);
     66         assertEquals(1 + 2 + 1 + 0, mList.size());
     67         assertEquals(0xa, mList.get(0));
     68         assertEquals(0xb, mList.get(1 + 2));
     69         assertEquals(1, mList.get(1));
     70     }
     71 
     72     public void testClear() {
     73         mList.add((byte) 1);
     74         mList.add((byte) 2);
     75         assertEquals(2, mList.size());
     76         mList.clear();
     77         assertEquals(0, mList.size());
     78     }
     79 
     80     public void testEnsure() {
     81         mList.setSize(0);
     82         assertEquals(0, mList.getMaxSize());
     83         mList.ensureCapacity(5);
     84         assertTrue(mList.getMaxSize() >= 5);
     85     }
     86 
     87     public void testEnsure_afterAdd() {
     88         mList.setSize(0);
     89         assertEquals(0, mList.getMaxSize());
     90         mList.add((byte) 1);
     91         assertTrue(mList.getMaxSize() >= 1);
     92     }
     93 
     94     public void testEnsure_oddGrowthFactor() {
     95         ByteArrayList list = new ByteArrayList(1, 1.1f);
     96         assertEquals(1, list.getMaxSize());
     97         list.ensureCapacity(2);
     98         assertTrue(list.getMaxSize() >= 2);
     99     }
    100 
    101     /**
    102      * Verify the fix for a bug in the implementation which would cause the List to try to allocate
    103      * infinity memory :o)
    104      */
    105     public void testEnsure_bugfix() {
    106         mList.ensureCapacity(931);
    107         assertTrue(mList.getMaxSize() >= 931);
    108         assertTrue(mList.getMaxSize() <= 931*3);
    109     }
    110 
    111     public void testEquals() {
    112         ByteArrayList list2 = new ByteArrayList();
    113         mList.add((byte) 1);
    114         mList.add((byte) 2);
    115         list2.add((byte) 1);
    116         list2.add((byte) 2);
    117         assertTrue(mList.equals(list2));
    118         assertTrue(list2.equals(mList));
    119     }
    120 
    121     public void testEquals_wrongLength() {
    122         ByteArrayList list2 = new ByteArrayList();
    123         mList.add((byte) 1);
    124         mList.add((byte) 2);
    125         list2.add((byte) 1);
    126         assertFalse(mList.equals(list2));
    127         assertFalse(list2.equals(mList));
    128     }
    129 
    130     public void testEquals_mismatch() {
    131         ByteArrayList list2 = new ByteArrayList();
    132         mList.add((byte) 1);
    133         mList.add((byte) 2);
    134         list2.add((byte) 1);
    135         list2.add((byte) 17);
    136         assertFalse(mList.equals(list2));
    137         assertFalse(list2.equals(mList));
    138     }
    139 
    140     public void testGetContents() {
    141         mList.add((byte) 1);
    142         mList.add((byte) 2);
    143         byte[] val = mList.getContents();
    144         assertEquals(2, val.length);
    145         assertEquals(1, val[0]);
    146     }
    147 
    148     public void testRetrieve() {
    149         mList.add((byte) 1);
    150         mList.add((byte) 2);
    151         assertEquals(1, mList.get(0));
    152         assertEquals(2, mList.get(1));
    153         try {
    154             mList.get(2);
    155             fail("IndexOutOfBoundsException not thrown");
    156         } catch (IndexOutOfBoundsException e) {
    157             // expected
    158         }
    159     }
    160 
    161     public void testSet() {
    162         mList.add((byte) 1);
    163         mList.add((byte) 2);
    164         mList.add((byte) 3);
    165 
    166         assertEquals(2, mList.set(1, (byte) 12));
    167         assertEquals(12, mList.get(1));
    168 
    169         assertEquals(3, mList.set(2, (byte) 13));
    170         assertEquals(13, mList.get(2));
    171     }
    172 
    173     public void testSetSize() {
    174         mList.setSize(256);
    175         assertEquals(256, mList.getMaxSize());
    176     }
    177 
    178     public void testSetSize_truncate() {
    179         mList.setSize(2);
    180         mList.add((byte) 1);
    181         mList.add((byte) 2);
    182         assertEquals(2, mList.getMaxSize());
    183         assertEquals(2, mList.size());
    184 
    185         mList.setSize(1);
    186         assertEquals(1, mList.getMaxSize());
    187         assertEquals(1, mList.size());
    188         assertEquals(1, mList.get(0));
    189     }
    190 
    191     public void testTrimToSize() {
    192         mList.add((byte) 1);
    193         mList.trimToSize();
    194         assertEquals(1, mList.size());
    195         assertEquals(1, mList.getMaxSize());
    196     }
    197 
    198     public void testLimits_setSize() {
    199         try {
    200             mList.setSize(-1);
    201             fail("IllegalArgumentException not thrown");
    202         } catch (IllegalArgumentException e) {
    203             // expected
    204             assertTrue(e.getMessage().contains("New size"));
    205             assertTrue(e.getMessage().contains("non-negative"));
    206         }
    207     }
    208 
    209     public void testLimits_ensureCapacity() {
    210         try {
    211             mList.ensureCapacity(-1);
    212             fail("IllegalArgumentException not thrown");
    213         } catch (IllegalArgumentException e) {
    214             // expected
    215             assertTrue(e.getMessage().contains("Minimum capacity"));
    216             assertTrue(e.getMessage().contains("non-negative"));
    217         }
    218     }
    219 
    220     public void testLimits_get() {
    221         try {
    222             mList.get(0);
    223             fail("IndexOutOfBoundsException not thrown");
    224         } catch (IndexOutOfBoundsException e) {
    225             // expected
    226         }
    227 
    228         mList.add((byte) 1);
    229         try {
    230             mList.get(-1);
    231             fail("IndexOutOfBoundsException not thrown");
    232         } catch (IndexOutOfBoundsException e) {
    233             // expected
    234         }
    235     }
    236 
    237     public void testLimits_set() {
    238         try {
    239             mList.set(0, (byte) 1);
    240             fail("IndexOutOfBoundsException not thrown");
    241         } catch (IndexOutOfBoundsException e) {
    242             // expected
    243         }
    244 
    245         mList.add((byte) 1);
    246         try {
    247             mList.get(-1);
    248             fail("IndexOutOfBoundsException not thrown");
    249         } catch (IndexOutOfBoundsException e) {
    250             // expected
    251         }
    252     }
    253 
    254     public void testLimits_constructor() {
    255         try {
    256             new ByteArrayList(-1);
    257             fail("IllegalArgumentException not thrown");
    258         } catch (IllegalArgumentException e) {
    259             // expected
    260             assertTrue(e.getMessage().contains("New size"));
    261             assertTrue(e.getMessage().contains("non-negative"));
    262         }
    263 
    264         try {
    265             new ByteArrayList(-1, 2.0f);
    266             fail("IllegalArgumentException not thrown");
    267         } catch (IllegalArgumentException e) {
    268             // expected
    269             assertTrue(e.getMessage().contains("New size"));
    270             assertTrue(e.getMessage().contains("non-negative"));
    271         }
    272 
    273         try {
    274             new ByteArrayList(1, 1.0f);
    275             fail("IllegalArgumentException not thrown");
    276         } catch (IllegalArgumentException e) {
    277             // expected
    278             assertTrue(e.getMessage().contains("Growth factor"));
    279             assertTrue(e.getMessage().contains("1.1"));
    280         }
    281     }
    282 }
    283 
    284