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 IdentifierTest : public SimplePreprocessorTest
18 {
19 protected:
expectIdentifier(const std::string & str)20 void expectIdentifier(const std::string &str)
21 {
22 const char *cstr = str.c_str();
23
24 pp::Token token;
25 lexSingleToken(cstr, &token);
26 EXPECT_EQ(pp::Token::IDENTIFIER, token.type);
27 EXPECT_EQ(str, token.text);
28 }
29 };
30
31 class SingleLetterIdentifierTest : public IdentifierTest, public testing::WithParamInterface<char>
32 {};
33
34 // This test covers identifier names of form [_a-zA-Z].
TEST_P(SingleLetterIdentifierTest,Identified)35 TEST_P(SingleLetterIdentifierTest, Identified)
36 {
37 std::string str(1, GetParam());
38 expectIdentifier(str);
39 }
40
41 // Test string: '_'
42 INSTANTIATE_TEST_SUITE_P(Underscore, SingleLetterIdentifierTest, testing::Values('_'));
43
44 // Test string: [a-z]
45 INSTANTIATE_TEST_SUITE_P(a_z, SingleLetterIdentifierTest, CLOSED_RANGE('a', 'z'));
46
47 // Test string: [A-Z]
48 INSTANTIATE_TEST_SUITE_P(A_Z, SingleLetterIdentifierTest, CLOSED_RANGE('A', 'Z'));
49
50 typedef std::tuple<char, char> IdentifierParams;
51 class DoubleLetterIdentifierTest : public IdentifierTest,
52 public testing::WithParamInterface<IdentifierParams>
53 {};
54
55 // This test covers identifier names of form [_a-zA-Z][_a-zA-Z0-9].
TEST_P(DoubleLetterIdentifierTest,Identified)56 TEST_P(DoubleLetterIdentifierTest, Identified)
57 {
58 std::string str;
59 str.push_back(std::get<0>(GetParam()));
60 str.push_back(std::get<1>(GetParam()));
61
62 expectIdentifier(str);
63 }
64
65 // Test string: "__"
66 INSTANTIATE_TEST_SUITE_P(Underscore_Underscore,
67 DoubleLetterIdentifierTest,
68 testing::Combine(testing::Values('_'), testing::Values('_')));
69
70 // Test string: "_"[a-z]
71 INSTANTIATE_TEST_SUITE_P(Underscore_a_z,
72 DoubleLetterIdentifierTest,
73 testing::Combine(testing::Values('_'), CLOSED_RANGE('a', 'z')));
74
75 // Test string: "_"[A-Z]
76 INSTANTIATE_TEST_SUITE_P(Underscore_A_Z,
77 DoubleLetterIdentifierTest,
78 testing::Combine(testing::Values('_'), CLOSED_RANGE('A', 'Z')));
79
80 // Test string: "_"[0-9]
81 INSTANTIATE_TEST_SUITE_P(Underscore_0_9,
82 DoubleLetterIdentifierTest,
83 testing::Combine(testing::Values('_'), CLOSED_RANGE('0', '9')));
84
85 // Test string: [a-z]"_"
86 INSTANTIATE_TEST_SUITE_P(a_z_Underscore,
87 DoubleLetterIdentifierTest,
88 testing::Combine(CLOSED_RANGE('a', 'z'), testing::Values('_')));
89
90 // Test string: [a-z][a-z]
91 INSTANTIATE_TEST_SUITE_P(a_z_a_z,
92 DoubleLetterIdentifierTest,
93 testing::Combine(CLOSED_RANGE('a', 'z'), CLOSED_RANGE('a', 'z')));
94
95 // Test string: [a-z][A-Z]
96 INSTANTIATE_TEST_SUITE_P(a_z_A_Z,
97 DoubleLetterIdentifierTest,
98 testing::Combine(CLOSED_RANGE('a', 'z'), CLOSED_RANGE('A', 'Z')));
99
100 // Test string: [a-z][0-9]
101 INSTANTIATE_TEST_SUITE_P(a_z_0_9,
102 DoubleLetterIdentifierTest,
103 testing::Combine(CLOSED_RANGE('a', 'z'), CLOSED_RANGE('0', '9')));
104
105 // Test string: [A-Z]"_"
106 INSTANTIATE_TEST_SUITE_P(A_Z_Underscore,
107 DoubleLetterIdentifierTest,
108 testing::Combine(CLOSED_RANGE('A', 'Z'), testing::Values('_')));
109
110 // Test string: [A-Z][a-z]
111 INSTANTIATE_TEST_SUITE_P(A_Z_a_z,
112 DoubleLetterIdentifierTest,
113 testing::Combine(CLOSED_RANGE('A', 'Z'), CLOSED_RANGE('a', 'z')));
114
115 // Test string: [A-Z][A-Z]
116 INSTANTIATE_TEST_SUITE_P(A_Z_A_Z,
117 DoubleLetterIdentifierTest,
118 testing::Combine(CLOSED_RANGE('A', 'Z'), CLOSED_RANGE('A', 'Z')));
119
120 // Test string: [A-Z][0-9]
121 INSTANTIATE_TEST_SUITE_P(A_Z_0_9,
122 DoubleLetterIdentifierTest,
123 testing::Combine(CLOSED_RANGE('A', 'Z'), CLOSED_RANGE('0', '9')));
124
125 // The tests above cover one-letter and various combinations of two-letter
126 // identifier names. This test covers all characters in a single string.
TEST_F(IdentifierTest,AllLetters)127 TEST_F(IdentifierTest, AllLetters)
128 {
129 std::string str;
130 for (char c = 'a'; c <= 'z'; ++c)
131 str.push_back(c);
132
133 str.push_back('_');
134
135 for (char c = 'A'; c <= 'Z'; ++c)
136 str.push_back(c);
137
138 str.push_back('_');
139
140 for (char c = '0'; c <= '9'; ++c)
141 str.push_back(c);
142
143 expectIdentifier(str);
144 }
145
146 } // namespace angle
147