< prev index next >

test/native/utilities/test_align.cpp

Print this page




  36   T max = std::numeric_limits<T>::max();
  37   return max ^ (max >> 1);
  38 }
  39 
  40 #define log(...) SCOPED_TRACE(err_msg(__VA_ARGS__))
  41 
  42 template <typename T, typename A>
  43 static void test_alignments() {
  44   log("### Test: %c" SIZE_FORMAT " " UINT64_FORMAT " : %c" SIZE_FORMAT " " UINT64_FORMAT " ###\n",
  45       std::numeric_limits<T>::is_signed ? 's' : 'u', sizeof(T), (uint64_t)std::numeric_limits<T>::max(),
  46       std::numeric_limits<A>::is_signed ? 's' : 'u', sizeof(A), (uint64_t)std::numeric_limits<A>::max());
  47 
  48   // Test all possible alignment values that fit in type A.
  49   for (A alignment = max_alignment<A>(); alignment > 0; alignment >>= 1) {
  50     log("=== Alignment: " UINT64_FORMAT " ===\n", (uint64_t)alignment);
  51 
  52     for (size_t i = 0; i < ARRAY_SIZE(values); i++) {
  53       log("--- Value: " UINT64_FORMAT "\n", values[i]);
  54 
  55       // Test align up
  56       const uint64_t up = align_size_up_(values[i], (uint64_t)alignment);
  57       if (0 < up && up <= (uint64_t)std::numeric_limits<T>::max()) {
  58         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);
  59 
  60         T value = T(values[i]);
  61 
  62         // Check against uint64_t version
  63         ASSERT_EQ(align_size_up((uint64_t)value, alignment), up);
  64         // Check inline function vs macro
  65         ASSERT_EQ(align_size_up(value, alignment), align_size_up_(value, alignment));
  66         // Sanity check
  67         ASSERT_GE(align_size_up(value, alignment), value);
  68       }
  69 
  70       // Test align down
  71       const uint64_t down = align_size_down_(values[i], (uint64_t)alignment);
  72       if (down <= (uint64_t)std::numeric_limits<T>::max()) {
  73         log("Testing align_size_down: alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: 0x" UINT64_FORMAT_X "\n", (uint64_t)alignment, values[i], down);
  74 
  75         T value = T(values[i]);
  76 
  77         // Check against uint64_t version
  78         ASSERT_EQ((uint64_t)align_size_down(value, alignment), down);
  79         // Check inline function vs macro
  80         ASSERT_EQ(align_size_down(value, alignment), align_size_down_(value, alignment));
  81         // Sanity check
  82         ASSERT_LE(align_size_down(value, alignment), value);
  83       }
  84 
  85       // Test is aligned
  86       const bool is = is_size_aligned_(values[i], (uint64_t)alignment);
  87       if (values[i] <= (uint64_t)std::numeric_limits<T>::max()) {
  88         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");
  89 
  90         T value = T(values[i]);
  91 
  92         // Check against uint64_t version
  93         ASSERT_EQ(is_size_aligned(value, alignment), is);
  94         // Check inline function vs macro
  95         ASSERT_EQ(is_size_aligned(value, alignment), is_size_aligned_(value, alignment));
  96       }
  97     }
  98   }
  99 }
 100 
 101 TEST(Align, functions_and_macros) {
 102   // Test the alignment functions with different type combinations.
 103 
 104   test_alignments<int64_t, uint8_t>();
 105   test_alignments<int64_t, uint16_t>();
 106   test_alignments<int64_t, uint32_t>();
 107   test_alignments<int64_t, int8_t>();
 108   test_alignments<int64_t, int16_t>();
 109   test_alignments<int64_t, int32_t>();
 110   test_alignments<int64_t, int64_t>();
 111 
 112   test_alignments<uint32_t, uint8_t>();
 113   test_alignments<uint32_t, uint16_t>();
 114   test_alignments<uint32_t, uint32_t>();
 115   test_alignments<uint32_t, int8_t>();




  36   T max = std::numeric_limits<T>::max();
  37   return max ^ (max >> 1);
  38 }
  39 
  40 #define log(...) SCOPED_TRACE(err_msg(__VA_ARGS__))
  41 
  42 template <typename T, typename A>
  43 static void test_alignments() {
  44   log("### Test: %c" SIZE_FORMAT " " UINT64_FORMAT " : %c" SIZE_FORMAT " " UINT64_FORMAT " ###\n",
  45       std::numeric_limits<T>::is_signed ? 's' : 'u', sizeof(T), (uint64_t)std::numeric_limits<T>::max(),
  46       std::numeric_limits<A>::is_signed ? 's' : 'u', sizeof(A), (uint64_t)std::numeric_limits<A>::max());
  47 
  48   // Test all possible alignment values that fit in type A.
  49   for (A alignment = max_alignment<A>(); alignment > 0; alignment >>= 1) {
  50     log("=== Alignment: " UINT64_FORMAT " ===\n", (uint64_t)alignment);
  51 
  52     for (size_t i = 0; i < ARRAY_SIZE(values); i++) {
  53       log("--- Value: " UINT64_FORMAT "\n", values[i]);
  54 
  55       // Test align up
  56       const uint64_t up = align_up_(values[i], (uint64_t)alignment);
  57       if (0 < up && up <= (uint64_t)std::numeric_limits<T>::max()) {
  58         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);
  59 
  60         T value = T(values[i]);
  61 
  62         // Check against uint64_t version
  63         ASSERT_EQ(align_up((uint64_t)value, alignment), up);
  64         // Check inline function vs macro
  65         ASSERT_EQ(align_up(value, alignment), align_up_(value, alignment));
  66         // Sanity check
  67         ASSERT_GE(align_up(value, alignment), value);
  68       }
  69 
  70       // Test align down
  71       const uint64_t down = align_down_(values[i], (uint64_t)alignment);
  72       if (down <= (uint64_t)std::numeric_limits<T>::max()) {
  73         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);
  74 
  75         T value = T(values[i]);
  76 
  77         // Check against uint64_t version
  78         ASSERT_EQ((uint64_t)align_down(value, alignment), down);
  79         // Check inline function vs macro
  80         ASSERT_EQ(align_down(value, alignment), align_down_(value, alignment));
  81         // Sanity check
  82         ASSERT_LE(align_down(value, alignment), value);
  83       }
  84 
  85       // Test is aligned
  86       const bool is = is_aligned_(values[i], (uint64_t)alignment);
  87       if (values[i] <= (uint64_t)std::numeric_limits<T>::max()) {
  88         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");
  89 
  90         T value = T(values[i]);
  91 
  92         // Check against uint64_t version
  93         ASSERT_EQ(is_aligned(value, alignment), is);
  94         // Check inline function vs macro
  95         ASSERT_EQ(is_aligned(value, alignment), is_aligned_(value, alignment));
  96       }
  97     }
  98   }
  99 }
 100 
 101 TEST(Align, functions_and_macros) {
 102   // Test the alignment functions with different type combinations.
 103 
 104   test_alignments<int64_t, uint8_t>();
 105   test_alignments<int64_t, uint16_t>();
 106   test_alignments<int64_t, uint32_t>();
 107   test_alignments<int64_t, int8_t>();
 108   test_alignments<int64_t, int16_t>();
 109   test_alignments<int64_t, int32_t>();
 110   test_alignments<int64_t, int64_t>();
 111 
 112   test_alignments<uint32_t, uint8_t>();
 113   test_alignments<uint32_t, uint16_t>();
 114   test_alignments<uint32_t, uint32_t>();
 115   test_alignments<uint32_t, int8_t>();


< prev index next >