1 //===- unittest/Format/IntegerLiteralSeparatorTest.cpp --------------------===//
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 "FormatTestBase.h"
11 #define DEBUG_TYPE "integer-literal-separator-test"
18 class IntegerLiteralSeparatorTest
: public FormatTestBase
{};
20 TEST_F(IntegerLiteralSeparatorTest
, SingleQuoteAsSeparator
) {
21 FormatStyle Style
= getLLVMStyle();
22 EXPECT_EQ(Style
.Language
, FormatStyle::LK_Cpp
);
23 EXPECT_EQ(Style
.IntegerLiteralSeparator
.Binary
, 0);
24 EXPECT_EQ(Style
.IntegerLiteralSeparator
.Decimal
, 0);
25 EXPECT_EQ(Style
.IntegerLiteralSeparator
.Hex
, 0);
27 const StringRef
Binary("b = 0b10011'11'0110'1u;");
28 verifyFormat(Binary
, Style
);
29 Style
.IntegerLiteralSeparator
.Binary
= -1;
30 verifyFormat("b = 0b100111101101u;", Binary
, Style
);
31 Style
.IntegerLiteralSeparator
.Binary
= 1;
32 verifyFormat("b = 0b1'0'0'1'1'1'1'0'1'1'0'1u;", Binary
, Style
);
33 Style
.IntegerLiteralSeparator
.Binary
= 4;
34 verifyFormat("b = 0b1001'1110'1101u;", Binary
, Style
);
36 const StringRef
Decimal("d = 184467'440737'0'95505'92Ull;");
37 verifyFormat(Decimal
, Style
);
38 Style
.IntegerLiteralSeparator
.Decimal
= -1;
39 verifyFormat("d = 18446744073709550592Ull;", Decimal
, Style
);
40 Style
.IntegerLiteralSeparator
.Decimal
= 3;
41 verifyFormat("d = 18'446'744'073'709'550'592Ull;", Decimal
, Style
);
43 const StringRef
Hex("h = 0xDEAD'BEEF'DE'AD'BEE'Fuz;");
44 verifyFormat(Hex
, Style
);
45 Style
.IntegerLiteralSeparator
.Hex
= -1;
46 verifyFormat("h = 0xDEADBEEFDEADBEEFuz;", Hex
, Style
);
47 Style
.IntegerLiteralSeparator
.Hex
= 2;
48 verifyFormat("h = 0xDE'AD'BE'EF'DE'AD'BE'EFuz;", Hex
, Style
);
50 verifyFormat("o0 = 0;\n"
55 verifyFormat("bi = 0b1'0000i;\n"
63 verifyFormat("bd = 0b1'0000d;\n"
79 verifyFormat("hd = 0xAB'Cd;", "hd = 0xABCd;", Style
);
81 verifyFormat("d = 5'678_km;\n"
88 TEST_F(IntegerLiteralSeparatorTest
, UnderscoreAsSeparator
) {
89 FormatStyle Style
= getLLVMStyle();
90 const StringRef
Binary("B = 0B10011_11_0110_1;");
91 const StringRef
Decimal("d = 184467_440737_0_95505_92;");
92 const StringRef
Hex("H = 0XDEAD_BEEF_DE_AD_BEE_F;");
94 auto TestUnderscore
= [&](auto Language
) {
95 Style
.Language
= Language
;
97 Style
.IntegerLiteralSeparator
.Binary
= 0;
98 verifyFormat(Binary
, Style
);
99 Style
.IntegerLiteralSeparator
.Binary
= -1;
100 verifyFormat("B = 0B100111101101;", Binary
, Style
);
101 Style
.IntegerLiteralSeparator
.Binary
= 4;
102 verifyFormat("B = 0B1001_1110_1101;", Binary
, Style
);
104 Style
.IntegerLiteralSeparator
.Decimal
= 0;
105 verifyFormat(Decimal
, Style
);
106 Style
.IntegerLiteralSeparator
.Decimal
= -1;
107 verifyFormat("d = 18446744073709550592;", Decimal
, Style
);
108 Style
.IntegerLiteralSeparator
.Decimal
= 3;
109 verifyFormat("d = 18_446_744_073_709_550_592;", Decimal
, Style
);
111 Style
.IntegerLiteralSeparator
.Hex
= 0;
112 verifyFormat(Hex
, Style
);
113 Style
.IntegerLiteralSeparator
.Hex
= -1;
114 verifyFormat("H = 0XDEADBEEFDEADBEEF;", Hex
, Style
);
115 Style
.IntegerLiteralSeparator
.Hex
= 2;
116 verifyFormat("H = 0XDE_AD_BE_EF_DE_AD_BE_EF;", Hex
, Style
);
119 TestUnderscore(FormatStyle::LK_CSharp
);
120 TestUnderscore(FormatStyle::LK_Java
);
121 TestUnderscore(FormatStyle::LK_JavaScript
);
123 verifyFormat("d = 9_007_199_254_740_995n;", Style
);
124 verifyFormat("d = 9_007_199_254_740_995n;", "d = 9007199254740995n;", Style
);
126 Style
.IntegerLiteralSeparator
.Binary
= 8;
128 "b = 0b100000_00000000_00000000_00000000_00000000_00000000_00000011n;",
129 "b = 0b100000000000000000000000000000000000000000000000000011n;", Style
);
131 verifyFormat("h = 0x20_00_00_00_00_00_03n;", Style
);
132 verifyFormat("h = 0x20_00_00_00_00_00_03n;", "h = 0x20000000000003n;", Style
);
134 verifyFormat("o = 0o400000000000000003n;", Style
);
137 TEST_F(IntegerLiteralSeparatorTest
, MinDigits
) {
138 FormatStyle Style
= getLLVMStyle();
139 Style
.IntegerLiteralSeparator
.Binary
= 3;
140 Style
.IntegerLiteralSeparator
.Decimal
= 3;
141 Style
.IntegerLiteralSeparator
.Hex
= 2;
143 Style
.IntegerLiteralSeparator
.BinaryMinDigits
= 7;
144 verifyFormat("b1 = 0b101101;\n"
150 Style
.IntegerLiteralSeparator
.DecimalMinDigits
= 5;
151 verifyFormat("d1 = 2023;\n"
157 Style
.IntegerLiteralSeparator
.DecimalMinDigits
= 3;
158 verifyFormat("d1 = 123;\n"
164 Style
.IntegerLiteralSeparator
.HexMinDigits
= 6;
165 verifyFormat("h1 = 0xABCDE;\n"
172 TEST_F(IntegerLiteralSeparatorTest
, FixRanges
) {
173 FormatStyle Style
= getLLVMStyle();
174 Style
.IntegerLiteralSeparator
.Decimal
= 3;
176 const StringRef
Code("i = -12'34;\n"
177 "// clang-format off\n"
179 "// clang-format on\n"
181 const StringRef
Expected("i = -1'234;\n"
182 "// clang-format off\n"
184 "// clang-format on\n"
187 verifyFormat(Expected
, Code
, Style
);
189 verifyFormat("i = -1'234;\n"
190 "// clang-format off\n"
192 "// clang-format on\n"
194 Code
, Style
, {tooling::Range(0, 11)}); // line 1
196 verifyFormat(Code
, Code
, Style
, {tooling::Range(32, 10)}); // line 3
198 verifyFormat("i = -12'34;\n"
199 "// clang-format off\n"
201 "// clang-format on\n"
203 Code
, Style
, {tooling::Range(61, 12)}); // line 5
205 verifyFormat(Expected
, Code
, Style
,
206 {tooling::Range(0, 11), tooling::Range(61, 12)}); // lines 1, 5
209 TEST_F(IntegerLiteralSeparatorTest
, FloatingPoint
) {
210 FormatStyle Style
= getLLVMStyle();
211 Style
.IntegerLiteralSeparator
.Decimal
= 3;
212 Style
.IntegerLiteralSeparator
.Hex
= 2;
214 verifyFormat("d0 = .0;\n"
221 Style
.Language
= FormatStyle::LK_JavaScript
;
222 verifyFormat("y = 7890.;\n"
226 Style
.Language
= FormatStyle::LK_Java
;
227 verifyFormat("y = 7890.;\n"
234 Style
.Language
= FormatStyle::LK_CSharp
;
235 verifyFormat("y = 7890.;\n"
245 } // namespace format