Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Anonymous union initialized using 'from_bytes' in some cases #447

Closed
tyuldashev opened this issue Sep 8, 2022 · 2 comments · Fixed by #467
Closed

[BUG] Anonymous union initialized using 'from_bytes' in some cases #447

tyuldashev opened this issue Sep 8, 2022 · 2 comments · Fixed by #467
Assignees
Labels
bug Something isn't working c-syntax Related to generation tests for C verified Bug fix is verified wrong generation Inadequate or empty test suite generated

Comments

@tyuldashev
Copy link
Collaborator

To Reproduce
Generate tests for following code sample:

struct WithAnonymous {
    union {
        int i, j;
    };
    int m;
};

int count_equal_members(struct WithAnonymous st) {
    if (st.i == st.m) {
        return 2;
    }
    return 1;
}

Resulting tests are:

TEST(regression, count_equal_members_test_1)
{
    int actual = count_equal_members(from_bytes<WithAnonymous>({0, 0, 0, 0, 0, 0, 0, 0}));
    EXPECT_EQ(2, actual);
}

TEST(regression, count_equal_members_test_2)
{
    int actual = count_equal_members(from_bytes<WithAnonymous>({2, 0, 0, 0, 0, 0, 0, 0}));
    EXPECT_EQ(1, actual);
}

Note that parameter initialized using from_bytes function which doesn't look user friendly.

Expected that it would be initialized with explicit values
Similar to what is generated in case when union has identificator.

struct WithAnonymous {
    union {
        int i, j;
    } io;
    int m;
};

For sample above more readable tests are generated, where is from_bytes is not used.

TEST(regression, count_equal_members_test_1)
{
    int actual = count_equal_members({
        .io = {
            .i = 0
            // .j = 0
        },
        .m = 0
    });
    EXPECT_EQ(2, actual);
}

TEST(regression, count_equal_members_test_2)
{
    int actual = count_equal_members({
        .io = {
            .i = 2
            // .j = 2
        },
        .m = 0
    });
    EXPECT_EQ(1, actual);
}
@tyuldashev tyuldashev added bug Something isn't working c-syntax Related to generation tests for C wrong generation Inadequate or empty test suite generated labels Sep 8, 2022
@tyuldashev tyuldashev moved this to Todo in UTBot C/C++ Sep 8, 2022
@alexey-utkin
Copy link
Collaborator

It was not trivial, but I did.
Now it generates

TEST(regression, count_equal_members_test_1)
{
    // Construct input
    struct WithAnonymous st = {
        /* { */
            .i = 0
            // .j = 0
        /* } */,
        .m = 0
    };

    // Expected output
    int expected = 2;

    // Trigger the function
    int actual = count_equal_members(st);

    // Check results
    EXPECT_EQ(expected, actual);
}

@tyuldashev
Copy link
Collaborator Author

Looks great, thanks. Only block comments around lonely curly bracket looks a bit weird /* { */

@tyuldashev tyuldashev added the verified Bug fix is verified label Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working c-syntax Related to generation tests for C verified Bug fix is verified wrong generation Inadequate or empty test suite generated
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants