Path: blob/main_old/src/tests/preprocessor_tests/number_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 <tuple>78#include "PreprocessorTest.h"9#include "compiler/preprocessor/Token.h"1011namespace angle12{1314#define CLOSED_RANGE(x, y) testing::Range(x, static_cast<char>((y) + 1))1516class InvalidNumberTest : public SimplePreprocessorTest,17public testing::WithParamInterface<const char *>18{};1920TEST_P(InvalidNumberTest, InvalidNumberIdentified)21{22const char *str = GetParam();2324using testing::_;25EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_NUMBER, _, str));2627preprocess(str);28}2930INSTANTIATE_TEST_SUITE_P(InvalidIntegers, InvalidNumberTest, testing::Values("1a", "08", "0xG"));3132INSTANTIATE_TEST_SUITE_P(InvalidFloats,33InvalidNumberTest,34testing::Values("1eg", "0.a", "0.1.2", ".0a", ".0.1"));3536typedef std::tuple<const char *, char> IntegerParams;37class IntegerTest : public SimplePreprocessorTest, public testing::WithParamInterface<IntegerParams>38{};3940TEST_P(IntegerTest, Identified)41{42std::string str(std::get<0>(GetParam())); // prefix.43str.push_back(std::get<1>(GetParam())); // digit.44const char *cstr = str.c_str();4546pp::Token token;47lexSingleToken(cstr, &token);48EXPECT_EQ(pp::Token::CONST_INT, token.type);49EXPECT_EQ(str, token.text);50}5152INSTANTIATE_TEST_SUITE_P(DecimalInteger,53IntegerTest,54testing::Combine(testing::Values(""), CLOSED_RANGE('0', '9')));5556INSTANTIATE_TEST_SUITE_P(OctalInteger,57IntegerTest,58testing::Combine(testing::Values("0"), CLOSED_RANGE('0', '7')));5960INSTANTIATE_TEST_SUITE_P(HexadecimalInteger_0_9,61IntegerTest,62testing::Combine(testing::Values("0x", "0X"), CLOSED_RANGE('0', '9')));6364INSTANTIATE_TEST_SUITE_P(HexadecimalInteger_a_f,65IntegerTest,66testing::Combine(testing::Values("0x", "0X"), CLOSED_RANGE('a', 'f')));6768INSTANTIATE_TEST_SUITE_P(HexadecimalInteger_A_F,69IntegerTest,70testing::Combine(testing::Values("0x", "0X"), CLOSED_RANGE('A', 'F')));7172class FloatTest : public SimplePreprocessorTest73{74protected:75void expectFloat(const std::string &str)76{77const char *cstr = str.c_str();7879pp::Token token;80lexSingleToken(cstr, &token);81EXPECT_EQ(pp::Token::CONST_FLOAT, token.type);82EXPECT_EQ(str, token.text);83}84};8586typedef std::tuple<char, char, const char *, char> FloatScientificParams;87class FloatScientificTest : public FloatTest,88public testing::WithParamInterface<FloatScientificParams>89{};9091// This test covers floating point numbers of form [0-9][eE][+-]?[0-9].92TEST_P(FloatScientificTest, FloatIdentified)93{94std::string str;95str.push_back(std::get<0>(GetParam())); // significand [0-9].96str.push_back(std::get<1>(GetParam())); // separator [eE].97str.append(std::get<2>(GetParam())); // sign [" " "+" "-"].98str.push_back(std::get<3>(GetParam())); // exponent [0-9].99100SCOPED_TRACE("FloatScientificTest");101expectFloat(str);102}103104INSTANTIATE_TEST_SUITE_P(FloatScientific,105FloatScientificTest,106testing::Combine(CLOSED_RANGE('0', '9'),107testing::Values('e', 'E'),108testing::Values("", "+", "-"),109CLOSED_RANGE('0', '9')));110111typedef std::tuple<char, char> FloatFractionParams;112class FloatFractionTest : public FloatTest, public testing::WithParamInterface<FloatFractionParams>113{};114115// This test covers floating point numbers of form [0-9]"." and [0-9]?"."[0-9].116TEST_P(FloatFractionTest, FloatIdentified)117{118std::string str;119120char significand = std::get<0>(GetParam());121if (significand != '\0')122str.push_back(significand);123124str.push_back('.');125126char fraction = std::get<1>(GetParam());127if (fraction != '\0')128str.push_back(fraction);129130SCOPED_TRACE("FloatFractionTest");131expectFloat(str);132}133134INSTANTIATE_TEST_SUITE_P(FloatFraction_X_X,135FloatFractionTest,136testing::Combine(CLOSED_RANGE('0', '9'), CLOSED_RANGE('0', '9')));137138INSTANTIATE_TEST_SUITE_P(FloatFraction_0_X,139FloatFractionTest,140testing::Combine(testing::Values('\0'), CLOSED_RANGE('0', '9')));141142INSTANTIATE_TEST_SUITE_P(FloatFraction_X_0,143FloatFractionTest,144testing::Combine(CLOSED_RANGE('0', '9'), testing::Values('\0')));145146// In the tests above we have tested individual parts of a float separately.147// This test has all parts of a float.148TEST_F(FloatTest, FractionScientific)149{150SCOPED_TRACE("FractionScientific");151expectFloat("0.1e+2");152}153154} // namespace angle155156157