Skip to content

Commit c4b8796

Browse files
committed
Remove JsonDocument::capacity()
1 parent 6afa6b6 commit c4b8796

File tree

9 files changed

+130
-94
lines changed

9 files changed

+130
-94
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ HEAD
1010
* Merge `DynamicJsonDocument` with `JsonDocument`
1111
* Remove `JSON_ARRAY_SIZE()`, `JSON_OBJECT_SIZE()`, and `JSON_STRING_SIZE()`
1212
* Remove `ARDUINOJSON_ENABLE_STRING_DEDUPLICATION` (string deduplication cannot be enabled anymore)
13+
* Remove `JsonDocument::capacity()`

extras/tests/JsonDocument/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
add_executable(JsonDocumentTests
66
add.cpp
77
assignment.cpp
8-
capacity.cpp
98
cast.cpp
109
compare.cpp
1110
constructor.cpp

extras/tests/JsonDocument/assignment.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ TEST_CASE("JsonDocument assignment") {
2222
doc2 = doc1;
2323

2424
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
25-
REQUIRE(doc2.capacity() == doc1.capacity());
2625
}
2726
REQUIRE(spyingAllocator.log() == AllocatorLog()
2827
<< AllocatorLog::Allocate(1024)
@@ -40,7 +39,6 @@ TEST_CASE("JsonDocument assignment") {
4039
doc2 = doc1;
4140

4241
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
43-
REQUIRE(doc2.capacity() == doc1.capacity());
4442
}
4543
REQUIRE(spyingAllocator.log() == AllocatorLog()
4644
<< AllocatorLog::Allocate(4096)
@@ -60,7 +58,6 @@ TEST_CASE("JsonDocument assignment") {
6058
doc2 = doc1;
6159

6260
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
63-
REQUIRE(doc2.capacity() == doc1.capacity());
6461
}
6562
REQUIRE(spyingAllocator.log() == AllocatorLog()
6663
<< AllocatorLog::Allocate(1024)
@@ -81,8 +78,6 @@ TEST_CASE("JsonDocument assignment") {
8178

8279
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
8380
REQUIRE(doc1.as<std::string>() == "null");
84-
REQUIRE(doc1.capacity() == 0);
85-
REQUIRE(doc2.capacity() == 4096);
8681
}
8782
REQUIRE(spyingAllocator.log() == AllocatorLog()
8883
<< AllocatorLog::Allocate(4096)
@@ -100,7 +95,6 @@ TEST_CASE("JsonDocument assignment") {
10095
doc2 = obj;
10196

10297
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
103-
REQUIRE(doc2.capacity() == 4096);
10498
}
10599

106100
SECTION("Assign from JsonArray") {
@@ -112,7 +106,6 @@ TEST_CASE("JsonDocument assignment") {
112106
doc2 = arr;
113107

114108
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
115-
REQUIRE(doc2.capacity() == 4096);
116109
}
117110

118111
SECTION("Assign from JsonVariant") {
@@ -123,7 +116,6 @@ TEST_CASE("JsonDocument assignment") {
123116
doc2 = doc1.as<JsonVariant>();
124117

125118
REQUIRE(doc2.as<std::string>() == "42");
126-
REQUIRE(doc2.capacity() == 4096);
127119
}
128120

129121
SECTION("Assign from MemberProxy") {
@@ -134,7 +126,6 @@ TEST_CASE("JsonDocument assignment") {
134126
doc2 = doc1["value"];
135127

136128
REQUIRE(doc2.as<std::string>() == "42");
137-
REQUIRE(doc2.capacity() == 4096);
138129
}
139130

140131
SECTION("Assign from ElementProxy") {
@@ -145,6 +136,5 @@ TEST_CASE("JsonDocument assignment") {
145136
doc2 = doc1[0];
146137

147138
REQUIRE(doc2.as<std::string>() == "42");
148-
REQUIRE(doc2.capacity() == 4096);
149139
}
150140
}

extras/tests/JsonDocument/capacity.cpp

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

extras/tests/JsonDocument/constructor.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ TEST_CASE("JsonDocument constructor") {
2828

2929
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
3030
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
31-
REQUIRE(doc2.capacity() == 4096);
3231
}
3332
REQUIRE(spyingAllocator.log() == AllocatorLog()
3433
<< AllocatorLog::Allocate(4096)
@@ -46,8 +45,6 @@ TEST_CASE("JsonDocument constructor") {
4645

4746
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
4847
REQUIRE(doc1.as<std::string>() == "null");
49-
REQUIRE(doc1.capacity() == 0);
50-
REQUIRE(doc2.capacity() == 4096);
5148
}
5249
REQUIRE(spyingAllocator.log() == AllocatorLog()
5350
<< AllocatorLog::Allocate(4096)
@@ -59,30 +56,33 @@ TEST_CASE("JsonDocument constructor") {
5956
JsonObject obj = doc1.to<JsonObject>();
6057
obj["hello"] = "world";
6158

62-
JsonDocument doc2 = obj;
59+
JsonDocument doc2(obj, &spyingAllocator);
6360

6461
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
65-
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
62+
REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(
63+
addPadding(doc1.memoryUsage())));
6664
}
6765

6866
SECTION("Construct from JsonArray") {
6967
JsonDocument doc1(200);
7068
JsonArray arr = doc1.to<JsonArray>();
7169
arr.add("hello");
7270

73-
JsonDocument doc2 = arr;
71+
JsonDocument doc2(arr, &spyingAllocator);
7472

7573
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
76-
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
74+
REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(
75+
addPadding(doc1.memoryUsage())));
7776
}
7877

7978
SECTION("Construct from JsonVariant") {
8079
JsonDocument doc1(200);
81-
deserializeJson(doc1, "42");
80+
deserializeJson(doc1, "\"hello\"");
8281

83-
JsonDocument doc2 = doc1.as<JsonVariant>();
82+
JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
8483

85-
REQUIRE(doc2.as<std::string>() == "42");
86-
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
84+
REQUIRE(doc2.as<std::string>() == "hello");
85+
REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(
86+
addPadding(doc1.memoryUsage())));
8787
}
8888
}

extras/tests/JsonDocument/garbageCollect.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,28 @@
1212
using ArduinoJson::detail::sizeofObject;
1313

1414
TEST_CASE("JsonDocument::garbageCollect()") {
15-
SpyingAllocator spyingAllocator;
1615
ControllableAllocator controllableAllocator;
17-
JsonDocument doc(4096, &controllableAllocator);
16+
SpyingAllocator spyingAllocator(&controllableAllocator);
17+
JsonDocument doc(4096, &spyingAllocator);
1818

1919
SECTION("when allocation succeeds") {
2020
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
21-
REQUIRE(doc.capacity() == 4096);
2221
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
2322
doc.remove("blanket");
2423

2524
bool result = doc.garbageCollect();
2625

2726
REQUIRE(result == true);
2827
REQUIRE(doc.memoryUsage() == sizeofObject(1) + 8);
29-
REQUIRE(doc.capacity() == 4096);
3028
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
29+
REQUIRE(spyingAllocator.log() == AllocatorLog()
30+
<< AllocatorLog::Allocate(4096)
31+
<< AllocatorLog::Allocate(4096)
32+
<< AllocatorLog::Deallocate(4096));
3133
}
3234

3335
SECTION("when allocation fails") {
3436
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
35-
REQUIRE(doc.capacity() == 4096);
3637
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
3738
doc.remove("blanket");
3839
controllableAllocator.disable();
@@ -41,7 +42,10 @@ TEST_CASE("JsonDocument::garbageCollect()") {
4142

4243
REQUIRE(result == false);
4344
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
44-
REQUIRE(doc.capacity() == 4096);
4545
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
46+
47+
REQUIRE(spyingAllocator.log() == AllocatorLog()
48+
<< AllocatorLog::Allocate(4096)
49+
<< AllocatorLog::AllocateFail(4096));
4650
}
4751
}

0 commit comments

Comments
 (0)