Skip to content

Commit 763753d

Browse files
committed
test
1 parent b9cec00 commit 763753d

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

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-
auto ch = static_cast<unsigned char>(get());
262+
auto 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 auto ch2 = static_cast<unsigned char>(get());
267+
const auto 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-
auto ch = static_cast<unsigned char>(peek());
290+
auto 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 auto ch2 = static_cast<unsigned char>(peek());
296+
const auto 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
@@ -2575,7 +2575,7 @@ static void readfile_nullbyte()
25752575
const char code[] = "ab\0cd";
25762576
simplecpp::OutputList outputList;
25772577
ASSERT_EQUALS("ab cd", readfile(code,sizeof(code), &outputList));
2578-
ASSERT_EQUALS(true, outputList.empty()); // should warning be written?
2578+
ASSERT_EQUALS(true, outputList.empty()); // TODO: should warning be written?
25792579
}
25802580

25812581
static void readfile_char()
@@ -2725,6 +2725,41 @@ static void readfile_file_not_found()
27252725
ASSERT_EQUALS("file0,1,file_not_found,File is missing: NotAFile\n", toString(outputList));
27262726
}
27272727

2728+
static void readfile_empty()
2729+
{
2730+
const char code[] = "";
2731+
simplecpp::OutputList outputList;
2732+
ASSERT_EQUALS("", readfile(code,sizeof(code), &outputList));
2733+
ASSERT_EQUALS(true, outputList.empty());
2734+
}
2735+
2736+
// the BOM/UTF-16 detection reads two bytes
2737+
static void readfile_onebyte()
2738+
{
2739+
const char code[] = ".";
2740+
simplecpp::OutputList outputList;
2741+
ASSERT_EQUALS(".", readfile(code,sizeof(code), &outputList));
2742+
ASSERT_EQUALS(true, outputList.empty());
2743+
}
2744+
2745+
static void readfile_utf16_unsupported()
2746+
{
2747+
const char code[] = "\xfe\xff\xd8\x3d\xde\x42"; // smiley emoji
2748+
simplecpp::OutputList outputList;
2749+
ASSERT_EQUALS("", readfile(code,sizeof(code), &outputList));
2750+
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));
2751+
}
2752+
2753+
static void readfile_utf16_incomplete()
2754+
{
2755+
const char code[] = "\xfe\xff\x00\x31\x00\x32\x00"; // the last UTF16 char is incomplete
2756+
simplecpp::OutputList outputList;
2757+
ASSERT_EQUALS("12", readfile(code,sizeof(code), &outputList));
2758+
ASSERT_EQUALS(true, outputList.empty());
2759+
}
2760+
2761+
// TODO: test with incomplete BOMs
2762+
27282763
static void stringify1()
27292764
{
27302765
const char code_c[] = "#include \"A.h\"\n"
@@ -3609,6 +3644,10 @@ int main(int argc, char **argv)
36093644
TEST_CASE(readfile_unhandled_chars);
36103645
TEST_CASE(readfile_error);
36113646
TEST_CASE(readfile_file_not_found);
3647+
TEST_CASE(readfile_empty);
3648+
TEST_CASE(readfile_onebyte);
3649+
TEST_CASE(readfile_utf16_unsupported);
3650+
TEST_CASE(readfile_utf16_incomplete);
36123651

36133652
TEST_CASE(stringify1);
36143653

0 commit comments

Comments
 (0)