]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/value_stack.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / json / test / value_stack.cpp
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
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)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9
10 // Test that header file is self-contained.
11 #include <boost/json/value_stack.hpp>
12
13 #include <boost/json/serialize.hpp>
14 #include <boost/json/static_resource.hpp>
15
16 #include "test_suite.hpp"
17
18 BOOST_JSON_NS_BEGIN
19
20 BOOST_STATIC_ASSERT( std::is_nothrow_destructible<value_stack>::value );
21
22 class value_stack_test
23 {
24 public:
25 // This is from the javadoc
26 void
27 testValueStack()
28 {
29
30 //----------------------------------
31 // value_stack
32 {
33 // This example builds a json::value without any dynamic memory allocations:
34
35 // Construct the value stack using a local buffer
36 unsigned char temp[4096];
37 value_stack st( storage_ptr(), temp, sizeof(temp) );
38
39 // Create a static resource with a local initial buffer
40 unsigned char buf[4096];
41 static_resource mr( buf, sizeof(buf) );
42
43 // All values on the stack will use `mr`
44 st.reset(&mr);
45
46 // Push the key/value pair "a":1.
47 st.push_key("a");
48 st.push_int64(1);
49
50 // Push "b":null
51 st.push_key("b");
52 st.push_null();
53
54 // Push "c":"hello"
55 st.push_key("c");
56 st.push_string("hello");
57
58 // Pop the three key/value pairs and push an object with those three values.
59 st.push_object(3);
60
61 // Pop the object from the stack and take ownership.
62 value jv = st.release();
63
64 assert( serialize(jv) == "{\"a\":1,\"b\":null,\"c\":\"hello\"}" );
65
66 // At this point we could re-use the stack by calling reset
67 }
68
69 //----------------------------------
70 // value_stack::push_array
71 {
72 value_stack st;
73
74 // reset must be called first or else the behavior is undefined
75 st.reset();
76
77 // Place three values on the stack
78 st.push_int64( 1 );
79 st.push_int64( 2 );
80 st.push_int64( 3 );
81
82 // Remove the 3 values, and push an array with those 3 elements on the stack
83 st.push_array( 3 );
84
85 // Pop the object from the stack and take ownership.
86 value jv = st.release();
87
88 assert( serialize(jv) == "[1,2,3]" );
89
90 // At this point, reset must be called again to use the stack
91 }
92
93 //----------------------------------
94 // value_stack::push_object
95 {
96 value_stack st;
97
98 // reset must be called first or else the behavior is undefined
99 st.reset();
100
101 // Place a key/value pair onto the stack
102 st.push_key( "x" );
103 st.push_bool( true );
104
105 // Replace the key/value pair with an object containing a single element
106 st.push_object( 1 );
107
108 // Pop the object from the stack and take ownership.
109 value jv = st.release();
110
111 assert( serialize(jv) == "{\"x\":true}" );
112
113 // At this point, reset must be called again to use the stack
114 }
115
116 }
117
118 void
119 run()
120 {
121 testValueStack();
122 }
123 };
124
125 TEST_SUITE(value_stack_test, "boost.json.value_stack");
126
127 BOOST_JSON_NS_END