Skip to content

Commit fa662cc

Browse files
committed
test
1 parent dee53af commit fa662cc

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

simplecpp.cpp

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

256256
unsigned char readChar() {
257-
auto ch = static_cast<unsigned char>(get());
257+
auto ch = static_cast<unsigned char>(get()); // TODO: check EOF?
258258

259259
// For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
260260
// character is non-ASCII character then replace it with 0xff
261261
if (isUtf16) {
262-
const auto ch2 = static_cast<unsigned char>(get());
262+
const auto ch2 = static_cast<unsigned char>(get()); // TODO: check EOF?
263263
const int ch16 = makeUtf16Char(ch, ch2);
264264
ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16));
265265
}
@@ -282,13 +282,13 @@ class simplecpp::TokenList::Stream {
282282
}
283283

284284
unsigned char peekChar() {
285-
auto ch = static_cast<unsigned char>(peek());
285+
auto ch = static_cast<unsigned char>(peek()); // TODO: check EOF?
286286

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

test.cpp

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2679,7 +2679,7 @@ static void readfile_nullbyte()
26792679
const char code[] = "ab\0cd";
26802680
simplecpp::OutputList outputList;
26812681
ASSERT_EQUALS("ab cd", readfile(code,sizeof(code), &outputList));
2682-
ASSERT_EQUALS(true, outputList.empty()); // should warning be written?
2682+
ASSERT_EQUALS(true, outputList.empty()); // TODO: should warning be written?
26832683
}
26842684

26852685
static void readfile_char()
@@ -2829,6 +2829,71 @@ static void readfile_file_not_found()
28292829
ASSERT_EQUALS("file0,0,file_not_found,File is missing: NotAFile\n", toString(outputList));
28302830
}
28312831

2832+
static void readfile_empty()
2833+
{
2834+
const char code[] = "";
2835+
simplecpp::OutputList outputList;
2836+
ASSERT_EQUALS("", readfile(code,sizeof(code), &outputList));
2837+
ASSERT_EQUALS(true, outputList.empty());
2838+
}
2839+
2840+
// the BOM/UTF-16 detection reads two bytes
2841+
static void readfile_onebyte()
2842+
{
2843+
const char code[] = ".";
2844+
simplecpp::OutputList outputList;
2845+
ASSERT_EQUALS(".", readfile(code,sizeof(code), &outputList));
2846+
ASSERT_EQUALS(true, outputList.empty());
2847+
}
2848+
2849+
static void readfile_utf16_le_unsupported()
2850+
{
2851+
const char code[] = "\xfe\xff\xd8\x3d\xde\x42"; // smiley emoji
2852+
simplecpp::OutputList outputList;
2853+
ASSERT_EQUALS("", readfile(code,sizeof(code), &outputList));
2854+
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));
2855+
}
2856+
2857+
static void readfile_utf16_be_unsupported()
2858+
{
2859+
const char code[] = "\xff\xfe\x3d\xd8\x42\xde"; // smiley emoji
2860+
simplecpp::OutputList outputList;
2861+
ASSERT_EQUALS("", readfile(code,sizeof(code), &outputList));
2862+
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));
2863+
}
2864+
2865+
static void readfile_utf16_le_incomplete()
2866+
{
2867+
const char code[] = "\xfe\xff\x00\x31\x00\x32\x00"; // the last UTF16 char is incomplete
2868+
simplecpp::OutputList outputList;
2869+
ASSERT_EQUALS("12", readfile(code,sizeof(code), &outputList));
2870+
ASSERT_EQUALS(true, outputList.empty());
2871+
}
2872+
2873+
static void readfile_utf16_be_incomplete()
2874+
{
2875+
const char code[] = "\xff\xfe\x31\x00\x32\x00\x33"; // the last UTF16 char is incomplete
2876+
simplecpp::OutputList outputList;
2877+
ASSERT_EQUALS("12", readfile(code,sizeof(code), &outputList));
2878+
ASSERT_EQUALS(true, outputList.empty());
2879+
}
2880+
2881+
static void readfile_utf16_le_bom_incomplete()
2882+
{
2883+
const char code[] = "\xfe";
2884+
simplecpp::OutputList outputList;
2885+
ASSERT_EQUALS("12", readfile(code,sizeof(code), &outputList));
2886+
ASSERT_EQUALS(true, outputList.empty());
2887+
}
2888+
2889+
static void readfile_utf16_be_bom_incomplete()
2890+
{
2891+
const char code[] = "\xff";
2892+
simplecpp::OutputList outputList;
2893+
ASSERT_EQUALS("12", readfile(code,sizeof(code), &outputList));
2894+
ASSERT_EQUALS(true, outputList.empty());
2895+
}
2896+
28322897
static void stringify1()
28332898
{
28342899
const char code_c[] = "#include \"A.h\"\n"
@@ -3869,6 +3934,14 @@ static void runTests(int argc, char **argv, Input input)
38693934
TEST_CASE(readfile_unhandled_chars);
38703935
TEST_CASE(readfile_error);
38713936
TEST_CASE(readfile_file_not_found);
3937+
TEST_CASE(readfile_empty);
3938+
TEST_CASE(readfile_onebyte);
3939+
TEST_CASE(readfile_utf16_le_unsupported);
3940+
TEST_CASE(readfile_utf16_be_unsupported);
3941+
TEST_CASE(readfile_utf16_le_incomplete);
3942+
TEST_CASE(readfile_utf16_be_incomplete);
3943+
TEST_CASE(readfile_utf16_le_bom_incomplete);
3944+
TEST_CASE(readfile_utf16_be_bom_incomplete);
38723945

38733946
TEST_CASE(stringify1);
38743947

0 commit comments

Comments
 (0)