Skip to content

Commit bc304cf

Browse files
committed
improved test coverage of throwing code
1 parent 7dcdbda commit bc304cf

1 file changed

Lines changed: 99 additions & 2 deletions

File tree

test.cpp

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ static std::string readfile(const char code[], std::size_t size, simplecpp::Outp
132132
return makeTokenList(code,size,files,std::string(),outputList).stringify();
133133
}
134134

135-
static std::string preprocess(const char code[], const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage = nullptr, std::list<simplecpp::IfCond> *ifCond = nullptr, const std::string &file = std::string())
135+
static std::string preprocess(const char code[], std::size_t size, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage = nullptr, std::list<simplecpp::IfCond> *ifCond = nullptr, const std::string &file = std::string())
136136
{
137137
std::vector<std::string> files;
138138
simplecpp::FileDataCache cache;
139-
simplecpp::TokenList tokens = makeTokenList(code,files, file);
139+
simplecpp::TokenList tokens = makeTokenList(code, size, files, file);
140140
if (dui.removeComments)
141141
tokens.removeComments();
142142
simplecpp::TokenList tokens2(files);
@@ -145,6 +145,11 @@ static std::string preprocess(const char code[], const simplecpp::DUI &dui, simp
145145
return tokens2.stringify();
146146
}
147147

148+
static std::string preprocess(const char code[], const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage = nullptr, std::list<simplecpp::IfCond> *ifCond = nullptr, const std::string &file = std::string())
149+
{
150+
return preprocess(code, strlen(code), dui, outputList, macroUsage, ifCond, file);
151+
}
152+
148153
static std::string preprocess(const char code[])
149154
{
150155
return preprocess(code, simplecpp::DUI(), nullptr);
@@ -165,6 +170,11 @@ static std::string preprocess(const char code[], simplecpp::OutputList *outputLi
165170
return preprocess(code, simplecpp::DUI(), outputList);
166171
}
167172

173+
static std::string preprocess(const char code[], std::size_t size, simplecpp::OutputList *outputList)
174+
{
175+
return preprocess(code, size, simplecpp::DUI(), outputList);
176+
}
177+
168178
static std::string preprocess(const char code[], std::list<simplecpp::IfCond> *ifCond)
169179
{
170180
return preprocess(code, simplecpp::DUI(), nullptr, nullptr, ifCond);
@@ -913,6 +923,84 @@ static void define_invalid_2()
913923
ASSERT_EQUALS("file0,1,syntax_error,Failed to parse #define, bad macro syntax\n", toString(outputList));
914924
}
915925

926+
static void define_invalid_3()
927+
{
928+
const char code[] = "#if'\\u'";
929+
simplecpp::OutputList outputList;
930+
ASSERT_EQUALS("", preprocess(code, &outputList));
931+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, expected digit\n", toString(outputList));
932+
}
933+
934+
static void define_invalid_4()
935+
{
936+
const char code[] = "#define R()\n"
937+
"R";
938+
simplecpp::OutputList outputList;
939+
ASSERT_EQUALS("", preprocess(code, &outputList));
940+
ASSERT_EQUALS("file0,2,syntax_error,failed to expand 'R', Wrong number of parameters for macro 'R'.\n", toString(outputList));
941+
}
942+
943+
static void define_invalid_5()
944+
{
945+
const char code[] = "#define X(...)\n"
946+
"X";
947+
simplecpp::OutputList outputList;
948+
ASSERT_EQUALS("", preprocess(code, &outputList));
949+
ASSERT_EQUALS("file0,2,syntax_error,failed to expand 'X', Wrong number of parameters for macro 'X'.\n", toString(outputList));
950+
}
951+
952+
static void define_invalid_6()
953+
{
954+
const char code[] = "#if-0xBBB4444444444444%~B";
955+
simplecpp::OutputList outputList;
956+
ASSERT_EQUALS("", preprocess(code, &outputList));
957+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, division overflow\n", toString(outputList));
958+
}
959+
960+
static void define_invalid_7()
961+
{
962+
const char code[] = "#define A)__has_include\n"
963+
"#if\u000BA";
964+
simplecpp::OutputList outputList;
965+
ASSERT_EQUALS("", preprocess(code, &outputList));
966+
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, missing __has_include argument\n", toString(outputList));
967+
}
968+
969+
static void define_invalid_8()
970+
{
971+
const char code[] = "#define f __has_include\n"
972+
"#if#f<";
973+
simplecpp::OutputList outputList;
974+
ASSERT_EQUALS("", preprocess(code, &outputList));
975+
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, missing __has_include argument\n", toString(outputList));
976+
}
977+
978+
static void define_invalid_9()
979+
{
980+
const char code[] = "#if@u'\\udefa'";
981+
simplecpp::OutputList outputList;
982+
ASSERT_EQUALS("", preprocess(code, &outputList));
983+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, surrogate code points not allowed in universal character names\n", toString(outputList));
984+
}
985+
986+
static void define_invalid_10()
987+
{
988+
const char code[] = "#define\u0000X\u0007__has_include(\n"
989+
"#if%X&";
990+
simplecpp::OutputList outputList;
991+
ASSERT_EQUALS("", preprocess(code, sizeof(code), &outputList));
992+
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, invalid __has_include expression\n", toString(outputList));
993+
}
994+
995+
static void define_invalid_11()
996+
{
997+
const char code[] = "#define\u0000X\u0000__has_include<2\n"
998+
"#if*X";
999+
simplecpp::OutputList outputList;
1000+
ASSERT_EQUALS("", preprocess(code, sizeof(code), &outputList));
1001+
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, invalid __has_include expression\n", toString(outputList));
1002+
}
1003+
9161004
static void define_define_1()
9171005
{
9181006
const char code[] = "#define A(x) (x+1)\n"
@@ -3929,6 +4017,15 @@ static void runTests(int argc, char **argv, Input input)
39294017
TEST_CASE(define23); // #40
39304018
TEST_CASE(define_invalid_1);
39314019
TEST_CASE(define_invalid_2);
4020+
TEST_CASE(define_invalid_3);
4021+
TEST_CASE(define_invalid_4);
4022+
TEST_CASE(define_invalid_5);
4023+
TEST_CASE(define_invalid_6);
4024+
TEST_CASE(define_invalid_7);
4025+
TEST_CASE(define_invalid_8);
4026+
TEST_CASE(define_invalid_9);
4027+
TEST_CASE(define_invalid_10);
4028+
TEST_CASE(define_invalid_11);
39324029
TEST_CASE(define_define_1);
39334030
TEST_CASE(define_define_2);
39344031
TEST_CASE(define_define_3);

0 commit comments

Comments
 (0)