| 1 |
//===- unittest/Format/NumericLiteralInfoTest.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 "../../lib/Format/NumericLiteralInfo.h" |
| 10 |
#include "gtest/gtest.h" |
| 11 |
|
| 12 |
namespace clang { |
| 13 |
namespace format { |
| 14 |
namespace { |
| 15 |
|
| 16 |
static constexpr auto npos = llvm::StringRef::npos; |
| 17 |
|
| 18 |
class NumericLiteralInfoTest : public testing::Test { |
| 19 |
protected: |
| 20 |
bool verifyInfo(const NumericLiteralInfo &Info, size_t BaseLetterPos = npos, |
| 21 |
size_t DotPos = npos, size_t ExponentLetterPos = npos, |
| 22 |
size_t SuffixPos = npos) { |
| 23 |
return Info.BaseLetterPos == BaseLetterPos && Info.DotPos == DotPos && |
| 24 |
Info.ExponentLetterPos == ExponentLetterPos && |
| 25 |
Info.SuffixPos == SuffixPos; |
| 26 |
} |
| 27 |
}; |
| 28 |
|
| 29 |
TEST_F(NumericLiteralInfoTest, IntegerLiteral) { |
| 30 |
// Decimal. |
| 31 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("90"))); |
| 32 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9L"), npos, npos, npos, 1)); |
| 33 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9'0U"), npos, npos, npos, 3)); |
| 34 |
|
| 35 |
// Octal. |
| 36 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0"))); |
| 37 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("07"))); |
| 38 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0z"), npos, npos, npos, 1)); |
| 39 |
// JavaScript. |
| 40 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0o7"), 1)); |
| 41 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0O7_0", '_'), 1)); |
| 42 |
|
| 43 |
// Binary. |
| 44 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0b1"), 1)); |
| 45 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0B1ul"), 1, npos, npos, 3)); |
| 46 |
|
| 47 |
// Hexadecimal. |
| 48 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xF"), 1)); |
| 49 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0XfZ"), 1, npos, npos, 3)); |
| 50 |
} |
| 51 |
|
| 52 |
TEST_F(NumericLiteralInfoTest, FloatingPointLiteral) { |
| 53 |
// Decimal. |
| 54 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo(".9"), npos, 0)); |
| 55 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9."), npos, 1)); |
| 56 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9.F"), npos, 1, npos, 2)); |
| 57 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9e9"), npos, npos, 1)); |
| 58 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9E-9f"), npos, npos, 1, 4)); |
| 59 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9.9e+9bf16"), npos, 1, 3, 6)); |
| 60 |
|
| 61 |
// Hexadecimal. |
| 62 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0X.Fp9"), 1, 2, 4)); |
| 63 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xF.P9"), 1, 3, 4)); |
| 64 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xFp9"), 1, npos, 3)); |
| 65 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xFp+9F128"), 1, npos, 3, 6)); |
| 66 |
EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xF.Fp-9_Pa"), 1, 3, 5, 8)); |
| 67 |
} |
| 68 |
|
| 69 |
} // namespace |
| 70 |
} // namespace format |
| 71 |
} // namespace clang |