test/hotspot/gtest/utilities/test_align.cpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46625 hotspot/test/native/utilities/test_align.cpp@edefffab74e2
child 49167 ba9da6aaae36
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
     1
/*
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
     4
 *
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
     8
 *
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    13
 * accompanied this code).
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    14
 *
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    18
 *
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    21
 * questions.
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    22
 */
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    23
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    24
#include "precompiled.hpp"
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents: 46619
diff changeset
    25
#include "utilities/align.hpp"
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    26
#include "utilities/formatBuffer.hpp"
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    27
#include "utilities/globalDefinitions.hpp"
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    28
#include "unittest.hpp"
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    29
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    30
#include <limits>
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    31
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    32
// A few arbitrarily chosen values to test the align functions on.
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    33
static uint64_t values[] = {1, 3, 10, 345, 1023, 1024, 1025, 23909034, INT_MAX, uint64_t(-1) / 2, uint64_t(-1) / 2 + 100, -1 };
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    34
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    35
template <typename T>
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    36
static T max_alignment() {
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    37
  T max = std::numeric_limits<T>::max();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    38
  return max ^ (max >> 1);
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    39
}
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    40
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    41
#define log(...) SCOPED_TRACE(err_msg(__VA_ARGS__).buffer())
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    42
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    43
template <typename T, typename A>
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    44
static void test_alignments() {
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    45
  log("### Test: %c" SIZE_FORMAT " " UINT64_FORMAT " : %c" SIZE_FORMAT " " UINT64_FORMAT " ###\n",
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    46
      std::numeric_limits<T>::is_signed ? 's' : 'u', sizeof(T), (uint64_t)std::numeric_limits<T>::max(),
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    47
      std::numeric_limits<A>::is_signed ? 's' : 'u', sizeof(A), (uint64_t)std::numeric_limits<A>::max());
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    48
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    49
  // Test all possible alignment values that fit in type A.
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    50
  for (A alignment = max_alignment<A>(); alignment > 0; alignment >>= 1) {
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    51
    log("=== Alignment: " UINT64_FORMAT " ===\n", (uint64_t)alignment);
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    52
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    53
    for (size_t i = 0; i < ARRAY_SIZE(values); i++) {
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    54
      log("--- Value: " UINT64_FORMAT "\n", values[i]);
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    55
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    56
      // Test align up
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    57
      const uint64_t up = align_up_(values[i], (uint64_t)alignment);
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    58
      if (0 < up && up <= (uint64_t)std::numeric_limits<T>::max()) {
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    59
        log("Testing align_up:   alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: 0x" UINT64_FORMAT_X "\n", (uint64_t)alignment, values[i], up);
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    60
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    61
        T value = T(values[i]);
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    62
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    63
        // Check against uint64_t version
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    64
        ASSERT_EQ(align_up((uint64_t)value, alignment), up);
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    65
        // Check inline function vs macro
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    66
        ASSERT_EQ(align_up(value, alignment), align_up_(value, alignment));
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    67
        // Sanity check
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    68
        ASSERT_GE(align_up(value, alignment), value);
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    69
      }
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    70
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    71
      // Test align down
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    72
      const uint64_t down = align_down_(values[i], (uint64_t)alignment);
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    73
      if (down <= (uint64_t)std::numeric_limits<T>::max()) {
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    74
        log("Testing align_down: alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: 0x" UINT64_FORMAT_X "\n", (uint64_t)alignment, values[i], down);
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    75
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    76
        T value = T(values[i]);
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    77
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    78
        // Check against uint64_t version
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    79
        ASSERT_EQ((uint64_t)align_down(value, alignment), down);
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    80
        // Check inline function vs macro
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    81
        ASSERT_EQ(align_down(value, alignment), align_down_(value, alignment));
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    82
        // Sanity check
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    83
        ASSERT_LE(align_down(value, alignment), value);
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    84
      }
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    85
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    86
      // Test is aligned
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    87
      const bool is = is_aligned_(values[i], (uint64_t)alignment);
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    88
      if (values[i] <= (uint64_t)std::numeric_limits<T>::max()) {
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    89
        log("Testing is_aligned: alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: %s\n", (uint64_t)alignment, values[i], is ? "true" : "false");
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    90
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    91
        T value = T(values[i]);
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    92
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    93
        // Check against uint64_t version
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    94
        ASSERT_EQ(is_aligned(value, alignment), is);
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    95
        // Check inline function vs macro
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46618
diff changeset
    96
        ASSERT_EQ(is_aligned(value, alignment), is_aligned_(value, alignment));
46617
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    97
      }
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    98
    }
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
    99
  }
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   100
}
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   101
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   102
TEST(Align, functions_and_macros) {
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   103
  // Test the alignment functions with different type combinations.
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   104
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   105
  test_alignments<int64_t, uint8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   106
  test_alignments<int64_t, uint16_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   107
  test_alignments<int64_t, uint32_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   108
  test_alignments<int64_t, int8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   109
  test_alignments<int64_t, int16_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   110
  test_alignments<int64_t, int32_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   111
  test_alignments<int64_t, int64_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   112
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   113
  test_alignments<uint32_t, uint8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   114
  test_alignments<uint32_t, uint16_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   115
  test_alignments<uint32_t, uint32_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   116
  test_alignments<uint32_t, int8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   117
  test_alignments<uint32_t, int16_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   118
  test_alignments<uint32_t, int32_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   119
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   120
  test_alignments<int32_t, uint8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   121
  test_alignments<int32_t, uint16_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   122
  test_alignments<int32_t, int8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   123
  test_alignments<int32_t, int16_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   124
  test_alignments<int32_t, int32_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   125
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   126
  test_alignments<uint16_t, uint8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   127
  test_alignments<uint16_t, uint16_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   128
  test_alignments<uint16_t, int8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   129
  test_alignments<uint16_t, int16_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   130
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   131
  test_alignments<int16_t, uint8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   132
  test_alignments<int16_t, int8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   133
  test_alignments<int16_t, int16_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   134
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   135
  test_alignments<uint8_t, int8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   136
  test_alignments<uint8_t, uint8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   137
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   138
  test_alignments<int8_t, int8_t>();
0330c5fc49ce 8178495: Bug in the align_size_up_ macro
stefank
parents:
diff changeset
   139
}