Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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.util.proto.cts;
     18 
     19 import android.util.proto.ProtoOutputStream;
     20 import android.util.proto.cts.nano.Test;
     21 
     22 import com.google.protobuf.nano.MessageNano;
     23 import junit.framework.TestCase;
     24 import org.junit.Assert;
     25 
     26 /**
     27  * Test the bool methods on the ProtoOutputStream class.
     28  */
     29 public class ProtoOutputStreamBoolTest extends TestCase {
     30 
     31     // ----------------------------------------------------------------------
     32     //  writeBool
     33     // ----------------------------------------------------------------------
     34 
     35     /**
     36      * Test writeBool.
     37      */
     38     public void testWrite() throws Exception {
     39         testWrite(0);
     40         testWrite(1);
     41         testWrite(5);
     42     }
     43 
     44     /**
     45      * Implementation of testWrite with a given chunkSize.
     46      */
     47     public void testWrite(int chunkSize) throws Exception {
     48         final ProtoOutputStream po = new ProtoOutputStream(chunkSize);
     49         final long fieldFlags = ProtoOutputStream.FIELD_COUNT_SINGLE | ProtoOutputStream.FIELD_TYPE_BOOL;
     50 
     51         po.writeBool(ProtoOutputStream.makeFieldId(1, fieldFlags), false);
     52         po.writeBool(ProtoOutputStream.makeFieldId(2, fieldFlags), true);
     53 
     54         final byte[] result = po.getBytes();
     55         Assert.assertArrayEquals(new byte[] {
     56                 // 1 -> 0 - default value, not written
     57                 // 2 -> 1
     58                 (byte)0x10,
     59                 (byte)0x01,
     60             }, result);
     61     }
     62 
     63     /**
     64      * Test that writing a with ProtoOutputStream matches, and can be read by standard proto.
     65      */
     66     public void testWriteCompat() throws Exception {
     67         testWriteCompat(false);
     68         testWriteCompat(true);
     69     }
     70 
     71     /**
     72      * Implementation of testWriteCompat with a given value.
     73      */
     74     public void testWriteCompat(boolean val) throws Exception {
     75         final int fieldId = 130;
     76         final long fieldFlags = ProtoOutputStream.FIELD_COUNT_SINGLE | ProtoOutputStream.FIELD_TYPE_BOOL;
     77 
     78         final Test.All all = new Test.All();
     79         final ProtoOutputStream po = new ProtoOutputStream(0);
     80 
     81         all.boolField = val;
     82         po.writeBool(ProtoOutputStream.makeFieldId(fieldId, fieldFlags), val);
     83 
     84         final byte[] result = po.getBytes();
     85         final byte[] expected = MessageNano.toByteArray(all);
     86 
     87         Assert.assertArrayEquals(expected, result);
     88 
     89         final Test.All readback = Test.All.parseFrom(result);
     90 
     91         assertEquals(val, readback.boolField);
     92     }
     93 
     94     // ----------------------------------------------------------------------
     95     //  writeRepeatedBool
     96     // ----------------------------------------------------------------------
     97 
     98     /**
     99      * Test writeRepeatedBool.
    100      */
    101     public void testRepeated() throws Exception {
    102         testRepeated(0);
    103         testRepeated(1);
    104         testRepeated(5);
    105     }
    106 
    107     /**
    108      * Implementation of testRepeated with a given chunkSize.
    109      */
    110     public void testRepeated(int chunkSize) throws Exception {
    111         final ProtoOutputStream po = new ProtoOutputStream(chunkSize);
    112         final long fieldFlags = ProtoOutputStream.FIELD_COUNT_REPEATED | ProtoOutputStream.FIELD_TYPE_BOOL;
    113 
    114         po.writeRepeatedBool(ProtoOutputStream.makeFieldId(1, fieldFlags), false);
    115         po.writeRepeatedBool(ProtoOutputStream.makeFieldId(2, fieldFlags), true);
    116 
    117         po.writeRepeatedBool(ProtoOutputStream.makeFieldId(1, fieldFlags), false);
    118         po.writeRepeatedBool(ProtoOutputStream.makeFieldId(2, fieldFlags), true);
    119 
    120         final byte[] result = po.getBytes();
    121         Assert.assertArrayEquals(new byte[] {
    122                 // 1 -> 0 - default value, written when repeated
    123                 (byte)0x08,
    124                 (byte)0x00,
    125                 // 2 -> 1
    126                 (byte)0x10,
    127                 (byte)0x01,
    128 
    129                 // 1 -> 0 - default value, written when repeated
    130                 (byte)0x08,
    131                 (byte)0x00,
    132                 // 2 -> 1
    133                 (byte)0x10,
    134                 (byte)0x01,
    135             }, result);
    136     }
    137 
    138     /**
    139      * Test that writing a with ProtoOutputStream matches, and can be read by standard proto.
    140      */
    141     public void testRepeatedCompat() throws Exception {
    142         testRepeatedCompat(new boolean[0]);
    143         testRepeatedCompat(new boolean[] { false, true });
    144     }
    145 
    146     /**
    147      * Implementation of testRepeatedCompat with a given value.
    148      */
    149     public void testRepeatedCompat(boolean[] val) throws Exception {
    150         final int fieldId = 131;
    151         final long fieldFlags = ProtoOutputStream.FIELD_COUNT_REPEATED | ProtoOutputStream.FIELD_TYPE_BOOL;
    152 
    153         final Test.All all = new Test.All();
    154         final ProtoOutputStream po = new ProtoOutputStream(0);
    155 
    156         all.boolFieldRepeated = val;
    157         for (int i=0; i<val.length; i++) {
    158             po.writeRepeatedBool(ProtoOutputStream.makeFieldId(fieldId, fieldFlags), val[i]);
    159         }
    160 
    161         final byte[] result = po.getBytes();
    162         final byte[] expected = MessageNano.toByteArray(all);
    163 
    164         Assert.assertArrayEquals(expected, result);
    165 
    166         final Test.All readback = Test.All.parseFrom(result);
    167 
    168         assertNotNull(readback.boolFieldRepeated);
    169         assertEquals(val.length, readback.boolFieldRepeated.length);
    170         for (int i=0; i<val.length; i++) {
    171             assertEquals(val[i], readback.boolFieldRepeated[i]);
    172         }
    173     }
    174 
    175     // ----------------------------------------------------------------------
    176     //  writePackedBool
    177     // ----------------------------------------------------------------------
    178 
    179     /**
    180      * Test writePackedBool.
    181      */
    182     public void testPacked() throws Exception {
    183         testPacked(0);
    184         testPacked(1);
    185         testPacked(5);
    186     }
    187 
    188     /**
    189      * Create an array of the val, and write it.
    190      */
    191     private void writePackedBool(ProtoOutputStream po, int fieldId, boolean val) {
    192         final long fieldFlags = ProtoOutputStream.FIELD_COUNT_PACKED | ProtoOutputStream.FIELD_TYPE_BOOL;
    193         po.writePackedBool(ProtoOutputStream.makeFieldId(fieldId, fieldFlags),
    194                 new boolean[] { val, val });
    195     }
    196 
    197     /**
    198      * Implementation of testPacked with a given chunkSize.
    199      */
    200     public void testPacked(int chunkSize) throws Exception {
    201         final ProtoOutputStream po = new ProtoOutputStream(chunkSize);
    202         final long fieldFlags = ProtoOutputStream.FIELD_COUNT_PACKED | ProtoOutputStream.FIELD_TYPE_BOOL;
    203 
    204         po.writePackedBool(ProtoOutputStream.makeFieldId(1000, fieldFlags), null);
    205         po.writePackedBool(ProtoOutputStream.makeFieldId(1001, fieldFlags), new boolean[0]);
    206         writePackedBool(po, 1, false);
    207         writePackedBool(po, 2, true);
    208 
    209         final byte[] result = po.getBytes();
    210         Assert.assertArrayEquals(new byte[] {
    211                 // 1 -> 0 - default value, written when repeated
    212                 (byte)0x0a,
    213                 (byte)0x02,
    214                 (byte)0x00,
    215                 (byte)0x00,
    216                 // 2 -> 1
    217                 (byte)0x12,
    218                 (byte)0x02,
    219                 (byte)0x01,
    220                 (byte)0x01,
    221             }, result);
    222     }
    223 
    224     /**
    225      * Test that writing a with ProtoOutputStream matches, and can be read by standard proto.
    226      */
    227     public void testPackedCompat() throws Exception {
    228         testPackedCompat(new boolean[] {});
    229         testPackedCompat(new boolean[] { false, true });
    230     }
    231 
    232     /**
    233      * Implementation of testPackedBoolCompat with a given value.
    234      */
    235     public void testPackedCompat(boolean[] val) throws Exception {
    236         final int fieldId = 132;
    237         final long fieldFlags = ProtoOutputStream.FIELD_COUNT_PACKED | ProtoOutputStream.FIELD_TYPE_BOOL;
    238 
    239         final Test.All all = new Test.All();
    240         final ProtoOutputStream po = new ProtoOutputStream(0);
    241 
    242         all.boolFieldPacked = val;
    243         po.writePackedBool(ProtoOutputStream.makeFieldId(fieldId, fieldFlags), val);
    244 
    245         final byte[] result = po.getBytes();
    246         final byte[] expected = MessageNano.toByteArray(all);
    247 
    248         Assert.assertArrayEquals(expected, result);
    249 
    250         final Test.All readback = Test.All.parseFrom(result);
    251 
    252         Assert.assertArrayEquals(val, readback.boolFieldPacked);
    253     }
    254 
    255     /**
    256      * Test that if you pass in the wrong type of fieldId, it throws.
    257      */
    258     public void testBadFieldIds() {
    259         // Single
    260 
    261         // Good Count / Bad Type
    262         try {
    263             final ProtoOutputStream po = new ProtoOutputStream();
    264             po.writeBool(ProtoOutputStream.makeFieldId(1,
    265                         ProtoOutputStream.FIELD_COUNT_SINGLE | ProtoOutputStream.FIELD_TYPE_DOUBLE), false);
    266         } catch (IllegalArgumentException ex) {
    267             // good
    268         }
    269 
    270         // Bad Count / Good Type
    271         try {
    272             final ProtoOutputStream po = new ProtoOutputStream();
    273             po.writeBool(ProtoOutputStream.makeFieldId(1,
    274                         ProtoOutputStream.FIELD_COUNT_PACKED | ProtoOutputStream.FIELD_TYPE_BOOL), false);
    275         } catch (IllegalArgumentException ex) {
    276             // good
    277         }
    278 
    279         // Repeated
    280 
    281         // Good Count / Bad Type
    282         try {
    283             final ProtoOutputStream po = new ProtoOutputStream();
    284             po.writeRepeatedBool(ProtoOutputStream.makeFieldId(1,
    285                         ProtoOutputStream.FIELD_COUNT_REPEATED | ProtoOutputStream.FIELD_TYPE_DOUBLE), false);
    286         } catch (IllegalArgumentException ex) {
    287             // good
    288         }
    289 
    290         // Bad Count / Good Type
    291         try {
    292             final ProtoOutputStream po = new ProtoOutputStream();
    293             po.writeRepeatedBool(ProtoOutputStream.makeFieldId(1,
    294                         ProtoOutputStream.FIELD_COUNT_PACKED | ProtoOutputStream.FIELD_TYPE_BOOL), false);
    295         } catch (IllegalArgumentException ex) {
    296             // good
    297         }
    298 
    299         // Packed
    300 
    301         // Good Count / Bad Type
    302         try {
    303             final ProtoOutputStream po = new ProtoOutputStream();
    304             po.writePackedBool(ProtoOutputStream.makeFieldId(1,
    305                         ProtoOutputStream.FIELD_COUNT_PACKED | ProtoOutputStream.FIELD_TYPE_DOUBLE),
    306                     new boolean[0]);
    307         } catch (IllegalArgumentException ex) {
    308             // good
    309         }
    310 
    311         // Bad Count / Good Type
    312         try {
    313             final ProtoOutputStream po = new ProtoOutputStream();
    314             po.writePackedBool(ProtoOutputStream.makeFieldId(1,
    315                         ProtoOutputStream.FIELD_COUNT_SINGLE | ProtoOutputStream.FIELD_TYPE_BOOL),
    316                     new boolean[0]);
    317         } catch (IllegalArgumentException ex) {
    318             // good
    319         }
    320     }
    321 }
    322