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
19public class ShortTest extends junit.framework.TestCase {
20    public void test_compare() throws Exception {
21        final short min = Short.MIN_VALUE;
22        final short zero = 0;
23        final short max = Short.MAX_VALUE;
24        assertTrue(Short.compare(max,  max)  == 0);
25        assertTrue(Short.compare(min,  min)  == 0);
26        assertTrue(Short.compare(zero, zero) == 0);
27        assertTrue(Short.compare(max,  zero) > 0);
28        assertTrue(Short.compare(max,  min)  > 0);
29        assertTrue(Short.compare(zero, max)  < 0);
30        assertTrue(Short.compare(zero, min)  > 0);
31        assertTrue(Short.compare(min,  zero) < 0);
32        assertTrue(Short.compare(min,  max)  < 0);
33    }
34
35    public void testStaticHashCode() {
36        assertEquals(Short.valueOf((short) 567).hashCode(), Short.hashCode((short) 567));
37    }
38
39    public void testBYTES() {
40        assertEquals(2, Short.BYTES);
41    }
42
43    public void testToUnsignedInt() {
44        for(int i = Short.MIN_VALUE; i < Short.MAX_VALUE; i++) {
45            final short b = (short) i;
46            final int ui = Short.toUnsignedInt(b);
47            assertEquals(0, ui >>> Short.BYTES * 8);
48            assertEquals(b, Integer.valueOf(b).shortValue());
49        }
50    }
51
52    public void testToUnsignedLong() {
53        for(int i = Short.MIN_VALUE; i < Short.MAX_VALUE; i++) {
54            final short b = (short) i;
55            final long ul = Short.toUnsignedLong(b);
56            assertEquals(0, ul >>> Short.BYTES * 8);
57            assertEquals(b, Long.valueOf(b).shortValue());
58        }
59    }
60}
61