Skip to content

Commit 6184a66

Browse files
authored
feat: make all hal::Can::Id functions constexpr and add static assert tests (#803)
* feat: make all hal::Can::Id functions constexpr and add static assert tests * chore: separate implementation from declaration * Update hal/interfaces/Can.hpp
1 parent 6284b6f commit 6184a66

File tree

4 files changed

+104
-86
lines changed

4 files changed

+104
-86
lines changed

hal/interfaces/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ target_sources(hal.interfaces PRIVATE
1616
AsyncGpio.cpp
1717
AsyncGpio.hpp
1818
BackupRam.hpp
19-
Can.cpp
2019
Can.hpp
2120
CommunicationConfigurator.hpp
2221
DigitalToAnalogPin.hpp

hal/interfaces/Can.cpp

Lines changed: 0 additions & 38 deletions
This file was deleted.

hal/interfaces/Can.hpp

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,36 @@
33

44
#include "infra/util/BoundedVector.hpp"
55
#include "infra/util/Function.hpp"
6+
#include <cassert>
67
#include <cstdint>
78

89
namespace hal
910
{
1011
class Can
1112
{
1213
public:
13-
class Id
14+
class [[nodiscard]] Id
1415
{
1516
public:
16-
static constexpr Id Create11BitId(uint32_t id)
17-
{
18-
return Can::Id(id);
19-
}
17+
static constexpr Id Create11BitId(uint32_t id);
18+
static constexpr Id Create29BitId(uint32_t id);
2019

21-
static constexpr Id Create29BitId(uint32_t id)
22-
{
23-
return Can::Id(id | indicator29Bit);
24-
}
20+
[[nodiscard]] constexpr bool Is11BitId() const;
21+
[[nodiscard]] constexpr bool Is29BitId() const;
2522

26-
bool Is11BitId() const;
27-
bool Is29BitId() const;
23+
[[nodiscard]] constexpr uint32_t Get11BitId() const;
24+
[[nodiscard]] constexpr uint32_t Get29BitId() const;
2825

29-
uint32_t Get11BitId() const;
30-
uint32_t Get29BitId() const;
31-
32-
bool operator==(const Id& other) const;
33-
bool operator!=(const Id& other) const;
26+
[[nodiscard]] constexpr bool operator==(const Id& other) const;
27+
[[nodiscard]] constexpr bool operator!=(const Id& other) const;
3428

3529
private:
3630
constexpr explicit Id(uint32_t id)
3731
: id(id)
3832
{}
3933

4034
private:
41-
static const uint32_t indicator29Bit = static_cast<uint32_t>(1) << 31;
35+
static constexpr uint32_t indicator29Bit{ 1u << 31 };
4236

4337
uint32_t id;
4438
};
@@ -56,6 +50,50 @@ namespace hal
5650
virtual void SendData(Id id, const Message& data, const infra::Function<void(bool success)>& actionOnCompletion) = 0;
5751
virtual void ReceiveData(const infra::Function<void(Id id, const Message& data)>& receivedAction) = 0;
5852
};
53+
54+
//// Implementation ////
55+
56+
constexpr Can::Id Can::Id::Create11BitId(uint32_t id)
57+
{
58+
return Can::Id(id);
59+
}
60+
61+
constexpr Can::Id Can::Id::Create29BitId(uint32_t id)
62+
{
63+
return Can::Id(id | indicator29Bit);
64+
}
65+
66+
constexpr bool Can::Id::Is11BitId() const
67+
{
68+
return (id & indicator29Bit) == 0;
69+
}
70+
71+
constexpr bool Can::Id::Is29BitId() const
72+
{
73+
return !Is11BitId();
74+
}
75+
76+
constexpr uint32_t Can::Id::Get11BitId() const
77+
{
78+
assert(Is11BitId());
79+
return id;
80+
}
81+
82+
constexpr uint32_t Can::Id::Get29BitId() const
83+
{
84+
assert(Is29BitId());
85+
return id ^ indicator29Bit;
86+
}
87+
88+
constexpr bool Can::Id::operator==(const Id& other) const
89+
{
90+
return id == other.id;
91+
}
92+
93+
constexpr bool Can::Id::operator!=(const Id& other) const
94+
{
95+
return !(*this == other);
96+
}
5997
}
6098

6199
#endif

hal/interfaces/test/TestCan.cpp

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,65 @@
11
#include "hal/interfaces/Can.hpp"
2+
#include "gmock/gmock.h"
23
#include "gtest/gtest.h"
34

5+
static_assert(hal::Can::Id::Create11BitId(0).Is11BitId());
6+
static_assert(!hal::Can::Id::Create11BitId(0).Is29BitId());
7+
static_assert(hal::Can::Id::Create11BitId(0).Get11BitId() == 0);
8+
9+
static_assert(!hal::Can::Id::Create29BitId(0).Is11BitId());
10+
static_assert(hal::Can::Id::Create29BitId(0).Is29BitId());
11+
static_assert(hal::Can::Id::Create29BitId(0).Get29BitId() == 0);
12+
13+
static_assert(hal::Can::Id::Create11BitId(0) == hal::Can::Id::Create11BitId(0));
14+
static_assert(hal::Can::Id::Create11BitId(0) != hal::Can::Id::Create11BitId(1));
15+
static_assert(hal::Can::Id::Create11BitId(0) != hal::Can::Id::Create29BitId(0));
16+
17+
static_assert(hal::Can::Id::Create29BitId(2) == hal::Can::Id::Create29BitId(2));
18+
static_assert(hal::Can::Id::Create29BitId(2) != hal::Can::Id::Create29BitId(1));
19+
static_assert(hal::Can::Id::Create29BitId(2) != hal::Can::Id::Create11BitId(2));
20+
421
TEST(CanTest, generate_11_bit_id)
522
{
6-
auto id = hal::Can::Id::Create11BitId(0);
23+
const auto id = hal::Can::Id::Create11BitId(0);
724

8-
EXPECT_TRUE(id.Is11BitId());
9-
EXPECT_EQ(0, id.Get11BitId());
25+
EXPECT_THAT(id.Is11BitId(), testing::IsTrue());
26+
EXPECT_THAT(id.Is29BitId(), testing::IsFalse());
27+
EXPECT_THAT(id.Get11BitId(), testing::Eq(0));
1028
}
1129

1230
TEST(CanTest, generate_29_bit_id)
1331
{
14-
auto id = hal::Can::Id::Create29BitId(0);
32+
const auto id = hal::Can::Id::Create29BitId(0);
1533

16-
EXPECT_TRUE(id.Is29BitId());
17-
EXPECT_EQ(0, id.Get29BitId());
34+
EXPECT_THAT(id.Is29BitId(), testing::IsTrue());
35+
EXPECT_THAT(id.Is11BitId(), testing::IsFalse());
36+
EXPECT_THAT(id.Get29BitId(), testing::Eq(0));
1837
}
1938

2039
TEST(CanTest, test_equality_mixed)
2140
{
22-
auto id11_0 = hal::Can::Id::Create11BitId(0);
23-
auto id11_1 = hal::Can::Id::Create11BitId(1);
24-
auto id29_0 = hal::Can::Id::Create29BitId(0);
25-
auto id29_1 = hal::Can::Id::Create29BitId(1);
26-
27-
EXPECT_TRUE(id11_0 == hal::Can::Id::Create11BitId(0));
28-
EXPECT_TRUE(id11_0 != id11_1);
29-
EXPECT_TRUE(id11_0 != id29_0);
30-
EXPECT_TRUE(id11_0 != id29_1);
31-
32-
EXPECT_TRUE(id11_1 != id11_0);
33-
EXPECT_TRUE(id11_1 == hal::Can::Id::Create11BitId(1));
34-
EXPECT_TRUE(id11_1 != id29_0);
35-
EXPECT_TRUE(id11_1 != id29_1);
36-
37-
EXPECT_TRUE(id29_0 != id11_0);
38-
EXPECT_TRUE(id29_0 != id11_1);
39-
EXPECT_TRUE(id29_0 == hal::Can::Id::Create29BitId(0));
40-
EXPECT_TRUE(id29_0 != id29_1);
41-
42-
EXPECT_TRUE(id29_1 != id11_0);
43-
EXPECT_TRUE(id29_1 != id11_1);
44-
EXPECT_TRUE(id29_1 != id29_0);
45-
EXPECT_TRUE(id29_1 == hal::Can::Id::Create29BitId(1));
41+
const auto id11_0 = hal::Can::Id::Create11BitId(0);
42+
const auto id11_1 = hal::Can::Id::Create11BitId(1);
43+
const auto id29_0 = hal::Can::Id::Create29BitId(0);
44+
const auto id29_1 = hal::Can::Id::Create29BitId(1);
45+
46+
EXPECT_THAT(id11_0, testing::Eq(hal::Can::Id::Create11BitId(0)));
47+
EXPECT_THAT(id11_0, testing::Ne(id11_1));
48+
EXPECT_THAT(id11_0, testing::Ne(id29_0));
49+
EXPECT_THAT(id11_0, testing::Ne(id29_1));
50+
51+
EXPECT_THAT(id11_1, testing::Ne(id11_0));
52+
EXPECT_THAT(id11_1, testing::Eq(hal::Can::Id::Create11BitId(1)));
53+
EXPECT_THAT(id11_1, testing::Ne(id29_0));
54+
EXPECT_THAT(id11_1, testing::Ne(id29_1));
55+
56+
EXPECT_THAT(id29_0, testing::Ne(id11_0));
57+
EXPECT_THAT(id29_0, testing::Ne(id11_1));
58+
EXPECT_THAT(id29_0, testing::Eq(hal::Can::Id::Create29BitId(0)));
59+
EXPECT_THAT(id29_0, testing::Ne(id29_1));
60+
61+
EXPECT_THAT(id29_1, testing::Ne(id11_0));
62+
EXPECT_THAT(id29_1, testing::Ne(id11_1));
63+
EXPECT_THAT(id29_1, testing::Ne(id29_0));
64+
EXPECT_THAT(id29_1, testing::Eq(hal::Can::Id::Create29BitId(1)));
4665
}

0 commit comments

Comments
 (0)