Skip to content

Commit f7921f0

Browse files
strongly-typedsalkinium
authored andcommitted
[tests] Easier creation of CAN messages from uint64_t for testing code
1 parent 30d4a56 commit f7921f0

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

src/modm/architecture/interface/can_message.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define MODM_CAN_MESSAGE_HPP
1515

1616
#include <stdint.h>
17+
#include <string.h> // strlen
1718
#include <algorithm>
1819
#include <modm/architecture/utils.hpp>
1920

@@ -29,6 +30,16 @@ struct Message
2930
{
3031
}
3132

33+
// Create CAN message from long data in Network Order.
34+
inline Message(uint32_t inIdentifier, uint8_t inLength, const uint64_t &inData, bool extended=false) :
35+
identifier(inIdentifier), length(std::min(inLength, uint8_t(8)))
36+
{
37+
flags.extended = extended;
38+
const uint8_t *inDataB = reinterpret_cast<const uint8_t *>(&inData);
39+
for (uint8_t ii = 0; ii < length; ++ii)
40+
data[ii] = inDataB[length - ii - 1];
41+
}
42+
3243
inline uint32_t
3344
getIdentifier() const
3445
{

test/modm/architecture/interface/can_message_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,23 @@ CanMessageTest::testEqualOperator()
4444
msgB.data[1] = 0x11;
4545
TEST_ASSERT_FALSE(msgA == msgB);
4646
}
47+
48+
void
49+
CanMessageTest::testConstructor()
50+
{
51+
modm::can::Message msgA(0x123, 7, 0x19821122aaffEE);
52+
TEST_ASSERT_EQUALS(msgA.data[0], 0x19);
53+
TEST_ASSERT_EQUALS(msgA.data[1], 0x82);
54+
TEST_ASSERT_EQUALS(msgA.data[2], 0x11);
55+
TEST_ASSERT_EQUALS(msgA.data[3], 0x22);
56+
TEST_ASSERT_EQUALS(msgA.data[4], 0xaa);
57+
TEST_ASSERT_EQUALS(msgA.data[5], 0xff);
58+
TEST_ASSERT_EQUALS(msgA.data[6], 0xee);
59+
TEST_ASSERT_EQUALS(msgA.getLength(), 7);
60+
TEST_ASSERT_FALSE(msgA.isExtended());
61+
62+
modm::can::Message msgB(0x432, 1, 0xab, true);
63+
TEST_ASSERT_EQUALS(msgB.data[0], 0xab);
64+
TEST_ASSERT_EQUALS(msgB.getLength(), 1);
65+
TEST_ASSERT_TRUE(msgB.isExtended());
66+
}

test/modm/architecture/interface/can_message_test.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 2009, Martin Rosekeit
33
* Copyright (c) 2009-2010, Fabian Greif
44
* Copyright (c) 2012, 2015, Niklas Hauser
5-
* Copyright (c) 2016, Sascha Schade
5+
* Copyright (c) 2016-2020, Sascha Schade
66
*
77
* This file is part of the modm project.
88
*
@@ -24,6 +24,9 @@ class CanMessageTest : public unittest::TestSuite
2424
public:
2525
void
2626
testEqualOperator();
27+
28+
void
29+
testConstructor();
2730
};
2831

2932
#endif // MODM_UNITTEST_CAN_MESSAGE_HPP

test/modm/driver/can/can_lawicel_formatter_test.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
void
2424
CanLawicelFormatterTest::testIdentifierToStringExtended()
2525
{
26-
modm::can::Message msg(0x75395165, 0);
26+
modm::can::Message msg(0x75395165);
2727

2828
char buffer[128];
2929
for (int i = 0; i < 128; ++i) {
@@ -44,7 +44,7 @@ CanLawicelFormatterTest::testIdentifierToStringExtended()
4444
void
4545
CanLawicelFormatterTest::testIdentifierToStringStandard()
4646
{
47-
modm::can::Message msg(0x123, 0);
47+
modm::can::Message msg(0x123);
4848
msg.flags.extended = false;
4949

5050
char buffer[128];
@@ -61,12 +61,7 @@ CanLawicelFormatterTest::testIdentifierToStringStandard()
6161
void
6262
CanLawicelFormatterTest::testMessageToStringStandard()
6363
{
64-
modm::can::Message msg(0x123, 4);
65-
msg.flags.extended = false;
66-
msg.data[0] = 0x44;
67-
msg.data[1] = 0xff;
68-
msg.data[2] = 0x1A;
69-
msg.data[3] = 0x12;
64+
modm::can::Message msg(0x123, 4, 0x44ff1a12, false);
7065

7166
char buffer[128];
7267
for (int i = 0; i < 128; ++i) {
@@ -87,12 +82,7 @@ CanLawicelFormatterTest::testMessageToStringStandard()
8782
void
8883
CanLawicelFormatterTest::testMessageToStringExtended()
8984
{
90-
modm::can::Message msg(0x123, 4);
91-
msg.flags.extended = true;
92-
msg.data[0] = 0x44;
93-
msg.data[1] = 0xff;
94-
msg.data[2] = 0x1A;
95-
msg.data[3] = 0x12;
85+
modm::can::Message msg(0x123, 4, 0x44ff1a12, true);
9686

9787
char buffer[128];
9888
for (int i = 0; i < 128; ++i) {
@@ -151,12 +141,7 @@ CanLawicelFormatterTest::testRoundtripMessage()
151141
void
152142
CanLawicelFormatterTest::testRoudtripString()
153143
{
154-
modm::can::Message msg(0x123, 4);
155-
msg.flags.extended = true;
156-
msg.data[0] = 0x44;
157-
msg.data[1] = 0xff;
158-
msg.data[2] = 0x1A;
159-
msg.data[3] = 0x12;
144+
modm::can::Message msg(0x123, 4, 0x44ff1a12, true);
160145

161146
char buffer[128];
162147
for (int i = 0; i < 128; ++i) {

0 commit comments

Comments
 (0)