Home | History | Annotate | Download | only in java
      1 /*
      2  * [The "BSD licence"]
      3  * Copyright (c) 2010 Ben Gruver (JesusFreke)
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 import org.jf.smali.LiteralTools;
     30 import org.junit.Assert;
     31 import org.junit.Test;
     32 
     33 public class ByteLiteralTest
     34 {
     35 
     36     @Test
     37     public void SuccessHexTests() {
     38 
     39         Assert.assertTrue(LiteralTools.parseByte("0x0T") == 0x0);
     40         Assert.assertTrue(LiteralTools.parseByte("0x00") == 0x0);
     41         Assert.assertTrue(LiteralTools.parseByte("0x1T") == 0x1);
     42         Assert.assertTrue(LiteralTools.parseByte("0x12") == 0x12);
     43         Assert.assertTrue(LiteralTools.parseByte("0x7fT") == 0x7f);
     44         Assert.assertTrue(LiteralTools.parseByte("0x80t") == Byte.MIN_VALUE);
     45         Assert.assertTrue(LiteralTools.parseByte("0xFFt") == -1);
     46 
     47         Assert.assertTrue(LiteralTools.parseByte("-0x00") == 0);
     48         Assert.assertTrue(LiteralTools.parseByte("-0x01") == -1);
     49         Assert.assertTrue(LiteralTools.parseByte("-0x12") == -0x12);
     50         Assert.assertTrue(LiteralTools.parseByte("-0x80") == Byte.MIN_VALUE);
     51         Assert.assertTrue(LiteralTools.parseByte("-0x1f") == -0x1f);
     52     }
     53 
     54     @Test(expected=NumberFormatException.class)
     55     public void FaileHexTest1() {
     56         LiteralTools.parseByte("-0x81");
     57     }
     58 
     59     @Test(expected=NumberFormatException.class)
     60     public void FailHexTest2() {
     61         LiteralTools.parseByte("-0xFF");
     62     }
     63 
     64     @Test(expected=NumberFormatException.class)
     65     public void FailHexTest3() {
     66         LiteralTools.parseByte("0x100");
     67     }
     68 
     69 
     70 
     71     @Test
     72     public void SuccessDecTests() {
     73         Assert.assertTrue(LiteralTools.parseByte("0") == 0);
     74         Assert.assertTrue(LiteralTools.parseByte("1t") == 1);
     75         Assert.assertTrue(LiteralTools.parseByte("123") == 123);
     76         Assert.assertTrue(LiteralTools.parseByte("127T") == 127);
     77         Assert.assertTrue(LiteralTools.parseByte("128") == Byte.MIN_VALUE);
     78         Assert.assertTrue(LiteralTools.parseByte("255") == -1);
     79 
     80 
     81         Assert.assertTrue(LiteralTools.parseByte("-0") == 0);
     82         Assert.assertTrue(LiteralTools.parseByte("-1") == -1);
     83         Assert.assertTrue(LiteralTools.parseByte("-123") == -123);
     84         Assert.assertTrue(LiteralTools.parseByte("-127") == -127);
     85         Assert.assertTrue(LiteralTools.parseByte("-128") == Byte.MIN_VALUE);
     86     }
     87 
     88     @Test(expected=NumberFormatException.class)
     89     public void FaileDecTest1() {
     90         LiteralTools.parseByte("-129");
     91     }
     92 
     93     @Test(expected=NumberFormatException.class)
     94     public void FailDecTest2() {
     95         LiteralTools.parseByte("-255");
     96     }
     97 
     98     @Test(expected=NumberFormatException.class)
     99     public void FailDecTest3() {
    100         LiteralTools.parseByte("256");
    101     }
    102 
    103     @Test(expected=NumberFormatException.class)
    104     public void FailDecTest4() {
    105         LiteralTools.parseByte("260");
    106     }
    107 
    108 
    109     @Test
    110     public void SuccessOctTests() {
    111         Assert.assertTrue(LiteralTools.parseByte("00") == 00);
    112         Assert.assertTrue(LiteralTools.parseByte("01") == 01);
    113         Assert.assertTrue(LiteralTools.parseByte("0123t") == 0123);
    114         Assert.assertTrue(LiteralTools.parseByte("0177") == Byte.MAX_VALUE);
    115         Assert.assertTrue(LiteralTools.parseByte("0200T") == Byte.MIN_VALUE);
    116         Assert.assertTrue(LiteralTools.parseByte("0377") == -1);
    117 
    118 
    119         Assert.assertTrue(LiteralTools.parseByte("-00") == 0);
    120         Assert.assertTrue(LiteralTools.parseByte("-01") == -1);
    121         Assert.assertTrue(LiteralTools.parseByte("-0123") == -0123);
    122         Assert.assertTrue(LiteralTools.parseByte("-0177") == -0177);
    123         Assert.assertTrue(LiteralTools.parseByte("-0200") == Byte.MIN_VALUE);
    124     }
    125 
    126     @Test(expected=NumberFormatException.class)
    127     public void FaileOctTest1() {
    128         LiteralTools.parseByte("-0201");
    129     }
    130 
    131     @Test(expected=NumberFormatException.class)
    132     public void FailOctTest2() {
    133         LiteralTools.parseByte("-0377");
    134     }
    135 
    136     @Test(expected=NumberFormatException.class)
    137     public void FailOctTest3() {
    138         LiteralTools.parseByte("0400");
    139     }
    140 }
    141