1 //
2 // Copyright 2012 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #include <tuple>
8
9 #include "PreprocessorTest.h"
10 #include "compiler/preprocessor/Token.h"
11
12 namespace angle
13 {
14
15 #define CLOSED_RANGE(x, y) testing::Range(x, static_cast<char>((y) + 1))
16
17 class InvalidNumberTest : public SimplePreprocessorTest,
18 public testing::WithParamInterface<const char *>
19 {};
20
TEST_P(InvalidNumberTest,InvalidNumberIdentified)21 TEST_P(InvalidNumberTest, InvalidNumberIdentified)
22 {
23 const char *str = GetParam();
24
25 using testing::_;
26 EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_NUMBER, _, str));
27
28 preprocess(str);
29 }
30
31 INSTANTIATE_TEST_SUITE_P(InvalidIntegers, InvalidNumberTest, testing::Values("1a", "08", "0xG"));
32
33 INSTANTIATE_TEST_SUITE_P(InvalidFloats,
34 InvalidNumberTest,
35 testing::Values("1eg", "0.a", "0.1.2", ".0a", ".0.1"));
36
37 typedef std::tuple<const char *, char> IntegerParams;
38 class IntegerTest : public SimplePreprocessorTest, public testing::WithParamInterface<IntegerParams>
39 {};
40
TEST_P(IntegerTest,Identified)41 TEST_P(IntegerTest, Identified)
42 {
43 std::string str(std::get<0>(GetParam())); // prefix.
44 str.push_back(std::get<1>(GetParam())); // digit.
45 const char *cstr = str.c_str();
46
47 pp::Token token;
48 lexSingleToken(cstr, &token);
49 EXPECT_EQ(pp::Token::CONST_INT, token.type);
50 EXPECT_EQ(str, token.text);
51 }
52
53 INSTANTIATE_TEST_SUITE_P(DecimalInteger,
54 IntegerTest,
55 testing::Combine(testing::Values(""), CLOSED_RANGE('0', '9')));
56
57 INSTANTIATE_TEST_SUITE_P(OctalInteger,
58 IntegerTest,
59 testing::Combine(testing::Values("0"), CLOSED_RANGE('0', '7')));
60
61 INSTANTIATE_TEST_SUITE_P(HexadecimalInteger_0_9,
62 IntegerTest,
63 testing::Combine(testing::Values("0x", "0X"), CLOSED_RANGE('0', '9')));
64
65 INSTANTIATE_TEST_SUITE_P(HexadecimalInteger_a_f,
66 IntegerTest,
67 testing::Combine(testing::Values("0x", "0X"), CLOSED_RANGE('a', 'f')));
68
69 INSTANTIATE_TEST_SUITE_P(HexadecimalInteger_A_F,
70 IntegerTest,
71 testing::Combine(testing::Values("0x", "0X"), CLOSED_RANGE('A', 'F')));
72
73 class FloatTest : public SimplePreprocessorTest
74 {
75 protected:
expectFloat(const std::string & str)76 void expectFloat(const std::string &str)
77 {
78 const char *cstr = str.c_str();
79
80 pp::Token token;
81 lexSingleToken(cstr, &token);
82 EXPECT_EQ(pp::Token::CONST_FLOAT, token.type);
83 EXPECT_EQ(str, token.text);
84 }
85 };
86
87 typedef std::tuple<char, char, const char *, char> FloatScientificParams;
88 class FloatScientificTest : public FloatTest,
89 public testing::WithParamInterface<FloatScientificParams>
90 {};
91
92 // This test covers floating point numbers of form [0-9][eE][+-]?[0-9].
TEST_P(FloatScientificTest,FloatIdentified)93 TEST_P(FloatScientificTest, FloatIdentified)
94 {
95 std::string str;
96 str.push_back(std::get<0>(GetParam())); // significand [0-9].
97 str.push_back(std::get<1>(GetParam())); // separator [eE].
98 str.append(std::get<2>(GetParam())); // sign [" " "+" "-"].
99 str.push_back(std::get<3>(GetParam())); // exponent [0-9].
100
101 SCOPED_TRACE("FloatScientificTest");
102 expectFloat(str);
103 }
104
105 INSTANTIATE_TEST_SUITE_P(FloatScientific,
106 FloatScientificTest,
107 testing::Combine(CLOSED_RANGE('0', '9'),
108 testing::Values('e', 'E'),
109 testing::Values("", "+", "-"),
110 CLOSED_RANGE('0', '9')));
111
112 typedef std::tuple<char, char> FloatFractionParams;
113 class FloatFractionTest : public FloatTest, public testing::WithParamInterface<FloatFractionParams>
114 {};
115
116 // This test covers floating point numbers of form [0-9]"." and [0-9]?"."[0-9].
TEST_P(FloatFractionTest,FloatIdentified)117 TEST_P(FloatFractionTest, FloatIdentified)
118 {
119 std::string str;
120
121 char significand = std::get<0>(GetParam());
122 if (significand != '\0')
123 str.push_back(significand);
124
125 str.push_back('.');
126
127 char fraction = std::get<1>(GetParam());
128 if (fraction != '\0')
129 str.push_back(fraction);
130
131 SCOPED_TRACE("FloatFractionTest");
132 expectFloat(str);
133 }
134
135 INSTANTIATE_TEST_SUITE_P(FloatFraction_X_X,
136 FloatFractionTest,
137 testing::Combine(CLOSED_RANGE('0', '9'), CLOSED_RANGE('0', '9')));
138
139 INSTANTIATE_TEST_SUITE_P(FloatFraction_0_X,
140 FloatFractionTest,
141 testing::Combine(testing::Values('\0'), CLOSED_RANGE('0', '9')));
142
143 INSTANTIATE_TEST_SUITE_P(FloatFraction_X_0,
144 FloatFractionTest,
145 testing::Combine(CLOSED_RANGE('0', '9'), testing::Values('\0')));
146
147 // In the tests above we have tested individual parts of a float separately.
148 // This test has all parts of a float.
TEST_F(FloatTest,FractionScientific)149 TEST_F(FloatTest, FractionScientific)
150 {
151 SCOPED_TRACE("FractionScientific");
152 expectFloat("0.1e+2");
153 }
154
155 } // namespace angle
156