|
9 | 9 | #include <cstdint> |
10 | 10 | #include <cstdlib> |
11 | 11 | #include <cstring> |
| 12 | +#include <cstdio> |
12 | 13 | #include <exception> |
| 14 | +#include <fstream> |
13 | 15 | #include <iostream> |
14 | 16 | #include <list> |
15 | 17 | #include <sstream> |
@@ -142,6 +144,20 @@ static std::string readfile(const char code[], std::size_t size, const simplecpp |
142 | 144 | return makeTokenList(code,size,files,std::string(),dui,outputList).stringify(); |
143 | 145 | } |
144 | 146 |
|
| 147 | +/** Write the code to a temporary file and read it with TokenList(filename), so FileStream is used */ |
| 148 | +static std::string readfileFromFile(const char code[], std::size_t size, simplecpp::OutputList *outputList = nullptr) |
| 149 | +{ |
| 150 | + const std::string filename = "testrunner_filestream.tmp"; |
| 151 | + { |
| 152 | + std::ofstream f(filename, std::ios::binary); |
| 153 | + f.write(code, static_cast<std::streamsize>(size)); |
| 154 | + } |
| 155 | + std::vector<std::string> files; |
| 156 | + std::string ret = simplecpp::TokenList(filename, files, {}, outputList).stringify(); |
| 157 | + std::remove(filename.c_str()); |
| 158 | + return ret; |
| 159 | +} |
| 160 | + |
145 | 161 | static std::string preprocess(const char code[], std::size_t size, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage = nullptr, std::list<simplecpp::IfCond> *ifCond = nullptr, const std::string &file = std::string()) |
146 | 162 | { |
147 | 163 | std::vector<std::string> files; |
@@ -3981,6 +3997,11 @@ static void readfile_char_error() |
3981 | 3997 |
|
3982 | 3998 | readfile("A = 's\n'", {}, &outputList); |
3983 | 3999 | ASSERT_EQUALS("file0,1,syntax_error,No pair for character (\'). Can't process file. File is either invalid or unicode, which is currently not supported.\n", toString(outputList)); |
| 4000 | + outputList.clear(); |
| 4001 | + |
| 4002 | + // the ' at the end of the file is not dropped after the peek for a digit separator |
| 4003 | + readfile("A = 1'", {}, &outputList); |
| 4004 | + ASSERT_EQUALS("file0,1,syntax_error,No pair for character (\'). Can't process file. File is either invalid or unicode, which is currently not supported.\n", toString(outputList)); |
3984 | 4005 | } |
3985 | 4006 |
|
3986 | 4007 | static void readfile_string() |
@@ -4168,6 +4189,72 @@ static void readfile_no_eof_newline() |
4168 | 4189 | readfile(code, sizeof(code)-1, {}, &outputList); |
4169 | 4190 | ASSERT_EQUALS("", toString(outputList)); |
4170 | 4191 | } |
| 4192 | + { |
| 4193 | + // CR is a newline |
| 4194 | + const char code[] = "x\r"; |
| 4195 | + simplecpp::OutputList outputList; |
| 4196 | + readfile(code, sizeof(code)-1, {}, &outputList); |
| 4197 | + ASSERT_EQUALS("", toString(outputList)); |
| 4198 | + } |
| 4199 | + { |
| 4200 | + const char code[] = "x\r\n"; |
| 4201 | + simplecpp::OutputList outputList; |
| 4202 | + readfile(code, sizeof(code)-1, {}, &outputList); |
| 4203 | + ASSERT_EQUALS("", toString(outputList)); |
| 4204 | + } |
| 4205 | +} |
| 4206 | + |
| 4207 | +static void readfile_cr_newlines() |
| 4208 | +{ |
| 4209 | + // CR, CR LF and LF are all newlines |
| 4210 | + ASSERT_EQUALS("a\nb\nc", readfile("a\rb\rc\r")); |
| 4211 | + ASSERT_EQUALS("a\nb\nc", readfile("a\r\nb\r\nc\r\n")); |
| 4212 | + ASSERT_EQUALS("a\nb\nc\n\n\nd", readfile("a\rb\r\nc\n\r\rd\n")); |
| 4213 | + ASSERT_EQUALS("# define A 1\nA", readfile("#define A 1\rA\r")); |
| 4214 | +} |
| 4215 | + |
| 4216 | +static void readfile_filestream() |
| 4217 | +{ |
| 4218 | + // the edge cases from the other readfile tests, read from a file |
| 4219 | + { |
| 4220 | + const char code[] = "a\rb\rc\r"; |
| 4221 | + simplecpp::OutputList outputList; |
| 4222 | + ASSERT_EQUALS("a\nb\nc", readfileFromFile(code, sizeof(code)-1, &outputList)); |
| 4223 | + ASSERT_EQUALS("", toString(outputList)); |
| 4224 | + } |
| 4225 | + { |
| 4226 | + const char code[] = "a\r\nb\r\nc\r\n"; |
| 4227 | + ASSERT_EQUALS("a\nb\nc", readfileFromFile(code, sizeof(code)-1)); |
| 4228 | + } |
| 4229 | + { |
| 4230 | + const char code[] = "a\rb\r\nc\n\r\rd\n"; |
| 4231 | + ASSERT_EQUALS("a\nb\nc\n\n\nd", readfileFromFile(code, sizeof(code)-1)); |
| 4232 | + } |
| 4233 | + { |
| 4234 | + const char code[] = "1'"; |
| 4235 | + simplecpp::OutputList outputList; |
| 4236 | + (void)readfileFromFile(code, sizeof(code)-1, &outputList); |
| 4237 | + ASSERT_EQUALS("file0,1,syntax_error,No pair for character (\'). Can't process file. File is either invalid or unicode, which is currently not supported.\n", toString(outputList)); |
| 4238 | + } |
| 4239 | + { |
| 4240 | + // UTF-16LE with CR newlines |
| 4241 | + const char code[] = "\xff\xfe\x78\x00\x0d\x00\x79\x00\x0d\x00"; |
| 4242 | + simplecpp::OutputList outputList; |
| 4243 | + ASSERT_EQUALS("x\ny", readfileFromFile(code, sizeof(code)-1, &outputList)); |
| 4244 | + ASSERT_EQUALS("", toString(outputList)); |
| 4245 | + } |
| 4246 | + { |
| 4247 | + // UTF-16BE with CR newlines |
| 4248 | + const char code[] = "\xfe\xff\x00\x78\x00\x0d\x00\x79\x00\x0d"; |
| 4249 | + ASSERT_EQUALS("x\ny", readfileFromFile(code, sizeof(code)-1)); |
| 4250 | + } |
| 4251 | + { |
| 4252 | + // UTF-16 with an odd number of bytes, the dangling last byte is dropped |
| 4253 | + const char code[] = "\xff\xfe\x31\x00\x0a"; |
| 4254 | + simplecpp::OutputList outputList; |
| 4255 | + ASSERT_EQUALS("1", readfileFromFile(code, sizeof(code)-1, &outputList)); |
| 4256 | + ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList)); |
| 4257 | + } |
4171 | 4258 | } |
4172 | 4259 |
|
4173 | 4260 | static void stringify1() |
@@ -4364,6 +4451,18 @@ static void unicode() |
4364 | 4451 | const char code[] = "\xff\xfe\x0d\x00\x0a\x00\x2f\x00\x2f\x00\x31\x00\x0d\x00\x0a\x00"; |
4365 | 4452 | ASSERT_EQUALS("\n//1", readfile(code, sizeof(code)-1)); |
4366 | 4453 | } |
| 4454 | + { |
| 4455 | + // CR newlines |
| 4456 | + const char code[] = "\xff\xfe\x78\x00\x0d\x00\x79\x00\x0d\x00"; |
| 4457 | + ASSERT_EQUALS("x\ny", readfile(code, sizeof(code)-1)); |
| 4458 | + } |
| 4459 | + { |
| 4460 | + // odd number of bytes, the dangling last byte is dropped |
| 4461 | + const char code[] = "\xff\xfe\x31\x00\x0a"; |
| 4462 | + simplecpp::OutputList outputList; |
| 4463 | + ASSERT_EQUALS("1", readfile(code, sizeof(code)-1, {}, &outputList)); |
| 4464 | + ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList)); |
| 4465 | + } |
4367 | 4466 | } |
4368 | 4467 |
|
4369 | 4468 | static void unicode_invalid() |
@@ -5335,6 +5434,8 @@ static void runTests(int argc, char **argv, Input input) |
5335 | 5434 | TEST_CASE(readfile_error); |
5336 | 5435 | TEST_CASE(readfile_file_not_found); |
5337 | 5436 | TEST_CASE(readfile_no_eof_newline); |
| 5437 | + TEST_CASE(readfile_cr_newlines); |
| 5438 | + TEST_CASE(readfile_filestream); |
5338 | 5439 |
|
5339 | 5440 | TEST_CASE(stringify1); |
5340 | 5441 |
|
|
0 commit comments