Skip to content

Commit eeb1f8c

Browse files
committed
test.cpp: run tests with std::istringstream and std::ifstream
1 parent 5c88852 commit eeb1f8c

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

test.cpp

+51-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
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;
2532
static 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+
4157
static 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+
80114
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)
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

86126
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()
24222462
ASSERT_EQUALS("\n201103L", preprocess(code, dui));
24232463
}
24242464

2425-
int main(int argc, char **argv)
2465+
static void runTests(int argc, char **argv, Input input)
24262466
{
2467+
USE_INPUT = input;
2468+
24272469
TEST_CASE(backslash);
24282470

24292471
TEST_CASE(builtin);
@@ -2628,6 +2670,11 @@ int main(int argc, char **argv)
26282670

26292671
TEST_CASE(stdcVersionDefine);
26302672
TEST_CASE(cpluscplusDefine);
2673+
}
26312674

2675+
int main(int argc, char **argv)
2676+
{
2677+
runTests(argc, argv, Stringstream);
2678+
runTests(argc, argv, Fstream);
26322679
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
26332680
}

0 commit comments

Comments
 (0)