18
18
19
19
#include < iostream>
20
20
#include < limits>
21
+ #include < fstream>
21
22
#include < sstream>
22
23
#include < vector>
23
24
#include " simplecpp.h"
24
25
26
+ enum Input {
27
+ Stringstream,
28
+ Fstream
29
+ };
30
+
31
+ static Input USE_INPUT = Stringstream;
25
32
static int numberOfFailedAssertions = 0 ;
26
33
27
34
#define ASSERT_EQUALS (expected, actual ) (assertEquals((expected), (actual), __LINE__))
@@ -38,11 +45,20 @@ static std::string pprint(const std::string &in)
38
45
return ret;
39
46
}
40
47
48
+ static const char * inputString (Input input) {
49
+ switch (input) {
50
+ case Stringstream:
51
+ return " Stringstream" ;
52
+ case Fstream:
53
+ return " Fstream" ;
54
+ }
55
+ }
56
+
41
57
static int assertEquals (const std::string &expected, const std::string &actual, int line)
42
58
{
43
59
if (expected != actual) {
44
60
numberOfFailedAssertions++;
45
- std::cerr << " ------ assertion failed ---------" << std::endl;
61
+ std::cerr << " ------ assertion failed ( " << inputString (USE_INPUT) << " ) ---------" << std::endl;
46
62
std::cerr << " line " << line << std::endl;
47
63
std::cerr << " expected:" << pprint (expected) << std::endl;
48
64
std::cerr << " actual:" << pprint (actual) << std::endl;
@@ -77,10 +93,34 @@ static void testcase(const std::string &name, void (*f)(), int argc, char * cons
77
93
78
94
#define TEST_CASE (F ) (testcase(#F, F, argc, argv))
79
95
96
+ static std::string writeFile (const char code[], std::size_t size, const std::string &filename) {
97
+ std::string tmpfile = filename.empty () ? " code.tmp" : filename;
98
+ {
99
+ std::ofstream of (tmpfile , std::ios_base::out | std::ios_base::binary | std::ios_base::trunc );
100
+ of.write (code, size);
101
+ }
102
+ return tmpfile ;
103
+ }
104
+
105
+ static simplecpp::TokenList makeTokenListFromFstream (const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename, simplecpp::OutputList *outputList)
106
+ {
107
+ const std::string tmpfile = writeFile (code, size, filename);
108
+ std::ifstream fin (tmpfile );
109
+ simplecpp::TokenList tokenList (fin, filenames, tmpfile , outputList);
110
+ remove (tmpfile .c_str ());
111
+ return tokenList;
112
+ }
113
+
80
114
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)
81
115
{
82
- std::istringstream istr (std::string (code, size));
83
- return simplecpp::TokenList (istr,filenames,filename,outputList);
116
+ switch (USE_INPUT) {
117
+ case Stringstream: {
118
+ std::istringstream istr (std::string (code, size));
119
+ return simplecpp::TokenList (istr, filenames, filename, outputList);
120
+ }
121
+ case Fstream:
122
+ return makeTokenListFromFstream (code, size, filenames, filename, outputList);
123
+ }
84
124
}
85
125
86
126
static simplecpp::TokenList makeTokenList (const char code[], std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList *outputList=nullptr)
@@ -2422,8 +2462,10 @@ static void cpluscplusDefine()
2422
2462
ASSERT_EQUALS (" \n 201103L" , preprocess (code, dui));
2423
2463
}
2424
2464
2425
- int main (int argc, char **argv)
2465
+ static void runTests (int argc, char **argv, Input input )
2426
2466
{
2467
+ USE_INPUT = input;
2468
+
2427
2469
TEST_CASE (backslash);
2428
2470
2429
2471
TEST_CASE (builtin);
@@ -2628,6 +2670,11 @@ int main(int argc, char **argv)
2628
2670
2629
2671
TEST_CASE (stdcVersionDefine);
2630
2672
TEST_CASE (cpluscplusDefine);
2673
+ }
2631
2674
2675
+ int main (int argc, char **argv)
2676
+ {
2677
+ runTests (argc, argv, Stringstream);
2678
+ runTests (argc, argv, Fstream);
2632
2679
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2633
2680
}
0 commit comments