Path: blob/main_old/src/tests/preprocessor_tests/char_test.cpp
1693 views
//1// Copyright 2012 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#include <algorithm>7#include <climits>89#include "PreprocessorTest.h"10#include "compiler/preprocessor/Token.h"1112namespace angle13{1415class CharTest : public PreprocessorTest, public testing::WithParamInterface<int>16{17public:18CharTest() : PreprocessorTest(SH_GLES2_SPEC) {}19};2021static const char kPunctuators[] = {'.', '+', '-', '/', '*', '%', '<', '>', '[', ']', '(', ')',22'{', '}', '^', '|', '&', '~', '=', '!', ':', ';', ',', '?'};23static const int kNumPunctuators = sizeof(kPunctuators) / sizeof(kPunctuators[0]);2425bool isPunctuator(char c)26{27static const char *kPunctuatorBeg = kPunctuators;28static const char *kPunctuatorEnd = kPunctuators + kNumPunctuators;29return std::find(kPunctuatorBeg, kPunctuatorEnd, c) != kPunctuatorEnd;30}3132static const char kWhitespaces[] = {' ', '\t', '\v', '\f', '\n', '\r'};33static const int kNumWhitespaces = sizeof(kWhitespaces) / sizeof(kWhitespaces[0]);3435bool isWhitespace(char c)36{37static const char *kWhitespaceBeg = kWhitespaces;38static const char *kWhitespaceEnd = kWhitespaces + kNumWhitespaces;39return std::find(kWhitespaceBeg, kWhitespaceEnd, c) != kWhitespaceEnd;40}4142TEST_P(CharTest, Identified)43{44std::string str(1, static_cast<char>(GetParam()));45const char *cstr = str.c_str();46int length = 1;4748// Note that we pass the length param as well because the invalid49// string may contain the null character.50ASSERT_TRUE(mPreprocessor.init(1, &cstr, &length));5152int expectedType = pp::Token::LAST;53std::string expectedValue;5455if (str[0] == '#')56{57// Lone '#' is ignored.58}59else if ((str[0] == '_') || ((str[0] >= 'a') && (str[0] <= 'z')) ||60((str[0] >= 'A') && (str[0] <= 'Z')))61{62expectedType = pp::Token::IDENTIFIER;63expectedValue = str;64}65else if (str[0] >= '0' && str[0] <= '9')66{67expectedType = pp::Token::CONST_INT;68expectedValue = str;69}70else if (isPunctuator(str[0]))71{72expectedType = str[0];73expectedValue = str;74}75else if (isWhitespace(str[0]))76{77// Whitespace is ignored.78}79else80{81// Everything else is invalid.82using testing::_;83EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_CHARACTER, _, str));84}8586pp::Token token;87mPreprocessor.lex(&token);88EXPECT_EQ(expectedType, token.type);89EXPECT_EQ(expectedValue, token.text);90}9192// Note +1 for the max-value in range. It is there because the max-value93// not included in the range.94INSTANTIATE_TEST_SUITE_P(All, CharTest, testing::Range(CHAR_MIN, CHAR_MAX + 1));9596} // namespace angle979899