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 "PreprocessorTest.h" 8 #include "Token.h" 9 10 #define CLOSED_RANGE(x, y) testing::Range(x, static_cast<char>((y) + 1)) 11 12 class InvalidNumberTest : public PreprocessorTest, 13 public testing::WithParamInterface<const char*> 14 { 15 }; 16 17 TEST_P(InvalidNumberTest, InvalidNumberIdentified) 18 { 19 const char* str = GetParam(); 20 ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); 21 22 using testing::_; 23 EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_NUMBER, _, str)); 24 25 pp::Token token; 26 mPreprocessor.lex(&token); 27 } 28 29 INSTANTIATE_TEST_CASE_P(InvalidIntegers, InvalidNumberTest, 30 testing::Values("1a", "08", "0xG")); 31 32 33 INSTANTIATE_TEST_CASE_P(InvalidFloats, InvalidNumberTest, 34 testing::Values("1eg", "0.a", "0.1.2", ".0a", ".0.1")); 35 36 typedef std::tr1::tuple<const char*, char> IntegerParams; 37 class IntegerTest : public PreprocessorTest, 38 public testing::WithParamInterface<IntegerParams> 39 { 40 }; 41 42 TEST_P(IntegerTest, Identified) 43 { 44 std::string str(std::tr1::get<0>(GetParam())); // prefix. 45 str.push_back(std::tr1::get<1>(GetParam())); // digit. 46 const char* cstr = str.c_str(); 47 48 ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0)); 49 50 pp::Token token; 51 mPreprocessor.lex(&token); 52 EXPECT_EQ(pp::Token::CONST_INT, token.type); 53 EXPECT_EQ(str, token.text); 54 } 55 56 INSTANTIATE_TEST_CASE_P(DecimalInteger, 57 IntegerTest, 58 testing::Combine(testing::Values(""), 59 CLOSED_RANGE('0', '9'))); 60 61 INSTANTIATE_TEST_CASE_P(OctalInteger, 62 IntegerTest, 63 testing::Combine(testing::Values("0"), 64 CLOSED_RANGE('0', '7'))); 65 66 INSTANTIATE_TEST_CASE_P(HexadecimalInteger_0_9, 67 IntegerTest, 68 testing::Combine(testing::Values("0x", "0X"), 69 CLOSED_RANGE('0', '9'))); 70 71 INSTANTIATE_TEST_CASE_P(HexadecimalInteger_a_f, 72 IntegerTest, 73 testing::Combine(testing::Values("0x", "0X"), 74 CLOSED_RANGE('a', 'f'))); 75 76 INSTANTIATE_TEST_CASE_P(HexadecimalInteger_A_F, 77 IntegerTest, 78 testing::Combine(testing::Values("0x", "0X"), 79 CLOSED_RANGE('A', 'F'))); 80 81 class FloatTest : public PreprocessorTest 82 { 83 protected: 84 void expectFloat(const std::string& str) 85 { 86 const char* cstr = str.c_str(); 87 ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0)); 88 89 pp::Token token; 90 mPreprocessor.lex(&token); 91 EXPECT_EQ(pp::Token::CONST_FLOAT, token.type); 92 EXPECT_EQ(str, token.text); 93 } 94 }; 95 96 typedef std::tr1::tuple<char, char, const char*, char> FloatScientificParams; 97 class FloatScientificTest : 98 public FloatTest, 99 public testing::WithParamInterface<FloatScientificParams> 100 { 101 }; 102 103 // This test covers floating point numbers of form [0-9][eE][+-]?[0-9]. 104 TEST_P(FloatScientificTest, FloatIdentified) 105 { 106 std::string str; 107 str.push_back(std::tr1::get<0>(GetParam())); // significand [0-9]. 108 str.push_back(std::tr1::get<1>(GetParam())); // separator [eE]. 109 str.append(std::tr1::get<2>(GetParam())); // sign [" " "+" "-"]. 110 str.push_back(std::tr1::get<3>(GetParam())); // exponent [0-9]. 111 112 SCOPED_TRACE("FloatScientificTest"); 113 expectFloat(str); 114 } 115 116 INSTANTIATE_TEST_CASE_P(FloatScientific, 117 FloatScientificTest, 118 testing::Combine(CLOSED_RANGE('0', '9'), 119 testing::Values('e', 'E'), 120 testing::Values("", "+", "-"), 121 CLOSED_RANGE('0', '9'))); 122 123 typedef std::tr1::tuple<char, char> FloatFractionParams; 124 class FloatFractionTest : 125 public FloatTest, 126 public testing::WithParamInterface<FloatFractionParams> 127 { 128 }; 129 130 // This test covers floating point numbers of form [0-9]"." and [0-9]?"."[0-9]. 131 TEST_P(FloatFractionTest, FloatIdentified) 132 { 133 std::string str; 134 135 char significand = std::tr1::get<0>(GetParam()); 136 if (significand != '\0') 137 str.push_back(significand); 138 139 str.push_back('.'); 140 141 char fraction = std::tr1::get<1>(GetParam()); 142 if (fraction != '\0') 143 str.push_back(fraction); 144 145 SCOPED_TRACE("FloatFractionTest"); 146 expectFloat(str); 147 } 148 149 INSTANTIATE_TEST_CASE_P(FloatFraction_X_X, 150 FloatFractionTest, 151 testing::Combine(CLOSED_RANGE('0', '9'), 152 CLOSED_RANGE('0', '9'))); 153 154 INSTANTIATE_TEST_CASE_P(FloatFraction_0_X, 155 FloatFractionTest, 156 testing::Combine(testing::Values('\0'), 157 CLOSED_RANGE('0', '9'))); 158 159 INSTANTIATE_TEST_CASE_P(FloatFraction_X_0, 160 FloatFractionTest, 161 testing::Combine(CLOSED_RANGE('0', '9'), 162 testing::Values('\0'))); 163 164 // In the tests above we have tested individual parts of a float separately. 165 // This test has all parts of a float. 166 TEST_F(FloatTest, FractionScientific) 167 { 168 SCOPED_TRACE("FractionScientific"); 169 expectFloat("0.1e+2"); 170 } 171