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 IntLiteralTest
34 {
35     @Test
SuccessHexTests()36     public void SuccessHexTests() {
37 
38         Assert.assertTrue(LiteralTools.parseInt("0x0") == 0x0);
39         Assert.assertTrue(LiteralTools.parseInt("0x00") == 0x0);
40         Assert.assertTrue(LiteralTools.parseInt("0x1") == 0x1);
41         Assert.assertTrue(LiteralTools.parseInt("0x12345678") == 0x12345678);
42         Assert.assertTrue(LiteralTools.parseInt("0x7fffffff") == 0x7fffffff);
43         Assert.assertTrue(LiteralTools.parseInt("0x80000000") == Integer.MIN_VALUE);
44         Assert.assertTrue(LiteralTools.parseInt("0xFFFFFFFF") == -1);
45 
46         Assert.assertTrue(LiteralTools.parseInt("-0x00") == 0);
47         Assert.assertTrue(LiteralTools.parseInt("-0x01") == -1);
48         Assert.assertTrue(LiteralTools.parseInt("-0x12345678") == -0x12345678);
49         Assert.assertTrue(LiteralTools.parseInt("-0x80000000") == Integer.MIN_VALUE);
50         Assert.assertTrue(LiteralTools.parseInt("-0x1FFFFFFF") == -0x1FFFFFFF);
51     }
52 
53     @Test(expected=NumberFormatException.class)
FaileHexTest1()54     public void FaileHexTest1() {
55         LiteralTools.parseInt("-0x80000001");
56     }
57 
58     @Test(expected=NumberFormatException.class)
FailHexTest2()59     public void FailHexTest2() {
60         LiteralTools.parseInt("-0xFFFFFFFF");
61     }
62 
63     @Test(expected=NumberFormatException.class)
FailHexTest3()64     public void FailHexTest3() {
65         LiteralTools.parseInt("0x100000000");
66     }
67 
68 
69 
70     @Test
SuccessDecTests()71     public void SuccessDecTests() {
72         Assert.assertTrue(LiteralTools.parseInt("0") == 0);
73         Assert.assertTrue(LiteralTools.parseInt("1") == 1);
74         Assert.assertTrue(LiteralTools.parseInt("1234567890") == 1234567890);
75         Assert.assertTrue(LiteralTools.parseInt("2147483647") == 2147483647);
76         Assert.assertTrue(LiteralTools.parseInt("2147483648") == Integer.MIN_VALUE);
77         Assert.assertTrue(LiteralTools.parseInt("4294967295") == -1);
78 
79 
80         Assert.assertTrue(LiteralTools.parseInt("-0") == 0);
81         Assert.assertTrue(LiteralTools.parseInt("-1") == -1);
82         Assert.assertTrue(LiteralTools.parseInt("-1234567890") == -1234567890);
83         Assert.assertTrue(LiteralTools.parseInt("-2147483647") == -2147483647);
84         Assert.assertTrue(LiteralTools.parseInt("-2147483648") == Integer.MIN_VALUE);
85     }
86 
87     @Test(expected=NumberFormatException.class)
FaileDecTest1()88     public void FaileDecTest1() {
89         LiteralTools.parseInt("-2147483649");
90     }
91 
92     @Test(expected=NumberFormatException.class)
FailDecTest2()93     public void FailDecTest2() {
94         LiteralTools.parseInt("-4294967295");
95     }
96 
97     @Test(expected=NumberFormatException.class)
FailDecTest3()98     public void FailDecTest3() {
99         LiteralTools.parseInt("4294967296");
100     }
101 
102     @Test(expected=NumberFormatException.class)
FailDecTest4()103     public void FailDecTest4() {
104         LiteralTools.parseInt("4294967300");
105     }
106 
107     @Test(expected=NumberFormatException.class)
FailDecTest5()108     public void FailDecTest5() {
109         LiteralTools.parseInt("8589934592");
110     }
111 
112 
113     @Test
SuccessOctTests()114     public void SuccessOctTests() {
115         Assert.assertTrue(LiteralTools.parseInt("00") == 00);
116         Assert.assertTrue(LiteralTools.parseInt("01") == 01);
117         Assert.assertTrue(LiteralTools.parseInt("012345670123") == 012345670123);
118         Assert.assertTrue(LiteralTools.parseInt("017777777777") == Integer.MAX_VALUE);
119         Assert.assertTrue(LiteralTools.parseInt("020000000000") == Integer.MIN_VALUE);
120         Assert.assertTrue(LiteralTools.parseInt("037777777777") == -1);
121 
122 
123         Assert.assertTrue(LiteralTools.parseInt("-00") == 0);
124         Assert.assertTrue(LiteralTools.parseInt("-01") == -1);
125         Assert.assertTrue(LiteralTools.parseInt("-012345670123") == -012345670123);
126         Assert.assertTrue(LiteralTools.parseInt("-017777777777") == -017777777777);
127         Assert.assertTrue(LiteralTools.parseInt("-020000000000") == Integer.MIN_VALUE);
128     }
129 
130     @Test(expected=NumberFormatException.class)
FaileOctTest1()131     public void FaileOctTest1() {
132         LiteralTools.parseInt("-020000000001");
133     }
134 
135     @Test(expected=NumberFormatException.class)
FailOctTest2()136     public void FailOctTest2() {
137         LiteralTools.parseInt("-037777777777");
138     }
139 
140     @Test(expected=NumberFormatException.class)
FailOctTest3()141     public void FailOctTest3() {
142         LiteralTools.parseInt("040000000000");
143     }
144 }
145