MemoryTest.java revision f934c3d2c8dd9e6bc5299cef41adace2a671637d
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package libcore.io;
19
20import dalvik.system.VMRuntime;
21import junit.framework.TestCase;
22
23public class MemoryTest extends TestCase {
24    public void testSetIntArray() {
25        int[] values = { 3, 7, 31, 127, 8191, 131071, 524287, 2147483647 };
26        int[] swappedValues = new int[values.length];
27        for (int i = 0; i < values.length; ++i) {
28            swappedValues[i] = Integer.reverseBytes(values[i]);
29        }
30
31        int scale = 4;
32        VMRuntime runtime = VMRuntime.getRuntime();
33        byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, scale * values.length);
34        int ptr = (int) runtime.addressOf(array);
35
36        // Regular copy.
37        Memory.pokeIntArray(ptr, values, 0, values.length, false);
38        assertIntsEqual(values, ptr, false);
39        assertIntsEqual(swappedValues, ptr, true);
40
41        // Swapped copy.
42        Memory.pokeIntArray(ptr, values, 0, values.length, true);
43        assertIntsEqual(values, ptr, true);
44        assertIntsEqual(swappedValues, ptr, false);
45
46        // Swapped copies of slices (to ensure we test non-zero offsets).
47        for (int i = 0; i < values.length; ++i) {
48            Memory.pokeIntArray(ptr + i * scale, values, i, 1, true);
49        }
50        assertIntsEqual(values, ptr, true);
51        assertIntsEqual(swappedValues, ptr, false);
52    }
53
54    private void assertIntsEqual(int[] expectedValues, int ptr, boolean swap) {
55        for (int i = 0; i < expectedValues.length; ++i) {
56            assertEquals(expectedValues[i], Memory.peekInt(ptr + 4 * i, swap));
57        }
58    }
59
60    public void testSetShortArray() {
61        short[] values = { 0x0001, 0x0020, 0x0300, 0x4000 };
62        short[] swappedValues = { 0x0100, 0x2000, 0x0003, 0x0040 };
63
64        int scale = 2;
65        VMRuntime runtime = VMRuntime.getRuntime();
66        byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, scale * values.length);
67        int ptr = (int) runtime.addressOf(array);
68
69        // Regular copy. Memset first so we start from a known state.
70        Memory.pokeShortArray(ptr, values, 0, values.length, false);
71        assertShortsEqual(values, ptr, false);
72        assertShortsEqual(swappedValues, ptr, true);
73
74        // Swapped copy.
75        Memory.pokeShortArray(ptr, values, 0, values.length, true);
76        assertShortsEqual(values, ptr, true);
77        assertShortsEqual(swappedValues, ptr, false);
78
79        // Swapped copies of slices (to ensure we test non-zero offsets).
80        for (int i = 0; i < values.length; ++i) {
81            Memory.pokeShortArray(ptr + i * scale, values, i, 1, true);
82        }
83        assertShortsEqual(values, ptr, true);
84        assertShortsEqual(swappedValues, ptr, false);
85    }
86
87    private void assertShortsEqual(short[] expectedValues, int ptr, boolean swap) {
88        for (int i = 0; i < expectedValues.length; ++i) {
89            assertEquals(expectedValues[i], Memory.peekShort(ptr + 2 * i, swap));
90        }
91    }
92}
93