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