aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Format/NumericLiteralInfoTest.cpp
blob: a892cfff531e382a92484e78424ae255a80ae691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//===- unittest/Format/NumericLiteralInfoTest.cpp -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "../../lib/Format/NumericLiteralInfo.h"
#include "gtest/gtest.h"

namespace clang {
namespace format {
namespace {

static constexpr auto npos = llvm::StringRef::npos;

class NumericLiteralInfoTest : public testing::Test {
protected:
  bool verifyInfo(const NumericLiteralInfo &Info, size_t BaseLetterPos = npos,
                  size_t DotPos = npos, size_t ExponentLetterPos = npos,
                  size_t SuffixPos = npos) {
    return Info.BaseLetterPos == BaseLetterPos && Info.DotPos == DotPos &&
           Info.ExponentLetterPos == ExponentLetterPos &&
           Info.SuffixPos == SuffixPos;
  }
};

TEST_F(NumericLiteralInfoTest, IntegerLiteral) {
  // Decimal.
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("90")));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9L"), npos, npos, npos, 1));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9'0U"), npos, npos, npos, 3));

  // Octal.
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0")));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("07")));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0z"), npos, npos, npos, 1));
  // JavaScript.
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0o7"), 1));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0O7_0", '_'), 1));

  // Binary.
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0b1"), 1));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0B1ul"), 1, npos, npos, 3));

  // Hexadecimal.
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xF"), 1));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0XfZ"), 1, npos, npos, 3));
}

TEST_F(NumericLiteralInfoTest, FloatingPointLiteral) {
  // Decimal.
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo(".9"), npos, 0));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9."), npos, 1));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9.F"), npos, 1, npos, 2));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9e9"), npos, npos, 1));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9E-9f"), npos, npos, 1, 4));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("9.9e+9bf16"), npos, 1, 3, 6));

  // Hexadecimal.
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0X.Fp9"), 1, 2, 4));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xF.P9"), 1, 3, 4));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xFp9"), 1, npos, 3));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xFp+9F128"), 1, npos, 3, 6));
  EXPECT_TRUE(verifyInfo(NumericLiteralInfo("0xF.Fp-9_Pa"), 1, 3, 5, 8));
}

} // namespace
} // namespace format
} // namespace clang