| 1 | //===- unittest/Format/IntegerLiteralSeparatorTest.cpp --------------------===// |
| 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 "FormatTestBase.h" |
| 10 | |
| 11 | #define DEBUG_TYPE "integer-literal-separator-test" |
| 12 | |
| 13 | namespace clang { |
| 14 | namespace format { |
| 15 | namespace test { |
| 16 | namespace { |
| 17 | |
| 18 | class IntegerLiteralSeparatorTest : public FormatTestBase {}; |
| 19 | |
| 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); |
| 26 | |
| 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); |
| 35 | |
| 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); |
| 42 | |
| 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); |
| 49 | |
| 50 | verifyFormat("o0 = 0;\n" |
| 51 | "o1 = 07;\n" |
| 52 | "o5 = 012345;" , |
| 53 | Style); |
| 54 | |
| 55 | verifyFormat("bi = 0b1'0000i;\n" |
| 56 | "dif = 1'234if;\n" |
| 57 | "hil = 0xA'BCil;" , |
| 58 | "bi = 0b10000i;\n" |
| 59 | "dif = 1234if;\n" |
| 60 | "hil = 0xABCil;" , |
| 61 | Style); |
| 62 | |
| 63 | verifyFormat("bd = 0b1'0000d;\n" |
| 64 | "dh = 1'234h;\n" |
| 65 | "dmin = 1'234min;\n" |
| 66 | "dns = 1'234ns;\n" |
| 67 | "ds = 1'234s;\n" |
| 68 | "dus = 1'234us;\n" |
| 69 | "hy = 0xA'BCy;" , |
| 70 | "bd = 0b10000d;\n" |
| 71 | "dh = 1234h;\n" |
| 72 | "dmin = 1234min;\n" |
| 73 | "dns = 1234ns;\n" |
| 74 | "ds = 1234s;\n" |
| 75 | "dus = 1234us;\n" |
| 76 | "hy = 0xABCy;" , |
| 77 | Style); |
| 78 | |
| 79 | verifyFormat("hd = 0xAB'Cd;" , "hd = 0xABCd;" , Style); |
| 80 | |
| 81 | verifyFormat("d = 5'678_km;\n" |
| 82 | "h = 0xD'EF_u16;" , |
| 83 | "d = 5678_km;\n" |
| 84 | "h = 0xDEF_u16;" , |
| 85 | Style); |
| 86 | } |
| 87 | |
| 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;" ); |
| 93 | |
| 94 | auto TestUnderscore = [&](auto Language) { |
| 95 | Style.Language = Language; |
| 96 | |
| 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); |
| 103 | |
| 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); |
| 110 | |
| 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); |
| 117 | }; |
| 118 | |
| 119 | TestUnderscore(FormatStyle::LK_CSharp); |
| 120 | TestUnderscore(FormatStyle::LK_Java); |
| 121 | TestUnderscore(FormatStyle::LK_JavaScript); |
| 122 | |
| 123 | verifyFormat("d = 9_007_199_254_740_995n;" , Style); |
| 124 | verifyFormat("d = 9_007_199_254_740_995n;" , "d = 9007199254740995n;" , Style); |
| 125 | |
| 126 | Style.IntegerLiteralSeparator.Binary = 8; |
| 127 | verifyFormat( |
| 128 | "b = 0b100000_00000000_00000000_00000000_00000000_00000000_00000011n;" , |
| 129 | "b = 0b100000000000000000000000000000000000000000000000000011n;" , Style); |
| 130 | |
| 131 | verifyFormat("h = 0x20_00_00_00_00_00_03n;" , Style); |
| 132 | verifyFormat("h = 0x20_00_00_00_00_00_03n;" , "h = 0x20000000000003n;" , Style); |
| 133 | |
| 134 | verifyFormat("o = 0o400000000000000003n;" , Style); |
| 135 | } |
| 136 | |
| 137 | TEST_F(IntegerLiteralSeparatorTest, MinDigits) { |
| 138 | FormatStyle Style = getLLVMStyle(); |
| 139 | Style.IntegerLiteralSeparator.Binary = 3; |
| 140 | Style.IntegerLiteralSeparator.Decimal = 3; |
| 141 | Style.IntegerLiteralSeparator.Hex = 2; |
| 142 | |
| 143 | Style.IntegerLiteralSeparator.BinaryMinDigits = 7; |
| 144 | verifyFormat("b1 = 0b101101;\n" |
| 145 | "b2 = 0b1'101'101;" , |
| 146 | "b1 = 0b101'101;\n" |
| 147 | "b2 = 0b1101101;" , |
| 148 | Style); |
| 149 | |
| 150 | Style.IntegerLiteralSeparator.DecimalMinDigits = 5; |
| 151 | verifyFormat("d1 = 2023;\n" |
| 152 | "d2 = 10'000;" , |
| 153 | "d1 = 2'023;\n" |
| 154 | "d2 = 100'00;" , |
| 155 | Style); |
| 156 | |
| 157 | Style.IntegerLiteralSeparator.DecimalMinDigits = 3; |
| 158 | verifyFormat("d1 = 123;\n" |
| 159 | "d2 = 1'234;" , |
| 160 | "d1 = 12'3;\n" |
| 161 | "d2 = 12'34;" , |
| 162 | Style); |
| 163 | |
| 164 | Style.IntegerLiteralSeparator.HexMinDigits = 6; |
| 165 | verifyFormat("h1 = 0xABCDE;\n" |
| 166 | "h2 = 0xAB'CD'EF;" , |
| 167 | "h1 = 0xA'BC'DE;\n" |
| 168 | "h2 = 0xABC'DEF;" , |
| 169 | Style); |
| 170 | } |
| 171 | |
| 172 | TEST_F(IntegerLiteralSeparatorTest, FixRanges) { |
| 173 | FormatStyle Style = getLLVMStyle(); |
| 174 | Style.IntegerLiteralSeparator.Decimal = 3; |
| 175 | |
| 176 | const StringRef Code("i = -12'34;\n" |
| 177 | "// clang-format off\n" |
| 178 | "j = 123'4;\n" |
| 179 | "// clang-format on\n" |
| 180 | "k = +1'23'4;" ); |
| 181 | const StringRef Expected("i = -1'234;\n" |
| 182 | "// clang-format off\n" |
| 183 | "j = 123'4;\n" |
| 184 | "// clang-format on\n" |
| 185 | "k = +1'234;" ); |
| 186 | |
| 187 | verifyFormat(Expected, Code, Style); |
| 188 | |
| 189 | verifyFormat("i = -1'234;\n" |
| 190 | "// clang-format off\n" |
| 191 | "j = 123'4;\n" |
| 192 | "// clang-format on\n" |
| 193 | "k = +1'23'4;" , |
| 194 | Code, Style, {tooling::Range(0, 11)}); // line 1 |
| 195 | |
| 196 | verifyFormat(Code, Code, Style, {tooling::Range(32, 10)}); // line 3 |
| 197 | |
| 198 | verifyFormat("i = -12'34;\n" |
| 199 | "// clang-format off\n" |
| 200 | "j = 123'4;\n" |
| 201 | "// clang-format on\n" |
| 202 | "k = +1'234;" , |
| 203 | Code, Style, {tooling::Range(61, 12)}); // line 5 |
| 204 | |
| 205 | verifyFormat(Expected, Code, Style, |
| 206 | {tooling::Range(0, 11), tooling::Range(61, 12)}); // lines 1, 5 |
| 207 | } |
| 208 | |
| 209 | TEST_F(IntegerLiteralSeparatorTest, FloatingPoint) { |
| 210 | FormatStyle Style = getLLVMStyle(); |
| 211 | Style.IntegerLiteralSeparator.Decimal = 3; |
| 212 | Style.IntegerLiteralSeparator.Hex = 2; |
| 213 | |
| 214 | verifyFormat("d0 = .0;\n" |
| 215 | "d1 = 0.;\n" |
| 216 | "y = 7890.;\n" |
| 217 | "E = 3456E2;\n" |
| 218 | "p = 0xABCp2;" , |
| 219 | Style); |
| 220 | |
| 221 | Style.Language = FormatStyle::LK_JavaScript; |
| 222 | verifyFormat("y = 7890.;\n" |
| 223 | "e = 3456e2;" , |
| 224 | Style); |
| 225 | |
| 226 | Style.Language = FormatStyle::LK_Java; |
| 227 | verifyFormat("y = 7890.;\n" |
| 228 | "E = 3456E2;\n" |
| 229 | "P = 0xABCP2;\n" |
| 230 | "f = 1234f;\n" |
| 231 | "D = 5678D;" , |
| 232 | Style); |
| 233 | |
| 234 | Style.Language = FormatStyle::LK_CSharp; |
| 235 | verifyFormat("y = 7890.;\n" |
| 236 | "e = 3456e2;\n" |
| 237 | "F = 1234F;\n" |
| 238 | "d = 5678d;\n" |
| 239 | "M = 9012M" , |
| 240 | Style); |
| 241 | } |
| 242 | |
| 243 | } // namespace |
| 244 | } // namespace test |
| 245 | } // namespace format |
| 246 | } // namespace clang |
| 247 | |