1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.lang;
18
19import java.util.Properties;
20
21import junit.framework.TestCase;
22
23public class LongTest extends TestCase {
24    private Properties orgProps;
25
26    @Override
27    protected void setUp() {
28        orgProps = System.getProperties();
29    }
30
31    @Override
32    protected void tearDown() {
33        System.setProperties(orgProps);
34    }
35
36    /**
37     * @tests java.lang.Long#byteValue()
38     */
39    public void test_byteValue() {
40        // Test for method byte java.lang.Long.byteValue()
41        Long l = new Long(127);
42        assertEquals("Returned incorrect byte value", 127, l.byteValue());
43        assertEquals("Returned incorrect byte value", -1, new Long(Long.MAX_VALUE)
44                .byteValue());
45    }
46
47    /**
48     * @tests java.lang.Long#compareTo(java.lang.Long)
49     */
50    public void test_compareToLjava_lang_Long() {
51        // Test for method int java.lang.Long.compareTo(java.lang.Long)
52        assertTrue("-2 compared to 1 gave non-negative answer", new Long(-2L)
53                .compareTo(new Long(1L)) < 0);
54        assertEquals("-2 compared to -2 gave non-zero answer", 0, new Long(-2L)
55                .compareTo(new Long(-2L)));
56        assertTrue("3 compared to 2 gave non-positive answer", new Long(3L)
57                .compareTo(new Long(2L)) > 0);
58
59        try {
60            new Long(0).compareTo(null);
61            fail("No NPE");
62        } catch (NullPointerException e) {
63        }
64    }
65
66    /**
67     * @tests java.lang.Long#decode(java.lang.String)
68     */
69    public void test_decodeLjava_lang_String2() {
70        // Test for method java.lang.Long
71        // java.lang.Long.decode(java.lang.String)
72        assertEquals("Returned incorrect value for hex string", 255L, Long.decode(
73                "0xFF").longValue());
74        assertEquals("Returned incorrect value for dec string", -89000L, Long.decode(
75                "-89000").longValue());
76        assertEquals("Returned incorrect value for 0 decimal", 0, Long.decode("0")
77                .longValue());
78        assertEquals("Returned incorrect value for 0 hex", 0, Long.decode("0x0")
79                .longValue());
80        assertTrue(
81                "Returned incorrect value for most negative value decimal",
82                Long.decode("-9223372036854775808").longValue() == 0x8000000000000000L);
83        assertTrue(
84                "Returned incorrect value for most negative value hex",
85                Long.decode("-0x8000000000000000").longValue() == 0x8000000000000000L);
86        assertTrue(
87                "Returned incorrect value for most positive value decimal",
88                Long.decode("9223372036854775807").longValue() == 0x7fffffffffffffffL);
89        assertTrue(
90                "Returned incorrect value for most positive value hex",
91                Long.decode("0x7fffffffffffffff").longValue() == 0x7fffffffffffffffL);
92        assertTrue("Failed for 07654321765432", Long.decode("07654321765432")
93                .longValue() == 07654321765432l);
94
95        boolean exception = false;
96        try {
97            Long
98                    .decode("999999999999999999999999999999999999999999999999999999");
99        } catch (NumberFormatException e) {
100            // Correct
101            exception = true;
102        }
103        assertTrue("Failed to throw exception for value > ilong", exception);
104
105        exception = false;
106        try {
107            Long.decode("9223372036854775808");
108        } catch (NumberFormatException e) {
109            // Correct
110            exception = true;
111        }
112        assertTrue("Failed to throw exception for MAX_VALUE + 1", exception);
113
114        exception = false;
115        try {
116            Long.decode("-9223372036854775809");
117        } catch (NumberFormatException e) {
118            // Correct
119            exception = true;
120        }
121        assertTrue("Failed to throw exception for MIN_VALUE - 1", exception);
122
123        exception = false;
124        try {
125            Long.decode("0x8000000000000000");
126        } catch (NumberFormatException e) {
127            // Correct
128            exception = true;
129        }
130        assertTrue("Failed to throw exception for hex MAX_VALUE + 1", exception);
131
132        exception = false;
133        try {
134            Long.decode("-0x8000000000000001");
135        } catch (NumberFormatException e) {
136            // Correct
137            exception = true;
138        }
139        assertTrue("Failed to throw exception for hex MIN_VALUE - 1", exception);
140
141        exception = false;
142        try {
143            Long.decode("42325917317067571199");
144        } catch (NumberFormatException e) {
145            // Correct
146            exception = true;
147        }
148        assertTrue("Failed to throw exception for 42325917317067571199",
149                exception);
150    }
151
152     /**
153     * @tests java.lang.Long#getLong(java.lang.String)
154     */
155    public void test_getLongLjava_lang_String() {
156        // Test for method java.lang.Long
157        // java.lang.Long.getLong(java.lang.String)
158        Properties tProps = new Properties();
159        tProps.put("testLong", "99");
160        System.setProperties(tProps);
161        assertTrue("returned incorrect Long", Long.getLong("testLong").equals(
162                new Long(99)));
163        assertNull("returned incorrect default Long",
164                Long.getLong("ff"));
165    }
166
167    /**
168     * @tests java.lang.Long#getLong(java.lang.String, long)
169     */
170    public void test_getLongLjava_lang_StringJ() {
171        // Test for method java.lang.Long
172        // java.lang.Long.getLong(java.lang.String, long)
173        Properties tProps = new Properties();
174        tProps.put("testLong", "99");
175        System.setProperties(tProps);
176        assertTrue("returned incorrect Long", Long.getLong("testLong", 4L)
177                .equals(new Long(99)));
178        assertTrue("returned incorrect default Long", Long.getLong("ff", 4L)
179                .equals(new Long(4)));
180    }
181
182    /**
183     * @tests java.lang.Long#getLong(java.lang.String, java.lang.Long)
184     */
185    public void test_getLongLjava_lang_StringLjava_lang_Long() {
186        // Test for method java.lang.Long
187        // java.lang.Long.getLong(java.lang.String, java.lang.Long)
188        Properties tProps = new Properties();
189        tProps.put("testLong", "99");
190        System.setProperties(tProps);
191        assertTrue("returned incorrect Long", Long.getLong("testLong",
192                new Long(4)).equals(new Long(99)));
193        assertTrue("returned incorrect default Long", Long.getLong("ff",
194                new Long(4)).equals(new Long(4)));
195    }
196
197    /**
198     * @tests java.lang.Long#parseLong(java.lang.String)
199     */
200    public void test_parseLongLjava_lang_String2() {
201        // Test for method long java.lang.Long.parseLong(java.lang.String)
202
203        long l = Long.parseLong("89000000005");
204        assertEquals("Parsed to incorrect long value", 89000000005L, l);
205        assertEquals("Returned incorrect value for 0", 0, Long.parseLong("0"));
206        assertTrue("Returned incorrect value for most negative value", Long
207                .parseLong("-9223372036854775808") == 0x8000000000000000L);
208        assertTrue("Returned incorrect value for most positive value", Long
209                .parseLong("9223372036854775807") == 0x7fffffffffffffffL);
210
211        boolean exception = false;
212        try {
213            Long.parseLong("9223372036854775808");
214        } catch (NumberFormatException e) {
215            // Correct
216            exception = true;
217        }
218        assertTrue("Failed to throw exception for MAX_VALUE + 1", exception);
219
220        exception = false;
221        try {
222            Long.parseLong("-9223372036854775809");
223        } catch (NumberFormatException e) {
224            // Correct
225            exception = true;
226        }
227        assertTrue("Failed to throw exception for MIN_VALUE - 1", exception);
228    }
229
230    /**
231     * @tests java.lang.Long#parseLong(java.lang.String, int)
232     */
233    public void test_parseLongLjava_lang_StringI() {
234        // Test for method long java.lang.Long.parseLong(java.lang.String, int)
235        assertEquals("Returned incorrect value",
236                100000000L, Long.parseLong("100000000", 10));
237        assertEquals("Returned incorrect value from hex string", 68719476735L, Long.parseLong(
238                "FFFFFFFFF", 16));
239        assertTrue("Returned incorrect value from octal string: "
240                + Long.parseLong("77777777777"), Long.parseLong("77777777777",
241                8) == 8589934591L);
242        assertEquals("Returned incorrect value for 0 hex", 0, Long
243                .parseLong("0", 16));
244        assertTrue("Returned incorrect value for most negative value hex", Long
245                .parseLong("-8000000000000000", 16) == 0x8000000000000000L);
246        assertTrue("Returned incorrect value for most positive value hex", Long
247                .parseLong("7fffffffffffffff", 16) == 0x7fffffffffffffffL);
248        assertEquals("Returned incorrect value for 0 decimal", 0, Long.parseLong(
249                "0", 10));
250        assertTrue(
251                "Returned incorrect value for most negative value decimal",
252                Long.parseLong("-9223372036854775808", 10) == 0x8000000000000000L);
253        assertTrue(
254                "Returned incorrect value for most positive value decimal",
255                Long.parseLong("9223372036854775807", 10) == 0x7fffffffffffffffL);
256
257        boolean exception = false;
258        try {
259            Long.parseLong("999999999999", 8);
260        } catch (NumberFormatException e) {
261            // correct
262            exception = true;
263        }
264        assertTrue("Failed to throw exception when passed invalid string",
265                exception);
266
267        exception = false;
268        try {
269            Long.parseLong("9223372036854775808", 10);
270        } catch (NumberFormatException e) {
271            // Correct
272            exception = true;
273        }
274        assertTrue("Failed to throw exception for MAX_VALUE + 1", exception);
275
276        exception = false;
277        try {
278            Long.parseLong("-9223372036854775809", 10);
279        } catch (NumberFormatException e) {
280            // Correct
281            exception = true;
282        }
283        assertTrue("Failed to throw exception for MIN_VALUE - 1", exception);
284
285        exception = false;
286        try {
287            Long.parseLong("8000000000000000", 16);
288        } catch (NumberFormatException e) {
289            // Correct
290            exception = true;
291        }
292        assertTrue("Failed to throw exception for hex MAX_VALUE + 1", exception);
293
294        exception = false;
295        try {
296            Long.parseLong("-8000000000000001", 16);
297        } catch (NumberFormatException e) {
298            // Correct
299            exception = true;
300        }
301        assertTrue("Failed to throw exception for hex MIN_VALUE + 1", exception);
302
303        exception = false;
304        try {
305            Long.parseLong("42325917317067571199", 10);
306        } catch (NumberFormatException e) {
307            // Correct
308            exception = true;
309        }
310        assertTrue("Failed to throw exception for 42325917317067571199",
311                exception);
312    }
313
314    /**
315     * @tests java.lang.Long#toBinaryString(long)
316     */
317    public void test_toBinaryStringJ() {
318        // Test for method java.lang.String java.lang.Long.toBinaryString(long)
319        assertEquals("Incorrect binary string returned", "11011001010010010000", Long.toBinaryString(
320                890000L));
321        assertEquals("Incorrect binary string returned",
322
323                                "1000000000000000000000000000000000000000000000000000000000000000", Long
324                        .toBinaryString(Long.MIN_VALUE)
325                        );
326        assertEquals("Incorrect binary string returned",
327
328                                "111111111111111111111111111111111111111111111111111111111111111", Long
329                        .toBinaryString(Long.MAX_VALUE)
330                        );
331    }
332
333    /**
334     * @tests java.lang.Long#toHexString(long)
335     */
336    public void test_toHexStringJ() {
337        // Test for method java.lang.String java.lang.Long.toHexString(long)
338        assertEquals("Incorrect hex string returned", "54e0845", Long.toHexString(89000005L)
339                );
340        assertEquals("Incorrect hex string returned", "8000000000000000", Long.toHexString(
341                Long.MIN_VALUE));
342        assertEquals("Incorrect hex string returned", "7fffffffffffffff", Long.toHexString(
343                Long.MAX_VALUE));
344    }
345
346    /**
347     * @tests java.lang.Long#toOctalString(long)
348     */
349    public void test_toOctalStringJ() {
350        // Test for method java.lang.String java.lang.Long.toOctalString(long)
351        assertEquals("Returned incorrect oct string", "77777777777", Long.toOctalString(
352                8589934591L));
353        assertEquals("Returned incorrect oct string", "1000000000000000000000", Long.toOctalString(
354                Long.MIN_VALUE));
355        assertEquals("Returned incorrect oct string", "777777777777777777777", Long.toOctalString(
356                Long.MAX_VALUE));
357    }
358
359    /**
360     * @tests java.lang.Long#toString()
361     */
362    public void test_toString2() {
363        // Test for method java.lang.String java.lang.Long.toString()
364        Long l = new Long(89000000005L);
365        assertEquals("Returned incorrect String",
366                "89000000005", l.toString());
367        assertEquals("Returned incorrect String", "-9223372036854775808", new Long(Long.MIN_VALUE)
368                .toString());
369        assertEquals("Returned incorrect String", "9223372036854775807", new Long(Long.MAX_VALUE)
370                .toString());
371    }
372
373    /**
374     * @tests java.lang.Long#toString(long)
375     */
376    public void test_toStringJ2() {
377        // Test for method java.lang.String java.lang.Long.toString(long)
378
379        assertEquals("Returned incorrect String", "89000000005", Long.toString(89000000005L)
380                );
381        assertEquals("Returned incorrect String", "-9223372036854775808", Long.toString(Long.MIN_VALUE)
382                );
383        assertEquals("Returned incorrect String", "9223372036854775807", Long.toString(Long.MAX_VALUE)
384                );
385    }
386
387    /**
388     * @tests java.lang.Long#toString(long, int)
389     */
390    public void test_toStringJI() {
391        // Test for method java.lang.String java.lang.Long.toString(long, int)
392        assertEquals("Returned incorrect dec string", "100000000", Long.toString(100000000L,
393                10));
394        assertEquals("Returned incorrect hex string", "fffffffff", Long.toString(68719476735L,
395                16));
396        assertEquals("Returned incorrect oct string", "77777777777", Long.toString(8589934591L,
397                8));
398        assertEquals("Returned incorrect bin string",
399                "1111111111111111111111111111111111111111111", Long.toString(
400                8796093022207L, 2));
401        assertEquals("Returned incorrect min string", "-9223372036854775808", Long.toString(
402                0x8000000000000000L, 10));
403        assertEquals("Returned incorrect max string", "9223372036854775807", Long.toString(
404                0x7fffffffffffffffL, 10));
405        assertEquals("Returned incorrect min string", "-8000000000000000", Long.toString(
406                0x8000000000000000L, 16));
407        assertEquals("Returned incorrect max string", "7fffffffffffffff", Long.toString(
408                0x7fffffffffffffffL, 16));
409    }
410
411    /**
412     * @tests java.lang.Long#valueOf(java.lang.String)
413     */
414    public void test_valueOfLjava_lang_String2() {
415        // Test for method java.lang.Long
416        // java.lang.Long.valueOf(java.lang.String)
417        assertEquals("Returned incorrect value", 100000000L, Long.valueOf("100000000")
418                .longValue());
419        assertTrue("Returned incorrect value", Long.valueOf(
420                "9223372036854775807").longValue() == Long.MAX_VALUE);
421        assertTrue("Returned incorrect value", Long.valueOf(
422                "-9223372036854775808").longValue() == Long.MIN_VALUE);
423
424        boolean exception = false;
425        try {
426            Long
427                    .valueOf("999999999999999999999999999999999999999999999999999999999999");
428        } catch (NumberFormatException e) {
429            // correct
430            exception = true;
431        }
432        assertTrue("Failed to throw exception when passed invalid string",
433                exception);
434
435        exception = false;
436        try {
437            Long.valueOf("9223372036854775808");
438        } catch (NumberFormatException e) {
439            // correct
440            exception = true;
441        }
442        assertTrue("Failed to throw exception when passed invalid string",
443                exception);
444
445        exception = false;
446        try {
447            Long.valueOf("-9223372036854775809");
448        } catch (NumberFormatException e) {
449            // correct
450            exception = true;
451        }
452        assertTrue("Failed to throw exception when passed invalid string",
453                exception);
454    }
455
456    /**
457     * @tests java.lang.Long#valueOf(java.lang.String, int)
458     */
459    public void test_valueOfLjava_lang_StringI() {
460        // Test for method java.lang.Long
461        // java.lang.Long.valueOf(java.lang.String, int)
462        assertEquals("Returned incorrect value", 100000000L, Long.valueOf("100000000", 10)
463                .longValue());
464        assertEquals("Returned incorrect value from hex string", 68719476735L, Long.valueOf(
465                "FFFFFFFFF", 16).longValue());
466        assertTrue("Returned incorrect value from octal string: "
467                + Long.valueOf("77777777777", 8).toString(), Long.valueOf(
468                "77777777777", 8).longValue() == 8589934591L);
469        assertTrue("Returned incorrect value", Long.valueOf(
470                "9223372036854775807", 10).longValue() == Long.MAX_VALUE);
471        assertTrue("Returned incorrect value", Long.valueOf(
472                "-9223372036854775808", 10).longValue() == Long.MIN_VALUE);
473        assertTrue("Returned incorrect value", Long.valueOf("7fffffffffffffff",
474                16).longValue() == Long.MAX_VALUE);
475        assertTrue("Returned incorrect value", Long.valueOf(
476                "-8000000000000000", 16).longValue() == Long.MIN_VALUE);
477
478        boolean exception = false;
479        try {
480            Long.valueOf("999999999999", 8);
481        } catch (NumberFormatException e) {
482            // correct
483            exception = true;
484        }
485        assertTrue("Failed to throw exception when passed invalid string",
486                exception);
487
488        exception = false;
489        try {
490            Long.valueOf("9223372036854775808", 10);
491        } catch (NumberFormatException e) {
492            // correct
493            exception = true;
494        }
495        assertTrue("Failed to throw exception when passed invalid string",
496                exception);
497
498        exception = false;
499        try {
500            Long.valueOf("-9223372036854775809", 10);
501        } catch (NumberFormatException e) {
502            // correct
503            exception = true;
504        }
505        assertTrue("Failed to throw exception when passed invalid string",
506                exception);
507    }
508    /**
509     * @tests java.lang.Long#valueOf(long)
510     */
511    public void test_valueOfJ() {
512        assertEquals(new Long(Long.MIN_VALUE), Long.valueOf(Long.MIN_VALUE));
513        assertEquals(new Long(Long.MAX_VALUE), Long.valueOf(Long.MAX_VALUE));
514        assertEquals(new Long( 0), Long.valueOf( 0));
515
516        long lng = -128;
517        while (lng < 128) {
518            assertEquals(new Long(lng), Long.valueOf(lng));
519            assertSame(Long.valueOf(lng), Long.valueOf(lng));
520            lng++;
521        }
522    }
523
524    /**
525     * @tests java.lang.Long#hashCode()
526     */
527    public void test_hashCode() {
528        assertEquals((int)(1L ^ (1L >>> 32)), new Long(1).hashCode());
529        assertEquals((int)(2L ^ (2L >>> 32)), new Long(2).hashCode());
530        assertEquals((int)(0L ^ (0L >>> 32)), new Long(0).hashCode());
531        assertEquals((int)(-1L ^ (-1L >>> 32)), new Long(-1).hashCode());
532    }
533
534    /**
535     * @tests java.lang.Long#Long(String)
536     */
537    public void test_ConstructorLjava_lang_String() {
538        assertEquals(new Long(0), new Long("0"));
539        assertEquals(new Long(1), new Long("1"));
540        assertEquals(new Long(-1), new Long("-1"));
541
542        try {
543            new Long("0x1");
544            fail("Expected NumberFormatException with hex string.");
545        } catch (NumberFormatException e) {}
546
547        try {
548            new Long("9.2");
549            fail("Expected NumberFormatException with floating point string.");
550        } catch (NumberFormatException e) {}
551
552        try {
553            new Long("");
554            fail("Expected NumberFormatException with empty string.");
555        } catch (NumberFormatException e) {}
556
557        try {
558            new Long(null);
559            fail("Expected NumberFormatException with null string.");
560        } catch (NumberFormatException e) {}
561    }
562
563    /**
564     * @tests java.lang.Long#Long
565     */
566    public void test_ConstructorJ() {
567        assertEquals(1, new Long(1).intValue());
568        assertEquals(2, new Long(2).intValue());
569        assertEquals(0, new Long(0).intValue());
570        assertEquals(-1, new Long(-1).intValue());
571    }
572
573    /**
574     * @tests java.lang.Long#byteValue()
575     */
576    public void test_booleanValue() {
577        assertEquals(1, new Long(1).byteValue());
578        assertEquals(2, new Long(2).byteValue());
579        assertEquals(0, new Long(0).byteValue());
580        assertEquals(-1, new Long(-1).byteValue());
581    }
582
583    /**
584     * @tests java.lang.Long#equals(Object)
585     */
586    public void test_equalsLjava_lang_Object() {
587        assertEquals(new Long(0), Long.valueOf(0));
588        assertEquals(new Long(1), Long.valueOf(1));
589        assertEquals(new Long(-1), Long.valueOf(-1));
590
591        Long fixture = new Long(25);
592        assertEquals(fixture, fixture);
593        assertFalse(fixture.equals(null));
594        assertFalse(fixture.equals("Not a Long"));
595    }
596
597    /**
598     * @tests java.lang.Long#toString()
599     */
600    public void test_toString() {
601        assertEquals("-1", new Long(-1).toString());
602        assertEquals("0", new Long(0).toString());
603        assertEquals("1", new Long(1).toString());
604        assertEquals("-1", new Long(0xFFFFFFFF).toString());
605    }
606
607    /**
608     * @tests java.lang.Long#toString
609     */
610    public void test_toStringJ() {
611        assertEquals("-1", Long.toString(-1));
612        assertEquals("0", Long.toString(0));
613        assertEquals("1", Long.toString(1));
614        assertEquals("-1", Long.toString(0xFFFFFFFF));
615    }
616
617    /**
618     * @tests java.lang.Long#valueOf(String)
619     */
620    public void test_valueOfLjava_lang_String() {
621        assertEquals(new Long(0), Long.valueOf("0"));
622        assertEquals(new Long(1), Long.valueOf("1"));
623        assertEquals(new Long(-1), Long.valueOf("-1"));
624
625        try {
626            Long.valueOf("0x1");
627            fail("Expected NumberFormatException with hex string.");
628        } catch (NumberFormatException e) {}
629
630        try {
631            Long.valueOf("9.2");
632            fail("Expected NumberFormatException with floating point string.");
633        } catch (NumberFormatException e) {}
634
635        try {
636            Long.valueOf("");
637            fail("Expected NumberFormatException with empty string.");
638        } catch (NumberFormatException e) {}
639
640        try {
641            Long.valueOf(null);
642            fail("Expected NumberFormatException with null string.");
643        } catch (NumberFormatException e) {}
644    }
645
646    /**
647     * @tests java.lang.Long#valueOf(String,long)
648     */
649    public void test_valueOfLjava_lang_StringJ() {
650        assertEquals(new Long(0), Long.valueOf("0", 10));
651        assertEquals(new Long(1), Long.valueOf("1", 10));
652        assertEquals(new Long(-1), Long.valueOf("-1", 10));
653
654        //must be consistent with Character.digit()
655        assertEquals(Character.digit('1', 2), Long.valueOf("1", 2).byteValue());
656        assertEquals(Character.digit('F', 16), Long.valueOf("F", 16).byteValue());
657
658        try {
659            Long.valueOf("0x1", 10);
660            fail("Expected NumberFormatException with hex string.");
661        } catch (NumberFormatException e) {}
662
663        try {
664            Long.valueOf("9.2", 10);
665            fail("Expected NumberFormatException with floating point string.");
666        } catch (NumberFormatException e) {}
667
668        try {
669            Long.valueOf("", 10);
670            fail("Expected NumberFormatException with empty string.");
671        } catch (NumberFormatException e) {}
672
673        try {
674            Long.valueOf(null, 10);
675            fail("Expected NumberFormatException with null string.");
676        } catch (NumberFormatException e) {}
677    }
678
679    /**
680     * @tests java.lang.Long#parseLong(String)
681     */
682    public void test_parseLongLjava_lang_String() {
683        assertEquals(0, Long.parseLong("0"));
684        assertEquals(1, Long.parseLong("1"));
685        assertEquals(-1, Long.parseLong("-1"));
686
687        try {
688            Long.parseLong("0x1");
689            fail("Expected NumberFormatException with hex string.");
690        } catch (NumberFormatException e) {}
691
692        try {
693            Long.parseLong("9.2");
694            fail("Expected NumberFormatException with floating point string.");
695        } catch (NumberFormatException e) {}
696
697        try {
698            Long.parseLong("");
699            fail("Expected NumberFormatException with empty string.");
700        } catch (NumberFormatException e) {}
701
702        try {
703            Long.parseLong(null);
704            fail("Expected NumberFormatException with null string.");
705        } catch (NumberFormatException e) {}
706    }
707
708    /**
709     * @tests java.lang.Long#parseLong(String,long)
710     */
711    public void test_parseLongLjava_lang_StringJ() {
712        assertEquals(0, Long.parseLong("0", 10));
713        assertEquals(1, Long.parseLong("1", 10));
714        assertEquals(-1, Long.parseLong("-1", 10));
715
716        //must be consistent with Character.digit()
717        assertEquals(Character.digit('1', 2), Long.parseLong("1", 2));
718        assertEquals(Character.digit('F', 16), Long.parseLong("F", 16));
719
720        try {
721            Long.parseLong("0x1", 10);
722            fail("Expected NumberFormatException with hex string.");
723        } catch (NumberFormatException e) {}
724
725        try {
726            Long.parseLong("9.2", 10);
727            fail("Expected NumberFormatException with floating point string.");
728        } catch (NumberFormatException e) {}
729
730        try {
731            Long.parseLong("", 10);
732            fail("Expected NumberFormatException with empty string.");
733        } catch (NumberFormatException e) {}
734
735        try {
736            Long.parseLong(null, 10);
737            fail("Expected NumberFormatException with null string.");
738        } catch (NumberFormatException e) {}
739    }
740
741    /**
742     * @tests java.lang.Long#decode(String)
743     */
744    public void test_decodeLjava_lang_String() {
745        assertEquals(new Long(0), Long.decode("0"));
746        assertEquals(new Long(1), Long.decode("1"));
747        assertEquals(new Long(-1), Long.decode("-1"));
748        assertEquals(new Long(0xF), Long.decode("0xF"));
749        assertEquals(new Long(0xF), Long.decode("#F"));
750        assertEquals(new Long(0xF), Long.decode("0XF"));
751        assertEquals(new Long(07), Long.decode("07"));
752
753        try {
754            Long.decode("9.2");
755            fail("Expected NumberFormatException with floating point string.");
756        } catch (NumberFormatException e) {}
757
758        try {
759            Long.decode("");
760            fail("Expected NumberFormatException with empty string.");
761        } catch (NumberFormatException e) {}
762
763        try {
764            Long.decode(null);
765            //undocumented NPE, but seems consistent across JREs
766            fail("Expected NullPointerException with null string.");
767        } catch (NullPointerException e) {}
768    }
769
770    /**
771     * @tests java.lang.Long#doubleValue()
772     */
773    public void test_doubleValue() {
774        assertEquals(-1D, new Long(-1).doubleValue(), 0D);
775        assertEquals(0D, new Long(0).doubleValue(), 0D);
776        assertEquals(1D, new Long(1).doubleValue(), 0D);
777    }
778
779    /**
780     * @tests java.lang.Long#floatValue()
781     */
782    public void test_floatValue() {
783        assertEquals(-1F, new Long(-1).floatValue(), 0F);
784        assertEquals(0F, new Long(0).floatValue(), 0F);
785        assertEquals(1F, new Long(1).floatValue(), 0F);
786    }
787
788    /**
789     * @tests java.lang.Long#intValue()
790     */
791    public void test_intValue() {
792        assertEquals(-1, new Long(-1).intValue());
793        assertEquals(0, new Long(0).intValue());
794        assertEquals(1, new Long(1).intValue());
795    }
796
797    /**
798     * @tests java.lang.Long#longValue()
799     */
800    public void test_longValue() {
801        assertEquals(-1L, new Long(-1).longValue());
802        assertEquals(0L, new Long(0).longValue());
803        assertEquals(1L, new Long(1).longValue());
804    }
805
806    /**
807     * @tests java.lang.Long#shortValue()
808     */
809    public void test_shortValue() {
810        assertEquals(-1, new Long(-1).shortValue());
811        assertEquals(0, new Long(0).shortValue());
812        assertEquals(1, new Long(1).shortValue());
813    }
814    /**
815     * @tests java.lang.Long#highestOneBit(long)
816     */
817    public void test_highestOneBitJ() {
818        assertEquals(0x08, Long.highestOneBit(0x0A));
819        assertEquals(0x08, Long.highestOneBit(0x0B));
820        assertEquals(0x08, Long.highestOneBit(0x0C));
821        assertEquals(0x08, Long.highestOneBit(0x0F));
822        assertEquals(0x80, Long.highestOneBit(0xFF));
823
824        assertEquals(0x080000, Long.highestOneBit(0x0F1234));
825        assertEquals(0x800000, Long.highestOneBit(0xFF9977));
826
827        assertEquals(0x8000000000000000L, Long.highestOneBit(0xFFFFFFFFFFFFFFFFL));
828
829        assertEquals(0, Long.highestOneBit(0));
830        assertEquals(1, Long.highestOneBit(1));
831        assertEquals(0x8000000000000000L, Long.highestOneBit(-1));
832    }
833
834    /**
835     * @tests java.lang.Long#lowestOneBit(long)
836     */
837    public void test_lowestOneBitJ() {
838        assertEquals(0x10, Long.lowestOneBit(0xF0));
839
840        assertEquals(0x10, Long.lowestOneBit(0x90));
841        assertEquals(0x10, Long.lowestOneBit(0xD0));
842
843        assertEquals(0x10, Long.lowestOneBit(0x123490));
844        assertEquals(0x10, Long.lowestOneBit(0x1234D0));
845
846        assertEquals(0x100000, Long.lowestOneBit(0x900000));
847        assertEquals(0x100000, Long.lowestOneBit(0xD00000));
848
849        assertEquals(0x40, Long.lowestOneBit(0x40));
850        assertEquals(0x40, Long.lowestOneBit(0xC0));
851
852        assertEquals(0x4000, Long.lowestOneBit(0x4000));
853        assertEquals(0x4000, Long.lowestOneBit(0xC000));
854
855        assertEquals(0x4000, Long.lowestOneBit(0x99994000));
856        assertEquals(0x4000, Long.lowestOneBit(0x9999C000));
857
858        assertEquals(0, Long.lowestOneBit(0));
859        assertEquals(1, Long.lowestOneBit(1));
860        assertEquals(1, Long.lowestOneBit(-1));
861    }
862    /**
863     * @tests java.lang.Long#numberOfLeadingZeros(long)
864     */
865    public void test_numberOfLeadingZerosJ() {
866        assertEquals(64, Long.numberOfLeadingZeros(0x0L));
867        assertEquals(63, Long.numberOfLeadingZeros(0x1));
868        assertEquals(62, Long.numberOfLeadingZeros(0x2));
869        assertEquals(62, Long.numberOfLeadingZeros(0x3));
870        assertEquals(61, Long.numberOfLeadingZeros(0x4));
871        assertEquals(61, Long.numberOfLeadingZeros(0x5));
872        assertEquals(61, Long.numberOfLeadingZeros(0x6));
873        assertEquals(61, Long.numberOfLeadingZeros(0x7));
874        assertEquals(60, Long.numberOfLeadingZeros(0x8));
875        assertEquals(60, Long.numberOfLeadingZeros(0x9));
876        assertEquals(60, Long.numberOfLeadingZeros(0xA));
877        assertEquals(60, Long.numberOfLeadingZeros(0xB));
878        assertEquals(60, Long.numberOfLeadingZeros(0xC));
879        assertEquals(60, Long.numberOfLeadingZeros(0xD));
880        assertEquals(60, Long.numberOfLeadingZeros(0xE));
881        assertEquals(60, Long.numberOfLeadingZeros(0xF));
882        assertEquals(59, Long.numberOfLeadingZeros(0x10));
883        assertEquals(56, Long.numberOfLeadingZeros(0x80));
884        assertEquals(56, Long.numberOfLeadingZeros(0xF0));
885        assertEquals(55, Long.numberOfLeadingZeros(0x100));
886        assertEquals(52, Long.numberOfLeadingZeros(0x800));
887        assertEquals(52, Long.numberOfLeadingZeros(0xF00));
888        assertEquals(51, Long.numberOfLeadingZeros(0x1000));
889        assertEquals(48, Long.numberOfLeadingZeros(0x8000));
890        assertEquals(48, Long.numberOfLeadingZeros(0xF000));
891        assertEquals(47, Long.numberOfLeadingZeros(0x10000));
892        assertEquals(44, Long.numberOfLeadingZeros(0x80000));
893        assertEquals(44, Long.numberOfLeadingZeros(0xF0000));
894        assertEquals(43, Long.numberOfLeadingZeros(0x100000));
895        assertEquals(40, Long.numberOfLeadingZeros(0x800000));
896        assertEquals(40, Long.numberOfLeadingZeros(0xF00000));
897        assertEquals(39, Long.numberOfLeadingZeros(0x1000000));
898        assertEquals(36, Long.numberOfLeadingZeros(0x8000000));
899        assertEquals(36, Long.numberOfLeadingZeros(0xF000000));
900        assertEquals(35, Long.numberOfLeadingZeros(0x10000000));
901        assertEquals(0, Long.numberOfLeadingZeros(0x80000000));
902        assertEquals(0, Long.numberOfLeadingZeros(0xF0000000));
903
904        assertEquals(1, Long.numberOfLeadingZeros(Long.MAX_VALUE));
905        assertEquals(0, Long.numberOfLeadingZeros(Long.MIN_VALUE));
906    }
907
908    /**
909     * @tests java.lang.Long#numberOfTrailingZeros(long)
910     */
911    public void test_numberOfTrailingZerosJ() {
912        assertEquals(64, Long.numberOfTrailingZeros(0x0));
913        assertEquals(63, Long.numberOfTrailingZeros(Long.MIN_VALUE));
914        assertEquals(0, Long.numberOfTrailingZeros(Long.MAX_VALUE));
915
916        assertEquals(0, Long.numberOfTrailingZeros(0x1));
917        assertEquals(3, Long.numberOfTrailingZeros(0x8));
918        assertEquals(0, Long.numberOfTrailingZeros(0xF));
919
920        assertEquals(4, Long.numberOfTrailingZeros(0x10));
921        assertEquals(7, Long.numberOfTrailingZeros(0x80));
922        assertEquals(4, Long.numberOfTrailingZeros(0xF0));
923
924        assertEquals(8, Long.numberOfTrailingZeros(0x100));
925        assertEquals(11, Long.numberOfTrailingZeros(0x800));
926        assertEquals(8, Long.numberOfTrailingZeros(0xF00));
927
928        assertEquals(12, Long.numberOfTrailingZeros(0x1000));
929        assertEquals(15, Long.numberOfTrailingZeros(0x8000));
930        assertEquals(12, Long.numberOfTrailingZeros(0xF000));
931
932        assertEquals(16, Long.numberOfTrailingZeros(0x10000));
933        assertEquals(19, Long.numberOfTrailingZeros(0x80000));
934        assertEquals(16, Long.numberOfTrailingZeros(0xF0000));
935
936        assertEquals(20, Long.numberOfTrailingZeros(0x100000));
937        assertEquals(23, Long.numberOfTrailingZeros(0x800000));
938        assertEquals(20, Long.numberOfTrailingZeros(0xF00000));
939
940        assertEquals(24, Long.numberOfTrailingZeros(0x1000000));
941        assertEquals(27, Long.numberOfTrailingZeros(0x8000000));
942        assertEquals(24, Long.numberOfTrailingZeros(0xF000000));
943
944        assertEquals(28, Long.numberOfTrailingZeros(0x10000000));
945        assertEquals(31, Long.numberOfTrailingZeros(0x80000000));
946        assertEquals(28, Long.numberOfTrailingZeros(0xF0000000));
947    }
948
949    /**
950     * @tests java.lang.Long#bitCount(long)
951     */
952    public void test_bitCountJ() {
953        assertEquals(0, Long.bitCount(0x0));
954        assertEquals(1, Long.bitCount(0x1));
955        assertEquals(1, Long.bitCount(0x2));
956        assertEquals(2, Long.bitCount(0x3));
957        assertEquals(1, Long.bitCount(0x4));
958        assertEquals(2, Long.bitCount(0x5));
959        assertEquals(2, Long.bitCount(0x6));
960        assertEquals(3, Long.bitCount(0x7));
961        assertEquals(1, Long.bitCount(0x8));
962        assertEquals(2, Long.bitCount(0x9));
963        assertEquals(2, Long.bitCount(0xA));
964        assertEquals(3, Long.bitCount(0xB));
965        assertEquals(2, Long.bitCount(0xC));
966        assertEquals(3, Long.bitCount(0xD));
967        assertEquals(3, Long.bitCount(0xE));
968        assertEquals(4, Long.bitCount(0xF));
969
970        assertEquals(8, Long.bitCount(0xFF));
971        assertEquals(12, Long.bitCount(0xFFF));
972        assertEquals(16, Long.bitCount(0xFFFF));
973        assertEquals(20, Long.bitCount(0xFFFFF));
974        assertEquals(24, Long.bitCount(0xFFFFFF));
975        assertEquals(28, Long.bitCount(0xFFFFFFF));
976        assertEquals(64, Long.bitCount(0xFFFFFFFFFFFFFFFFL));
977    }
978
979    /**
980     * @tests java.lang.Long#rotateLeft(long,long)
981     */
982    public void test_rotateLeftJI() {
983        assertEquals(0xF, Long.rotateLeft(0xF, 0));
984        assertEquals(0xF0, Long.rotateLeft(0xF, 4));
985        assertEquals(0xF00, Long.rotateLeft(0xF, 8));
986        assertEquals(0xF000, Long.rotateLeft(0xF, 12));
987        assertEquals(0xF0000, Long.rotateLeft(0xF, 16));
988        assertEquals(0xF00000, Long.rotateLeft(0xF, 20));
989        assertEquals(0xF000000, Long.rotateLeft(0xF, 24));
990        assertEquals(0xF0000000L, Long.rotateLeft(0xF, 28));
991        assertEquals(0xF000000000000000L, Long.rotateLeft(0xF000000000000000L, 64));
992    }
993
994    /**
995     * @tests java.lang.Long#rotateRight(long,long)
996     */
997    public void test_rotateRightJI() {
998        assertEquals(0xF, Long.rotateRight(0xF0, 4));
999        assertEquals(0xF, Long.rotateRight(0xF00, 8));
1000        assertEquals(0xF, Long.rotateRight(0xF000, 12));
1001        assertEquals(0xF, Long.rotateRight(0xF0000, 16));
1002        assertEquals(0xF, Long.rotateRight(0xF00000, 20));
1003        assertEquals(0xF, Long.rotateRight(0xF000000, 24));
1004        assertEquals(0xF, Long.rotateRight(0xF0000000L, 28));
1005        assertEquals(0xF000000000000000L, Long.rotateRight(0xF000000000000000L, 64));
1006        assertEquals(0xF000000000000000L, Long.rotateRight(0xF000000000000000L, 0));
1007
1008    }
1009
1010    /**
1011     * @tests java.lang.Long#reverseBytes(long)
1012     */
1013    public void test_reverseBytesJ() {
1014        assertEquals(0xAABBCCDD00112233L, Long.reverseBytes(0x33221100DDCCBBAAL));
1015        assertEquals(0x1122334455667788L, Long.reverseBytes(0x8877665544332211L));
1016        assertEquals(0x0011223344556677L, Long.reverseBytes(0x7766554433221100L));
1017        assertEquals(0x2000000000000002L, Long.reverseBytes(0x0200000000000020L));
1018    }
1019
1020    /**
1021     * @tests java.lang.Long#reverse(long)
1022     */
1023    public void test_reverseJ() {
1024        assertEquals(0, Long.reverse(0));
1025        assertEquals(-1, Long.reverse(-1));
1026        assertEquals(0x8000000000000000L,Long.reverse(1));
1027    }
1028
1029    /**
1030     * @tests java.lang.Long#signum(long)
1031     */
1032    public void test_signumJ() {
1033        for (int i = -128; i<0; i++) {
1034            assertEquals(-1, Long.signum(i));
1035        }
1036        assertEquals(0, Long.signum(0));
1037        for (int i = 1; i<=127; i++) {
1038            assertEquals(1, Long.signum(i));
1039        }
1040    }
1041}