1 //===- llvm/unittest/Support/NativeFormatTests.cpp - formatting tests -----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/SmallString.h"
10 #include "llvm/Support/NativeFormatting.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
14 #include <type_traits>
20 template <typename T
> std::string
format_number(T N
, IntegerStyle Style
) {
22 llvm::raw_string_ostream
Str(S
);
23 write_integer(Str
, N
, 0, Style
);
28 std::string
format_number(uint64_t N
, HexPrintStyle Style
,
29 Optional
<size_t> Width
= None
) {
31 llvm::raw_string_ostream
Str(S
);
32 write_hex(Str
, N
, Style
, Width
);
37 std::string
format_number(double D
, FloatStyle Style
,
38 Optional
<size_t> Precision
= None
) {
40 llvm::raw_string_ostream
Str(S
);
41 write_double(Str
, D
, Style
, Precision
);
46 // Test basic number formatting with various styles and default width and
48 TEST(NativeFormatTest
, BasicIntegerTests
) {
49 // Simple integers with no decimal.
50 EXPECT_EQ("0", format_number(0, IntegerStyle::Integer
));
51 EXPECT_EQ("2425", format_number(2425, IntegerStyle::Integer
));
52 EXPECT_EQ("-2425", format_number(-2425, IntegerStyle::Integer
));
54 EXPECT_EQ("0", format_number(0LL, IntegerStyle::Integer
));
55 EXPECT_EQ("257257257235709",
56 format_number(257257257235709LL, IntegerStyle::Integer
));
57 EXPECT_EQ("-257257257235709",
58 format_number(-257257257235709LL, IntegerStyle::Integer
));
61 EXPECT_EQ("0", format_number(0, IntegerStyle::Number
));
62 EXPECT_EQ("2,425", format_number(2425, IntegerStyle::Number
));
63 EXPECT_EQ("-2,425", format_number(-2425, IntegerStyle::Number
));
64 EXPECT_EQ("257,257,257,235,709",
65 format_number(257257257235709LL, IntegerStyle::Number
));
66 EXPECT_EQ("-257,257,257,235,709",
67 format_number(-257257257235709LL, IntegerStyle::Number
));
70 // lower case, prefix.
71 EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixLower
));
72 EXPECT_EQ("0xbeef", format_number(0xbeefLL
, HexPrintStyle::PrefixLower
));
73 EXPECT_EQ("0xdeadbeef",
74 format_number(0xdeadbeefLL
, HexPrintStyle::PrefixLower
));
76 // upper-case, prefix.
77 EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixUpper
));
78 EXPECT_EQ("0xBEEF", format_number(0xbeefLL
, HexPrintStyle::PrefixUpper
));
79 EXPECT_EQ("0xDEADBEEF",
80 format_number(0xdeadbeefLL
, HexPrintStyle::PrefixUpper
));
82 // lower-case, no prefix
83 EXPECT_EQ("0", format_number(0, HexPrintStyle::Lower
));
84 EXPECT_EQ("beef", format_number(0xbeefLL
, HexPrintStyle::Lower
));
85 EXPECT_EQ("deadbeef", format_number(0xdeadbeefLL
, HexPrintStyle::Lower
));
87 // upper-case, no prefix.
88 EXPECT_EQ("0", format_number(0, HexPrintStyle::Upper
));
89 EXPECT_EQ("BEEF", format_number(0xbeef, HexPrintStyle::Upper
));
90 EXPECT_EQ("DEADBEEF", format_number(0xdeadbeef, HexPrintStyle::Upper
));
93 // Test basic floating point formatting with various styles and default width
95 TEST(NativeFormatTest
, BasicFloatingPointTests
) {
97 EXPECT_EQ("0.000000e+00", format_number(0.0, FloatStyle::Exponent
));
98 EXPECT_EQ("-0.000000e+00", format_number(-0.0, FloatStyle::Exponent
));
99 EXPECT_EQ("1.100000e+00", format_number(1.1, FloatStyle::Exponent
));
100 EXPECT_EQ("1.100000E+00", format_number(1.1, FloatStyle::ExponentUpper
));
102 // Default precision is 2 for floating points.
103 EXPECT_EQ("1.10", format_number(1.1, FloatStyle::Fixed
));
104 EXPECT_EQ("1.34", format_number(1.34, FloatStyle::Fixed
));
105 EXPECT_EQ("1.34", format_number(1.344, FloatStyle::Fixed
));
106 EXPECT_EQ("1.35", format_number(1.346, FloatStyle::Fixed
));
109 // Test common boundary cases and min/max conditions.
110 TEST(NativeFormatTest
, BoundaryTests
) {
112 EXPECT_EQ("18446744073709551615",
113 format_number(UINT64_MAX
, IntegerStyle::Integer
));
115 EXPECT_EQ("9223372036854775807",
116 format_number(INT64_MAX
, IntegerStyle::Integer
));
117 EXPECT_EQ("-9223372036854775808",
118 format_number(INT64_MIN
, IntegerStyle::Integer
));
120 EXPECT_EQ("4294967295", format_number(UINT32_MAX
, IntegerStyle::Integer
));
121 EXPECT_EQ("2147483647", format_number(INT32_MAX
, IntegerStyle::Integer
));
122 EXPECT_EQ("-2147483648", format_number(INT32_MIN
, IntegerStyle::Integer
));
124 EXPECT_EQ("nan", format_number(std::numeric_limits
<double>::quiet_NaN(),
126 EXPECT_EQ("INF", format_number(std::numeric_limits
<double>::infinity(),
128 EXPECT_EQ("-INF", format_number(-std::numeric_limits
<double>::infinity(),
132 TEST(NativeFormatTest
, HexTests
) {
133 // Test hex formatting with different widths and precisions.
135 // Width less than the value should print the full value anyway.
136 EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixLower
, 0));
137 EXPECT_EQ("0xabcde", format_number(0xABCDE, HexPrintStyle::PrefixLower
, 3));
139 // Precision greater than the value should pad with 0s.
140 // TODO: The prefix should not be counted in the precision. But unfortunately
141 // it is and we have to live with it unless we fix all existing users of
142 // prefixed hex formatting.
143 EXPECT_EQ("0x000", format_number(0, HexPrintStyle::PrefixLower
, 5));
144 EXPECT_EQ("0x0abcde", format_number(0xABCDE, HexPrintStyle::PrefixLower
, 8));
146 EXPECT_EQ("00000", format_number(0, HexPrintStyle::Lower
, 5));
147 EXPECT_EQ("000abcde", format_number(0xABCDE, HexPrintStyle::Lower
, 8));
149 // Try printing more digits than can fit in a uint64.
150 EXPECT_EQ("0x00000000000000abcde",
151 format_number(0xABCDE, HexPrintStyle::PrefixLower
, 21));
154 TEST(NativeFormatTest
, IntegerTests
) {
155 EXPECT_EQ("-10", format_number(-10, IntegerStyle::Integer
));
156 EXPECT_EQ("-100", format_number(-100, IntegerStyle::Integer
));
157 EXPECT_EQ("-1000", format_number(-1000, IntegerStyle::Integer
));
158 EXPECT_EQ("-1234567890", format_number(-1234567890, IntegerStyle::Integer
));
159 EXPECT_EQ("10", format_number(10, IntegerStyle::Integer
));
160 EXPECT_EQ("100", format_number(100, IntegerStyle::Integer
));
161 EXPECT_EQ("1000", format_number(1000, IntegerStyle::Integer
));
162 EXPECT_EQ("1234567890", format_number(1234567890, IntegerStyle::Integer
));
165 TEST(NativeFormatTest
, CommaTests
) {
166 EXPECT_EQ("0", format_number(0, IntegerStyle::Number
));
167 EXPECT_EQ("10", format_number(10, IntegerStyle::Number
));
168 EXPECT_EQ("100", format_number(100, IntegerStyle::Number
));
169 EXPECT_EQ("1,000", format_number(1000, IntegerStyle::Number
));
170 EXPECT_EQ("1,234,567,890", format_number(1234567890, IntegerStyle::Number
));
172 EXPECT_EQ("-10", format_number(-10, IntegerStyle::Number
));
173 EXPECT_EQ("-100", format_number(-100, IntegerStyle::Number
));
174 EXPECT_EQ("-1,000", format_number(-1000, IntegerStyle::Number
));
175 EXPECT_EQ("-1,234,567,890", format_number(-1234567890, IntegerStyle::Number
));