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