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 java.util.Arrays;
22import junit.framework.TestCase;
23
24public class MemoryTest extends TestCase {
25    public void testSetIntArray() {
26        int[] values = { 3, 7, 31, 127, 8191, 131071, 524287, 2147483647 };
27        int[] swappedValues = new int[values.length];
28        for (int i = 0; i < values.length; ++i) {
29            swappedValues[i] = Integer.reverseBytes(values[i]);
30        }
31
32        int scale = SizeOf.INT;
33        VMRuntime runtime = VMRuntime.getRuntime();
34        byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, scale * values.length + 1);
35        long base_ptr = runtime.addressOf(array);
36
37        for (int ptr_offset = 0; ptr_offset < 2; ++ptr_offset) {
38            long ptr = base_ptr + ptr_offset; // To test aligned and unaligned accesses.
39            Arrays.fill(array, (byte) 0);
40
41            // Regular copy.
42            Memory.pokeIntArray(ptr, values, 0, values.length, false);
43            assertIntsEqual(values, ptr, false);
44            assertIntsEqual(swappedValues, ptr, true);
45
46            // Swapped copy.
47            Memory.pokeIntArray(ptr, values, 0, values.length, true);
48            assertIntsEqual(values, ptr, true);
49            assertIntsEqual(swappedValues, ptr, false);
50
51            // Swapped copies of slices (to ensure we test non-zero offsets).
52            for (int i = 0; i < values.length; ++i) {
53                Memory.pokeIntArray(ptr + i * scale, values, i, 1, true);
54            }
55            assertIntsEqual(values, ptr, true);
56            assertIntsEqual(swappedValues, ptr, false);
57        }
58    }
59
60    private void assertIntsEqual(int[] expectedValues, long ptr, boolean swap) {
61        for (int i = 0; i < expectedValues.length; ++i) {
62            assertEquals(expectedValues[i], Memory.peekInt(ptr + SizeOf.INT * i, swap));
63        }
64    }
65
66    public void testSetLongArray() {
67        long[] values = { 0x1020304050607080L, 0xffeeddccbbaa9988L };
68        long[] swappedValues = new long[values.length];
69        for (int i = 0; i < values.length; ++i) {
70            swappedValues[i] = Long.reverseBytes(values[i]);
71        }
72
73        int scale = SizeOf.LONG;
74        VMRuntime runtime = VMRuntime.getRuntime();
75        byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, scale * values.length + 1);
76        long base_ptr = runtime.addressOf(array);
77
78        for (int ptr_offset = 0; ptr_offset < 2; ++ptr_offset) {
79            long ptr = base_ptr + ptr_offset; // To test aligned and unaligned accesses.
80            Arrays.fill(array, (byte) 0);
81
82            // Regular copy.
83            Memory.pokeLongArray(ptr, values, 0, values.length, false);
84            assertLongsEqual(values, ptr, false);
85            assertLongsEqual(swappedValues, ptr, true);
86
87            // Swapped copy.
88            Memory.pokeLongArray(ptr, values, 0, values.length, true);
89            assertLongsEqual(values, ptr, true);
90            assertLongsEqual(swappedValues, ptr, false);
91
92            // Swapped copies of slices (to ensure we test non-zero offsets).
93            for (int i = 0; i < values.length; ++i) {
94                Memory.pokeLongArray(ptr + i * scale, values, i, 1, true);
95            }
96            assertLongsEqual(values, ptr, true);
97            assertLongsEqual(swappedValues, ptr, false);
98        }
99    }
100
101    private void assertLongsEqual(long[] expectedValues, long ptr, boolean swap) {
102      for (int i = 0; i < expectedValues.length; ++i) {
103        assertEquals(expectedValues[i], Memory.peekLong(ptr + SizeOf.LONG * i, swap));
104      }
105    }
106
107    public void testSetShortArray() {
108        short[] values = { 0x0001, 0x0020, 0x0300, 0x4000 };
109        short[] swappedValues = { 0x0100, 0x2000, 0x0003, 0x0040 };
110
111        int scale = SizeOf.SHORT;
112        VMRuntime runtime = VMRuntime.getRuntime();
113        byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, scale * values.length + 1);
114        long base_ptr = runtime.addressOf(array);
115
116        for (int ptr_offset = 0; ptr_offset < 2; ++ptr_offset) {
117            long ptr = base_ptr + ptr_offset; // To test aligned and unaligned accesses.
118            Arrays.fill(array, (byte) 0);
119
120            // Regular copy.
121            Memory.pokeShortArray(ptr, values, 0, values.length, false);
122            assertShortsEqual(values, ptr, false);
123            assertShortsEqual(swappedValues, ptr, true);
124
125            // Swapped copy.
126            Memory.pokeShortArray(ptr, values, 0, values.length, true);
127            assertShortsEqual(values, ptr, true);
128            assertShortsEqual(swappedValues, ptr, false);
129
130            // Swapped copies of slices (to ensure we test non-zero offsets).
131            for (int i = 0; i < values.length; ++i) {
132                Memory.pokeShortArray(ptr + i * scale, values, i, 1, true);
133            }
134            assertShortsEqual(values, ptr, true);
135            assertShortsEqual(swappedValues, ptr, false);
136        }
137    }
138
139    private void assertShortsEqual(short[] expectedValues, long ptr, boolean swap) {
140        for (int i = 0; i < expectedValues.length; ++i) {
141            assertEquals(expectedValues[i], Memory.peekShort(ptr + SizeOf.SHORT * i, swap));
142        }
143    }
144}
145