Home | History | Annotate | Download | only in runtime
      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 #include "bit_memory_region.h"
     18 #include "memory_region.h"
     19 
     20 #include "gtest/gtest.h"
     21 
     22 namespace art {
     23 
     24 TEST(MemoryRegion, LoadUnaligned) {
     25   const size_t n = 8;
     26   uint8_t data[n] = { 0, 1, 2, 3, 4, 5, 6, 7 };
     27   MemoryRegion region(&data, n);
     28 
     29   ASSERT_EQ(0, region.LoadUnaligned<char>(0));
     30   ASSERT_EQ(1u
     31             + (2u << kBitsPerByte)
     32             + (3u << 2 * kBitsPerByte)
     33             + (4u << 3 * kBitsPerByte),
     34             region.LoadUnaligned<uint32_t>(1));
     35   ASSERT_EQ(5 + (6 << kBitsPerByte), region.LoadUnaligned<int16_t>(5));
     36   ASSERT_EQ(7u, region.LoadUnaligned<unsigned char>(7));
     37 }
     38 
     39 TEST(MemoryRegion, StoreUnaligned) {
     40   const size_t n = 8;
     41   uint8_t data[n] = { 0, 0, 0, 0, 0, 0, 0, 0 };
     42   MemoryRegion region(&data, n);
     43 
     44   region.StoreUnaligned<unsigned char>(0u, 7);
     45   region.StoreUnaligned<int16_t>(1, 6 + (5 << kBitsPerByte));
     46   region.StoreUnaligned<uint32_t>(3,
     47                                   4u
     48                                   + (3u << kBitsPerByte)
     49                                   + (2u << 2 * kBitsPerByte)
     50                                   + (1u << 3 * kBitsPerByte));
     51   region.StoreUnaligned<char>(7, 0);
     52 
     53   uint8_t expected[n] = { 7, 6, 5, 4, 3, 2, 1, 0 };
     54   for (size_t i = 0; i < n; ++i) {
     55     ASSERT_EQ(expected[i], data[i]);
     56   }
     57 }
     58 
     59 TEST(MemoryRegion, TestBits) {
     60   const size_t n = 8;
     61   uint8_t data[n] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
     62   MemoryRegion region(&data, n);
     63   uint32_t value = 0xDEADBEEF;
     64   // Try various offsets and lengths.
     65   for (size_t bit_offset = 0; bit_offset < 2 * kBitsPerByte; ++bit_offset) {
     66     for (size_t length = 0; length < 2 * kBitsPerByte; ++length) {
     67       const uint32_t length_mask = (1 << length) - 1;
     68       uint32_t masked_value = value & length_mask;
     69       BitMemoryRegion bmr(region, bit_offset, length);
     70       region.StoreBits(bit_offset, masked_value, length);
     71       EXPECT_EQ(region.LoadBits(bit_offset, length), masked_value);
     72       EXPECT_EQ(bmr.LoadBits(0, length), masked_value);
     73       // Check adjacent bits to make sure they were not incorrectly cleared.
     74       EXPECT_EQ(region.LoadBits(0, bit_offset), (1u << bit_offset) - 1);
     75       EXPECT_EQ(region.LoadBits(bit_offset + length, length), length_mask);
     76       region.StoreBits(bit_offset, length_mask, length);
     77       // Store with bit memory region.
     78       bmr.StoreBits(0, masked_value, length);
     79       EXPECT_EQ(bmr.LoadBits(0, length), masked_value);
     80       // Check adjacent bits to make sure they were not incorrectly cleared.
     81       EXPECT_EQ(region.LoadBits(0, bit_offset), (1u << bit_offset) - 1);
     82       EXPECT_EQ(region.LoadBits(bit_offset + length, length), length_mask);
     83       region.StoreBits(bit_offset, length_mask, length);
     84       // Flip the value to try different edge bit combinations.
     85       value = ~value;
     86     }
     87   }
     88 }
     89 
     90 }  // namespace art
     91