• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/SmallString.h"
10 #include "llvm/Support/NativeFormatting.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
13 
14 #include <type_traits>
15 
16 using namespace llvm;
17 
18 namespace {
19 
format_number(T N,IntegerStyle Style)20 template <typename T> std::string format_number(T N, IntegerStyle Style) {
21   std::string S;
22   llvm::raw_string_ostream Str(S);
23   write_integer(Str, N, 0, Style);
24   Str.flush();
25   return S;
26 }
27 
format_number(uint64_t N,HexPrintStyle Style,Optional<size_t> Width=None)28 std::string format_number(uint64_t N, HexPrintStyle Style,
29                           Optional<size_t> Width = None) {
30   std::string S;
31   llvm::raw_string_ostream Str(S);
32   write_hex(Str, N, Style, Width);
33   Str.flush();
34   return S;
35 }
36 
format_number(double D,FloatStyle Style,Optional<size_t> Precision=None)37 std::string format_number(double D, FloatStyle Style,
38                           Optional<size_t> Precision = None) {
39   std::string S;
40   llvm::raw_string_ostream Str(S);
41   write_double(Str, D, Style, Precision);
42   Str.flush();
43   return S;
44 }
45 
46 // Test basic number formatting with various styles and default width and
47 // precision.
TEST(NativeFormatTest,BasicIntegerTests)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));
53 
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));
59 
60   // Number formatting.
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));
68 
69   // Hex formatting.
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));
75 
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));
81 
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));
86 
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));
91 }
92 
93 // Test basic floating point formatting with various styles and default width
94 // and precision.
TEST(NativeFormatTest,BasicFloatingPointTests)95 TEST(NativeFormatTest, BasicFloatingPointTests) {
96   // Double
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));
101 
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));
107 }
108 
109 // Test common boundary cases and min/max conditions.
TEST(NativeFormatTest,BoundaryTests)110 TEST(NativeFormatTest, BoundaryTests) {
111   // Min and max.
112   EXPECT_EQ("18446744073709551615",
113             format_number(UINT64_MAX, IntegerStyle::Integer));
114 
115   EXPECT_EQ("9223372036854775807",
116             format_number(INT64_MAX, IntegerStyle::Integer));
117   EXPECT_EQ("-9223372036854775808",
118             format_number(INT64_MIN, IntegerStyle::Integer));
119 
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));
123 
124   EXPECT_EQ("nan", format_number(std::numeric_limits<double>::quiet_NaN(),
125                                  FloatStyle::Fixed));
126   EXPECT_EQ("INF", format_number(std::numeric_limits<double>::infinity(),
127                                  FloatStyle::Fixed));
128 }
129 
TEST(NativeFormatTest,HexTests)130 TEST(NativeFormatTest, HexTests) {
131   // Test hex formatting with different widths and precisions.
132 
133   // Width less than the value should print the full value anyway.
134   EXPECT_EQ("0x0", format_number(0, HexPrintStyle::PrefixLower, 0));
135   EXPECT_EQ("0xabcde", format_number(0xABCDE, HexPrintStyle::PrefixLower, 3));
136 
137   // Precision greater than the value should pad with 0s.
138   // TODO: The prefix should not be counted in the precision.  But unfortunately
139   // it is and we have to live with it unless we fix all existing users of
140   // prefixed hex formatting.
141   EXPECT_EQ("0x000", format_number(0, HexPrintStyle::PrefixLower, 5));
142   EXPECT_EQ("0x0abcde", format_number(0xABCDE, HexPrintStyle::PrefixLower, 8));
143 
144   EXPECT_EQ("00000", format_number(0, HexPrintStyle::Lower, 5));
145   EXPECT_EQ("000abcde", format_number(0xABCDE, HexPrintStyle::Lower, 8));
146 
147   // Try printing more digits than can fit in a uint64.
148   EXPECT_EQ("0x00000000000000abcde",
149             format_number(0xABCDE, HexPrintStyle::PrefixLower, 21));
150 }
151 
TEST(NativeFormatTest,IntegerTests)152 TEST(NativeFormatTest, IntegerTests) {
153   EXPECT_EQ("-10", format_number(-10, IntegerStyle::Integer));
154   EXPECT_EQ("-100", format_number(-100, IntegerStyle::Integer));
155   EXPECT_EQ("-1000", format_number(-1000, IntegerStyle::Integer));
156   EXPECT_EQ("-1234567890", format_number(-1234567890, IntegerStyle::Integer));
157   EXPECT_EQ("10", format_number(10, IntegerStyle::Integer));
158   EXPECT_EQ("100", format_number(100, IntegerStyle::Integer));
159   EXPECT_EQ("1000", format_number(1000, IntegerStyle::Integer));
160   EXPECT_EQ("1234567890", format_number(1234567890, IntegerStyle::Integer));
161 }
162 
TEST(NativeFormatTest,CommaTests)163 TEST(NativeFormatTest, CommaTests) {
164   EXPECT_EQ("0", format_number(0, IntegerStyle::Number));
165   EXPECT_EQ("10", format_number(10, IntegerStyle::Number));
166   EXPECT_EQ("100", format_number(100, IntegerStyle::Number));
167   EXPECT_EQ("1,000", format_number(1000, IntegerStyle::Number));
168   EXPECT_EQ("1,234,567,890", format_number(1234567890, IntegerStyle::Number));
169 
170   EXPECT_EQ("-10", format_number(-10, IntegerStyle::Number));
171   EXPECT_EQ("-100", format_number(-100, IntegerStyle::Number));
172   EXPECT_EQ("-1,000", format_number(-1000, IntegerStyle::Number));
173   EXPECT_EQ("-1,234,567,890", format_number(-1234567890, IntegerStyle::Number));
174 }
175 }
176