UnitTestBot / UTBotCpp

Tool that generates unit test by C/C++ source code, trying to reach all branches and maximize code coverage
Apache License 2.0
158 stars 27 forks source link

Anonymous unions cannot be initialized with `from_bytes` function #623

Closed IDKWNTCMF closed 1 year ago

IDKWNTCMF commented 1 year ago

Description In some cases UTBot tries to construct anonymous union from bytes returned by KLEE. But from_bytes function needs constructed type as a template parameter, so generated tests cannot be compiled.

Example

struct StructWithAnonymousUnion {
   union {
       int x;
       struct {
           char c;
           double d;
       };
       long long *ptr;
   };
};

struct StructWithAnonymousUnion struct_with_anonymous_union_as_return_type(int a, int b) {
   struct StructWithAnonymousUnion ans;
   if (a > b) {
       ans.ptr = 0;
   } else if (a < b) {
       ans.x = 153;
   } else {
       ans.c = 'k';
       ans.d = 1.0101;
   }
   return ans;
}

To Reproduce Steps to reproduce the behavior:

  1. Copy the example above to your project
  2. Generate tests for function struct_with_anonymous_union_as_return_type
  3. Try to run generated tests

Expected behavior Tests are supposed to be executed.

Actual behavior Compilation error.

Generated test

TEST(regression, struct_with_anonymous_union_as_return_type_test1)
{
    // Construct input
    int a = 0;
    int b = 0;

    // Expected output
    struct StructWithAnonymousUnion expected = {
        from_bytes<StructWithAnonymousUnion::>({107, -85, -85, -85, -85, -85, -85, -85, -102, 8, 27, -98, 94, 41, -16, 63})
    };

    // Trigger the function
    struct StructWithAnonymousUnion actual = struct_with_anonymous_union_as_return_type(a, b);

    // Check results
    EXPECT_EQ(expected.x, actual.x);
    EXPECT_EQ(expected.c, actual.c);
    EXPECT_DOUBLE_EQ(expected.d, actual.d);
}

Logs

/home/utbot/UTBotCpp/integration-tests/c-example/utbot_tests/makefiles/lib/structures/structs/../../../../../utbot_tests/lib/structures/structs/complex_structs_dot_c_test.cpp:21:46: error: expected unqualified-id
        from_bytes<StructWithAnonymousUnion::>({107, -85, -85, -85, -85, -85, -85, -85, -102, 8, 27, -98, 94, 41, -16, 63})
                                             ^
1 error generated.