Skip to content

Commit bd34628

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

1 file changed

Lines changed: 51 additions & 4 deletions

File tree

test.cpp

Lines changed: 51 additions & 4 deletions
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)
@@ -2362,8 +2402,10 @@ static void cpluscplusDefine()
23622402
ASSERT_EQUALS("\n201103L", preprocess(code, dui));
23632403
}
23642404

2365-
int main(int argc, char **argv)
2405+
static void runTests(int argc, char **argv, Input input)
23662406
{
2407+
USE_INPUT = input;
2408+
23672409
TEST_CASE(backslash);
23682410

23692411
TEST_CASE(builtin);
@@ -2560,6 +2602,11 @@ int main(int argc, char **argv)
25602602

25612603
TEST_CASE(stdcVersionDefine);
25622604
TEST_CASE(cpluscplusDefine);
2605+
}
25632606

2607+
int main(int argc, char **argv)
2608+
{
2609+
runTests(argc, argv, Stringstream);
2610+
runTests(argc, argv, Fstream);
25642611
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
25652612
}

0 commit comments

Comments
 (0)