Skip to content

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

@tyuldashev

Description

@tyuldashev

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);
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingc-syntaxRelated to generation tests for CverifiedBug fix is verifiedwrong generationInadequate or empty test suite generated

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions