22
22
#include < cstdlib>
23
23
#include < cstring>
24
24
#include < exception>
25
+ #include < fstream>
25
26
#include < iostream>
27
+ #include < limits>
26
28
#include < map>
27
29
#include < sstream>
28
30
#include < stdexcept>
29
31
#include < string>
30
32
#include < vector>
31
33
34
+ enum Input {
35
+ Stringstream,
36
+ Fstream
37
+ };
38
+
39
+ static Input USE_INPUT = Stringstream;
32
40
static int numberOfFailedAssertions = 0 ;
33
41
34
42
#define ASSERT_EQUALS (expected, actual ) (assertEquals((expected), (actual), __LINE__))
@@ -45,11 +53,20 @@ static std::string pprint(const std::string &in)
45
53
return ret;
46
54
}
47
55
56
+ static const char * inputString (Input input) {
57
+ switch (input) {
58
+ case Stringstream:
59
+ return " Stringstream" ;
60
+ case Fstream:
61
+ return " Fstream" ;
62
+ }
63
+ }
64
+
48
65
static int assertEquals (const std::string &expected, const std::string &actual, int line)
49
66
{
50
67
if (expected != actual) {
51
68
numberOfFailedAssertions++;
52
- std::cerr << " ------ assertion failed ---------" << std::endl;
69
+ std::cerr << " ------ assertion failed ( " << inputString (USE_INPUT) << " ) ---------" << std::endl;
53
70
std::cerr << " line " << line << std::endl;
54
71
std::cerr << " expected:" << pprint (expected) << std::endl;
55
72
std::cerr << " actual:" << pprint (actual) << std::endl;
@@ -84,10 +101,34 @@ static void testcase(const std::string &name, void (*f)(), int argc, char * cons
84
101
85
102
#define TEST_CASE (F ) (testcase(#F, F, argc, argv))
86
103
104
+ static std::string writeFile (const char code[], std::size_t size, const std::string &filename) {
105
+ std::string tmpfile = filename.empty () ? " code.tmp" : filename;
106
+ {
107
+ std::ofstream of (tmpfile , std::ios_base::out | std::ios_base::binary | std::ios_base::trunc );
108
+ of.write (code, size);
109
+ }
110
+ return tmpfile ;
111
+ }
112
+
113
+ static simplecpp::TokenList makeTokenListFromFstream (const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename, simplecpp::OutputList *outputList)
114
+ {
115
+ const std::string tmpfile = writeFile (code, size, filename);
116
+ std::ifstream fin (tmpfile );
117
+ simplecpp::TokenList tokenList (fin, filenames, tmpfile , outputList);
118
+ remove (tmpfile .c_str ());
119
+ return tokenList;
120
+ }
121
+
87
122
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)
88
123
{
89
- std::istringstream istr (std::string (code, size));
90
- return simplecpp::TokenList (istr,filenames,filename,outputList);
124
+ switch (USE_INPUT) {
125
+ case Stringstream: {
126
+ std::istringstream istr (std::string (code, size));
127
+ return simplecpp::TokenList (istr, filenames, filename, outputList);
128
+ }
129
+ case Fstream:
130
+ return makeTokenListFromFstream (code, size, filenames, filename, outputList);
131
+ }
91
132
}
92
133
93
134
static simplecpp::TokenList makeTokenList (const char code[], std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList *outputList=nullptr)
@@ -2624,8 +2665,10 @@ static void token()
2624
2665
ASSERT_TOKEN (" +22" , false , true , false );
2625
2666
}
2626
2667
2627
- int main (int argc, char **argv)
2668
+ static void runTests (int argc, char **argv, Input input )
2628
2669
{
2670
+ USE_INPUT = input;
2671
+
2629
2672
TEST_CASE (backslash);
2630
2673
2631
2674
TEST_CASE (builtin);
@@ -2842,6 +2885,11 @@ int main(int argc, char **argv)
2842
2885
TEST_CASE (cpluscplusDefine);
2843
2886
2844
2887
TEST_CASE (token);
2888
+ }
2845
2889
2890
+ int main (int argc, char **argv)
2891
+ {
2892
+ runTests (argc, argv, Stringstream);
2893
+ runTests (argc, argv, Fstream);
2846
2894
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2847
2895
}
0 commit comments