Skip to content

Commit 706ffdc

Browse files
committed
Add test case for CloudFloat
1 parent c383fd7 commit 706ffdc

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

extras/test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ set(TEST_SRCS
6666
src/test_addPropertyReal.cpp
6767
src/test_callback.cpp
6868
src/test_CloudColor.cpp
69+
src/test_CloudFloat.cpp
6970
src/test_CloudLocation.cpp
7071
src/test_CloudSchedule.cpp
7172
src/test_decode.cpp

extras/test/src/test_CloudFloat.cpp

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright (c) 2019 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch2/catch_test_macros.hpp>
10+
11+
#include <CBORDecoder.h>
12+
13+
#include <property/types/CloudFloat.h>
14+
15+
/**************************************************************************************
16+
TEST CODE
17+
**************************************************************************************/
18+
19+
SCENARIO("Arduino Cloud Properties ", "[ArduinoCloudThing::CloudFloat]")
20+
{
21+
WHEN("NAN value from cloud is received")
22+
{
23+
PropertyContainer property_container;
24+
CloudFloat prop = 2.0f;
25+
prop.writeOnDemand();
26+
addPropertyToContainer(property_container, prop, "test", Permission::ReadWrite);
27+
28+
/* [{0: "test", 2: NaN}] = 81A200647465737402F97E00 */
29+
uint8_t const payload[] = { 0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0xF9, 0x7E, 0x00 };
30+
int const payload_length = sizeof(payload) / sizeof(uint8_t);
31+
CBORDecoder::decode(property_container, payload, payload_length);
32+
33+
REQUIRE(prop.isDifferentFromCloud() == true);
34+
}
35+
36+
WHEN("Local value is NAN")
37+
{
38+
PropertyContainer property_container;
39+
CloudFloat prop = NAN;
40+
prop.writeOnDemand();
41+
addPropertyToContainer(property_container, prop, "test", Permission::ReadWrite);
42+
43+
/* [{0: "test", 2: 1.5}] = 81A200647465737402F93E00 */
44+
uint8_t const payload[] = { 0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0xF9, 0x3E, 0x00 };
45+
int const payload_length = sizeof(payload) / sizeof(uint8_t);
46+
CBORDecoder::decode(property_container, payload, payload_length);
47+
48+
REQUIRE(prop.isDifferentFromCloud() == true);
49+
}
50+
51+
WHEN("both, the local and the cloud values are NaN")
52+
{
53+
PropertyContainer property_container;
54+
CloudFloat prop = NAN;
55+
prop.writeOnDemand();
56+
addPropertyToContainer(property_container, prop, "test", Permission::ReadWrite);
57+
58+
/* [{0: "test", 2: NaN}] = 81A200647465737402F97E00 */
59+
uint8_t const payload[] = { 0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0xF9, 0x7E, 0x00 };
60+
int const payload_length = sizeof(payload) / sizeof(uint8_t);
61+
CBORDecoder::decode(property_container, payload, payload_length);
62+
63+
REQUIRE(prop.isDifferentFromCloud() == false);
64+
}
65+
66+
WHEN("the local and cloud values are the same")
67+
{
68+
PropertyContainer property_container;
69+
CloudFloat prop = 1.5;
70+
prop.writeOnDemand();
71+
addPropertyToContainer(property_container, prop, "test", Permission::ReadWrite);
72+
73+
/* [{0: "test", 2: 1.5}] = 81A200647465737402F93E00 */
74+
uint8_t const payload[] = { 0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0xF9, 0x3E, 0x00 };
75+
int const payload_length = sizeof(payload) / sizeof(uint8_t);
76+
CBORDecoder::decode(property_container, payload, payload_length);
77+
78+
REQUIRE(prop.isDifferentFromCloud() == false);
79+
}
80+
81+
WHEN("the local and the cloud values differ")
82+
{
83+
PropertyContainer property_container;
84+
CloudFloat prop = 1.0f;
85+
prop.writeOnDemand();
86+
addPropertyToContainer(property_container, prop, "test", Permission::ReadWrite);
87+
88+
/* [{0: "test", 2: 1.5}] = 81A200647465737402F97E00 */
89+
uint8_t const payload[] = { 0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0xF9, 0x7E, 0x00 };
90+
int const payload_length = sizeof(payload) / sizeof(uint8_t);
91+
CBORDecoder::decode(property_container, payload, payload_length);
92+
93+
REQUIRE(prop.isDifferentFromCloud() == true);
94+
}
95+
96+
WHEN("the local value differs by 0.25 from the cloud value")
97+
{
98+
PropertyContainer property_container;
99+
CloudFloat prop = 1.0f;
100+
prop.writeOnDemand();
101+
addPropertyToContainer(property_container, prop, "test", Permission::ReadWrite);
102+
103+
/* [{0: "test", 2: 1.5}] = 81A200647465737402F97E00 */
104+
uint8_t const payload[] = { 0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0xF9, 0x7E, 0x00 };
105+
int const payload_length = sizeof(payload) / sizeof(uint8_t);
106+
CBORDecoder::decode(property_container, payload, payload_length);
107+
prop.publishOnChange(0.25f, 0); // 0.25 min delta
108+
109+
REQUIRE(prop.isDifferentFromCloud() == true);
110+
}
111+
112+
}

0 commit comments

Comments
 (0)