LongLiteralTest.java revision 58b9ba9e08bd877f1b9824ee0bf6c74b8f20e2cf
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
29import org.jf.smali.literalTools;
30import org.junit.Assert;
31import org.junit.Test;
32
33public class LongLiteralTest
34{
35    @Test
36    public void SuccessHexTests() {
37        Assert.assertTrue(literalTools.parseLong("0x0L") == 0x0);
38        Assert.assertTrue(literalTools.parseLong("0x00") == 0x0);
39        Assert.assertTrue(literalTools.parseLong("0x1L") == 0x1);
40        Assert.assertTrue(literalTools.parseLong("0x1234567890123456L") == 0x1234567890123456L);
41        Assert.assertTrue(literalTools.parseLong("0x7fffffffffffffffL") == 0x7fffffffffffffffL);
42        Assert.assertTrue(literalTools.parseLong("0x8000000000000000L") == Long.MIN_VALUE);
43        Assert.assertTrue(literalTools.parseLong("0xFFFFFFFFFFFFFFFFL") == -1);
44
45        Assert.assertTrue(literalTools.parseLong("-0x00L") == 0);
46        Assert.assertTrue(literalTools.parseLong("-0x01L") == -1);
47        Assert.assertTrue(literalTools.parseLong("-0x1234567890123456L") == -0x1234567890123456L);
48        Assert.assertTrue(literalTools.parseLong("-0x8000000000000000") == Long.MIN_VALUE);
49    }
50
51    @Test(expected=NumberFormatException.class)
52    public void FaileHexTest1() {
53        literalTools.parseLong("-0x8000000000000001");
54    }
55
56    @Test(expected=NumberFormatException.class)
57    public void FailHexTest2() {
58        literalTools.parseLong("-0xFFFFFFFFFFFFFFFF");
59    }
60
61    @Test(expected=NumberFormatException.class)
62    public void FailHexTest3() {
63        literalTools.parseLong("0x10000000000000000");
64    }
65
66    @Test
67    public void SuccessDecTests() {
68        Assert.assertTrue(literalTools.parseLong("0L") == 0);
69        Assert.assertTrue(literalTools.parseLong("1") == 1);
70        Assert.assertTrue(literalTools.parseLong("1234567890123456789") == 1234567890123456789L);
71        Assert.assertTrue(literalTools.parseLong("9223372036854775807") == 9223372036854775807L);
72        Assert.assertTrue(literalTools.parseLong("9223372036854775808") == Long.MIN_VALUE);
73        Assert.assertTrue(literalTools.parseLong("18446744073709551615L") == -1);
74
75        Assert.assertTrue(literalTools.parseLong("-0") == 0);
76        Assert.assertTrue(literalTools.parseLong("-1") == -1);
77        Assert.assertTrue(literalTools.parseLong("-1234567890123456789") == -1234567890123456789L);
78        Assert.assertTrue(literalTools.parseLong("-9223372036854775807") == -9223372036854775807L);
79        Assert.assertTrue(literalTools.parseLong("-9223372036854775808") == Long.MIN_VALUE);
80    }
81
82    @Test(expected=NumberFormatException.class)
83    public void FaileDecTest1() {
84        literalTools.parseLong("-9223372036854775809");
85    }
86
87    @Test(expected=NumberFormatException.class)
88    public void FailDecTest2() {
89        literalTools.parseLong("-18446744073709551616");
90    }
91
92    @Test(expected=NumberFormatException.class)
93    public void FailDecTest3() {
94        literalTools.parseLong("18446744073709551617");
95    }
96
97    @Test(expected=NumberFormatException.class)
98    public void FailDecTest4() {
99        literalTools.parseLong("18446744073709551700");
100    }
101
102    @Test
103    public void SuccessOctTests() {
104        Assert.assertTrue(literalTools.parseLong("00") == 00);
105        Assert.assertTrue(literalTools.parseLong("01") == 01);
106        Assert.assertTrue(literalTools.parseLong("0123456701234567012345") == 0123456701234567012345L);
107        Assert.assertTrue(literalTools.parseLong("0777777777777777777777") == Long.MAX_VALUE);
108        Assert.assertTrue(literalTools.parseLong("01000000000000000000000") == Long.MIN_VALUE);
109        Assert.assertTrue(literalTools.parseLong("01777777777777777777777") == -1);
110
111        Assert.assertTrue(literalTools.parseLong("-00") == 0);
112        Assert.assertTrue(literalTools.parseLong("-01") == -1);
113        Assert.assertTrue(literalTools.parseLong("-0123456701234567012345") == -0123456701234567012345L);
114        Assert.assertTrue(literalTools.parseLong("-0777777777777777777777") == -0777777777777777777777L);
115        Assert.assertTrue(literalTools.parseLong("-01000000000000000000000") == Long.MIN_VALUE);
116    }
117
118    @Test(expected=NumberFormatException.class)
119    public void FaileOctTest1() {
120        literalTools.parseLong("-01000000000000000000001");
121    }
122
123    @Test(expected=NumberFormatException.class)
124    public void FailOctTest2() {
125        literalTools.parseLong("-01777777777777777777777");
126    }
127
128    @Test(expected=NumberFormatException.class)
129    public void FailOctTest3() {
130        literalTools.parseLong("02000000000000000000000");
131    }
132}
133