1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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 libcore.java.lang;
18
19import java.util.Properties;
20
21public class LongTest extends junit.framework.TestCase {
22
23    public void testSystemProperties() {
24        Properties originalProperties = System.getProperties();
25        try {
26            Properties testProperties = new Properties();
27            testProperties.put("testIncLong", "string");
28            System.setProperties(testProperties);
29            assertNull(Long.getLong("testIncLong"));
30            assertEquals(new Long(4), Long.getLong("testIncLong", 4L));
31            assertEquals(new Long(4), Long.getLong("testIncLong", new Long(4)));
32        } finally {
33            System.setProperties(originalProperties);
34        }
35    }
36
37    public void testCompare() throws Exception {
38        final long min = Long.MIN_VALUE;
39        final long zero = 0L;
40        final long max = Long.MAX_VALUE;
41        assertTrue(Long.compare(max,  max)  == 0);
42        assertTrue(Long.compare(min,  min)  == 0);
43        assertTrue(Long.compare(zero, zero) == 0);
44        assertTrue(Long.compare(max,  zero) > 0);
45        assertTrue(Long.compare(max,  min)  > 0);
46        assertTrue(Long.compare(zero, max)  < 0);
47        assertTrue(Long.compare(zero, min)  > 0);
48        assertTrue(Long.compare(min,  zero) < 0);
49        assertTrue(Long.compare(min,  max)  < 0);
50    }
51
52    public void testSignum() throws Exception {
53        assertEquals(0, Long.signum(0));
54        assertEquals(1, Long.signum(1));
55        assertEquals(-1, Long.signum(-1));
56        assertEquals(1, Long.signum(Long.MAX_VALUE));
57        assertEquals(-1, Long.signum(Long.MIN_VALUE));
58    }
59
60    public void testParsePositiveLong() throws Exception {
61        assertEquals(0, Long.parsePositiveLong("0", 10));
62        assertEquals(473, Long.parsePositiveLong("473", 10));
63        assertEquals(255, Long.parsePositiveLong("FF", 16));
64
65        try {
66            Long.parsePositiveLong("-1", 10);
67            fail();
68        } catch (NumberFormatException e) {}
69
70        try {
71            Long.parsePositiveLong("+1", 10);
72            fail();
73        } catch (NumberFormatException e) {}
74
75        try {
76            Long.parsePositiveLong("+0", 16);
77            fail();
78        } catch (NumberFormatException e) {}
79    }
80
81    public void testParseLong() throws Exception {
82        assertEquals(0, Long.parseLong("+0", 10));
83        assertEquals(473, Long.parseLong("+473", 10));
84        assertEquals(255, Long.parseLong("+FF", 16));
85        assertEquals(102, Long.parseLong("+1100110", 2));
86        assertEquals(Long.MAX_VALUE, Long.parseLong("+" + Long.MAX_VALUE, 10));
87        assertEquals(411787, Long.parseLong("Kona", 27));
88        assertEquals(411787, Long.parseLong("+Kona", 27));
89        assertEquals(-145, Long.parseLong("-145", 10));
90
91        try {
92            Long.parseLong("--1", 10); // multiple sign chars
93            fail();
94        } catch (NumberFormatException expected) {}
95
96        try {
97            Long.parseLong("++1", 10); // multiple sign chars
98            fail();
99        } catch (NumberFormatException expected) {}
100
101        try {
102            Long.parseLong("Kona", 10); // base to small
103            fail();
104        } catch (NumberFormatException expected) {}
105    }
106
107    public void testDecodeLong() throws Exception {
108        assertEquals(0, Long.decode("+0").longValue());
109        assertEquals(473, Long.decode("+473").longValue());
110        assertEquals(255, Long.decode("+0xFF").longValue());
111        assertEquals(16, Long.decode("+020").longValue());
112        assertEquals(Long.MAX_VALUE, Long.decode("+" + Long.MAX_VALUE).longValue());
113        assertEquals(-73, Long.decode("-73").longValue());
114        assertEquals(-255, Long.decode("-0xFF").longValue());
115        assertEquals(255, Long.decode("+#FF").longValue());
116        assertEquals(-255, Long.decode("-#FF").longValue());
117
118        try {
119            Long.decode("--1"); // multiple sign chars
120            fail();
121        } catch (NumberFormatException expected) {}
122
123        try {
124            Long.decode("++1"); // multiple sign chars
125            fail();
126        } catch (NumberFormatException expected) {}
127
128        try {
129            Long.decode("+-1"); // multiple sign chars
130            fail();
131        } catch (NumberFormatException expected) {}
132
133        try {
134            Long.decode("Kona"); // invalid number
135            fail();
136        } catch (NumberFormatException expected) {}
137    }
138
139}
140