Skip to content

Commit

Permalink
Fixed logic bug in tauShouldDecomposeMacro
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonak committed Dec 11, 2024
1 parent fdff38e commit 39bfc7e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
2 changes: 1 addition & 1 deletion tau/tau.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ static inline int tauShouldDecomposeMacro(const char* const actual, const char*
// number constants since according to the standard identifiers cannot begin with digits.
// This accounts for number constants such as -44, 0xFA, 400000000UL, 0b0010(c++) and 1'000'123(c++).
if(!isStringCmp) {
if (!tauIsNumberConstant(*actual))
if (!tauIsNumberConstant(*actual) || !tauIsNumberConstant(*expected))
return 1;
}
// Inside a string comparison, we search for common expression tokens like the following:
Expand Down
36 changes: 24 additions & 12 deletions test/InternalTests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,72 +261,84 @@ TEST_F(MyTestF, c2) {
TEST(test_tauShouldDecomposeMacro_numbers, decimals)
{
CHECK_EQ(123456789, 123456789);
CHECK_EQ(tauShouldDecomposeMacro("123456789", "123456789", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "123456789", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("123456789", "1", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, decimals_with_sign)
{
CHECK_EQ(-123456789, -123456789);
CHECK_EQ(tauShouldDecomposeMacro("-123456789", "-123456789", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("-123456789", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "-123456789", 0), 0);
CHECK_EQ(+123456789, +123456789);
CHECK_EQ(tauShouldDecomposeMacro("+123456789", "+123456789", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("+123456789", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "+123456789", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, decimals_with_suffix)
{
CHECK_EQ(987654321012345678UL, 987654321012345678UL);
CHECK_EQ(tauShouldDecomposeMacro("987654321012345678UL", "987654321012345678UL", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("987654321012345678UL", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "987654321012345678UL", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, hexadecimals)
{
CHECK_EQ(0xFFFFFFFF, 0xFFFFFFFF);
CHECK_EQ(tauShouldDecomposeMacro("0xFFFFFFFF", "0xFFFFFFFF", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("0xFFFFFFFF", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "0xFFFFFFFF", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, octals)
{
CHECK_EQ(007, 007);
CHECK_EQ(tauShouldDecomposeMacro("007", "007", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "007", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("007", "1", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, floats)
{
CHECK_EQ(420.69, 420.69);
CHECK_EQ(tauShouldDecomposeMacro("420.69", "420.69", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("420.69", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "420.69", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, floats_only_fractional_part)
{
CHECK_EQ(.69, .69);
CHECK_EQ(tauShouldDecomposeMacro(".69", ".69", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro(".69", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", ".69", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_c_identifiers, letters)
{
const int myVar = 1;
CHECK_EQ(myVar, myVar);
CHECK_EQ(tauShouldDecomposeMacro("myVar", "myVar", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("myVar", "a", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("a", "myVar", 0), 1);
}

TEST(test_tauShouldDecomposeMacro_c_identifiers, letters_and_numbers)
{
const int v1 = 1;
CHECK_EQ(v1, v1);
CHECK_EQ(tauShouldDecomposeMacro("v1", "v1", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("v1", "a", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("a", "v1", 0), 1);
}

TEST(test_tauShouldDecomposeMacro_c_identifiers, underscores_and_letters)
{
const int __myVar__ = 1;
CHECK_EQ(__myVar__, __myVar__);
CHECK_EQ(tauShouldDecomposeMacro("__myVar__", "__myVar__", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("__myVar__", "a", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("a", "__myVar__", 0), 1);
}

TEST(test_tauShouldDecomposeMacro_c_identifiers, mixed)
{
#define MYMACRO_2024dec10 1
CHECK_EQ(MYMACRO_2024dec10, MYMACRO_2024dec10);
#undef MYMACRO_2024dec10
CHECK_EQ(tauShouldDecomposeMacro("MYMACRO_2024dec10", "MYMACRO_2024dec10", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("MYMACRO_2024dec10", "a", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("a", "MYMACRO_2024dec10", 0), 1);
}
36 changes: 24 additions & 12 deletions test/InternalTests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,72 +398,84 @@ TEST_F(Stack, TestStackDetails) {
TEST(test_tauShouldDecomposeMacro_numbers, decimals)
{
CHECK_EQ(123456789, 123456789);
CHECK_EQ(tauShouldDecomposeMacro("123456789", "123456789", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "123456789", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("123456789", "1", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, decimals_with_sign)
{
CHECK_EQ(-123456789, -123456789);
CHECK_EQ(tauShouldDecomposeMacro("-123456789", "-123456789", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("-123456789", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "-123456789", 0), 0);
CHECK_EQ(+123456789, +123456789);
CHECK_EQ(tauShouldDecomposeMacro("+123456789", "+123456789", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("+123456789", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "+123456789", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, decimals_with_suffix)
{
CHECK_EQ(987654321012345678UL, 987654321012345678UL);
CHECK_EQ(tauShouldDecomposeMacro("987654321012345678UL", "987654321012345678UL", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("987654321012345678UL", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "987654321012345678UL", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, hexadecimals)
{
CHECK_EQ(0xFFFFFFFF, 0xFFFFFFFF);
CHECK_EQ(tauShouldDecomposeMacro("0xFFFFFFFF", "0xFFFFFFFF", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("0xFFFFFFFF", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "0xFFFFFFFF", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, octals)
{
CHECK_EQ(007, 007);
CHECK_EQ(tauShouldDecomposeMacro("007", "007", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "007", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("007", "1", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, floats)
{
CHECK_EQ(420.69, 420.69);
CHECK_EQ(tauShouldDecomposeMacro("420.69", "420.69", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("420.69", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", "420.69", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_numbers, floats_only_fractional_part)
{
CHECK_EQ(.69, .69);
CHECK_EQ(tauShouldDecomposeMacro(".69", ".69", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro(".69", "1", 0), 0);
CHECK_EQ(tauShouldDecomposeMacro("1", ".69", 0), 0);
}

TEST(test_tauShouldDecomposeMacro_c_identifiers, letters)
{
const int myVar = 1;
CHECK_EQ(myVar, myVar);
CHECK_EQ(tauShouldDecomposeMacro("myVar", "myVar", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("myVar", "a", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("a", "myVar", 0), 1);
}

TEST(test_tauShouldDecomposeMacro_c_identifiers, letters_and_numbers)
{
const int v1 = 1;
CHECK_EQ(v1, v1);
CHECK_EQ(tauShouldDecomposeMacro("v1", "v1", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("v1", "a", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("a", "v1", 0), 1);
}

TEST(test_tauShouldDecomposeMacro_c_identifiers, underscores_and_letters)
{
const int __myVar__ = 1;
CHECK_EQ(__myVar__, __myVar__);
CHECK_EQ(tauShouldDecomposeMacro("__myVar__", "__myVar__", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("__myVar__", "a", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("a", "__myVar__", 0), 1);
}

TEST(test_tauShouldDecomposeMacro_c_identifiers, mixed)
{
#define MYMACRO_2024dec10 1
CHECK_EQ(MYMACRO_2024dec10, MYMACRO_2024dec10);
#undef MYMACRO_2024dec10
CHECK_EQ(tauShouldDecomposeMacro("MYMACRO_2024dec10", "MYMACRO_2024dec10", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("MYMACRO_2024dec10", "a", 0), 1);
CHECK_EQ(tauShouldDecomposeMacro("a", "MYMACRO_2024dec10", 0), 1);
}

0 comments on commit 39bfc7e

Please sign in to comment.