Skip to content

Commit a00088b

Browse files
committed
test.cpp: run tests with std::istringstream, std::ifstream and file input
1 parent 9eb8e18 commit a00088b

1 file changed

Lines changed: 65 additions & 4 deletions

File tree

test.cpp

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@
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+
File
30+
};
31+
32+
static Input USE_INPUT = Stringstream;
2533
static int numberOfFailedAssertions = 0;
2634

2735
#define ASSERT_EQUALS(expected, actual) (assertEquals((expected), (actual), __LINE__))
@@ -38,11 +46,22 @@ static std::string pprint(const std::string &in)
3846
return ret;
3947
}
4048

49+
static const char* inputString(Input input) {
50+
switch (input) {
51+
case Stringstream:
52+
return "Stringstream";
53+
case Fstream:
54+
return "Fstream";
55+
case File:
56+
return "File";
57+
}
58+
}
59+
4160
static int assertEquals(const std::string &expected, const std::string &actual, int line)
4261
{
4362
if (expected != actual) {
4463
numberOfFailedAssertions++;
45-
std::cerr << "------ assertion failed ---------" << std::endl;
64+
std::cerr << "------ assertion failed (" << inputString(USE_INPUT) << ") ---------" << std::endl;
4665
std::cerr << "line " << line << std::endl;
4766
std::cerr << "expected:" << pprint(expected) << std::endl;
4867
std::cerr << "actual:" << pprint(actual) << std::endl;
@@ -77,10 +96,44 @@ static void testcase(const std::string &name, void (*f)(), int argc, char * cons
7796

7897
#define TEST_CASE(F) (testcase(#F, F, argc, argv))
7998

99+
static std::string writeFile(const char code[], std::size_t size, const std::string &filename) {
100+
std::string tmpfile = filename.empty() ? "code.tmp" : filename;
101+
{
102+
std::ofstream of(tmpfile, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
103+
of.write(code, size);
104+
}
105+
return tmpfile;
106+
}
107+
108+
static simplecpp::TokenList makeTokenListFromFstream(const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename, simplecpp::OutputList *outputList)
109+
{
110+
const std::string tmpfile = writeFile(code, size, filename);
111+
std::ifstream fin(tmpfile);
112+
simplecpp::TokenList tokenList(fin, filenames, tmpfile, outputList);
113+
remove(tmpfile.c_str());
114+
return tokenList;
115+
}
116+
117+
static simplecpp::TokenList makeTokenListFromFile(const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename, simplecpp::OutputList *outputList)
118+
{
119+
const std::string tmpfile = writeFile(code, size, filename);
120+
simplecpp::TokenList tokenList(filenames, tmpfile, outputList);
121+
remove(tmpfile.c_str());
122+
return tokenList;
123+
}
124+
80125
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)
81126
{
82-
std::istringstream istr(std::string(code, size));
83-
return simplecpp::TokenList(istr,filenames,filename,outputList);
127+
switch (USE_INPUT) {
128+
case Stringstream: {
129+
std::istringstream istr(std::string(code, size));
130+
return simplecpp::TokenList(istr, filenames, filename, outputList);
131+
}
132+
case Fstream:
133+
return makeTokenListFromFstream(code, size, filenames, filename, outputList);
134+
case File:
135+
return makeTokenListFromFile(code, size, filenames, filename, outputList);
136+
}
84137
}
85138

86139
static simplecpp::TokenList makeTokenList(const char code[], std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList *outputList=nullptr)
@@ -2362,8 +2415,10 @@ static void cpluscplusDefine()
23622415
ASSERT_EQUALS("\n201103L", preprocess(code, dui));
23632416
}
23642417

2365-
int main(int argc, char **argv)
2418+
static void runTests(int argc, char **argv, Input input)
23662419
{
2420+
USE_INPUT = input;
2421+
23672422
TEST_CASE(backslash);
23682423

23692424
TEST_CASE(builtin);
@@ -2560,6 +2615,12 @@ int main(int argc, char **argv)
25602615

25612616
TEST_CASE(stdcVersionDefine);
25622617
TEST_CASE(cpluscplusDefine);
2618+
}
25632619

2620+
int main(int argc, char **argv)
2621+
{
2622+
runTests(argc, argv, Stringstream);
2623+
runTests(argc, argv, Fstream);
2624+
runTests(argc, argv, File);
25642625
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
25652626
}

0 commit comments

Comments
 (0)