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)
@@ -2550,8 +2590,10 @@ static void token()
2550
2590
ASSERT_TOKEN (" +22" , false , true , false );
2551
2591
}
2552
2592
2553
- int main (int argc, char **argv)
2593
+ static void runTests (int argc, char **argv, Input input )
2554
2594
{
2595
+ USE_INPUT = input;
2596
+
2555
2597
TEST_CASE (backslash);
2556
2598
2557
2599
TEST_CASE (builtin);
@@ -2762,6 +2804,11 @@ int main(int argc, char **argv)
2762
2804
TEST_CASE (cpluscplusDefine);
2763
2805
2764
2806
TEST_CASE (token);
2807
+ }
2765
2808
2809
+ int main (int argc, char **argv)
2810
+ {
2811
+ runTests (argc, argv, Stringstream);
2812
+ runTests (argc, argv, Fstream);
2766
2813
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2767
2814
}
0 commit comments