2 // Copyright (c) 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.
10 #include "PreprocessorTest.h"
13 class CharTest : public PreprocessorTest,
14 public testing::WithParamInterface<int>
18 static const char kPunctuators[] = {
19 '.', '+', '-', '/', '*', '%', '<', '>', '[', ']', '(', ')', '{', '}',
20 '^', '|', '&', '~', '=', '!', ':', ';', ',', '?'};
21 static const int kNumPunctuators =
22 sizeof(kPunctuators) / sizeof(kPunctuators[0]);
24 bool isPunctuator(char c)
26 static const char* kPunctuatorBeg = kPunctuators;
27 static const char* kPunctuatorEnd = kPunctuators + kNumPunctuators;
28 return std::find(kPunctuatorBeg, kPunctuatorEnd, c) != kPunctuatorEnd;
31 static const char kWhitespaces[] = {' ', '\t', '\v', '\f', '\n', '\r'};
32 static const int kNumWhitespaces =
33 sizeof(kWhitespaces) / sizeof(kWhitespaces[0]);
35 bool isWhitespace(char c)
37 static const char* kWhitespaceBeg = kWhitespaces;
38 static const char* kWhitespaceEnd = kWhitespaces + kNumWhitespaces;
39 return std::find(kWhitespaceBeg, kWhitespaceEnd, c) != kWhitespaceEnd;
42 TEST_P(CharTest, Identified)
44 std::string str(1, static_cast<char>(GetParam()));
45 const char* cstr = str.c_str();
48 // Note that we pass the length param as well because the invalid
49 // string may contain the null character.
50 ASSERT_TRUE(mPreprocessor.init(1, &cstr, &length));
52 int expectedType = pp::Token::LAST;
53 std::string expectedValue;
57 // Lone '#' is ignored.
59 else if ((str[0] == '_') ||
60 ((str[0] >= 'a') && (str[0] <= 'z')) ||
61 ((str[0] >= 'A') && (str[0] <= 'Z')))
63 expectedType = pp::Token::IDENTIFIER;
66 else if (str[0] >= '0' && str[0] <= '9')
68 expectedType = pp::Token::CONST_INT;
71 else if (isPunctuator(str[0]))
73 expectedType = str[0];
76 else if (isWhitespace(str[0]))
78 // Whitespace is ignored.
82 // Everything else is invalid.
84 EXPECT_CALL(mDiagnostics,
85 print(pp::Diagnostics::PP_INVALID_CHARACTER, _, str));
89 mPreprocessor.lex(&token);
90 EXPECT_EQ(expectedType, token.type);
91 EXPECT_EQ(expectedValue, token.text);
94 // Note +1 for the max-value in range. It is there because the max-value
95 // not included in the range.
96 INSTANTIATE_TEST_CASE_P(All, CharTest,
97 testing::Range(CHAR_MIN, CHAR_MAX + 1));