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