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 ShortLiteralTest 34{ 35 36 @Test 37 public void SuccessHexTests() { 38 39 Assert.assertTrue(LiteralTools.parseShort("0x0") == 0x0); 40 Assert.assertTrue(LiteralTools.parseShort("0x00") == 0x0); 41 Assert.assertTrue(LiteralTools.parseShort("0x1") == 0x1); 42 Assert.assertTrue(LiteralTools.parseShort("0x1234") == 0x1234); 43 Assert.assertTrue(LiteralTools.parseShort("0x7fff") == 0x7fff); 44 Assert.assertTrue(LiteralTools.parseShort("0x8000") == Short.MIN_VALUE); 45 Assert.assertTrue(LiteralTools.parseShort("0xFFFF") == -1); 46 47 Assert.assertTrue(LiteralTools.parseShort("-0x00") == 0); 48 Assert.assertTrue(LiteralTools.parseShort("-0x01") == -1); 49 Assert.assertTrue(LiteralTools.parseShort("-0x1234") == -0x1234); 50 Assert.assertTrue(LiteralTools.parseShort("-0x8000") == Short.MIN_VALUE); 51 Assert.assertTrue(LiteralTools.parseShort("-0x1fff") == -0x1fff); 52 } 53 54 @Test(expected=NumberFormatException.class) 55 public void FaileHexTest1() { 56 LiteralTools.parseShort("-0x8001"); 57 } 58 59 @Test(expected=NumberFormatException.class) 60 public void FailHexTest2() { 61 LiteralTools.parseShort("-0xFFFF"); 62 } 63 64 @Test(expected=NumberFormatException.class) 65 public void FailHexTest3() { 66 LiteralTools.parseShort("0x100000"); 67 } 68 69 70 71 @Test 72 public void SuccessDecTests() { 73 Assert.assertTrue(LiteralTools.parseShort("0") == 0); 74 Assert.assertTrue(LiteralTools.parseShort("1") == 1); 75 Assert.assertTrue(LiteralTools.parseShort("12345") == 12345); 76 Assert.assertTrue(LiteralTools.parseShort("32767") == 32767); 77 Assert.assertTrue(LiteralTools.parseShort("32768") == Short.MIN_VALUE); 78 Assert.assertTrue(LiteralTools.parseShort("65535") == -1); 79 80 81 Assert.assertTrue(LiteralTools.parseShort("-0") == 0); 82 Assert.assertTrue(LiteralTools.parseShort("-1") == -1); 83 Assert.assertTrue(LiteralTools.parseShort("-12345") == -12345); 84 Assert.assertTrue(LiteralTools.parseShort("-32767") == -32767); 85 Assert.assertTrue(LiteralTools.parseShort("-32768") == Short.MIN_VALUE); 86 } 87 88 @Test(expected=NumberFormatException.class) 89 public void FaileDecTest1() { 90 LiteralTools.parseShort("-32769"); 91 } 92 93 @Test(expected=NumberFormatException.class) 94 public void FailDecTest2() { 95 LiteralTools.parseShort("-65535"); 96 } 97 98 @Test(expected=NumberFormatException.class) 99 public void FailDecTest3() { 100 LiteralTools.parseShort("65536"); 101 } 102 103 @Test(expected=NumberFormatException.class) 104 public void FailDecTest4() { 105 LiteralTools.parseShort("65600"); 106 } 107 108 109 @Test 110 public void SuccessOctTests() { 111 Assert.assertTrue(LiteralTools.parseShort("00") == 00); 112 Assert.assertTrue(LiteralTools.parseShort("01") == 01); 113 Assert.assertTrue(LiteralTools.parseShort("012345") == 012345); 114 Assert.assertTrue(LiteralTools.parseShort("077777") == Short.MAX_VALUE); 115 Assert.assertTrue(LiteralTools.parseShort("0100000") == Short.MIN_VALUE); 116 Assert.assertTrue(LiteralTools.parseShort("0177777") == -1); 117 118 119 Assert.assertTrue(LiteralTools.parseShort("-00") == 0); 120 Assert.assertTrue(LiteralTools.parseShort("-01") == -1); 121 Assert.assertTrue(LiteralTools.parseShort("-012345") == -012345); 122 Assert.assertTrue(LiteralTools.parseShort("-077777") == -077777); 123 Assert.assertTrue(LiteralTools.parseShort("-0100000") == Short.MIN_VALUE); 124 } 125 126 @Test(expected=NumberFormatException.class) 127 public void FaileOctTest1() { 128 LiteralTools.parseShort("-0100001"); 129 } 130 131 @Test(expected=NumberFormatException.class) 132 public void FailOctTest2() { 133 LiteralTools.parseShort("-0177777"); 134 } 135 136 @Test(expected=NumberFormatException.class) 137 public void FailOctTest3() { 138 LiteralTools.parseShort("0200000"); 139 } 140} 141