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 
21 namespace art {
22 
TEST(MemoryRegion,LoadUnaligned)23 TEST(MemoryRegion, LoadUnaligned) {
24   const size_t n = 8;
25   uint8_t data[n] = { 0, 1, 2, 3, 4, 5, 6, 7 };
26   MemoryRegion region(&data, n);
27 
28   ASSERT_EQ(0, region.LoadUnaligned<char>(0));
29   ASSERT_EQ(1u
30             + (2u << kBitsPerByte)
31             + (3u << 2 * kBitsPerByte)
32             + (4u << 3 * kBitsPerByte),
33             region.LoadUnaligned<uint32_t>(1));
34   ASSERT_EQ(5 + (6 << kBitsPerByte), region.LoadUnaligned<int16_t>(5));
35   ASSERT_EQ(7u, region.LoadUnaligned<unsigned char>(7));
36 }
37 
TEST(MemoryRegion,StoreUnaligned)38 TEST(MemoryRegion, StoreUnaligned) {
39   const size_t n = 8;
40   uint8_t data[n] = { 0, 0, 0, 0, 0, 0, 0, 0 };
41   MemoryRegion region(&data, n);
42 
43   region.StoreUnaligned<unsigned char>(0u, 7);
44   region.StoreUnaligned<int16_t>(1, 6 + (5 << kBitsPerByte));
45   region.StoreUnaligned<uint32_t>(3,
46                                   4u
47                                   + (3u << kBitsPerByte)
48                                   + (2u << 2 * kBitsPerByte)
49                                   + (1u << 3 * kBitsPerByte));
50   region.StoreUnaligned<char>(7, 0);
51 
52   uint8_t expected[n] = { 7, 6, 5, 4, 3, 2, 1, 0 };
53   for (size_t i = 0; i < n; ++i) {
54     ASSERT_EQ(expected[i], data[i]);
55   }
56 }
57 
58 }  // namespace art
59