|
5 | 5 | #include <ArduinoJson.h>
|
6 | 6 | #include <catch.hpp>
|
7 | 7 |
|
| 8 | +#include "Allocators.hpp" |
| 9 | + |
8 | 10 | using ArduinoJson::detail::sizeofArray;
|
9 | 11 | using ArduinoJson::detail::sizeofObject;
|
10 | 12 |
|
11 | 13 | 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 | + } |
18 | 33 |
|
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)); |
21 | 52 | }
|
22 | 53 |
|
23 | 54 | 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 | + } |
29 | 73 |
|
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)); |
32 | 92 | }
|
33 | 93 |
|
34 | 94 | SECTION("Assign from JsonObject") {
|
|
0 commit comments