1818
1919#include < iostream>
2020#include < limits>
21+ #include < fstream>
2122#include < sstream>
2223#include < vector>
2324#include " simplecpp.h"
2425
26+ enum Input {
27+ Stringstream,
28+ Fstream
29+ };
30+
31+ static Input USE_INPUT = Stringstream;
2532static int numberOfFailedAssertions = 0 ;
2633
2734#define ASSERT_EQUALS (expected, actual ) (assertEquals((expected), (actual), __LINE__))
@@ -38,11 +45,20 @@ static std::string pprint(const std::string &in)
3845 return ret;
3946}
4047
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+
4157static int assertEquals (const std::string &expected, const std::string &actual, int line)
4258{
4359 if (expected != actual) {
4460 numberOfFailedAssertions++;
45- std::cerr << " ------ assertion failed ---------" << std::endl;
61+ std::cerr << " ------ assertion failed ( " << inputString (USE_INPUT) << " ) ---------" << std::endl;
4662 std::cerr << " line " << line << std::endl;
4763 std::cerr << " expected:" << pprint (expected) << std::endl;
4864 std::cerr << " actual:" << pprint (actual) << std::endl;
@@ -77,10 +93,34 @@ static void testcase(const std::string &name, void (*f)(), int argc, char * cons
7793
7894#define TEST_CASE (F ) (testcase(#F, F, argc, argv))
7995
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+
80114static 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)
81115{
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+ }
84124}
85125
86126static simplecpp::TokenList makeTokenList (const char code[], std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList *outputList=nullptr)
@@ -2538,8 +2578,10 @@ static void token()
25382578 ASSERT_TOKEN (" +22" , false , true , false );
25392579}
25402580
2541- int main (int argc, char **argv)
2581+ static void runTests (int argc, char **argv, Input input )
25422582{
2583+ USE_INPUT = input;
2584+
25432585 TEST_CASE (backslash);
25442586
25452587 TEST_CASE (builtin);
@@ -2749,6 +2791,11 @@ int main(int argc, char **argv)
27492791 TEST_CASE (cpluscplusDefine);
27502792
27512793 TEST_CASE (token);
2794+ }
27522795
2796+ int main (int argc, char **argv)
2797+ {
2798+ runTests (argc, argv, Stringstream);
2799+ runTests (argc, argv, Fstream);
27532800 return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
27542801}
0 commit comments