Home | History | Annotate | Download | only in text
      1 /*
      2  * Copyright (C) 2007 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 
     17 package android.text;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 
     21 import android.platform.test.annotations.Presubmit;
     22 import android.support.test.filters.SmallTest;
     23 import android.support.test.runner.AndroidJUnit4;
     24 
     25 import org.junit.Test;
     26 import org.junit.runner.RunWith;
     27 
     28 /**
     29  * PackedIntVectorTest tests the features of android.util.PackedIntVector.
     30  */
     31 @Presubmit
     32 @SmallTest
     33 @RunWith(AndroidJUnit4.class)
     34 public class PackedIntVectorTest {
     35 
     36     @Test
     37     public void testBasic() {
     38         for (int width = 0; width < 10; width++) {
     39             PackedIntVector p = new PackedIntVector(width);
     40             int[] ins = new int[width];
     41 
     42             for (int height = width * 2; height < width * 4; height++) {
     43                 assertEquals(p.width(), width);
     44 
     45                 // Test adding rows.
     46 
     47                 for (int i = 0; i < height; i++) {
     48                     int at;
     49 
     50                     if (i % 2 == 0) {
     51                         at = i;
     52                     } else {
     53                         at = p.size() - i;
     54                     }
     55 
     56                     for (int j = 0; j < width; j++) {
     57                         ins[j] = i + j;
     58                     }
     59 
     60                     if (i == height / 2) {
     61                         p.insertAt(at, null);
     62                     } else {
     63                         p.insertAt(at, ins);
     64                     }
     65 
     66                     assertEquals(p.size(), i + 1);
     67 
     68                     for (int j = 0; j < width; j++) {
     69                         if (i == height / 2) {
     70                             assertEquals(0, p.getValue(at, j));
     71                         } else {
     72                             assertEquals(p.getValue(at, j), i + j);
     73                         }
     74                     }
     75                 }
     76 
     77                 // Test setting values.
     78 
     79                 for (int i = 0; i < height; i++) {
     80                     for (int j = 0; j < width; j++) {
     81                         p.setValue(i, j, i * j);
     82 
     83                         assertEquals(p.getValue(i, j), i * j);
     84                     }
     85                 }
     86 
     87                 // Test offsetting values.
     88 
     89                 for (int j = 0; j < width; j++) {
     90                     p.adjustValuesBelow(j * 2, j, j + 27);
     91                 }
     92 
     93                 for (int i = 0; i < height; i++) {
     94                     for (int j = 0; j < width; j++) {
     95                         int expect = i * j;
     96 
     97                         if (i >= j * 2) {
     98                             expect += j + 27;
     99                         }
    100 
    101                         assertEquals(p.getValue(i, j), expect);
    102                     }
    103                 }
    104 
    105                 for (int j = 0; j < width; j++) {
    106                     p.adjustValuesBelow(j, j, j * j + 14);
    107                 }
    108 
    109                 for (int i = 0; i < height; i++) {
    110                     for (int j = 0; j < width; j++) {
    111                         int expect = i * j;
    112 
    113                         if (i >= j * 2) {
    114                             expect += j + 27;
    115                         }
    116                         if (i >= j) {
    117                             expect += j * j + 14;
    118                         }
    119 
    120                         assertEquals(p.getValue(i, j), expect);
    121                     }
    122                 }
    123 
    124                 // Test undoing offsets.
    125 
    126                 for (int j = 0; j < width; j++) {
    127                     p.adjustValuesBelow(j * 2, j, -(j + 27));
    128                     p.adjustValuesBelow(j, j, -(j * j + 14));
    129                 }
    130 
    131                 for (int i = 0; i < height; i++) {
    132                     for (int j = 0; j < width; j++) {
    133                         assertEquals(p.getValue(i, j), i * j);
    134                     }
    135                 }
    136 
    137                 // Test deleting rows.
    138 
    139                 while (p.size() > 0) {
    140                     int osize = p.size();
    141                     int del = osize / 3;
    142 
    143                     if (del == 0) {
    144                         del = 1;
    145                     }
    146 
    147                     int at = (osize - del) / 2;
    148                     p.deleteAt(at, del);
    149 
    150                     assertEquals(p.size(), osize - del);
    151 
    152                     for (int i = 0; i < at; i++) {
    153                         for (int j = 0; j < width; j++) {
    154                             assertEquals(p.getValue(i, j), i * j);
    155                         }
    156                     }
    157 
    158                     for (int i = at; i < p.size(); i++) {
    159                         for (int j = 0; j < width; j++) {
    160                             assertEquals(p.getValue(i, j), (i + height - p.size()) * j);
    161                         }
    162                     }
    163                 }
    164 
    165                 assertEquals(0, p.size());
    166             }
    167         }
    168     }
    169 }
    170