Skip to content

Commit 043802d

Browse files
authored
test encoding of str
1 parent 8f3c668 commit 043802d

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/Datapoint/Converter.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ int Div2Convert::toString(char* buf, std::size_t maxLen, VariantValue value) con
8181
return snprintf(buf, maxLen, "%.1f", static_cast<float>(value));
8282
}
8383

84+
void Div2Convert::fromString(const char* buf, VariantValue* result) const {
85+
char* end = nullptr;
86+
float ret = std::strtof(buf, &end);
87+
if (buf == end || ret == HUGE_VALF) {
88+
vw_log_e("Could not convert string to float");
89+
}
90+
*result = VariantValue(ret);
91+
}
92+
8493
VariantValue Div3600Convert::decode(const uint8_t* data, uint8_t len) const {
8594
assert(len == 4);
8695
(void) len;
@@ -104,6 +113,15 @@ int Div3600Convert::toString(char* buf, std::size_t maxLen, VariantValue value)
104113
return snprintf(buf, maxLen, "%.4f", static_cast<float>(value));
105114
}
106115

116+
void Div3600Convert::fromString(const char* buf, VariantValue* result) const {
117+
char* end = nullptr;
118+
float ret = std::strtof(buf, &end);
119+
if (buf == end || ret == HUGE_VALF) {
120+
vw_log_e("Could not convert string to float");
121+
}
122+
*result = VariantValue(ret);
123+
}
124+
107125
VariantValue NoconvConvert::decode(const uint8_t* data, uint8_t len) const {
108126
// assert(len == 1 || len == 2 || len == 4);
109127
if (len == 1) {

src/Datapoint/Converter.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Converter {
5858
virtual VariantValue decode(const uint8_t* data, uint8_t len) const = 0;
5959
virtual void encode(uint8_t* buf, uint8_t len, const VariantValue& val) const = 0;
6060
virtual int toString(char* buf, std::size_t maxLen, VariantValue value) const;
61-
virtual void fromString(const char* buf, VariantValue* result) const;
61+
virtual void fromString(const char* buf, VariantValue* result) const; // buf = c-string, 0-terminated
6262
bool operator==(const Converter& rhs) const {
6363
return (this == &rhs);
6464
}
@@ -69,21 +69,23 @@ class Div10Convert : public Converter {
6969
VariantValue decode(const uint8_t* data, uint8_t len) const override;
7070
void encode(uint8_t* buf, uint8_t len, const VariantValue& val) const override;
7171
int toString(char* buf, std::size_t maxLen, VariantValue value) const override;
72-
void fromString(const char* buf, VariantValue* result) const override; // C-string = 0-terminated
72+
void fromString(const char* buf, VariantValue* result) const override;
7373
};
7474

7575
class Div2Convert : public Converter {
7676
public:
7777
VariantValue decode(const uint8_t* data, uint8_t len) const override;
7878
void encode(uint8_t* buf, uint8_t len, const VariantValue& val) const override;
7979
int toString(char* buf, std::size_t maxLen, VariantValue value) const override;
80+
void fromString(const char* buf, VariantValue* result) const override;
8081
};
8182

8283
class Div3600Convert : public Converter {
8384
public:
8485
VariantValue decode(const uint8_t* data, uint8_t len) const override;
8586
void encode(uint8_t* buf, uint8_t len, const VariantValue& val) const override;
8687
int toString(char* buf, std::size_t maxLen, VariantValue value) const override;
88+
void fromString(const char* buf, VariantValue* result) const override;
8789
};
8890

8991
class NoconvConvert : public Converter {

test/test_Datapoint/test_Datapoint.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ void test_HourEncode() {
171171
TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, buffer, len);
172172
}
173173

174+
void test_HourEncodeStr() {
175+
Datapoint dp("hour", 0x0000, 4, VitoWiFi::div3600);
176+
const uint8_t expected[] = {0x80, 0x8F, 0x21, 0x61};
177+
const char* value = "452663.72";
178+
const uint8_t len = 4;
179+
uint8_t buffer[len] = {0};
180+
181+
dp.encode(buffer, len, value);
182+
183+
TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, buffer, len);
184+
}
185+
174186

175187
void test_CountDecode() {
176188
Datapoint dp("count", 0x0000, 4, VitoWiFi::noconv);
@@ -247,7 +259,7 @@ void test_COPDecodeStr() {
247259
}
248260

249261
void test_COPEncode() {
250-
Datapoint dp("count", 0x0000, 2, VitoWiFi::div10);
262+
Datapoint dp("count", 0x0000, 1, VitoWiFi::div10);
251263
const uint8_t expected[] = {0x1A};
252264
const float value = 2.6;
253265
const uint8_t len = 1;
@@ -258,6 +270,18 @@ void test_COPEncode() {
258270
TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, buffer, len);
259271
}
260272

273+
void test_COPEncodeStr() {
274+
Datapoint dp("count", 0x0000, 1, VitoWiFi::div10);
275+
const uint8_t expected[] = {0x1A};
276+
const char* value = "2.6";
277+
const uint8_t len = 1;
278+
uint8_t buffer[len] = {0};
279+
280+
dp.encode(buffer, len, value);
281+
282+
TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, buffer, len);
283+
}
284+
261285
void test_ScheduleEncode() {
262286
const char* schedule = "7:30 08:30 16:20 23:10";
263287
const uint8_t expected[] = {0x3B, 0x43, 0x82, 0xB9, 0x00, 0x00, 0x00, 0x00};
@@ -298,13 +322,15 @@ int main() {
298322
RUN_TEST(test_HourDecode);
299323
RUN_TEST(test_HourDecodeStr);
300324
RUN_TEST(test_HourEncode);
325+
RUN_TEST(test_HourEncodeStr);
301326
RUN_TEST(test_CountDecode);
302327
RUN_TEST(test_CountEncode);
303328
RUN_TEST(test_CountShortDecode);
304329
RUN_TEST(test_CountShortEncode);
305330
RUN_TEST(test_COPDecode);
306331
RUN_TEST(test_COPDecodeStr);
307332
RUN_TEST(test_COPEncode);
333+
RUN_TEST(test_COPEncodeStr);
308334
RUN_TEST(test_ScheduleEncode);
309335
RUN_TEST(test_ScheduleDecode);
310336
return UNITY_END();

0 commit comments

Comments
 (0)