reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
//===- llvm/unittest/Support/NativeFormatTests.cpp - formatting tests -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/SmallString.h"
#include "llvm/Support/NativeFormatting.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"

#include <type_traits>

using namespace llvm;

namespace {

template <typename T> std::string format_number(T N, IntegerStyle Style) {
  std::string S;
  llvm::raw_string_ostream Str(S);
  write_integer(Str, N, 0, Style);
  Str.flush();
  return S;
}

std::string format_number(uint64_t N, HexPrintStyle Style,
                          Optional<size_t> Width = None) {
  std::string S;
  llvm::raw_string_ostream Str(S);
  write_hex(Str, N, Style, Width);
  Str.flush();
  return S;
}

std::string format_number(double D, FloatStyle Style,
                          Optional<size_t> Precision = None) {
  std::string S;
  llvm::raw_string_ostream Str(S);
  write_double(Str, D, Style, Precision);
  Str.flush();
  return S;
}

// Test basic number formatting with various styles and default width and
// precision.
TEST(NativeFormatTest, BasicIntegerTests) {
  // Simple integers with no decimal.
  EXPECT_EQ("0", format_number(0, IntegerStyle::Integer));
  EXPECT_EQ("2425", format_number(2425, IntegerStyle::Integer));
  EXPECT_EQ("-2425", format_number(-2425, IntegerStyle::Integer));

  EXPECT_EQ("0", format_number(0LL, IntegerStyle::Integer));
  EXPECT_EQ("257257257235709",
            format_number(257257257235709LL, IntegerStyle::Integer));
  EXPECT_EQ("-257257257235709",
            format_number(-257257257235709LL, IntegerStyle::Integer));

  // Number formatting.
  EXPECT_EQ("0", format_number(0, IntegerStyle::Number));
  EXPECT_EQ("2,425", format_number(2425, IntegerStyle::Number));
  EXPECT_EQ("-2,425", format_number(-2425, IntegerStyle::Number));
  EXPECT_EQ("257,257,257,235,709",
            format_number(257257257235709LL, IntegerStyle::Number));
  EXPECT_EQ("-257,257,257,235,709",
            format_number(-257257257235709LL, IntegerStyle::Number));

  // Hex formatting.
  // lower case, prefix.
  EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixLower));
  EXPECT_EQ("0xbeef", format_number(0xbeefLL, HexPrintStyle::PrefixLower));
  EXPECT_EQ("0xdeadbeef",
            format_number(0xdeadbeefLL, HexPrintStyle::PrefixLower));

  // upper-case, prefix.
  EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixUpper));
  EXPECT_EQ("0xBEEF", format_number(0xbeefLL, HexPrintStyle::PrefixUpper));
  EXPECT_EQ("0xDEADBEEF",
            format_number(0xdeadbeefLL, HexPrintStyle::PrefixUpper));

  // lower-case, no prefix
  EXPECT_EQ("0", format_number(0, HexPrintStyle::Lower));
  EXPECT_EQ("beef", format_number(0xbeefLL, HexPrintStyle::Lower));
  EXPECT_EQ("deadbeef", format_number(0xdeadbeefLL, HexPrintStyle::Lower));

  // upper-case, no prefix.
  EXPECT_EQ("0", format_number(0, HexPrintStyle::Upper));
  EXPECT_EQ("BEEF", format_number(0xbeef, HexPrintStyle::Upper));
  EXPECT_EQ("DEADBEEF", format_number(0xdeadbeef, HexPrintStyle::Upper));
}

// Test basic floating point formatting with various styles and default width
// and precision.
TEST(NativeFormatTest, BasicFloatingPointTests) {
  // Double
  EXPECT_EQ("0.000000e+00", format_number(0.0, FloatStyle::Exponent));
  EXPECT_EQ("-0.000000e+00", format_number(-0.0, FloatStyle::Exponent));
  EXPECT_EQ("1.100000e+00", format_number(1.1, FloatStyle::Exponent));
  EXPECT_EQ("1.100000E+00", format_number(1.1, FloatStyle::ExponentUpper));

  // Default precision is 2 for floating points.
  EXPECT_EQ("1.10", format_number(1.1, FloatStyle::Fixed));
  EXPECT_EQ("1.34", format_number(1.34, FloatStyle::Fixed));
  EXPECT_EQ("1.34", format_number(1.344, FloatStyle::Fixed));
  EXPECT_EQ("1.35", format_number(1.346, FloatStyle::Fixed));
}

