Skip to content

Commit

Permalink
asn1: Add '==' and '!=' overload to asn1c wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
v0-e committed Jan 29, 2024
1 parent a275baf commit 48f6488
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
10 changes: 10 additions & 0 deletions vanetza/asn1/asn1c_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ class asn1c_wrapper_common
const asn1c_type* content() const { return m_struct; }
asn1c_type* content() { return m_struct; }

// compare semantics
bool operator==(const asn1c_wrapper_common& rhs) const
{
return vanetza::asn1::compare(m_type, m_struct, rhs.m_struct) == 0;
}
bool operator!=(const asn1c_wrapper_common& rhs) const
{
return vanetza::asn1::compare(m_type, m_struct, rhs.m_struct) != 0;
}

/**
* Check ASN.1 constraints
* \param error (optional) copy of error message
Expand Down
20 changes: 13 additions & 7 deletions vanetza/asn1/tests/asn1c_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,27 @@ TEST(asn1c_wrapper, validate) {
TEST(asn1c_wrapper, compare) {
test_wrapper wrapper1(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper1->string, "1234");
EXPECT_TRUE(!wrapper1.compare(wrapper1));

test_wrapper wrapper2(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper2->string, "1234");
EXPECT_TRUE(!wrapper1.compare(wrapper2));

test_wrapper wrapper3(asn_DEF_VanetzaTest);
OCTET_STRING_fromString(&wrapper3->string, "0123");
EXPECT_FALSE(!wrapper1.compare(wrapper3));
EXPECT_TRUE(wrapper1.compare(wrapper3) > 0);

test_wrapper wrapper4(asn_DEF_VanetzaTest);
wrapper4->field = 5;
OCTET_STRING_fromString(&wrapper4->string, "1234");

// .compare()
EXPECT_TRUE(!wrapper1.compare(wrapper1));
EXPECT_TRUE(!wrapper1.compare(wrapper2));
EXPECT_FALSE(!wrapper1.compare(wrapper3));
EXPECT_TRUE(wrapper1.compare(wrapper3) > 0);
EXPECT_FALSE(!wrapper1.compare(wrapper4));

// operators == and !=
EXPECT_TRUE(wrapper1 == wrapper1);
EXPECT_TRUE(wrapper1 == wrapper2);
EXPECT_FALSE(wrapper1 == wrapper3);
EXPECT_TRUE(wrapper1 != wrapper3);
EXPECT_TRUE(wrapper1 != wrapper4);
}

TEST(asn1c_wrapper, print) {
Expand Down

0 comments on commit 48f6488

Please sign in to comment.