1 // 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. 5 // 6 7 #include <algorithm> 8 #include <climits> 9 10 #include "PreprocessorTest.h" 11 #include "Token.h" 12 13 class CharTest : public PreprocessorTest, 14 public testing::WithParamInterface<int> 15 { 16 }; 17 18 static const char kPunctuators[] = { 19 '.', '+', '-', '/', '*', '%', '<', '>', '[', ']', '(', ')', '{', '}', 20 '^', '|', '&', '~', '=', '!', ':', ';', ',', '?'}; 21 static const int kNumPunctuators = 22 sizeof(kPunctuators) / sizeof(kPunctuators[0]); 23 24 bool isPunctuator(char c) 25 { 26 static const char* kPunctuatorBeg = kPunctuators; 27 static const char* kPunctuatorEnd = kPunctuators + kNumPunctuators; 28 return std::find(kPunctuatorBeg, kPunctuatorEnd, c) != kPunctuatorEnd; 29 } 30 31 static const char kWhitespaces[] = {' ', '\t', '\v', '\f', '\n', '\r'}; 32 static const int kNumWhitespaces = 33 sizeof(kWhitespaces) / sizeof(kWhitespaces[0]); 34 35 bool isWhitespace(char c) 36 { 37 static const char* kWhitespaceBeg = kWhitespaces; 38 static const char* kWhitespaceEnd = kWhitespaces + kNumWhitespaces; 39 return std::find(kWhitespaceBeg, kWhitespaceEnd, c) != kWhitespaceEnd; 40 } 41 42 TEST_P(CharTest, Identified) 43 { 44 std::string str(1, GetParam()); 45 const char* cstr = str.c_str(); 46 int length = 1; 47 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)); 51 52 int expectedType = pp::Token::LAST; 53 std::string expectedValue; 54 55 if (str[0] == '#') 56 { 57 // Lone '#' is ignored. 58 } 59 else if ((str[0] == '_') || 60 ((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, 85 print(pp::Diagnostics::INVALID_CHARACTER, _, str)); 86 } 87 88 pp::Token token; 89 mPreprocessor.lex(&token); 90 EXPECT_EQ(expectedType, token.type); 91 EXPECT_EQ(expectedValue, token.text); 92 }; 93 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)); 98 99