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("0x00L") == 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("-0x8000000000000000L") == Long.MIN_VALUE); 49 Assert.assertTrue(LiteralTools.parseLong("-0x1fffffffffffffffL") == -0x1fffffffffffffffL); 50 } 51 52 @Test(expected=NumberFormatException.class) 53 public void FaileHexTest1() { 54 LiteralTools.parseLong("-0x8000000000000001"); 55 } 56 57 @Test(expected=NumberFormatException.class) 58 public void FailHexTest2() { 59 LiteralTools.parseLong("-0xFFFFFFFFFFFFFFFF"); 60 } 61 62 @Test(expected=NumberFormatException.class) 63 public void FailHexTest3() { 64 LiteralTools.parseLong("0x10000000000000000"); 65 } 66 67 @Test 68 public void SuccessDecTests() { 69 Assert.assertTrue(LiteralTools.parseLong("0L") == 0); 70 Assert.assertTrue(LiteralTools.parseLong("1") == 1); 71 Assert.assertTrue(LiteralTools.parseLong("1234567890123456789") == 1234567890123456789L); 72 Assert.assertTrue(LiteralTools.parseLong("9223372036854775807") == 9223372036854775807L); 73 Assert.assertTrue(LiteralTools.parseLong("9223372036854775808") == Long.MIN_VALUE); 74 Assert.assertTrue(LiteralTools.parseLong("18446744073709551615L") == -1); 75 76 Assert.assertTrue(LiteralTools.parseLong("-0") == 0); 77 Assert.assertTrue(LiteralTools.parseLong("-1") == -1); 78 Assert.assertTrue(LiteralTools.parseLong("-1234567890123456789") == -1234567890123456789L); 79 Assert.assertTrue(LiteralTools.parseLong("-9223372036854775807") == -9223372036854775807L); 80 Assert.assertTrue(LiteralTools.parseLong("-9223372036854775808") == Long.MIN_VALUE); 81 } 82 83 @Test(expected=NumberFormatException.class) 84 public void FaileDecTest1() { 85 LiteralTools.parseLong("-9223372036854775809"); 86 } 87 88 @Test(expected=NumberFormatException.class) 89 public void FailDecTest2() { 90 LiteralTools.parseLong("-18446744073709551616"); 91 } 92 93 @Test(expected=NumberFormatException.class) 94 public void FailDecTest3() { 95 LiteralTools.parseLong("18446744073709551617"); 96 } 97 98 @Test(expected=NumberFormatException.class) 99 public void FailDecTest4() { 100 LiteralTools.parseLong("18446744073709551700"); 101 } 102 103 @Test 104 public void SuccessOctTests() { 105 Assert.assertTrue(LiteralTools.parseLong("00") == 00); 106 Assert.assertTrue(LiteralTools.parseLong("01") == 01); 107 Assert.assertTrue(LiteralTools.parseLong("0123456701234567012345") == 0123456701234567012345L); 108 Assert.assertTrue(LiteralTools.parseLong("0777777777777777777777") == Long.MAX_VALUE); 109 Assert.assertTrue(LiteralTools.parseLong("01000000000000000000000") == Long.MIN_VALUE); 110 Assert.assertTrue(LiteralTools.parseLong("01777777777777777777777") == -1); 111 112 Assert.assertTrue(LiteralTools.parseLong("-00") == 0); 113 Assert.assertTrue(LiteralTools.parseLong("-01") == -1); 114 Assert.assertTrue(LiteralTools.parseLong("-0123456701234567012345") == -0123456701234567012345L); 115 Assert.assertTrue(LiteralTools.parseLong("-0777777777777777777777") == -0777777777777777777777L); 116 Assert.assertTrue(LiteralTools.parseLong("-01000000000000000000000") == Long.MIN_VALUE); 117 } 118 119 @Test(expected=NumberFormatException.class) 120 public void FaileOctTest1() { 121 LiteralTools.parseLong("-01000000000000000000001"); 122 } 123 124 @Test(expected=NumberFormatException.class) 125 public void FailOctTest2() { 126 LiteralTools.parseLong("-01777777777777777777777"); 127 } 128 129 @Test(expected=NumberFormatException.class) 130 public void FailOctTest3() { 131 LiteralTools.parseLong("02000000000000000000000"); 132 } 133} 134