mirror of
				https://github.com/bblanchon/ArduinoJson.git
				synced 2025-11-04 08:31:36 +01:00 
			
		
		
		
	Because a slot id is smaller than a pointer, this change will ultimately allow reducing the slot size.
		
			
				
	
	
		
			70 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			70 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
// ArduinoJson - https://arduinojson.org
 | 
						|
// Copyright © 2014-2025, Benoit BLANCHON
 | 
						|
// MIT License
 | 
						|
 | 
						|
#include <ArduinoJson.h>
 | 
						|
#include <catch.hpp>
 | 
						|
 | 
						|
#include <string>
 | 
						|
 | 
						|
using ArduinoJson::detail::is_base_of;
 | 
						|
 | 
						|
static std::string allocatorLog;
 | 
						|
 | 
						|
struct CustomAllocator {
 | 
						|
  CustomAllocator() {
 | 
						|
    allocatorLog = "";
 | 
						|
  }
 | 
						|
 | 
						|
  void* allocate(size_t n) {
 | 
						|
    allocatorLog += "A";
 | 
						|
    return malloc(n);
 | 
						|
  }
 | 
						|
 | 
						|
  void deallocate(void* p) {
 | 
						|
    free(p);
 | 
						|
    allocatorLog += "D";
 | 
						|
  }
 | 
						|
 | 
						|
  void* reallocate(void* p, size_t n) {
 | 
						|
    allocatorLog += "R";
 | 
						|
    return realloc(p, n);
 | 
						|
  }
 | 
						|
};
 | 
						|
 | 
						|
TEST_CASE("BasicJsonDocument") {
 | 
						|
  allocatorLog.clear();
 | 
						|
 | 
						|
  SECTION("is a JsonDocument") {
 | 
						|
    REQUIRE(
 | 
						|
        is_base_of<JsonDocument, BasicJsonDocument<CustomAllocator>>::value ==
 | 
						|
        true);
 | 
						|
  }
 | 
						|
 | 
						|
  SECTION("deserialize / serialize") {
 | 
						|
    BasicJsonDocument<CustomAllocator> doc(256);
 | 
						|
    deserializeJson(doc, "{\"hello\":\"world\"}");
 | 
						|
    REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
 | 
						|
    doc.clear();
 | 
						|
    REQUIRE(allocatorLog == "AARARDDD");
 | 
						|
  }
 | 
						|
 | 
						|
  SECTION("copy") {
 | 
						|
    BasicJsonDocument<CustomAllocator> doc(256);
 | 
						|
    doc["hello"] = "world";
 | 
						|
    auto copy = doc;
 | 
						|
    REQUIRE(copy.as<std::string>() == "{\"hello\":\"world\"}");
 | 
						|
    REQUIRE(allocatorLog == "AAAA");
 | 
						|
  }
 | 
						|
 | 
						|
  SECTION("capacity") {
 | 
						|
    BasicJsonDocument<CustomAllocator> doc(256);
 | 
						|
    REQUIRE(doc.capacity() == 256);
 | 
						|
  }
 | 
						|
 | 
						|
  SECTION("garbageCollect()") {
 | 
						|
    BasicJsonDocument<CustomAllocator> doc(256);
 | 
						|
    doc.garbageCollect();
 | 
						|
  }
 | 
						|
}
 |