1 /*
2  * Copyright (C) 2015 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.tools.build.apkzlib.zip.utils;
18 
19 import static junit.framework.TestCase.assertEquals;
20 import static org.junit.Assert.assertArrayEquals;
21 
22 import java.nio.ByteBuffer;
23 import java.util.Random;
24 import org.junit.Test;
25 
26 public class LittleEndianUtilsTest {
27     @Test
read2Le()28     public void read2Le() throws Exception {
29         assertEquals(0x0102, LittleEndianUtils.readUnsigned2Le(ByteBuffer.wrap(
30                 new byte[] { 2, 1 })));
31         assertEquals(0xfedc, LittleEndianUtils.readUnsigned2Le(ByteBuffer.wrap(
32                 new byte[] { (byte) 0xdc, (byte) 0xfe })));
33     }
34 
35     @Test
write2Le()36     public void write2Le() throws Exception {
37         ByteBuffer out = ByteBuffer.allocate(2);
38         LittleEndianUtils.writeUnsigned2Le(out, 0x0102);
39         assertArrayEquals(new byte[] { 2, 1 }, out.array());
40 
41         out = ByteBuffer.allocate(2);
42         LittleEndianUtils.writeUnsigned2Le(out, 0xfedc);
43         assertArrayEquals(new byte[] { (byte) 0xdc, (byte) 0xfe }, out.array());
44     }
45 
46     @Test
readWrite2Le()47     public void readWrite2Le() throws Exception {
48         Random r = new Random();
49 
50         int range = 0x0000ffff;
51 
52         final int COUNT = 1000;
53         int[] data = new int[COUNT];
54         for (int i = 0; i < data.length; i++) {
55             data[i] = r.nextInt(range);
56         }
57 
58         ByteBuffer out = ByteBuffer.allocate(COUNT * 2);
59         for (int d : data) {
60             LittleEndianUtils.writeUnsigned2Le(out, d);
61         }
62 
63         ByteBuffer in = ByteBuffer.wrap(out.array());
64         for (int i = 0; i < data.length; i++) {
65             assertEquals(data[i], LittleEndianUtils.readUnsigned2Le(in));
66         }
67     }
68 
69     @Test
read4Le()70     public void read4Le() throws Exception {
71         assertEquals(0x01020304, LittleEndianUtils.readUnsigned4Le(ByteBuffer.wrap(
72                 new byte[] { 4, 3, 2, 1 })));
73         assertEquals(0xfedcba98L, LittleEndianUtils.readUnsigned4Le(ByteBuffer.wrap(
74                 new byte[] { (byte) 0x98, (byte) 0xba, (byte) 0xdc, (byte) 0xfe })));
75     }
76 
77     @Test
write4Le()78     public void write4Le() throws Exception {
79         ByteBuffer out = ByteBuffer.allocate(4);
80         LittleEndianUtils.writeUnsigned4Le(out, 0x01020304);
81         assertArrayEquals(new byte[] { 4, 3, 2, 1 }, out.array());
82 
83         out = ByteBuffer.allocate(4);
84         LittleEndianUtils.writeUnsigned4Le(out, 0xfedcba98L);
85         assertArrayEquals(new byte[] { (byte) 0x98, (byte) 0xba, (byte) 0xdc, (byte) 0xfe },
86                 out.array());
87     }
88 
89     @Test
readWrite4Le()90     public void readWrite4Le() throws Exception {
91         Random r = new Random();
92 
93         final int COUNT = 1000;
94         long[] data = new long[COUNT];
95         for (int i = 0; i < data.length; i++) {
96             do {
97                 data[i] = r.nextInt() - (long) Integer.MIN_VALUE;
98             } while (data[i] < 0);
99         }
100 
101         ByteBuffer out = ByteBuffer.allocate(COUNT * 4);
102         for (long d : data) {
103             LittleEndianUtils.writeUnsigned4Le(out, d);
104         }
105 
106         ByteBuffer in = ByteBuffer.wrap(out.array());
107         for (int i = 0; i < data.length; i++) {
108             assertEquals(data[i], LittleEndianUtils.readUnsigned4Le(in));
109         }
110     }
111 }
112