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 <algorithm>
8 #include <climits>
9 
10 #include "PreprocessorTest.h"
11 #include "compiler/preprocessor/Token.h"
12 
13 namespace angle
14 {
15 
16 class CharTest : public PreprocessorTest, public testing::WithParamInterface<int>
17 {
18   public:
CharTest()19     CharTest() : PreprocessorTest(SH_GLES2_SPEC) {}
20 };
21 
22 static const char kPunctuators[] = {'.', '+', '-', '/', '*', '%', '<', '>', '[', ']', '(', ')',
23                                     '{', '}', '^', '|', '&', '~', '=', '!', ':', ';', ',', '?'};
24 static const int kNumPunctuators = sizeof(kPunctuators) / sizeof(kPunctuators[0]);
25 
isPunctuator(char c)26 bool isPunctuator(char c)
27 {
28     static const char *kPunctuatorBeg = kPunctuators;
29     static const char *kPunctuatorEnd = kPunctuators + kNumPunctuators;
30     return std::find(kPunctuatorBeg, kPunctuatorEnd, c) != kPunctuatorEnd;
31 }
32 
33 static const char kWhitespaces[] = {' ', '\t', '\v', '\f', '\n', '\r'};
34 static const int kNumWhitespaces = sizeof(kWhitespaces) / sizeof(kWhitespaces[0]);
35 
isWhitespace(char c)36 bool isWhitespace(char c)
37 {
38     static const char *kWhitespaceBeg = kWhitespaces;
39     static const char *kWhitespaceEnd = kWhitespaces + kNumWhitespaces;
40     return std::find(kWhitespaceBeg, kWhitespaceEnd, c) != kWhitespaceEnd;
41 }
42 
TEST_P(CharTest,Identified)43 TEST_P(CharTest, Identified)
44 {
45     std::string str(1, static_cast<char>(GetParam()));
46     const char *cstr = str.c_str();
47     int length       = 1;
48 
49     // Note that we pass the length param as well because the invalid
50     // string may contain the null character.
51     ASSERT_TRUE(mPreprocessor.init(1, &cstr, &length));
52 
53     int expectedType = pp::Token::LAST;
54     std::string expectedValue;
55 
56     if (str[0] == '#')
57     {
58         // Lone '#' is ignored.
59     }
60     else if ((str[0] == '_') || ((str[0] >= 'a') && (str[0] <= 'z')) ||
61              ((str[0] >= 'A') && (str[0] <= 'Z')))
62     {
63         expectedType  = pp::Token::IDENTIFIER;
64         expectedValue = str;
65     }
66     else if (str[0] >= '0' && str[0] <= '9')
67     {
68         expectedType  = pp::Token::CONST_INT;
69         expectedValue = str;
70     }
71     else if (isPunctuator(str[0]))
72     {
73         expectedType  = str[0];
74         expectedValue = str;
75     }
76     else if (isWhitespace(str[0]))
77     {
78         // Whitespace is ignored.
79     }
80     else
81     {
82         // Everything else is invalid.
83         using testing::_;
84         EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_CHARACTER, _, str));
85     }
86 
87     pp::Token token;
88     mPreprocessor.lex(&token);
89     EXPECT_EQ(expectedType, token.type);
90     EXPECT_EQ(expectedValue, token.text);
91 }
92 
93 // Note +1 for the max-value in range. It is there because the max-value
94 // not included in the range.
95 INSTANTIATE_TEST_SUITE_P(All, CharTest, testing::Range(CHAR_MIN, CHAR_MAX + 1));
96 
97 }  // namespace angle
98