Skip to content

Commit 563457c

Browse files
committed
test
1 parent b1a8c29 commit 563457c

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

simplecpp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ class simplecpp::TokenList::Stream {
259259
virtual bool good() = 0;
260260

261261
unsigned char readChar() {
262-
unsigned char ch = static_cast<unsigned char>(get());
262+
unsigned char ch = static_cast<unsigned char>(get()); // TODO: check EOF?
263263

264264
// For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
265265
// character is non-ASCII character then replace it with 0xff
266266
if (isUtf16) {
267-
const unsigned char ch2 = static_cast<unsigned char>(get());
267+
const unsigned char ch2 = static_cast<unsigned char>(get()); // TODO: check EOF?
268268
const int ch16 = makeUtf16Char(ch, ch2);
269269
ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16));
270270
}
@@ -287,13 +287,13 @@ class simplecpp::TokenList::Stream {
287287
}
288288

289289
unsigned char peekChar() {
290-
unsigned char ch = static_cast<unsigned char>(peek());
290+
unsigned char ch = static_cast<unsigned char>(peek()); // TODO: check EOF?
291291

292292
// For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
293293
// character is non-ASCII character then replace it with 0xff
294294
if (isUtf16) {
295295
(void)get();
296-
const unsigned char ch2 = static_cast<unsigned char>(peek());
296+
const unsigned char ch2 = static_cast<unsigned char>(peek()); // TODO: check EOF?
297297
unget();
298298
const int ch16 = makeUtf16Char(ch, ch2);
299299
ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16));

test.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2504,7 +2504,7 @@ static void readfile_nullbyte()
25042504
const char code[] = "ab\0cd";
25052505
simplecpp::OutputList outputList;
25062506
ASSERT_EQUALS("ab cd", readfile(code,sizeof(code), &outputList));
2507-
ASSERT_EQUALS(true, outputList.empty()); // should warning be written?
2507+
ASSERT_EQUALS(true, outputList.empty()); // TODO: should warning be written?
25082508
}
25092509

25102510
static void readfile_char()
@@ -2654,6 +2654,41 @@ static void readfile_file_not_found()
26542654
ASSERT_EQUALS("file0,1,file_not_found,File is missing: NotAFile\n", toString(outputList));
26552655
}
26562656

2657+
static void readfile_empty()
2658+
{
2659+
const char code[] = "";
2660+
simplecpp::OutputList outputList;
2661+
ASSERT_EQUALS("", readfile(code,sizeof(code), &outputList));
2662+
ASSERT_EQUALS(true, outputList.empty());
2663+
}
2664+
2665+
// the BOM/UTF-16 detection reads two bytes
2666+
static void readfile_onebyte()
2667+
{
2668+
const char code[] = ".";
2669+
simplecpp::OutputList outputList;
2670+
ASSERT_EQUALS(".", readfile(code,sizeof(code), &outputList));
2671+
ASSERT_EQUALS(true, outputList.empty());
2672+
}
2673+
2674+
static void readfile_utf16_unsupported()
2675+
{
2676+
const char code[] = "\xfe\xff\xd8\x3d\xde\x42"; // smiley emoji
2677+
simplecpp::OutputList outputList;
2678+
ASSERT_EQUALS("", readfile(code,sizeof(code), &outputList));
2679+
ASSERT_EQUALS("file0,1,unhandled_char_error,The code contains unhandled character(s) (character code=255). Neither unicode nor extended ascii is supported.\n", toString(outputList));
2680+
}
2681+
2682+
static void readfile_utf16_incomplete()
2683+
{
2684+
const char code[] = "\xfe\xff\x00\x31\x00\x32\x00"; // the last UTF16 char is incomplete
2685+
simplecpp::OutputList outputList;
2686+
ASSERT_EQUALS("12", readfile(code,sizeof(code), &outputList));
2687+
ASSERT_EQUALS(true, outputList.empty());
2688+
}
2689+
2690+
// TODO: test with incomplete BOMs
2691+
26572692
static void stringify1()
26582693
{
26592694
const char code_c[] = "#include \"A.h\"\n"
@@ -3532,6 +3567,10 @@ int main(int argc, char **argv)
35323567
TEST_CASE(readfile_unhandled_chars);
35333568
TEST_CASE(readfile_error);
35343569
TEST_CASE(readfile_file_not_found);
3570+
TEST_CASE(readfile_empty);
3571+
TEST_CASE(readfile_onebyte);
3572+
TEST_CASE(readfile_utf16_unsupported);
3573+
TEST_CASE(readfile_utf16_incomplete);
35353574

35363575
TEST_CASE(stringify1);
35373576

0 commit comments

Comments
 (0)