blob: 6ea5d8088521c571597e539c34f007597077b100 [file] [log] [blame]
alokp@chromium.org23ff36a2012-04-23 19:27:35 +00001//
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>
alokp@chromium.org28182482012-04-24 23:07:34 +00008#include <climits>
alokp@chromium.org23ff36a2012-04-23 19:27:35 +00009
alokp@chromium.orgfc8b7202012-05-25 00:01:13 +000010#include "PreprocessorTest.h"
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000011#include "Token.h"
12
alokp@chromium.orgfc8b7202012-05-25 00:01:13 +000013class CharTest : public PreprocessorTest,
14 public testing::WithParamInterface<int>
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000015{
16};
17
18static const char kPunctuators[] = {
19 '.', '+', '-', '/', '*', '%', '<', '>', '[', ']', '(', ')', '{', '}',
20 '^', '|', '&', '~', '=', '!', ':', ';', ',', '?'};
21static const int kNumPunctuators =
22 sizeof(kPunctuators) / sizeof(kPunctuators[0]);
23
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000024bool 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
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000031static const char kWhitespaces[] = {' ', '\t', '\v', '\f', '\n', '\r'};
32static const int kNumWhitespaces =
33 sizeof(kWhitespaces) / sizeof(kWhitespaces[0]);
34
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000035bool 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
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000042TEST_P(CharTest, Identified)
43{
44 std::string str(1, GetParam());
45 const char* cstr = str.c_str();
46 int length = 1;
47
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000048 // Note that we pass the length param as well because the invalid
49 // string may contain the null character.
alokp@chromium.orgfc8b7202012-05-25 00:01:13 +000050 ASSERT_TRUE(mPreprocessor.init(1, &cstr, &length));
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000051
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000052 int expectedType = pp::Token::LAST;
53 std::string expectedValue;
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000054
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000055 if (str[0] == '#')
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000056 {
57 // Lone '#' is ignored.
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000058 }
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];
alokp@chromium.org07d921d2012-05-22 20:22:08 +000074 expectedValue = str;
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000075 }
76 else if (isWhitespace(str[0]))
77 {
78 // Whitespace is ignored.
79 }
80 else
81 {
82 // Everything else is invalid.
83 using testing::_;
alokp@chromium.orgfc8b7202012-05-25 00:01:13 +000084 EXPECT_CALL(mDiagnostics,
Geoff Lang7095d7a2013-11-20 14:48:17 -050085 print(pp::Diagnostics::PP_INVALID_CHARACTER, _, str));
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000086 }
87
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000088 pp::Token token;
alokp@chromium.orgfc8b7202012-05-25 00:01:13 +000089 mPreprocessor.lex(&token);
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000090 EXPECT_EQ(expectedType, token.type);
alokp@chromium.org5b6a68e2012-06-28 20:29:13 +000091 EXPECT_EQ(expectedValue, token.text);
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000092};
93
94// Note +1 for the max-value in range. It is there because the max-value
95// not included in the range.
alokp@chromium.org28182482012-04-24 23:07:34 +000096INSTANTIATE_TEST_CASE_P(All, CharTest,
97 testing::Range(CHAR_MIN, CHAR_MAX + 1));
alokp@chromium.org23ff36a2012-04-23 19:27:35 +000098