Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / Support / NativeFormatTests.cpp
blob11753068b0fa2c9075deb72b417bce6382a66706
1 //===- llvm/unittest/Support/NativeFormatTests.cpp - formatting tests -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/NativeFormatting.h"
10 #include "llvm/Support/raw_ostream.h"
11 #include "gtest/gtest.h"
13 #include <type_traits>
15 using namespace llvm;
17 namespace {
19 template <typename T> std::string format_number(T N, IntegerStyle Style) {
20 std::string S;
21 llvm::raw_string_ostream Str(S);
22 write_integer(Str, N, 0, Style);
23 Str.flush();
24 return S;
27 std::string format_number(uint64_t N, HexPrintStyle Style,
28 std::optional<size_t> Width = std::nullopt) {
29 std::string S;
30 llvm::raw_string_ostream Str(S);
31 write_hex(Str, N, Style, Width);
32 Str.flush();
33 return S;
36 std::string format_number(double D, FloatStyle Style,
37 std::optional<size_t> Precision = std::nullopt) {
38 std::string S;
39 llvm::raw_string_ostream Str(S);
40 write_double(Str, D, Style, Precision);
41 Str.flush();
42 return S;
45 // Test basic number formatting with various styles and default width and
46 // precision.
47 TEST(NativeFormatTest, BasicIntegerTests) {
48 // Simple integers with no decimal.
49 EXPECT_EQ("0", format_number(0, IntegerStyle::Integer));
50 EXPECT_EQ("1", format_number(1, IntegerStyle::Integer));
51 EXPECT_EQ("-1", format_number(-1, IntegerStyle::Integer));
52 EXPECT_EQ("2425", format_number(2425, IntegerStyle::Integer));
53 EXPECT_EQ("-2425", format_number(-2425, IntegerStyle::Integer));
55 EXPECT_EQ("0", format_number(0LL, IntegerStyle::Integer));
56 EXPECT_EQ("257257257235709",
57 format_number(257257257235709LL, IntegerStyle::Integer));
58 EXPECT_EQ("-257257257235709",
59 format_number(-257257257235709LL, IntegerStyle::Integer));
61 // Number formatting.
62 EXPECT_EQ("0", format_number(0, IntegerStyle::Number));
63 EXPECT_EQ("2,425", format_number(2425, IntegerStyle::Number));
64 EXPECT_EQ("-2,425", format_number(-2425, IntegerStyle::Number));
65 EXPECT_EQ("257,257,257,235,709",
66 format_number(257257257235709LL, IntegerStyle::Number));
67 EXPECT_EQ("-257,257,257,235,709",
68 format_number(-257257257235709LL, IntegerStyle::Number));
70 // Hex formatting.
71 // lower case, prefix.
72 EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixLower));
73 EXPECT_EQ("0xbeef", format_number(0xbeefLL, HexPrintStyle::PrefixLower));
74 EXPECT_EQ("0xdeadbeef",
75 format_number(0xdeadbeefLL, HexPrintStyle::PrefixLower));
77 // upper-case, prefix.
78 EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixUpper));
79 EXPECT_EQ("0xBEEF", format_number(0xbeefLL, HexPrintStyle::PrefixUpper));
80 EXPECT_EQ("0xDEADBEEF",
81 format_number(0xdeadbeefLL, HexPrintStyle::PrefixUpper));
83 // lower-case, no prefix
84 EXPECT_EQ("0", format_number(0, HexPrintStyle::Lower));
85 EXPECT_EQ("beef", format_number(0xbeefLL, HexPrintStyle::Lower));
86 EXPECT_EQ("deadbeef", format_number(0xdeadbeefLL, HexPrintStyle::Lower));
88 // upper-case, no prefix.
89 EXPECT_EQ("0", format_number(0, HexPrintStyle::Upper));
90 EXPECT_EQ("BEEF", format_number(0xbeef, HexPrintStyle::Upper));
91 EXPECT_EQ("DEADBEEF", format_number(0xdeadbeef, HexPrintStyle::Upper));
94 // Test basic floating point formatting with various styles and default width
95 // and precision.
96 TEST(NativeFormatTest, BasicFloatingPointTests) {
97 // Double
98 EXPECT_EQ("0.000000e+00", format_number(0.0, FloatStyle::Exponent));
99 EXPECT_EQ("-0.000000e+00", format_number(-0.0, FloatStyle::Exponent));
100 EXPECT_EQ("1.100000e+00", format_number(1.1, FloatStyle::Exponent));
101 EXPECT_EQ("1.100000E+00", format_number(1.1, FloatStyle::ExponentUpper));
103 // Default precision is 2 for floating points.
104 EXPECT_EQ("1.10", format_number(1.1, FloatStyle::Fixed));
105 EXPECT_EQ("1.34", format_number(1.34, FloatStyle::Fixed));
106 EXPECT_EQ("1.34", format_number(1.344, FloatStyle::Fixed));
107 EXPECT_EQ("1.35", format_number(1.346, FloatStyle::Fixed));
110 // Test common boundary cases and min/max conditions.
111 TEST(NativeFormatTest, BoundaryTests) {
112 // Min and max.
113 EXPECT_EQ("18446744073709551615",
114 format_number(UINT64_MAX, IntegerStyle::Integer));
116 EXPECT_EQ("9223372036854775807",
117 format_number(INT64_MAX, IntegerStyle::Integer));
118 EXPECT_EQ("-9223372036854775808",
119 format_number(INT64_MIN, IntegerStyle::Integer));
121 EXPECT_EQ("4294967295", format_number(UINT32_MAX, IntegerStyle::Integer));
122 EXPECT_EQ("2147483647", format_number(INT32_MAX, IntegerStyle::Integer));
123 EXPECT_EQ("-2147483648", format_number(INT32_MIN, IntegerStyle::Integer));
125 EXPECT_EQ("nan", format_number(std::numeric_limits<double>::quiet_NaN(),
126 FloatStyle::Fixed));
127 EXPECT_EQ("INF", format_number(std::numeric_limits<double>::infinity(),
128 FloatStyle::Fixed));
129 EXPECT_EQ("-INF", format_number(-std::numeric_limits<double>::infinity(),
130 FloatStyle::Fixed));
133 TEST(NativeFormatTest, HexTests) {
134 // Test hex formatting with different widths and precisions.
136 // Width less than the value should print the full value anyway.
137 EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixLower, 0));
138 EXPECT_EQ("0xabcde", format_number(0xABCDE, HexPrintStyle::PrefixLower, 3));
140 // Precision greater than the value should pad with 0s.
141 // TODO: The prefix should not be counted in the precision. But unfortunately
142 // it is and we have to live with it unless we fix all existing users of
143 // prefixed hex formatting.
144 EXPECT_EQ("0x000", format_number(0, HexPrintStyle::PrefixLower, 5));
145 EXPECT_EQ("0x0abcde", format_number(0xABCDE, HexPrintStyle::PrefixLower, 8));
147 EXPECT_EQ("00000", format_number(0, HexPrintStyle::Lower, 5));
148 EXPECT_EQ("000abcde", format_number(0xABCDE, HexPrintStyle::Lower, 8));
150 // Try printing more digits than can fit in a uint64.
151 EXPECT_EQ("0x00000000000000abcde",
152 format_number(0xABCDE, HexPrintStyle::PrefixLower, 21));
155 TEST(NativeFormatTest, IntegerTests) {
156 EXPECT_EQ("-10", format_number(-10, IntegerStyle::Integer));
157 EXPECT_EQ("-100", format_number(-100, IntegerStyle::Integer));
158 EXPECT_EQ("-1000", format_number(-1000, IntegerStyle::Integer));
159 EXPECT_EQ("-1234567890", format_number(-1234567890, IntegerStyle::Integer));
160 EXPECT_EQ("10", format_number(10, IntegerStyle::Integer));
161 EXPECT_EQ("100", format_number(100, IntegerStyle::Integer));
162 EXPECT_EQ("1000", format_number(1000, IntegerStyle::Integer));
163 EXPECT_EQ("1234567890", format_number(1234567890, IntegerStyle::Integer));
166 TEST(NativeFormatTest, CommaTests) {
167 EXPECT_EQ("0", format_number(0, IntegerStyle::Number));
168 EXPECT_EQ("10", format_number(10, IntegerStyle::Number));
169 EXPECT_EQ("100", format_number(100, IntegerStyle::Number));
170 EXPECT_EQ("1,000", format_number(1000, IntegerStyle::Number));
171 EXPECT_EQ("1,234,567,890", format_number(1234567890, IntegerStyle::Number));
173 EXPECT_EQ("-10", format_number(-10, IntegerStyle::Number));
174 EXPECT_EQ("-100", format_number(-100, IntegerStyle::Number));
175 EXPECT_EQ("-1,000", format_number(-1000, IntegerStyle::Number));
176 EXPECT_EQ("-1,234,567,890", format_number(-1234567890, IntegerStyle::Number));