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