Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add tests
  • Loading branch information
ludviggunne committed Jul 9, 2026
commit 0618d9e5bd80ccafaf52bf0bef0a024f667b2fed
47 changes: 47 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,52 @@ static void ifUndefFuncStyleMacro()
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, undefined function-like macro invocation: A( ... )\n", toString(outputList));
}

static void testIfIntegerLiteral(const std::string &value, const std::string &literal, bool expectError)
{
const std::string code = "#define A " + value + "\n"
"#if A == " + literal + "\n"
"int x = " + literal + ";\n"
"#endif\n";

simplecpp::OutputList outputList;

std::string literalPreprocessed = literal;
if (literalPreprocessed[0] == '-')
{
literalPreprocessed = "- " + literalPreprocessed.substr(1);
}

if (expectError) {
ASSERT_EQUALS("", preprocess(code.c_str(), &outputList));
ASSERT_EQUALS("file0,2,syntax_error,failed to evaluate #if condition, invalid integer literal\n", toString(outputList));
} else {
ASSERT_EQUALS("\n\nint x = " + literalPreprocessed + " ;", preprocess(code.c_str(), &outputList));
ASSERT_EQUALS("", toString(outputList));
}
}

static void ifIntegerLiteral()
{
testIfIntegerLiteral("123", "123", false);
testIfIntegerLiteral("-123", "-123", false);

testIfIntegerLiteral("123", "0x7b", false);
testIfIntegerLiteral("-123", "-0x7b", false);

testIfIntegerLiteral("123", "0x7B", false);
testIfIntegerLiteral("-123", "-0x7B", false);

testIfIntegerLiteral("123", "0173", false);
testIfIntegerLiteral("-123", "-0173", false);

testIfIntegerLiteral("123", "0b1111011", false);
testIfIntegerLiteral("-123", "-0b1111011", false);

testIfIntegerLiteral("123", "0xGH", true);
testIfIntegerLiteral("123", "019", true);
testIfIntegerLiteral("123", "0b30", true);
}

static void location1()
{
const char *code;
Expand Down Expand Up @@ -4141,6 +4187,7 @@ static void runTests(int argc, char **argv, Input input)
TEST_CASE(ifalt); // using "and", "or", etc
TEST_CASE(ifexpr);
TEST_CASE(ifUndefFuncStyleMacro);
TEST_CASE(ifIntegerLiteral);

TEST_CASE(location1);
TEST_CASE(location2);
Expand Down