]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/value_stack.cpp
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/json
10 // Test that header file is self-contained.
11 #include <boost/json/value_stack.hpp>
13 #include <boost/json/serialize.hpp>
14 #include <boost/json/static_resource.hpp>
16 #include "test_suite.hpp"
20 BOOST_STATIC_ASSERT( std::is_nothrow_destructible
<value_stack
>::value
);
22 class value_stack_test
25 // This is from the javadoc
30 //----------------------------------
33 // This example builds a json::value without any dynamic memory allocations:
35 // Construct the value stack using a local buffer
36 unsigned char temp
[4096];
37 value_stack
st( storage_ptr(), temp
, sizeof(temp
) );
39 // Create a static resource with a local initial buffer
40 unsigned char buf
[4096];
41 static_resource
mr( buf
, sizeof(buf
) );
43 // All values on the stack will use `mr`
46 // Push the key/value pair "a":1.
56 st
.push_string("hello");
58 // Pop the three key/value pairs and push an object with those three values.
61 // Pop the object from the stack and take ownership.
62 value jv
= st
.release();
64 assert( serialize(jv
) == "{\"a\":1,\"b\":null,\"c\":\"hello\"}" );
66 // At this point we could re-use the stack by calling reset
69 //----------------------------------
70 // value_stack::push_array
74 // reset must be called first or else the behavior is undefined
77 // Place three values on the stack
82 // Remove the 3 values, and push an array with those 3 elements on the stack
85 // Pop the object from the stack and take ownership.
86 value jv
= st
.release();
88 assert( serialize(jv
) == "[1,2,3]" );
90 // At this point, reset must be called again to use the stack
93 //----------------------------------
94 // value_stack::push_object
98 // reset must be called first or else the behavior is undefined
101 // Place a key/value pair onto the stack
103 st
.push_bool( true );
105 // Replace the key/value pair with an object containing a single element
108 // Pop the object from the stack and take ownership.
109 value jv
= st
.release();
111 assert( serialize(jv
) == "{\"x\":true}" );
113 // At this point, reset must be called again to use the stack
125 TEST_SUITE(value_stack_test
, "boost.json.value_stack");