• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
17 package com.android.internal.util;
18 
19 import android.support.test.filters.SmallTest;
20 import android.support.test.runner.AndroidJUnit4;
21 import java.nio.ByteBuffer;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 
25 import static org.junit.Assert.assertEquals;
26 import static com.android.internal.util.BitUtils.*;
27 
28 @SmallTest
29 @RunWith(AndroidJUnit4.class)
30 public class BitUtilsTest {
31 
32     @Test
testUnsignedByteWideningConversions()33     public void testUnsignedByteWideningConversions() {
34         byte b0 = 0;
35         byte b1 = 1;
36         byte bm1 = -1;
37         assertEquals(0, uint8(b0));
38         assertEquals(1, uint8(b1));
39         assertEquals(127, uint8(Byte.MAX_VALUE));
40         assertEquals(128, uint8(Byte.MIN_VALUE));
41         assertEquals(255, uint8(bm1));
42         assertEquals(255, uint8((byte)255));
43     }
44 
45     @Test
testUnsignedShortWideningConversions()46     public void testUnsignedShortWideningConversions() {
47         short s0 = 0;
48         short s1 = 1;
49         short sm1 = -1;
50         assertEquals(0, uint16(s0));
51         assertEquals(1, uint16(s1));
52         assertEquals(32767, uint16(Short.MAX_VALUE));
53         assertEquals(32768, uint16(Short.MIN_VALUE));
54         assertEquals(65535, uint16(sm1));
55         assertEquals(65535, uint16((short)65535));
56     }
57 
58     @Test
testUnsignedShortComposition()59     public void testUnsignedShortComposition() {
60         byte b0 = 0;
61         byte b1 = 1;
62         byte b2 = 2;
63         byte b10 = 10;
64         byte b16 = 16;
65         byte b128 = -128;
66         byte b224 = -32;
67         byte b255 = -1;
68         assertEquals(0x0000, uint16(b0, b0));
69         assertEquals(0xffff, uint16(b255, b255));
70         assertEquals(0x0a01, uint16(b10, b1));
71         assertEquals(0x8002, uint16(b128, b2));
72         assertEquals(0x01ff, uint16(b1, b255));
73         assertEquals(0x80ff, uint16(b128, b255));
74         assertEquals(0xe010, uint16(b224, b16));
75     }
76 
77     @Test
testUnsignedIntWideningConversions()78     public void testUnsignedIntWideningConversions() {
79         assertEquals(0, uint32(0));
80         assertEquals(1, uint32(1));
81         assertEquals(2147483647L, uint32(Integer.MAX_VALUE));
82         assertEquals(2147483648L, uint32(Integer.MIN_VALUE));
83         assertEquals(4294967295L, uint32(-1));
84         assertEquals(4294967295L, uint32((int)4294967295L));
85     }
86 
87     @Test
testBytesToInt()88     public void testBytesToInt() {
89         assertEquals(0x00000000, bytesToBEInt(bytes(0, 0, 0, 0)));
90         assertEquals(0xffffffff, bytesToBEInt(bytes(255, 255, 255, 255)));
91         assertEquals(0x0a000001, bytesToBEInt(bytes(10, 0, 0, 1)));
92         assertEquals(0x0a000002, bytesToBEInt(bytes(10, 0, 0, 2)));
93         assertEquals(0x0a001fff, bytesToBEInt(bytes(10, 0, 31, 255)));
94         assertEquals(0xe0000001, bytesToBEInt(bytes(224, 0, 0, 1)));
95 
96         assertEquals(0x00000000, bytesToLEInt(bytes(0, 0, 0, 0)));
97         assertEquals(0x01020304, bytesToLEInt(bytes(4, 3, 2, 1)));
98         assertEquals(0xffff0000, bytesToLEInt(bytes(0, 0, 255, 255)));
99     }
100 
101     @Test
testUnsignedGetters()102     public void testUnsignedGetters() {
103       ByteBuffer b = ByteBuffer.allocate(4);
104       b.putInt(0xffff);
105 
106       assertEquals(0x0, getUint8(b, 0));
107       assertEquals(0x0, getUint8(b, 1));
108       assertEquals(0xff, getUint8(b, 2));
109       assertEquals(0xff, getUint8(b, 3));
110 
111       assertEquals(0x0, getUint16(b, 0));
112       assertEquals(0xffff, getUint16(b, 2));
113 
114       b.rewind();
115       b.putInt(0xffffffff);
116       assertEquals(0xffffffffL, getUint32(b, 0));
117     }
118 
bytes(int b1, int b2, int b3, int b4)119     static byte[] bytes(int b1, int b2, int b3, int b4) {
120         return new byte[] {b(b1), b(b2), b(b3), b(b4)};
121     }
122 
b(int i)123     static byte b(int i) {
124         return (byte) i;
125     }
126 }
127