Skip to content

Commit f743f3c

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

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

test.cpp

+52-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@
2222
#include <cstdlib>
2323
#include <cstring>
2424
#include <exception>
25+
#include <fstream>
2526
#include <iostream>
27+
#include <limits>
2628
#include <map>
2729
#include <sstream>
2830
#include <stdexcept>
2931
#include <string>
3032
#include <vector>
3133

34+
enum Input {
35+
Stringstream,
36+
Fstream
37+
};
38+
39+
static Input USE_INPUT = Stringstream;
3240
static int numberOfFailedAssertions = 0;
3341

3442
#define ASSERT_EQUALS(expected, actual) (assertEquals((expected), (actual), __LINE__))
@@ -45,11 +53,20 @@ static std::string pprint(const std::string &in)
4553
return ret;
4654
}
4755

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+
4865
static int assertEquals(const std::string &expected, const std::string &actual, int line)
4966
{
5067
if (expected != actual) {
5168
numberOfFailedAssertions++;
52-
std::cerr << "------ assertion failed ---------" << std::endl;
69+
std::cerr << "------ assertion failed (" << inputString(USE_INPUT) << ") ---------" << std::endl;
5370
std::cerr << "line " << line << std::endl;
5471
std::cerr << "expected:" << pprint(expected) << std::endl;
5572
std::cerr << "actual:" << pprint(actual) << std::endl;
@@ -84,10 +101,34 @@ static void testcase(const std::string &name, void (*f)(), int argc, char * cons
84101

85102
#define TEST_CASE(F) (testcase(#F, F, argc, argv))
86103

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+
87122
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)
88123
{
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+
}
91132
}
92133

93134
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()
26242665
ASSERT_TOKEN("+22", false, true, false);
26252666
}
26262667

2627-
int main(int argc, char **argv)
2668+
static void runTests(int argc, char **argv, Input input)
26282669
{
2670+
USE_INPUT = input;
2671+
26292672
TEST_CASE(backslash);
26302673

26312674
TEST_CASE(builtin);
@@ -2842,6 +2885,11 @@ int main(int argc, char **argv)
28422885
TEST_CASE(cpluscplusDefine);
28432886

28442887
TEST_CASE(token);
2888+
}
28452889

2890+
int main(int argc, char **argv)
2891+
{
2892+
runTests(argc, argv, Stringstream);
2893+
runTests(argc, argv, Fstream);
28462894
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
28472895
}

0 commit comments

Comments
 (0)