// Test common boundary cases and min/max conditions.
TEST(NativeFormatTest, BoundaryTests) {
  // Min and max.
  EXPECT_EQ("18446744073709551615",
            format_number(UINT64_MAX, IntegerStyle::Integer));

  EXPECT_EQ("9223372036854775807",
            format_number(INT64_MAX, IntegerStyle::Integer));
  EXPECT_EQ("-9223372036854775808",
            format_number(INT64_MIN, IntegerStyle::Integer));

  EXPECT_EQ("4294967295", format_number(UINT32_MAX, IntegerStyle::Integer));
  EXPECT_EQ("2147483647", format_number(INT32_MAX, IntegerStyle::Integer));
  EXPECT_EQ("-2147483648", format_number(INT32_MIN, IntegerStyle::Integer));

  EXPECT_EQ("nan", format_number(std::numeric_limits<double>::quiet_NaN(),
                                 FloatStyle::Fixed));
  EXPECT_EQ("INF", format_number(std::numeric_limits<double>::infinity(),
                                 FloatStyle::Fixed));
}

TEST(NativeFormatTest, HexTests) {
  // Test hex formatting with different widths and precisions.

  // Width less than the value should print the full value anyway.
  EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixLower, 0));
  EXPECT_EQ("0xabcde", format_number(0xABCDE, HexPrintStyle::PrefixLower, 3));

  // Precision greater than the value should pad with 0s.
  // TODO: The prefix should not be counted in the precision.  But unfortunately
  // it is and we have to live with it unless we fix all existing users of
  // prefixed hex formatting.
  EXPECT_EQ("0x000", format_number(0, HexPrintStyle::PrefixLower, 5));
  EXPECT_EQ("0x0abcde", format_number(0xABCDE, HexPrintStyle::PrefixLower, 8));

  EXPECT_EQ("00000", format_number(0, HexPrintStyle::Lower, 5));
  EXPECT_EQ("000abcde", format_number(0xABCDE, HexPrintStyle::Lower, 8));

  // Try printing more digits than can fit in a uint64.
  EXPECT_EQ("0x00000000000000abcde",
            format_number(0xABCDE, HexPrintStyle::PrefixLower, 21));
}

TEST(NativeFormatTest, IntegerTests) {
  EXPECT_EQ("-10", format_number(-10, IntegerStyle::Integer));
  EXPECT_EQ("-100", format_number(-100, IntegerStyle::Integer));
  EXPECT_EQ("-1000", format_number(-1000, IntegerStyle::Integer));
  EXPECT_EQ("-1234567890", format_number(-1234567890, IntegerStyle::Integer));
  EXPECT_EQ("10", format_number(10, IntegerStyle::Integer));
  EXPECT_EQ("100", format_number(100, IntegerStyle::Integer));
  EXPECT_EQ("1000", format_number(1000, IntegerStyle::Integer));
  EXPECT_EQ("1234567890", format_number(1234567890, IntegerStyle::Integer));
}

TEST(NativeFormatTest, CommaTests) {
  EXPECT_EQ("0", format_number(0, IntegerStyle::Number));
  EXPECT_EQ("10", format_number(10, IntegerStyle::Number));
  EXPECT_EQ("100", format_number(100, IntegerStyle::Number));
  EXPECT_EQ("1,000", format_number(1000, IntegerStyle::Number));
  EXPECT_EQ("1,234,567,890", format_number(1234567890, IntegerStyle::Number));

  EXPECT_EQ("-10", format_number(-10, IntegerStyle::Number));
  EXPECT_EQ("-100", format_number(-100, IntegerStyle::Number));
  EXPECT_EQ("-1,000", format_number(-1000, IntegerStyle::Number));
  EXPECT_EQ("-1,234,567,890", format_number(-1234567890, IntegerStyle::Number));
}
}