summaryrefslogtreecommitdiff
path: root/src/test/java/com/android/apkzlib/zip/utils/LittleEndianUtilsTest.java
blob: 326429029050d516e30680a2c17ea80e3b5759b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.apkzlib.zip.utils;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertArrayEquals;

import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Random;

public class LittleEndianUtilsTest {
    @Test
    public void read2Le() throws Exception {
        assertEquals(0x0102, LittleEndianUtils.readUnsigned2Le(ByteBuffer.wrap(
                new byte[] { 2, 1 })));
        assertEquals(0xfedc, LittleEndianUtils.readUnsigned2Le(ByteBuffer.wrap(
                new byte[] { (byte) 0xdc, (byte) 0xfe })));
    }

    @Test
    public void write2Le() throws Exception {
        ByteBuffer out = ByteBuffer.allocate(2);
        LittleEndianUtils.writeUnsigned2Le(out, 0x0102);
        assertArrayEquals(new byte[] { 2, 1 }, out.array());

        out = ByteBuffer.allocate(2);
        LittleEndianUtils.writeUnsigned2Le(out, 0xfedc);
        assertArrayEquals(new byte[] { (byte) 0xdc, (byte) 0xfe }, out.array());
    }

    @Test
    public void readWrite2Le() throws Exception {
        Random r = new Random();

        int range = 0x0000ffff;

        final int COUNT = 1000;
        int[] data = new int[COUNT];
        for (int i = 0; i < data.length; i++) {
            data[i] = r.nextInt(range);
        }

        ByteBuffer out = ByteBuffer.allocate(COUNT * 2);
        for (int d : data) {
            LittleEndianUtils.writeUnsigned2Le(out, d);
        }

        ByteBuffer in = ByteBuffer.wrap(out.array());
        for (int i = 0; i < data.length; i++) {
            assertEquals(data[i], LittleEndianUtils.readUnsigned2Le(in));
        }
    }

    @Test
    public void read4Le() throws Exception {
        assertEquals(0x01020304, LittleEndianUtils.readUnsigned4Le(ByteBuffer.wrap(
                new byte[] { 4, 3, 2, 1 })));
        assertEquals(0xfedcba98L, LittleEndianUtils.readUnsigned4Le(ByteBuffer.wrap(
                new byte[] { (byte) 0x98, (byte) 0xba, (byte) 0xdc, (byte) 0xfe })));
    }

    @Test
    public void write4Le() throws Exception {
        ByteBuffer out = ByteBuffer.allocate(4);
        LittleEndianUtils.writeUnsigned4Le(out, 0x01020304);
        assertArrayEquals(new byte[] { 4, 3, 2, 1 }, out.array());

        out = ByteBuffer.allocate(4);
        LittleEndianUtils.writeUnsigned4Le(out, 0xfedcba98L);
        assertArrayEquals(new byte[] { (byte) 0x98, (byte) 0xba, (byte) 0xdc, (byte) 0xfe },
                out.array());
    }

    @Test
    public void readWrite4Le() throws Exception {
        Random r = new Random();

        final int COUNT = 1000;
        long[] data = new long[COUNT];
        for (int i = 0; i < data.length; i++) {
            do {
                data[i] = r.nextInt() - (long) Integer.MIN_VALUE;
            } while (data[i] < 0);
        }

        ByteBuffer out = ByteBuffer.allocate(COUNT * 4);
        for (long d : data) {
            LittleEndianUtils.writeUnsigned4Le(out, d);
        }

        ByteBuffer in = ByteBuffer.wrap(out.array());
        for (int i = 0; i < data.length; i++) {
            assertEquals(data[i], LittleEndianUtils.readUnsigned4Le(in));
        }
    }
}