Skip to content

Commit

Permalink
Coverage for optional
Browse files Browse the repository at this point in the history
  • Loading branch information
joajfreitas committed Jan 3, 2025
1 parent a909b2d commit e571a24
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
12 changes: 11 additions & 1 deletion plugins/fcp_cpp/fcp_cpp/decoders.h.j2
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,18 @@ template<typename T>
class Optional {
public:

Optional(): data_{} {}
Optional(const std::optional<T>& value): data_{value} {}
Optional(): data_{std::nullopt} {}

Optional& operator=(const T that) {
data_ = that;
return *this;
}

Optional& operator=(std::nullopt_t that) {
data_ = that;
return *this;
}

static Optional None() {
return Optional(std::nullopt);
Expand Down
4 changes: 2 additions & 2 deletions plugins/fcp_cpp/tests/009_optional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
UTEST_MAIN()

UTEST(BasicStruct, EncodeOptionalWithValue) {
auto s1 = fcp::S1{{1}};
auto s1 = fcp::S1{fcp::S1::S1Type{1}};
std::vector<std::uint8_t> encoded = s1.encode().GetData();

std::vector<uint8_t> bytes{0x01, 0x1};
Expand All @@ -27,7 +27,7 @@ UTEST(BasicStruct, DecodeOptionalWithValue) {
std::vector<uint8_t> bytes{0x01, 0x01};

auto foo = fcp::S1::Decode(bytes.begin(), bytes.end());
auto expected = fcp::S1{{1}};
auto expected = fcp::S1{fcp::S1::S1Type{1}};

EXPECT_TRUE(foo==expected);
}
Expand Down
10 changes: 8 additions & 2 deletions plugins/fcp_cpp/tests/test_standard_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
ComposedTypeCategory,
ArrayType,
DynamicArrayType,
OptionalType,
)
from fcp.xpath import Xpath

Expand Down Expand Up @@ -76,9 +77,9 @@ def to_constant(fcp: FcpV2, type: Type, value: Any) -> str:
"""Convert a value to a constant."""
if isinstance(type, BuiltinType):
if type.is_signed():
return str(value + "LL")
return str(value) + "LL"
elif type.is_unsigned():
return str(value + "ULL")
return str(value) + "ULL"
elif type.is_str():
return str('"' + value + '"')
else:
Expand All @@ -91,6 +92,11 @@ def to_constant(fcp: FcpV2, type: Type, value: Any) -> str:
+ ", ".join([to_constant(fcp, type.underlying_type, v) for v in value])
+ "}"
)
elif isinstance(type, OptionalType):
if value is None:
return "std::nullopt"
else:
return to_constant(fcp, type.underlying_type, value)
else:
raise ValueError(f"Unknown type: {type}")

Expand Down
4 changes: 4 additions & 0 deletions tests/standardized/001_basic_values.fcp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ struct S11 {
struct S12 {
s1 @ 0: [E],
}

struct S13 {
s1 @0: Optional[u8],
}
16 changes: 16 additions & 0 deletions tests/standardized/fcp_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@
"S12:s1": ["S0","S1","S2", "S0", "S1", "S2"]
},
"encoded": [6, 0, 0, 0, 36, 9]
},
{
"name": "optional_unsigned_eight_bit_with_value",
"datatype": "S13",
"decoded": {
"S13:s1": 255
},
"encoded": [1, 255]
},
{
"name": "optional_unsigned_eight_bit_with_no_value",
"datatype": "S13",
"decoded": {
"S13:s1": null
},
"encoded": [0]
}
]
}
Expand Down

0 comments on commit e571a24

Please sign in to comment.