8000 Test copy StaticJsonArray · ROMSDEV/ArduinoJson@1bbb1e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bbb1e6

Browse files
committed
Test copy StaticJsonArray
1 parent 78b47fa commit 1bbb1e6

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/ArduinoJson/DynamicJsonArray.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ class DynamicJsonArray : public JsonArray {
2626
JsonArray::operator=(other);
2727
}
2828

29-
using JsonArray::operator=;
30-
3129
DynamicJsonArray& operator=(const DynamicJsonArray& other) {
30+
_buffer.clear();
3231
JsonArray::operator=(other);
3332
return *this;
3433
}
3534

35+
DynamicJsonArray& operator=(const JsonArray& other) {
36+
_buffer.clear();
37+
JsonArray::operator=(other);
38+
return *this;
39+
}
40+
41+
size_t memoryUsage() const {
42+
return _buffer.size() + sizeof(JsonArray);
43+
}
44+
3645
Internals::DynamicJsonBuffer& buffer() {
3746
return _buffer;
3847
}

src/ArduinoJson/StaticJsonArray.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ class StaticJsonArray : public JsonArray {
2424
JsonArray::operator=(other);
2525
}
2626

27+
StaticJsonArray<CAPACITY>& operator=(const StaticJsonArray<CAPACITY>& other) {
8000
28+
_buffer.clear();
29+
JsonArray::operator=(other);
30+
return *this;
31+
}
32+
33+
StaticJsonArray<CAPACITY>& operator=(const JsonArray& other) {
34+
_buffer.clear();
35+
JsonArray::operator=(other);
36+
return *this;
37+
}
38+
2739
size_t memoryUsage() const {
2840
return _buffer.size() + sizeof(JsonArray);
2941
}

test/JsonArray/copy.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ TArray buildArray() {
1616
return array;
1717
}
1818

19-
void validateArray(JsonArray& array) {
19+
template <typename TArray>
20+
void validateArray(TArray& array) {
2021
CHECK(array.size() == 2);
2122
REQUIRE(array[0] == 42);
22-
REQUIRE(array[1].is<JsonObject>());
23+
REQUIRE(array[1].template is<JsonObject>());
2324
REQUIRE(array[1]["hello"] == std::string("world"));
25+
26+
const int expectedSize = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(1);
27+
REQUIRE(expectedSize == array.memoryUsage());
2428
}
2529

2630
TEST_CASE("DynamicJsonArray::operator=()") {
@@ -39,3 +43,20 @@ TEST_CASE("DynamicJsonArray::operator=()") {
3943
validateArray(array);
4044
}
4145
}
46+
47+
TEST_CASE("StaticJsonArray::operator=()") {
48+
const size_t SIZE = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(1);
49+
StaticJsonArray<SIZE> array;
50+
array.add(666);
51+
array.add(666);
52+
53+
SECTION("operator=(const DynamicJsonArray&)") {
54+
array = buildArray<DynamicJsonArray>();
55+
validateArray(array);
56+
}
57+
58+
SECTION("operator=(const StaticJsonArray<N>&)") {
59+
array = buildArray<StaticJsonArray<SIZE> >();
60+
validateArray(array);
61+
}
62+
}

0 commit comments

Comments
 (0)
0