Skip to content

Commit 0643c2e

Browse files
committed
Test: gather JsonDocument constructor and assignment tests
1 parent bcf1339 commit 0643c2e

File tree

5 files changed

+165
-196
lines changed

5 files changed

+165
-196
lines changed

extras/tests/JsonDocument/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
add_executable(JsonDocumentTests
66
add.cpp
7-
allocator.cpp
87
assignment.cpp
98
capacity.cpp
109
cast.cpp
@@ -13,6 +12,7 @@ add_executable(JsonDocumentTests
1312
containsKey.cpp
1413
createNested.cpp
1514
ElementProxy.cpp
15+
garbageCollect.cpp
1616
isNull.cpp
1717
issue1120.cpp
1818
MemberProxy.cpp

extras/tests/JsonDocument/allocator.cpp

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

extras/tests/JsonDocument/assignment.cpp

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,90 @@
55
#include <ArduinoJson.h>
66
#include <catch.hpp>
77

8+
#include "Allocators.hpp"
9+
810
using ArduinoJson::detail::sizeofArray;
911
using ArduinoJson::detail::sizeofObject;
1012

1113
TEST_CASE("JsonDocument assignment") {
12-
SECTION("Copy assignment reallocates when capacity is smaller") {
13-
JsonDocument doc1(1234);
14-
deserializeJson(doc1, "{\"hello\":\"world\"}");
15-
JsonDocument doc2(8);
16-
17-
doc2 = doc1;
14+
SpyingAllocator spyingAllocator;
15+
16+
SECTION("Copy assignment same capacity") {
17+
{
18+
JsonDocument doc1(1024, &spyingAllocator);
19+
deserializeJson(doc1, "{\"hello\":\"world\"}");
20+
JsonDocument doc2(1024, &spyingAllocator);
21+
22+
doc2 = doc1;
23+
24+
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
25+
REQUIRE(doc2.capacity() == doc1.capacity());
26+
}
27+
REQUIRE(spyingAllocator.log() == AllocatorLog()
28+
<< AllocatorLog::Allocate(1024)
29+
<< AllocatorLog::Allocate(1024)
30+
<< AllocatorLog::Deallocate(1024)
31+
<< AllocatorLog::Deallocate(1024));
32+
}
1833

19-
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
20-
REQUIRE(doc2.capacity() == doc1.capacity());
34+
SECTION("Copy assignment reallocates when capacity is smaller") {
35+
{
36+
JsonDocument doc1(4096, &spyingAllocator);
37+
deserializeJson(doc1, "{\"hello\":\"world\"}");
38+
JsonDocument doc2(8, &spyingAllocator);
39+
40+
doc2 = doc1;
41+
42+
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
43+
REQUIRE(doc2.capacity() == doc1.capacity());
44+
}
45+
REQUIRE(spyingAllocator.log() == AllocatorLog()
46+
<< AllocatorLog::Allocate(4096)
47+
<< AllocatorLog::Allocate(8)
48+
<< AllocatorLog::Deallocate(8)
49+
<< AllocatorLog::Allocate(4096)
50+
<< AllocatorLog::Deallocate(4096)
51+
<< AllocatorLog::Deallocate(4096));
2152
}
2253

2354
SECTION("Copy assignment reallocates when capacity is larger") {
24-
JsonDocument doc1(100);
25-
deserializeJson(doc1, "{\"hello\":\"world\"}");
26-
JsonDocument doc2(1234);
27-
28-
doc2 = doc1;
55+
{
56+
JsonDocument doc1(1024, &spyingAllocator);
57+
deserializeJson(doc1, "{\"hello\":\"world\"}");
58+
JsonDocument doc2(4096, &spyingAllocator);
59+
60+
doc2 = doc1;
61+
62+
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
63+
REQUIRE(doc2.capacity() == doc1.capacity());
64+
}
65+
REQUIRE(spyingAllocator.log() == AllocatorLog()
66+
<< AllocatorLog::Allocate(1024)
67+
<< AllocatorLog::Allocate(4096)
68+
<< AllocatorLog::Deallocate(4096)
69+
<< AllocatorLog::Allocate(1024)
70+
<< AllocatorLog::Deallocate(1024)
71+
<< AllocatorLog::Deallocate(1024));
72+
}
2973

30-
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
31-
REQUIRE(doc2.capacity() == doc1.capacity());
74+
SECTION("Move assign") {
75+
{
76+
JsonDocument doc1(4096, &spyingAllocator);
77+
doc1.set(std::string("The size of this string is 32!!"));
78+
JsonDocument doc2(8, &spyingAllocator);
79+
80+
doc2 = std::move(doc1);
81+
82+
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
83+
REQUIRE(doc1.as<std::string>() == "null");
84+
REQUIRE(doc1.capacity() == 0);
85+
REQUIRE(doc2.capacity() == 4096);
86+
}
87+
REQUIRE(spyingAllocator.log() == AllocatorLog()
88+
<< AllocatorLog::Allocate(4096)
89+
<< AllocatorLog::Allocate(8)
90+
<< AllocatorLog::Deallocate(8)
91+
<< AllocatorLog::Deallocate(4096));
3292
}
3393

3494
SECTION("Assign from JsonObject") {

extras/tests/JsonDocument/constructor.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,56 @@
55
#include <ArduinoJson.h>
66
#include <catch.hpp>
77

8+
#include "Allocators.hpp"
9+
810
using ArduinoJson::detail::addPadding;
911

1012
TEST_CASE("JsonDocument constructor") {
11-
SECTION("Copy constructor") {
12-
JsonDocument doc1(1234);
13-
deserializeJson(doc1, "{\"hello\":\"world\"}");
13+
SpyingAllocator spyingAllocator;
14+
15+
SECTION("JsonDocument(size_t)") {
16+
{ JsonDocument doc(4096, &spyingAllocator); }
17+
REQUIRE(spyingAllocator.log() == AllocatorLog()
18+
<< AllocatorLog::Allocate(4096)
19+
<< AllocatorLog::Deallocate(4096));
20+
}
1421

15-
JsonDocument doc2 = doc1;
22+
SECTION("JsonDocument(const JsonDocument&)") {
23+
{
24+
JsonDocument doc1(4096, &spyingAllocator);
25+
doc1.set(std::string("The size of this string is 32!!"));
1626

17-
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
27+
JsonDocument doc2(doc1);
28+
29+
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
30+
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
31+
REQUIRE(doc2.capacity() == 4096);
32+
}
33+
REQUIRE(spyingAllocator.log() == AllocatorLog()
34+
<< AllocatorLog::Allocate(4096)
35+
<< AllocatorLog::Allocate(4096)
36+
<< AllocatorLog::Deallocate(4096)
37+
<< AllocatorLog::Deallocate(4096));
38+
}
39+
40+
SECTION("JsonDocument(JsonDocument&&)") {
41+
{
42+
JsonDocument doc1(4096, &spyingAllocator);
43+
doc1.set(std::string("The size of this string is 32!!"));
44+
45+
JsonDocument doc2(std::move(doc1));
1846

19-
REQUIRE(doc2.capacity() == doc1.capacity());
47+
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
48+
REQUIRE(doc1.as<std::string>() == "null");
49+
REQUIRE(doc1.capacity() == 0);
50+
REQUIRE(doc2.capacity() == 4096);
51+
}
52+
REQUIRE(spyingAllocator.log() == AllocatorLog()
53+
<< AllocatorLog::Allocate(4096)
54+
<< AllocatorLog::Deallocate(4096));
2055
}
2156

22-
SECTION("Construct from JsonObject") {
57+
SECTION("JsonDocument(JsonObject)") {
2358
JsonDocument doc1(200);
2459
JsonObject obj = doc1.to<JsonObject>();
2560
obj["hello"] = "world";

0 commit comments

Comments
 (0)