test_align.cpp revision 13249:a2753984d2c1
1153838Sdfr/*
2153838Sdfr * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3153838Sdfr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4153838Sdfr *
5153838Sdfr * This code is free software; you can redistribute it and/or modify it
6153838Sdfr * under the terms of the GNU General Public License version 2 only, as
7153838Sdfr * published by the Free Software Foundation.
8153838Sdfr *
9153838Sdfr * This code is distributed in the hope that it will be useful, but WITHOUT
10153838Sdfr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11153838Sdfr * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12153838Sdfr * version 2 for more details (a copy is included in the LICENSE file that
13153838Sdfr * accompanied this code).
14153838Sdfr *
15153838Sdfr * You should have received a copy of the GNU General Public License version
16153838Sdfr * 2 along with this work; if not, write to the Free Software Foundation,
17153838Sdfr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18153838Sdfr *
19153838Sdfr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20153838Sdfr * or visit www.oracle.com if you need additional information or have any
21153838Sdfr * questions.
22153838Sdfr */
23153838Sdfr
24153838Sdfr#include "precompiled.hpp"
25153838Sdfr#include "utilities/align.hpp"
26153838Sdfr#include "utilities/formatBuffer.hpp"
27153838Sdfr#include "utilities/globalDefinitions.hpp"
28153838Sdfr#include "unittest.hpp"
29153838Sdfr
30153838Sdfr#include <limits>
31153838Sdfr
32153838Sdfr// A few arbitrarily chosen values to test the align functions on.
33153838Sdfrstatic uint64_t values[] = {1, 3, 10, 345, 1023, 1024, 1025, 23909034, INT_MAX, uint64_t(-1) / 2, uint64_t(-1) / 2 + 100, -1 };
34153838Sdfr
35153838Sdfrtemplate <typename T>
36153838Sdfrstatic T max_alignment() {
37153838Sdfr  T max = std::numeric_limits<T>::max();
38153838Sdfr  return max ^ (max >> 1);
39153838Sdfr}
40153838Sdfr
41153838Sdfr#define log(...) SCOPED_TRACE(err_msg(__VA_ARGS__).buffer())
42
43template <typename T, typename A>
44static void test_alignments() {
45  log("### Test: %c" SIZE_FORMAT " " UINT64_FORMAT " : %c" SIZE_FORMAT " " UINT64_FORMAT " ###\n",
46      std::numeric_limits<T>::is_signed ? 's' : 'u', sizeof(T), (uint64_t)std::numeric_limits<T>::max(),
47      std::numeric_limits<A>::is_signed ? 's' : 'u', sizeof(A), (uint64_t)std::numeric_limits<A>::max());
48
49  // Test all possible alignment values that fit in type A.
50  for (A alignment = max_alignment<A>(); alignment > 0; alignment >>= 1) {
51    log("=== Alignment: " UINT64_FORMAT " ===\n", (uint64_t)alignment);
52
53    for (size_t i = 0; i < ARRAY_SIZE(values); i++) {
54      log("--- Value: " UINT64_FORMAT "\n", values[i]);
55
56      // Test align up
57      const uint64_t up = align_up_(values[i], (uint64_t)alignment);
58      if (0 < up && up <= (uint64_t)std::numeric_limits<T>::max()) {
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);
60
61        T value = T(values[i]);
62
63        // Check against uint64_t version
64        ASSERT_EQ(align_up((uint64_t)value, alignment), up);
65        // Check inline function vs macro
66        ASSERT_EQ(align_up(value, alignment), align_up_(value, alignment));
67        // Sanity check
68        ASSERT_GE(align_up(value, alignment), value);
69      }
70
71      // Test align down
72      const uint64_t down = align_down_(values[i], (uint64_t)alignment);
73      if (down <= (uint64_t)std::numeric_limits<T>::max()) {
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);
75
76        T value = T(values[i]);
77
78        // Check against uint64_t version
79        ASSERT_EQ((uint64_t)align_down(value, alignment), down);
80        // Check inline function vs macro
81        ASSERT_EQ(align_down(value, alignment), align_down_(value, alignment));
82        // Sanity check
83        ASSERT_LE(align_down(value, alignment), value);
84      }
85
86      // Test is aligned
87      const bool is = is_aligned_(values[i], (uint64_t)alignment);
88      if (values[i] <= (uint64_t)std::numeric_limits<T>::max()) {
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");
90
91        T value = T(values[i]);
92
93        // Check against uint64_t version
94        ASSERT_EQ(is_aligned(value, alignment), is);
95        // Check inline function vs macro
96        ASSERT_EQ(is_aligned(value, alignment), is_aligned_(value, alignment));
97      }
98    }
99  }
100}
101
102TEST(Align, functions_and_macros) {
103  // Test the alignment functions with different type combinations.
104
105  test_alignments<int64_t, uint8_t>();
106  test_alignments<int64_t, uint16_t>();
107  test_alignments<int64_t, uint32_t>();
108  test_alignments<int64_t, int8_t>();
109  test_alignments<int64_t, int16_t>();
110  test_alignments<int64_t, int32_t>();
111  test_alignments<int64_t, int64_t>();
112
113  test_alignments<uint32_t, uint8_t>();
114  test_alignments<uint32_t, uint16_t>();
115  test_alignments<uint32_t, uint32_t>();
116  test_alignments<uint32_t, int8_t>();
117  test_alignments<uint32_t, int16_t>();
118  test_alignments<uint32_t, int32_t>();
119
120  test_alignments<int32_t, uint8_t>();
121  test_alignments<int32_t, uint16_t>();
122  test_alignments<int32_t, int8_t>();
123  test_alignments<int32_t, int16_t>();
124  test_alignments<int32_t, int32_t>();
125
126  test_alignments<uint16_t, uint8_t>();
127  test_alignments<uint16_t, uint16_t>();
128  test_alignments<uint16_t, int8_t>();
129  test_alignments<uint16_t, int16_t>();
130
131  test_alignments<int16_t, uint8_t>();
132  test_alignments<int16_t, int8_t>();
133  test_alignments<int16_t, int16_t>();
134
135  test_alignments<uint8_t, int8_t>();
136  test_alignments<uint8_t, uint8_t>();
137
138  test_alignments<int8_t, int8_t>();
139}
140