0001: /* Licensed to the Apache Software Foundation (ASF) under one or more
0002: * contributor license agreements. See the NOTICE file distributed with
0003: * this work for additional information regarding copyright ownership.
0004: * The ASF licenses this file to You under the Apache License, Version 2.0
0005: * (the "License"); you may not use this file except in compliance with
0006: * the License. You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016:
0017: package org.apache.harmony.luni.tests.java.lang;
0018:
0019: import java.util.Properties;
0020:
0021: import junit.framework.TestCase;
0022:
0023: public class LongTest extends TestCase {
0024: private Properties orgProps;
0025:
0026: @Override
0027: protected void setUp() {
0028: orgProps = System.getProperties();
0029: }
0030:
0031: @Override
0032: protected void tearDown() {
0033: System.setProperties(orgProps);
0034: }
0035:
0036: /**
0037: * @tests java.lang.Long#byteValue()
0038: */
0039: public void test_byteValue() {
0040: // Test for method byte java.lang.Long.byteValue()
0041: Long l = new Long(127);
0042: assertEquals("Returned incorrect byte value", 127, l
0043: .byteValue());
0044: assertEquals("Returned incorrect byte value", -1, new Long(
0045: Long.MAX_VALUE).byteValue());
0046: }
0047:
0048: /**
0049: * @tests java.lang.Long#compareTo(java.lang.Long)
0050: */
0051: public void test_compareToLjava_lang_Long() {
0052: // Test for method int java.lang.Long.compareTo(java.lang.Long)
0053: assertTrue("-2 compared to 1 gave non-negative answer",
0054: new Long(-2L).compareTo(new Long(1L)) < 0);
0055: assertEquals("-2 compared to -2 gave non-zero answer", 0,
0056: new Long(-2L).compareTo(new Long(-2L)));
0057: assertTrue("3 compared to 2 gave non-positive answer",
0058: new Long(3L).compareTo(new Long(2L)) > 0);
0059:
0060: try {
0061: new Long(0).compareTo(null);
0062: fail("No NPE");
0063: } catch (NullPointerException e) {
0064: }
0065: }
0066:
0067: /**
0068: * @tests java.lang.Long#decode(java.lang.String)
0069: */
0070: public void test_decodeLjava_lang_String2() {
0071: // Test for method java.lang.Long
0072: // java.lang.Long.decode(java.lang.String)
0073: assertEquals("Returned incorrect value for hex string", 255L,
0074: Long.decode("0xFF").longValue());
0075: assertEquals("Returned incorrect value for dec string",
0076: -89000L, Long.decode("-89000").longValue());
0077: assertEquals("Returned incorrect value for 0 decimal", 0, Long
0078: .decode("0").longValue());
0079: assertEquals("Returned incorrect value for 0 hex", 0, Long
0080: .decode("0x0").longValue());
0081: assertTrue(
0082: "Returned incorrect value for most negative value decimal",
0083: Long.decode("-9223372036854775808").longValue() == 0x8000000000000000L);
0084: assertTrue(
0085: "Returned incorrect value for most negative value hex",
0086: Long.decode("-0x8000000000000000").longValue() == 0x8000000000000000L);
0087: assertTrue(
0088: "Returned incorrect value for most positive value decimal",
0089: Long.decode("9223372036854775807").longValue() == 0x7fffffffffffffffL);
0090: assertTrue(
0091: "Returned incorrect value for most positive value hex",
0092: Long.decode("0x7fffffffffffffff").longValue() == 0x7fffffffffffffffL);
0093: assertTrue("Failed for 07654321765432", Long.decode(
0094: "07654321765432").longValue() == 07654321765432l);
0095:
0096: boolean exception = false;
0097: try {
0098: Long
0099: .decode("999999999999999999999999999999999999999999999999999999");
0100: } catch (NumberFormatException e) {
0101: // Correct
0102: exception = true;
0103: }
0104: assertTrue("Failed to throw exception for value > ilong",
0105: exception);
0106:
0107: exception = false;
0108: try {
0109: Long.decode("9223372036854775808");
0110: } catch (NumberFormatException e) {
0111: // Correct
0112: exception = true;
0113: }
0114: assertTrue("Failed to throw exception for MAX_VALUE + 1",
0115: exception);
0116:
0117: exception = false;
0118: try {
0119: Long.decode("-9223372036854775809");
0120: } catch (NumberFormatException e) {
0121: // Correct
0122: exception = true;
0123: }
0124: assertTrue("Failed to throw exception for MIN_VALUE - 1",
0125: exception);
0126:
0127: exception = false;
0128: try {
0129: Long.decode("0x8000000000000000");
0130: } catch (NumberFormatException e) {
0131: // Correct
0132: exception = true;
0133: }
0134: assertTrue("Failed to throw exception for hex MAX_VALUE + 1",
0135: exception);
0136:
0137: exception = false;
0138: try {
0139: Long.decode("-0x8000000000000001");
0140: } catch (NumberFormatException e) {
0141: // Correct
0142: exception = true;
0143: }
0144: assertTrue("Failed to throw exception for hex MIN_VALUE - 1",
0145: exception);
0146:
0147: exception = false;
0148: try {
0149: Long.decode("42325917317067571199");
0150: } catch (NumberFormatException e) {
0151: // Correct
0152: exception = true;
0153: }
0154: assertTrue(
0155: "Failed to throw exception for 42325917317067571199",
0156: exception);
0157: }
0158:
0159: /**
0160: * @tests java.lang.Long#getLong(java.lang.String)
0161: */
0162: public void test_getLongLjava_lang_String() {
0163: // Test for method java.lang.Long
0164: // java.lang.Long.getLong(java.lang.String)
0165: Properties tProps = new Properties();
0166: tProps.put("testLong", "99");
0167: System.setProperties(tProps);
0168: assertTrue("returned incorrect Long", Long.getLong("testLong")
0169: .equals(new Long(99)));
0170: assertNull("returned incorrect default Long", Long
0171: .getLong("ff"));
0172: }
0173:
0174: /**
0175: * @tests java.lang.Long#getLong(java.lang.String, long)
0176: */
0177: public void test_getLongLjava_lang_StringJ() {
0178: // Test for method java.lang.Long
0179: // java.lang.Long.getLong(java.lang.String, long)
0180: Properties tProps = new Properties();
0181: tProps.put("testLong", "99");
0182: System.setProperties(tProps);
0183: assertTrue("returned incorrect Long", Long.getLong("testLong",
0184: 4L).equals(new Long(99)));
0185: assertTrue("returned incorrect default Long", Long.getLong(
0186: "ff", 4L).equals(new Long(4)));
0187: }
0188:
0189: /**
0190: * @tests java.lang.Long#getLong(java.lang.String, java.lang.Long)
0191: */
0192: public void test_getLongLjava_lang_StringLjava_lang_Long() {
0193: // Test for method java.lang.Long
0194: // java.lang.Long.getLong(java.lang.String, java.lang.Long)
0195: Properties tProps = new Properties();
0196: tProps.put("testLong", "99");
0197: System.setProperties(tProps);
0198: assertTrue("returned incorrect Long", Long.getLong("testLong",
0199: new Long(4)).equals(new Long(99)));
0200: assertTrue("returned incorrect default Long", Long.getLong(
0201: "ff", new Long(4)).equals(new Long(4)));
0202: }
0203:
0204: /**
0205: * @tests java.lang.Long#parseLong(java.lang.String)
0206: */
0207: public void test_parseLongLjava_lang_String2() {
0208: // Test for method long java.lang.Long.parseLong(java.lang.String)
0209:
0210: long l = Long.parseLong("89000000005");
0211: assertEquals("Parsed to incorrect long value", 89000000005L, l);
0212: assertEquals("Returned incorrect value for 0", 0, Long
0213: .parseLong("0"));
0214: assertTrue(
0215: "Returned incorrect value for most negative value",
0216: Long.parseLong("-9223372036854775808") == 0x8000000000000000L);
0217: assertTrue(
0218: "Returned incorrect value for most positive value",
0219: Long.parseLong("9223372036854775807") == 0x7fffffffffffffffL);
0220:
0221: boolean exception = false;
0222: try {
0223: Long.parseLong("9223372036854775808");
0224: } catch (NumberFormatException e) {
0225: // Correct
0226: exception = true;
0227: }
0228: assertTrue("Failed to throw exception for MAX_VALUE + 1",
0229: exception);
0230:
0231: exception = false;
0232: try {
0233: Long.parseLong("-9223372036854775809");
0234: } catch (NumberFormatException e) {
0235: // Correct
0236: exception = true;
0237: }
0238: assertTrue("Failed to throw exception for MIN_VALUE - 1",
0239: exception);
0240: }
0241:
0242: /**
0243: * @tests java.lang.Long#parseLong(java.lang.String, int)
0244: */
0245: public void test_parseLongLjava_lang_StringI() {
0246: // Test for method long java.lang.Long.parseLong(java.lang.String, int)
0247: assertEquals("Returned incorrect value", 100000000L, Long
0248: .parseLong("100000000", 10));
0249: assertEquals("Returned incorrect value from hex string",
0250: 68719476735L, Long.parseLong("FFFFFFFFF", 16));
0251: assertTrue("Returned incorrect value from octal string: "
0252: + Long.parseLong("77777777777"), Long.parseLong(
0253: "77777777777", 8) == 8589934591L);
0254: assertEquals("Returned incorrect value for 0 hex", 0, Long
0255: .parseLong("0", 16));
0256: assertTrue(
0257: "Returned incorrect value for most negative value hex",
0258: Long.parseLong("-8000000000000000", 16) == 0x8000000000000000L);
0259: assertTrue(
0260: "Returned incorrect value for most positive value hex",
0261: Long.parseLong("7fffffffffffffff", 16) == 0x7fffffffffffffffL);
0262: assertEquals("Returned incorrect value for 0 decimal", 0, Long
0263: .parseLong("0", 10));
0264: assertTrue(
0265: "Returned incorrect value for most negative value decimal",
0266: Long.parseLong("-9223372036854775808", 10) == 0x8000000000000000L);
0267: assertTrue(
0268: "Returned incorrect value for most positive value decimal",
0269: Long.parseLong("9223372036854775807", 10) == 0x7fffffffffffffffL);
0270:
0271: boolean exception = false;
0272: try {
0273: Long.parseLong("999999999999", 8);
0274: } catch (NumberFormatException e) {
0275: // correct
0276: exception = true;
0277: }
0278: assertTrue(
0279: "Failed to throw exception when passed invalid string",
0280: exception);
0281:
0282: exception = false;
0283: try {
0284: Long.parseLong("9223372036854775808", 10);
0285: } catch (NumberFormatException e) {
0286: // Correct
0287: exception = true;
0288: }
0289: assertTrue("Failed to throw exception for MAX_VALUE + 1",
0290: exception);
0291:
0292: exception = false;
0293: try {
0294: Long.parseLong("-9223372036854775809", 10);
0295: } catch (NumberFormatException e) {
0296: // Correct
0297: exception = true;
0298: }
0299: assertTrue("Failed to throw exception for MIN_VALUE - 1",
0300: exception);
0301:
0302: exception = false;
0303: try {
0304: Long.parseLong("8000000000000000", 16);
0305: } catch (NumberFormatException e) {
0306: // Correct
0307: exception = true;
0308: }
0309: assertTrue("Failed to throw exception for hex MAX_VALUE + 1",
0310: exception);
0311:
0312: exception = false;
0313: try {
0314: Long.parseLong("-8000000000000001", 16);
0315: } catch (NumberFormatException e) {
0316: // Correct
0317: exception = true;
0318: }
0319: assertTrue("Failed to throw exception for hex MIN_VALUE + 1",
0320: exception);
0321:
0322: exception = false;
0323: try {
0324: Long.parseLong("42325917317067571199", 10);
0325: } catch (NumberFormatException e) {
0326: // Correct
0327: exception = true;
0328: }
0329: assertTrue(
0330: "Failed to throw exception for 42325917317067571199",
0331: exception);
0332: }
0333:
0334: /**
0335: * @tests java.lang.Long#toBinaryString(long)
0336: */
0337: public void test_toBinaryStringJ() {
0338: // Test for method java.lang.String java.lang.Long.toBinaryString(long)
0339: assertEquals("Incorrect binary string returned",
0340: "11011001010010010000", Long.toBinaryString(890000L));
0341: assertEquals(
0342: "Incorrect binary string returned",
0343:
0344: "1000000000000000000000000000000000000000000000000000000000000000",
0345: Long.toBinaryString(Long.MIN_VALUE));
0346: assertEquals(
0347: "Incorrect binary string returned",
0348:
0349: "111111111111111111111111111111111111111111111111111111111111111",
0350: Long.toBinaryString(Long.MAX_VALUE));
0351: }
0352:
0353: /**
0354: * @tests java.lang.Long#toHexString(long)
0355: */
0356: public void test_toHexStringJ() {
0357: // Test for method java.lang.String java.lang.Long.toHexString(long)
0358: assertEquals("Incorrect hex string returned", "54e0845", Long
0359: .toHexString(89000005L));
0360: assertEquals("Incorrect hex string returned",
0361: "8000000000000000", Long.toHexString(Long.MIN_VALUE));
0362: assertEquals("Incorrect hex string returned",
0363: "7fffffffffffffff", Long.toHexString(Long.MAX_VALUE));
0364: }
0365:
0366: /**
0367: * @tests java.lang.Long#toOctalString(long)
0368: */
0369: public void test_toOctalStringJ() {
0370: // Test for method java.lang.String java.lang.Long.toOctalString(long)
0371: assertEquals("Returned incorrect oct string", "77777777777",
0372: Long.toOctalString(8589934591L));
0373: assertEquals("Returned incorrect oct string",
0374: "1000000000000000000000", Long
0375: .toOctalString(Long.MIN_VALUE));
0376: assertEquals("Returned incorrect oct string",
0377: "777777777777777777777", Long
0378: .toOctalString(Long.MAX_VALUE));
0379: }
0380:
0381: /**
0382: * @tests java.lang.Long#toString()
0383: */
0384: public void test_toString2() {
0385: // Test for method java.lang.String java.lang.Long.toString()
0386: Long l = new Long(89000000005L);
0387: assertEquals("Returned incorrect String", "89000000005", l
0388: .toString());
0389: assertEquals("Returned incorrect String",
0390: "-9223372036854775808", new Long(Long.MIN_VALUE)
0391: .toString());
0392: assertEquals("Returned incorrect String",
0393: "9223372036854775807", new Long(Long.MAX_VALUE)
0394: .toString());
0395: }
0396:
0397: /**
0398: * @tests java.lang.Long#toString(long)
0399: */
0400: public void test_toStringJ2() {
0401: // Test for method java.lang.String java.lang.Long.toString(long)
0402:
0403: assertEquals("Returned incorrect String", "89000000005", Long
0404: .toString(89000000005L));
0405: assertEquals("Returned incorrect String",
0406: "-9223372036854775808", Long.toString(Long.MIN_VALUE));
0407: assertEquals("Returned incorrect String",
0408: "9223372036854775807", Long.toString(Long.MAX_VALUE));
0409: }
0410:
0411: /**
0412: * @tests java.lang.Long#toString(long, int)
0413: */
0414: public void test_toStringJI() {
0415: // Test for method java.lang.String java.lang.Long.toString(long, int)
0416: assertEquals("Returned incorrect dec string", "100000000", Long
0417: .toString(100000000L, 10));
0418: assertEquals("Returned incorrect hex string", "fffffffff", Long
0419: .toString(68719476735L, 16));
0420: assertEquals("Returned incorrect oct string", "77777777777",
0421: Long.toString(8589934591L, 8));
0422: assertEquals("Returned incorrect bin string",
0423: "1111111111111111111111111111111111111111111", Long
0424: .toString(8796093022207L, 2));
0425: assertEquals("Returned incorrect min string",
0426: "-9223372036854775808", Long.toString(
0427: 0x8000000000000000L, 10));
0428: assertEquals("Returned incorrect max string",
0429: "9223372036854775807", Long.toString(
0430: 0x7fffffffffffffffL, 10));
0431: assertEquals("Returned incorrect min string",
0432: "-8000000000000000", Long.toString(0x8000000000000000L,
0433: 16));
0434: assertEquals("Returned incorrect max string",
0435: "7fffffffffffffff", Long.toString(0x7fffffffffffffffL,
0436: 16));
0437: }
0438:
0439: /**
0440: * @tests java.lang.Long#valueOf(java.lang.String)
0441: */
0442: public void test_valueOfLjava_lang_String2() {
0443: // Test for method java.lang.Long
0444: // java.lang.Long.valueOf(java.lang.String)
0445: assertEquals("Returned incorrect value", 100000000L, Long
0446: .valueOf("100000000").longValue());
0447: assertTrue("Returned incorrect value", Long.valueOf(
0448: "9223372036854775807").longValue() == Long.MAX_VALUE);
0449: assertTrue("Returned incorrect value", Long.valueOf(
0450: "-9223372036854775808").longValue() == Long.MIN_VALUE);
0451:
0452: boolean exception = false;
0453: try {
0454: Long
0455: .valueOf("999999999999999999999999999999999999999999999999999999999999");
0456: } catch (NumberFormatException e) {
0457: // correct
0458: exception = true;
0459: }
0460: assertTrue(
0461: "Failed to throw exception when passed invalid string",
0462: exception);
0463:
0464: exception = false;
0465: try {
0466: Long.valueOf("9223372036854775808");
0467: } catch (NumberFormatException e) {
0468: // correct
0469: exception = true;
0470: }
0471: assertTrue(
0472: "Failed to throw exception when passed invalid string",
0473: exception);
0474:
0475: exception = false;
0476: try {
0477: Long.valueOf("-9223372036854775809");
0478: } catch (NumberFormatException e) {
0479: // correct
0480: exception = true;
0481: }
0482: assertTrue(
0483: "Failed to throw exception when passed invalid string",
0484: exception);
0485: }
0486:
0487: /**
0488: * @tests java.lang.Long#valueOf(java.lang.String, int)
0489: */
0490: public void test_valueOfLjava_lang_StringI() {
0491: // Test for method java.lang.Long
0492: // java.lang.Long.valueOf(java.lang.String, int)
0493: assertEquals("Returned incorrect value", 100000000L, Long
0494: .valueOf("100000000", 10).longValue());
0495: assertEquals("Returned incorrect value from hex string",
0496: 68719476735L, Long.valueOf("FFFFFFFFF", 16).longValue());
0497: assertTrue("Returned incorrect value from octal string: "
0498: + Long.valueOf("77777777777", 8).toString(), Long
0499: .valueOf("77777777777", 8).longValue() == 8589934591L);
0500: assertTrue(
0501: "Returned incorrect value",
0502: Long.valueOf("9223372036854775807", 10).longValue() == Long.MAX_VALUE);
0503: assertTrue(
0504: "Returned incorrect value",
0505: Long.valueOf("-9223372036854775808", 10).longValue() == Long.MIN_VALUE);
0506: assertTrue("Returned incorrect value", Long.valueOf(
0507: "7fffffffffffffff", 16).longValue() == Long.MAX_VALUE);
0508: assertTrue("Returned incorrect value", Long.valueOf(
0509: "-8000000000000000", 16).longValue() == Long.MIN_VALUE);
0510:
0511: boolean exception = false;
0512: try {
0513: Long.valueOf("999999999999", 8);
0514: } catch (NumberFormatException e) {
0515: // correct
0516: exception = true;
0517: }
0518: assertTrue(
0519: "Failed to throw exception when passed invalid string",
0520: exception);
0521:
0522: exception = false;
0523: try {
0524: Long.valueOf("9223372036854775808", 10);
0525: } catch (NumberFormatException e) {
0526: // correct
0527: exception = true;
0528: }
0529: assertTrue(
0530: "Failed to throw exception when passed invalid string",
0531: exception);
0532:
0533: exception = false;
0534: try {
0535: Long.valueOf("-9223372036854775809", 10);
0536: } catch (NumberFormatException e) {
0537: // correct
0538: exception = true;
0539: }
0540: assertTrue(
0541: "Failed to throw exception when passed invalid string",
0542: exception);
0543: }
0544:
0545: /**
0546: * @tests java.lang.Long#valueOf(long)
0547: */
0548: public void test_valueOfJ() {
0549: assertEquals(new Long(Long.MIN_VALUE), Long
0550: .valueOf(Long.MIN_VALUE));
0551: assertEquals(new Long(Long.MAX_VALUE), Long
0552: .valueOf(Long.MAX_VALUE));
0553: assertEquals(new Long(0), Long.valueOf(0));
0554:
0555: long lng = -128;
0556: while (lng < 128) {
0557: assertEquals(new Long(lng), Long.valueOf(lng));
0558: assertSame(Long.valueOf(lng), Long.valueOf(lng));
0559: lng++;
0560: }
0561: }
0562:
0563: /**
0564: * @tests java.lang.Long#hashCode()
0565: */
0566: public void test_hashCode() {
0567: assertEquals((int) (1L ^ (1L >>> 32)), new Long(1).hashCode());
0568: assertEquals((int) (2L ^ (2L >>> 32)), new Long(2).hashCode());
0569: assertEquals((int) (0L ^ (0L >>> 32)), new Long(0).hashCode());
0570: assertEquals((int) (-1L ^ (-1L >>> 32)), new Long(-1)
0571: .hashCode());
0572: }
0573:
0574: /**
0575: * @tests java.lang.Long#Long(String)
0576: */
0577: public void test_ConstructorLjava_lang_String() {
0578: assertEquals(new Long(0), new Long("0"));
0579: assertEquals(new Long(1), new Long("1"));
0580: assertEquals(new Long(-1), new Long("-1"));
0581:
0582: try {
0583: new Long("0x1");
0584: fail("Expected NumberFormatException with hex string.");
0585: } catch (NumberFormatException e) {
0586: }
0587:
0588: try {
0589: new Long("9.2");
0590: fail("Expected NumberFormatException with floating point string.");
0591: } catch (NumberFormatException e) {
0592: }
0593:
0594: try {
0595: new Long("");
0596: fail("Expected NumberFormatException with empty string.");
0597: } catch (NumberFormatException e) {
0598: }
0599:
0600: try {
0601: new Long(null);
0602: fail("Expected NumberFormatException with null string.");
0603: } catch (NumberFormatException e) {
0604: }
0605: }
0606:
0607: /**
0608: * @tests java.lang.Long#Long
0609: */
0610: public void test_ConstructorJ() {
0611: assertEquals(1, new Long(1).intValue());
0612: assertEquals(2, new Long(2).intValue());
0613: assertEquals(0, new Long(0).intValue());
0614: assertEquals(-1, new Long(-1).intValue());
0615: }
0616:
0617: /**
0618: * @tests java.lang.Long#byteValue()
0619: */
0620: public void test_booleanValue() {
0621: assertEquals(1, new Long(1).byteValue());
0622: assertEquals(2, new Long(2).byteValue());
0623: assertEquals(0, new Long(0).byteValue());
0624: assertEquals(-1, new Long(-1).byteValue());
0625: }
0626:
0627: /**
0628: * @tests java.lang.Long#equals(Object)
0629: */
0630: public void test_equalsLjava_lang_Object() {
0631: assertEquals(new Long(0), Long.valueOf(0));
0632: assertEquals(new Long(1), Long.valueOf(1));
0633: assertEquals(new Long(-1), Long.valueOf(-1));
0634:
0635: Long fixture = new Long(25);
0636: assertEquals(fixture, fixture);
0637: assertFalse(fixture.equals(null));
0638: assertFalse(fixture.equals("Not a Long"));
0639: }
0640:
0641: /**
0642: * @tests java.lang.Long#toString()
0643: */
0644: public void test_toString() {
0645: assertEquals("-1", new Long(-1).toString());
0646: assertEquals("0", new Long(0).toString());
0647: assertEquals("1", new Long(1).toString());
0648: assertEquals("-1", new Long(0xFFFFFFFF).toString());
0649: }
0650:
0651: /**
0652: * @tests java.lang.Long#toString
0653: */
0654: public void test_toStringJ() {
0655: assertEquals("-1", Long.toString(-1));
0656: assertEquals("0", Long.toString(0));
0657: assertEquals("1", Long.toString(1));
0658: assertEquals("-1", Long.toString(0xFFFFFFFF));
0659: }
0660:
0661: /**
0662: * @tests java.lang.Long#valueOf(String)
0663: */
0664: public void test_valueOfLjava_lang_String() {
0665: assertEquals(new Long(0), Long.valueOf("0"));
0666: assertEquals(new Long(1), Long.valueOf("1"));
0667: assertEquals(new Long(-1), Long.valueOf("-1"));
0668:
0669: try {
0670: Long.valueOf("0x1");
0671: fail("Expected NumberFormatException with hex string.");
0672: } catch (NumberFormatException e) {
0673: }
0674:
0675: try {
0676: Long.valueOf("9.2");
0677: fail("Expected NumberFormatException with floating point string.");
0678: } catch (NumberFormatException e) {
0679: }
0680:
0681: try {
0682: Long.valueOf("");
0683: fail("Expected NumberFormatException with empty string.");
0684: } catch (NumberFormatException e) {
0685: }
0686:
0687: try {
0688: Long.valueOf(null);
0689: fail("Expected NumberFormatException with null string.");
0690: } catch (NumberFormatException e) {
0691: }
0692: }
0693:
0694: /**
0695: * @tests java.lang.Long#valueOf(String,long)
0696: */
0697: public void test_valueOfLjava_lang_StringJ() {
0698: assertEquals(new Long(0), Long.valueOf("0", 10));
0699: assertEquals(new Long(1), Long.valueOf("1", 10));
0700: assertEquals(new Long(-1), Long.valueOf("-1", 10));
0701:
0702: //must be consistent with Character.digit()
0703: assertEquals(Character.digit('1', 2), Long.valueOf("1", 2)
0704: .byteValue());
0705: assertEquals(Character.digit('F', 16), Long.valueOf("F", 16)
0706: .byteValue());
0707:
0708: try {
0709: Long.valueOf("0x1", 10);
0710: fail("Expected NumberFormatException with hex string.");
0711: } catch (NumberFormatException e) {
0712: }
0713:
0714: try {
0715: Long.valueOf("9.2", 10);
0716: fail("Expected NumberFormatException with floating point string.");
0717: } catch (NumberFormatException e) {
0718: }
0719:
0720: try {
0721: Long.valueOf("", 10);
0722: fail("Expected NumberFormatException with empty string.");
0723: } catch (NumberFormatException e) {
0724: }
0725:
0726: try {
0727: Long.valueOf(null, 10);
0728: fail("Expected NumberFormatException with null string.");
0729: } catch (NumberFormatException e) {
0730: }
0731: }
0732:
0733: /**
0734: * @tests java.lang.Long#parseLong(String)
0735: */
0736: public void test_parseLongLjava_lang_String() {
0737: assertEquals(0, Long.parseLong("0"));
0738: assertEquals(1, Long.parseLong("1"));
0739: assertEquals(-1, Long.parseLong("-1"));
0740:
0741: try {
0742: Long.parseLong("0x1");
0743: fail("Expected NumberFormatException with hex string.");
0744: } catch (NumberFormatException e) {
0745: }
0746:
0747: try {
0748: Long.parseLong("9.2");
0749: fail("Expected NumberFormatException with floating point string.");
0750: } catch (NumberFormatException e) {
0751: }
0752:
0753: try {
0754: Long.parseLong("");
0755: fail("Expected NumberFormatException with empty string.");
0756: } catch (NumberFormatException e) {
0757: }
0758:
0759: try {
0760: Long.parseLong(null);
0761: fail("Expected NumberFormatException with null string.");
0762: } catch (NumberFormatException e) {
0763: }
0764: }
0765:
0766: /**
0767: * @tests java.lang.Long#parseLong(String,long)
0768: */
0769: public void test_parseLongLjava_lang_StringJ() {
0770: assertEquals(0, Long.parseLong("0", 10));
0771: assertEquals(1, Long.parseLong("1", 10));
0772: assertEquals(-1, Long.parseLong("-1", 10));
0773:
0774: //must be consistent with Character.digit()
0775: assertEquals(Character.digit('1', 2), Long.parseLong("1", 2));
0776: assertEquals(Character.digit('F', 16), Long.parseLong("F", 16));
0777:
0778: try {
0779: Long.parseLong("0x1", 10);
0780: fail("Expected NumberFormatException with hex string.");
0781: } catch (NumberFormatException e) {
0782: }
0783:
0784: try {
0785: Long.parseLong("9.2", 10);
0786: fail("Expected NumberFormatException with floating point string.");
0787: } catch (NumberFormatException e) {
0788: }
0789:
0790: try {
0791: Long.parseLong("", 10);
0792: fail("Expected NumberFormatException with empty string.");
0793: } catch (NumberFormatException e) {
0794: }
0795:
0796: try {
0797: Long.parseLong(null, 10);
0798: fail("Expected NumberFormatException with null string.");
0799: } catch (NumberFormatException e) {
0800: }
0801: }
0802:
0803: /**
0804: * @tests java.lang.Long#decode(String)
0805: */
0806: public void test_decodeLjava_lang_String() {
0807: assertEquals(new Long(0), Long.decode("0"));
0808: assertEquals(new Long(1), Long.decode("1"));
0809: assertEquals(new Long(-1), Long.decode("-1"));
0810: assertEquals(new Long(0xF), Long.decode("0xF"));
0811: assertEquals(new Long(0xF), Long.decode("#F"));
0812: assertEquals(new Long(0xF), Long.decode("0XF"));
0813: assertEquals(new Long(07), Long.decode("07"));
0814:
0815: try {
0816: Long.decode("9.2");
0817: fail("Expected NumberFormatException with floating point string.");
0818: } catch (NumberFormatException e) {
0819: }
0820:
0821: try {
0822: Long.decode("");
0823: fail("Expected NumberFormatException with empty string.");
0824: } catch (NumberFormatException e) {
0825: }
0826:
0827: try {
0828: Long.decode(null);
0829: //undocumented NPE, but seems consistent across JREs
0830: fail("Expected NullPointerException with null string.");
0831: } catch (NullPointerException e) {
0832: }
0833: }
0834:
0835: /**
0836: * @tests java.lang.Long#doubleValue()
0837: */
0838: public void test_doubleValue() {
0839: assertEquals(-1D, new Long(-1).doubleValue(), 0D);
0840: assertEquals(0D, new Long(0).doubleValue(), 0D);
0841: assertEquals(1D, new Long(1).doubleValue(), 0D);
0842: }
0843:
0844: /**
0845: * @tests java.lang.Long#floatValue()
0846: */
0847: public void test_floatValue() {
0848: assertEquals(-1F, new Long(-1).floatValue(), 0F);
0849: assertEquals(0F, new Long(0).floatValue(), 0F);
0850: assertEquals(1F, new Long(1).floatValue(), 0F);
0851: }
0852:
0853: /**
0854: * @tests java.lang.Long#intValue()
0855: */
0856: public void test_intValue() {
0857: assertEquals(-1, new Long(-1).intValue());
0858: assertEquals(0, new Long(0).intValue());
0859: assertEquals(1, new Long(1).intValue());
0860: }
0861:
0862: /**
0863: * @tests java.lang.Long#longValue()
0864: */
0865: public void test_longValue() {
0866: assertEquals(-1L, new Long(-1).longValue());
0867: assertEquals(0L, new Long(0).longValue());
0868: assertEquals(1L, new Long(1).longValue());
0869: }
0870:
0871: /**
0872: * @tests java.lang.Long#shortValue()
0873: */
0874: public void test_shortValue() {
0875: assertEquals(-1, new Long(-1).shortValue());
0876: assertEquals(0, new Long(0).shortValue());
0877: assertEquals(1, new Long(1).shortValue());
0878: }
0879:
0880: /**
0881: * @tests java.lang.Long#highestOneBit(long)
0882: */
0883: public void test_highestOneBitJ() {
0884: assertEquals(0x08, Long.highestOneBit(0x0A));
0885: assertEquals(0x08, Long.highestOneBit(0x0B));
0886: assertEquals(0x08, Long.highestOneBit(0x0C));
0887: assertEquals(0x08, Long.highestOneBit(0x0F));
0888: assertEquals(0x80, Long.highestOneBit(0xFF));
0889:
0890: assertEquals(0x080000, Long.highestOneBit(0x0F1234));
0891: assertEquals(0x800000, Long.highestOneBit(0xFF9977));
0892:
0893: assertEquals(0x8000000000000000L, Long
0894: .highestOneBit(0xFFFFFFFFFFFFFFFFL));
0895:
0896: assertEquals(0, Long.highestOneBit(0));
0897: assertEquals(1, Long.highestOneBit(1));
0898: assertEquals(0x8000000000000000L, Long.highestOneBit(-1));
0899: }
0900:
0901: /**
0902: * @tests java.lang.Long#lowestOneBit(long)
0903: */
0904: public void test_lowestOneBitJ() {
0905: assertEquals(0x10, Long.lowestOneBit(0xF0));
0906:
0907: assertEquals(0x10, Long.lowestOneBit(0x90));
0908: assertEquals(0x10, Long.lowestOneBit(0xD0));
0909:
0910: assertEquals(0x10, Long.lowestOneBit(0x123490));
0911: assertEquals(0x10, Long.lowestOneBit(0x1234D0));
0912:
0913: assertEquals(0x100000, Long.lowestOneBit(0x900000));
0914: assertEquals(0x100000, Long.lowestOneBit(0xD00000));
0915:
0916: assertEquals(0x40, Long.lowestOneBit(0x40));
0917: assertEquals(0x40, Long.lowestOneBit(0xC0));
0918:
0919: assertEquals(0x4000, Long.lowestOneBit(0x4000));
0920: assertEquals(0x4000, Long.lowestOneBit(0xC000));
0921:
0922: assertEquals(0x4000, Long.lowestOneBit(0x99994000));
0923: assertEquals(0x4000, Long.lowestOneBit(0x9999C000));
0924:
0925: assertEquals(0, Long.lowestOneBit(0));
0926: assertEquals(1, Long.lowestOneBit(1));
0927: assertEquals(1, Long.lowestOneBit(-1));
0928: }
0929:
0930: /**
0931: * @tests java.lang.Long#numberOfLeadingZeros(long)
0932: */
0933: public void test_numberOfLeadingZerosJ() {
0934: assertEquals(64, Long.numberOfLeadingZeros(0x0L));
0935: assertEquals(63, Long.numberOfLeadingZeros(0x1));
0936: assertEquals(62, Long.numberOfLeadingZeros(0x2));
0937: assertEquals(62, Long.numberOfLeadingZeros(0x3));
0938: assertEquals(61, Long.numberOfLeadingZeros(0x4));
0939: assertEquals(61, Long.numberOfLeadingZeros(0x5));
0940: assertEquals(61, Long.numberOfLeadingZeros(0x6));
0941: assertEquals(61, Long.numberOfLeadingZeros(0x7));
0942: assertEquals(60, Long.numberOfLeadingZeros(0x8));
0943: assertEquals(60, Long.numberOfLeadingZeros(0x9));
0944: assertEquals(60, Long.numberOfLeadingZeros(0xA));
0945: assertEquals(60, Long.numberOfLeadingZeros(0xB));
0946: assertEquals(60, Long.numberOfLeadingZeros(0xC));
0947: assertEquals(60, Long.numberOfLeadingZeros(0xD));
0948: assertEquals(60, Long.numberOfLeadingZeros(0xE));
0949: assertEquals(60, Long.numberOfLeadingZeros(0xF));
0950: assertEquals(59, Long.numberOfLeadingZeros(0x10));
0951: assertEquals(56, Long.numberOfLeadingZeros(0x80));
0952: assertEquals(56, Long.numberOfLeadingZeros(0xF0));
0953: assertEquals(55, Long.numberOfLeadingZeros(0x100));
0954: assertEquals(52, Long.numberOfLeadingZeros(0x800));
0955: assertEquals(52, Long.numberOfLeadingZeros(0xF00));
0956: assertEquals(51, Long.numberOfLeadingZeros(0x1000));
0957: assertEquals(48, Long.numberOfLeadingZeros(0x8000));
0958: assertEquals(48, Long.numberOfLeadingZeros(0xF000));
0959: assertEquals(47, Long.numberOfLeadingZeros(0x10000));
0960: assertEquals(44, Long.numberOfLeadingZeros(0x80000));
0961: assertEquals(44, Long.numberOfLeadingZeros(0xF0000));
0962: assertEquals(43, Long.numberOfLeadingZeros(0x100000));
0963: assertEquals(40, Long.numberOfLeadingZeros(0x800000));
0964: assertEquals(40, Long.numberOfLeadingZeros(0xF00000));
0965: assertEquals(39, Long.numberOfLeadingZeros(0x1000000));
0966: assertEquals(36, Long.numberOfLeadingZeros(0x8000000));
0967: assertEquals(36, Long.numberOfLeadingZeros(0xF000000));
0968: assertEquals(35, Long.numberOfLeadingZeros(0x10000000));
0969: assertEquals(0, Long.numberOfLeadingZeros(0x80000000));
0970: assertEquals(0, Long.numberOfLeadingZeros(0xF0000000));
0971:
0972: assertEquals(1, Long.numberOfLeadingZeros(Long.MAX_VALUE));
0973: assertEquals(0, Long.numberOfLeadingZeros(Long.MIN_VALUE));
0974: }
0975:
0976: /**
0977: * @tests java.lang.Long#numberOfTrailingZeros(long)
0978: */
0979: public void test_numberOfTrailingZerosJ() {
0980: assertEquals(64, Long.numberOfTrailingZeros(0x0));
0981: assertEquals(63, Long.numberOfTrailingZeros(Long.MIN_VALUE));
0982: assertEquals(0, Long.numberOfTrailingZeros(Long.MAX_VALUE));
0983:
0984: assertEquals(0, Long.numberOfTrailingZeros(0x1));
0985: assertEquals(3, Long.numberOfTrailingZeros(0x8));
0986: assertEquals(0, Long.numberOfTrailingZeros(0xF));
0987:
0988: assertEquals(4, Long.numberOfTrailingZeros(0x10));
0989: assertEquals(7, Long.numberOfTrailingZeros(0x80));
0990: assertEquals(4, Long.numberOfTrailingZeros(0xF0));
0991:
0992: assertEquals(8, Long.numberOfTrailingZeros(0x100));
0993: assertEquals(11, Long.numberOfTrailingZeros(0x800));
0994: assertEquals(8, Long.numberOfTrailingZeros(0xF00));
0995:
0996: assertEquals(12, Long.numberOfTrailingZeros(0x1000));
0997: assertEquals(15, Long.numberOfTrailingZeros(0x8000));
0998: assertEquals(12, Long.numberOfTrailingZeros(0xF000));
0999:
1000: assertEquals(16, Long.numberOfTrailingZeros(0x10000));
1001: assertEquals(19, Long.numberOfTrailingZeros(0x80000));
1002: assertEquals(16, Long.numberOfTrailingZeros(0xF0000));
1003:
1004: assertEquals(20, Long.numberOfTrailingZeros(0x100000));
1005: assertEquals(23, Long.numberOfTrailingZeros(0x800000));
1006: assertEquals(20, Long.numberOfTrailingZeros(0xF00000));
1007:
1008: assertEquals(24, Long.numberOfTrailingZeros(0x1000000));
1009: assertEquals(27, Long.numberOfTrailingZeros(0x8000000));
1010: assertEquals(24, Long.numberOfTrailingZeros(0xF000000));
1011:
1012: assertEquals(28, Long.numberOfTrailingZeros(0x10000000));
1013: assertEquals(31, Long.numberOfTrailingZeros(0x80000000));
1014: assertEquals(28, Long.numberOfTrailingZeros(0xF0000000));
1015: }
1016:
1017: /**
1018: * @tests java.lang.Long#bitCount(long)
1019: */
1020: public void test_bitCountJ() {
1021: assertEquals(0, Long.bitCount(0x0));
1022: assertEquals(1, Long.bitCount(0x1));
1023: assertEquals(1, Long.bitCount(0x2));
1024: assertEquals(2, Long.bitCount(0x3));
1025: assertEquals(1, Long.bitCount(0x4));
1026: assertEquals(2, Long.bitCount(0x5));
1027: assertEquals(2, Long.bitCount(0x6));
1028: assertEquals(3, Long.bitCount(0x7));
1029: assertEquals(1, Long.bitCount(0x8));
1030: assertEquals(2, Long.bitCount(0x9));
1031: assertEquals(2, Long.bitCount(0xA));
1032: assertEquals(3, Long.bitCount(0xB));
1033: assertEquals(2, Long.bitCount(0xC));
1034: assertEquals(3, Long.bitCount(0xD));
1035: assertEquals(3, Long.bitCount(0xE));
1036: assertEquals(4, Long.bitCount(0xF));
1037:
1038: assertEquals(8, Long.bitCount(0xFF));
1039: assertEquals(12, Long.bitCount(0xFFF));
1040: assertEquals(16, Long.bitCount(0xFFFF));
1041: assertEquals(20, Long.bitCount(0xFFFFF));
1042: assertEquals(24, Long.bitCount(0xFFFFFF));
1043: assertEquals(28, Long.bitCount(0xFFFFFFF));
1044: assertEquals(64, Long.bitCount(0xFFFFFFFFFFFFFFFFL));
1045: }
1046:
1047: /**
1048: * @tests java.lang.Long#rotateLeft(long,long)
1049: */
1050: public void test_rotateLeftJI() {
1051: assertEquals(0xF, Long.rotateLeft(0xF, 0));
1052: assertEquals(0xF0, Long.rotateLeft(0xF, 4));
1053: assertEquals(0xF00, Long.rotateLeft(0xF, 8));
1054: assertEquals(0xF000, Long.rotateLeft(0xF, 12));
1055: assertEquals(0xF0000, Long.rotateLeft(0xF, 16));
1056: assertEquals(0xF00000, Long.rotateLeft(0xF, 20));
1057: assertEquals(0xF000000, Long.rotateLeft(0xF, 24));
1058: assertEquals(0xF0000000L, Long.rotateLeft(0xF, 28));
1059: assertEquals(0xF000000000000000L, Long.rotateLeft(
1060: 0xF000000000000000L, 64));
1061: }
1062:
1063: /**
1064: * @tests java.lang.Long#rotateRight(long,long)
1065: */
1066: public void test_rotateRightJI() {
1067: assertEquals(0xF, Long.rotateRight(0xF0, 4));
1068: assertEquals(0xF, Long.rotateRight(0xF00, 8));
1069: assertEquals(0xF, Long.rotateRight(0xF000, 12));
1070: assertEquals(0xF, Long.rotateRight(0xF0000, 16));
1071: assertEquals(0xF, Long.rotateRight(0xF00000, 20));
1072: assertEquals(0xF, Long.rotateRight(0xF000000, 24));
1073: assertEquals(0xF, Long.rotateRight(0xF0000000L, 28));
1074: assertEquals(0xF000000000000000L, Long.rotateRight(
1075: 0xF000000000000000L, 64));
1076: assertEquals(0xF000000000000000L, Long.rotateRight(
1077: 0xF000000000000000L, 0));
1078:
1079: }
1080:
1081: /**
1082: * @tests java.lang.Long#reverseBytes(long)
1083: */
1084: public void test_reverseBytesJ() {
1085: assertEquals(0xAABBCCDD00112233L, Long
1086: .reverseBytes(0x33221100DDCCBBAAL));
1087: assertEquals(0x1122334455667788L, Long
1088: .reverseBytes(0x8877665544332211L));
1089: assertEquals(0x0011223344556677L, Long
1090: .reverseBytes(0x7766554433221100L));
1091: assertEquals(0x2000000000000002L, Long
1092: .reverseBytes(0x0200000000000020L));
1093: }
1094:
1095: /**
1096: * @tests java.lang.Long#reverse(long)
1097: */
1098: public void test_reverseJ() {
1099: assertEquals(0, Long.reverse(0));
1100: assertEquals(-1, Long.reverse(-1));
1101: assertEquals(0x8000000000000000L, Long.reverse(1));
1102: }
1103:
1104: /**
1105: * @tests java.lang.Long#signum(long)
1106: */
1107: public void test_signumJ() {
1108: for (int i = -128; i < 0; i++) {
1109: assertEquals(-1, Long.signum(i));
1110: }
1111: assertEquals(0, Long.signum(0));
1112: for (int i = 1; i <= 127; i++) {
1113: assertEquals(1, Long.signum(i));
1114: }
1115: }
1116: }
|