Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
This fixes the issue of comparison operators not returning true for
unions with default cases not associated with a type for said default
case
Also added unittests for union comparison operators

Signed-off-by: Martijn Reicher <[email protected]>
  • Loading branch information
reicheratwork committed Nov 6, 2023
1 parent 0ddd39b commit f7b2bf9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/ddscxx/tests/Regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,63 @@ TEST_F(Regression, unaligned_access)
ASSERT_EQ(s.ll(), int64_t(0x08090A0B0C0D0E0F)); //size 4 reads should be done at 4 byte offsets in stream
}

TEST_F(Regression, union_comparisons)
{
regression_models::union_without_default u_1, u_2, u_3, u_4;
u_1.s("abcdef", regression_enum::case_1);
u_2.s("fedcba", regression_enum::case_1);
u_3.s("abcdef", regression_enum::case_2);
u_4._d(regression_enum::case_3);

regression_models::union_with_default w_1, w_2, w_3, w_4, w_5;
w_1.s("abcdef", regression_enum::case_1);
w_2.s("fedcba", regression_enum::case_1);
w_3.s("abcdef", regression_enum::case_2);
w_4.i(123);
w_5.i(456);

EXPECT_EQ(u_1, u_1);
EXPECT_NE(u_1, u_2);
EXPECT_NE(u_1, u_3);
EXPECT_NE(u_1, u_4);
EXPECT_NE(u_2, u_1);
EXPECT_EQ(u_2, u_2);
EXPECT_NE(u_2, u_3);
EXPECT_NE(u_2, u_4);
EXPECT_NE(u_3, u_1);
EXPECT_NE(u_3, u_2);
EXPECT_EQ(u_3, u_3);
EXPECT_NE(u_3, u_4);
EXPECT_NE(u_4, u_1);
EXPECT_NE(u_4, u_2);
EXPECT_NE(u_4, u_3);
EXPECT_EQ(u_4, u_4);

EXPECT_EQ(w_1, w_1);
EXPECT_NE(w_1, w_2);
EXPECT_NE(w_1, w_3);
EXPECT_NE(w_1, w_4);
EXPECT_NE(w_1, w_5);
EXPECT_NE(w_2, w_1);
EXPECT_EQ(w_2, w_2);
EXPECT_NE(w_2, w_3);
EXPECT_NE(w_2, w_4);
EXPECT_NE(w_2, w_5);
EXPECT_NE(w_3, w_1);
EXPECT_NE(w_3, w_2);
EXPECT_EQ(w_3, w_3);
EXPECT_NE(w_3, w_4);
EXPECT_NE(w_3, w_5);
EXPECT_NE(w_4, w_1);
EXPECT_NE(w_4, w_2);
EXPECT_NE(w_4, w_3);
EXPECT_EQ(w_4, w_4);
EXPECT_NE(w_4, w_5);
EXPECT_NE(w_5, w_1);
EXPECT_NE(w_5, w_2);
EXPECT_NE(w_5, w_3);
EXPECT_NE(w_5, w_4);
EXPECT_EQ(w_5, w_5);
}

DDSRT_WARNING_GNUC_ON(maybe-uninitialized)
20 changes: 20 additions & 0 deletions src/ddscxx/tests/data/RegressionModels.idl
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,24 @@ struct s_unaligned_access {
long long ll;
};

enum regression_enum {
case_1,
case_2,
case_3
};

union union_without_default switch (regression_enum) {
case case_1:
case case_2:
string s;
};

union union_with_default switch (regression_enum) {
case case_1:
case case_2:
string s;
default:
long i;
};

};
16 changes: 16 additions & 0 deletions src/idlcxx/src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ emit_union(
memset(&visitor, 0, sizeof(visitor));

_union = node;
const bool gen_default = (idl_parent(_union->default_case) == _union);
type_spec = _union->switch_type_spec->type_spec;

name = get_cpp11_name(_union);
Expand Down Expand Up @@ -1016,6 +1017,13 @@ emit_union(
if ((ret = idl_visit(pstate, _union->cases, &visitor, user_data)))
return ret;

if (gen_default) {
fmt = " default:\n"
" break;\n";
if (idl_fprintf(gen->header.handle, fmt, type) < 0)
return IDL_RETCODE_NO_MEMORY;
}

fmt = " }\n"
" return _default_discriminator;\n"
" }\n\n"
Expand Down Expand Up @@ -1073,6 +1081,14 @@ emit_union(
if ((ret = idl_visit(pstate, _union->cases, &visitor, user_data)))
return ret;

/* check whether default label is associated with a union case */
if (gen_default) {
fmt = " default:\n"
" return true;\n";
if (idl_fprintf(gen->header.handle, fmt, name) < 0)
return IDL_RETCODE_NO_MEMORY;
}

fmt = " }\n"
" return false;\n"
" }\n\n"
Expand Down

0 comments on commit f7b2bf9

Please sign in to comment.