Skip to content

Commit cf79906

Browse files
committed
test.cpp: run tests with std::istringstream and std::ifstream
1 parent ad9b49d commit cf79906

1 file changed

Lines changed: 55 additions & 7 deletions

File tree

test.cpp

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@
99
#include <cstdlib>
1010
#include <cstring>
1111
#include <exception>
12+
#include <fstream>
1213
#include <iostream>
14+
#include <limits>
1315
#include <map>
1416
#include <sstream>
1517
#include <stdexcept>
1618
#include <string>
1719
#include <vector>
1820

21+
enum Input {
22+
Stringstream,
23+
Fstream
24+
};
25+
26+
static Input USE_INPUT = Stringstream;
1927
static int numberOfFailedAssertions = 0;
2028

2129
#define ASSERT_EQUALS(expected, actual) (assertEquals((expected), (actual), __LINE__))
@@ -32,11 +40,20 @@ static std::string pprint(const std::string &in)
3240
return ret;
3341
}
3442

43+
static const char* inputString(Input input) {
44+
switch (input) {
45+
case Stringstream:
46+
return "Stringstream";
47+
case Fstream:
48+
return "Fstream";
49+
}
50+
}
51+
3552
static int assertEquals(const std::string &expected, const std::string &actual, int line)
3653
{
3754
if (expected != actual) {
3855
numberOfFailedAssertions++;
39-
std::cerr << "------ assertion failed ---------" << std::endl;
56+
std::cerr << "------ assertion failed (" << inputString(USE_INPUT) << ") ---------" << std::endl;
4057
std::cerr << "line " << line << std::endl;
4158
std::cerr << "expected:" << pprint(expected) << std::endl;
4259
std::cerr << "actual:" << pprint(actual) << std::endl;
@@ -71,10 +88,34 @@ static void testcase(const std::string &name, void (*f)(), int argc, char * cons
7188

7289
#define TEST_CASE(F) (testcase(#F, F, argc, argv))
7390

91+
static std::string writeFile(const char code[], std::size_t size, const std::string &filename) {
92+
std::string tmpfile = filename.empty() ? "code.tmp" : filename;
93+
{
94+
std::ofstream of(tmpfile, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
95+
of.write(code, size);
96+
}
97+
return tmpfile;
98+
}
99+
100+
static simplecpp::TokenList makeTokenListFromFstream(const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename, simplecpp::OutputList *outputList)
101+
{
102+
const std::string tmpfile = writeFile(code, size, filename);
103+
std::ifstream fin(tmpfile);
104+
simplecpp::TokenList tokenList(fin, filenames, tmpfile, outputList);
105+
remove(tmpfile.c_str());
106+
return tokenList;
107+
}
108+
74109
static simplecpp::TokenList makeTokenList(const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList *outputList=nullptr)
75110
{
76-
std::istringstream istr(std::string(code, size));
77-
return simplecpp::TokenList(istr,filenames,filename,outputList);
111+
switch (USE_INPUT) {
112+
case Stringstream: {
113+
std::istringstream istr(std::string(code, size));
114+
return simplecpp::TokenList(istr, filenames, filename, outputList);
115+
}
116+
case Fstream:
117+
return makeTokenListFromFstream(code, size, filenames, filename, outputList);
118+
}
78119
}
79120

80121
static simplecpp::TokenList makeTokenList(const char code[], std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList *outputList=nullptr)
@@ -811,7 +852,7 @@ static void define_va_args_4() // cppcheck trac #9754
811852
ASSERT_EQUALS("\nprintf ( 1 , 2 )", preprocess(code));
812853
}
813854

814-
static void define_va_opt_1()
855+
static void define_va_opt_1()
815856
{
816857
const char code[] = "#define p1(fmt, args...) printf(fmt __VA_OPT__(,) args)\n"
817858
"p1(\"hello\");\n"
@@ -822,7 +863,7 @@ static void define_va_opt_1()
822863
preprocess(code));
823864
}
824865

825-
static void define_va_opt_2()
866+
static void define_va_opt_2()
826867
{
827868
const char code[] = "#define err(...)\\\n"
828869
"__VA_OPT__(\\\n"
@@ -859,7 +900,7 @@ static void define_va_opt_3()
859900
toString(outputList));
860901
}
861902

862-
static void define_va_opt_4()
903+
static void define_va_opt_4()
863904
{
864905
// missing parenthesis
865906
const char code1[] = "#define err(...) __VA_OPT__ something\n"
@@ -2723,8 +2764,10 @@ static void fuzz_crash()
27232764
}
27242765
}
27252766

2726-
int main(int argc, char **argv)
2767+
static void runTests(int argc, char **argv, Input input)
27272768
{
2769+
USE_INPUT = input;
2770+
27282771
TEST_CASE(backslash);
27292772

27302773
TEST_CASE(builtin);
@@ -2950,6 +2993,11 @@ int main(int argc, char **argv)
29502993
TEST_CASE(token);
29512994

29522995
TEST_CASE(fuzz_crash);
2996+
}
29532997

2998+
int main(int argc, char **argv)
2999+
{
3000+
runTests(argc, argv, Stringstream);
3001+
runTests(argc, argv, Fstream);
29543002
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
29553003
}

0 commit comments

Comments
 (0)