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