Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2017 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 <gtest/gtest.h>
     18 #include <util.h>
     19 
     20 TEST(benchmark, memory_align) {
     21   std::vector<char> buf(100);
     22   for (size_t alignment = 1; alignment <= 32; alignment *= 2) {
     23     for (size_t or_mask = 0; or_mask < alignment; ++or_mask) {
     24       uintptr_t aligned_ptr = reinterpret_cast<uintptr_t>(GetAlignedMemory(buf.data(), alignment,
     25                                                                            or_mask));
     26       ASSERT_EQ(aligned_ptr % alignment, or_mask);
     27       ASSERT_EQ(aligned_ptr & alignment, alignment);
     28     }
     29   }
     30 }
     31 
     32 TEST(benchmark, ptr_align) {
     33   std::vector<char> buf;
     34   for (size_t alignment = 1; alignment <= 2048; alignment *= 2) {
     35     uintptr_t aligned_ptr = reinterpret_cast<uintptr_t>(GetAlignedPtr(&buf, alignment, 100));
     36     ASSERT_EQ(aligned_ptr & alignment, alignment);
     37     ASSERT_EQ(aligned_ptr & (alignment - 1), 0u);
     38   }
     39 }
     40