1 //
2 // Copyright (c) 2020 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/static_resource.hpp>
12 
13 #include <boost/json/parse.hpp>
14 #include <boost/json/serialize.hpp>
15 #include <iostream>
16 
17 #include "test_suite.hpp"
18 
19 BOOST_JSON_NS_BEGIN
20 
21 BOOST_STATIC_ASSERT( std::is_nothrow_destructible<static_resource>::value );
22 
23 class static_resource_test
24 {
25 public:
26     void
testJavadocs()27     testJavadocs()
28     {
29     //--------------------------------------
30 
31     unsigned char buf[ 4000 ];
32     static_resource mr( buf );
33 
34     // Parse the string, using our memory resource
35     value jv = parse( "[1,2,3]", &mr );
36 
37     // Print the JSON
38     std::cout << jv;
39 
40     //--------------------------------------
41     }
42 
43     void
test()44     test()
45     {
46         // static_resource(unsigned char*, size_t)
47         {
48             unsigned char buf[1000];
49             static_resource mr(
50                 &buf[0], sizeof(buf));
51             BOOST_TEST(serialize(parse(
52                 "[1,2,3]", &mr)) == "[1,2,3]");
53         }
54 
55     #if defined(__cpp_lib_byte)
56         // static_resource(std::byte*, size_t)
57         {
58             std::byte buf[1000];
59             static_resource mr(
60                 &buf[0], sizeof(buf));
61             BOOST_TEST(serialize(parse(
62                 "[1,2,3]", &mr)) == "[1,2,3]");
63         }
64     #endif
65 
66         // static_resource(unsigned char[N])
67         {
68             unsigned char buf[10];
69             static_resource mr(buf);
70             BOOST_TEST_THROWS(
71                 serialize(parse("[1,2,3]", &mr)),
72                 std::bad_alloc);
73         }
74 
75     #if defined(__cpp_lib_byte)
76         // static_resource(std::byte[N])
77         {
78             std::byte buf[10];
79             static_resource mr(buf);
80             BOOST_TEST_THROWS(
81                 serialize(parse("[1,2,3]", &mr)),
82                 std::bad_alloc);
83         }
84     #endif
85 
86         // static_resource(unsigned char[N], size_t)
87         {
88             unsigned char buf[1000];
89             static_resource mr(
90                 buf, 500);
91             BOOST_TEST(serialize(parse(
92                 "[1,2,3]", &mr)) == "[1,2,3]");
93         }
94 
95     #if defined(__cpp_lib_byte)
96         // static_resource(std::byte[N])
97         {
98             std::byte buf[1000];
99             static_resource mr(
100                 buf, 500);
101             BOOST_TEST(serialize(parse(
102                 "[1,2,3]", &mr)) == "[1,2,3]");
103         }
104     #endif
105 
106         // release()
107         {
108             unsigned char buf[10];
109             static_resource mr(
110                 buf, sizeof(buf));
111             (void)mr.allocate(10,1);
112             BOOST_TEST_THROWS(
113                 mr.allocate(10,1),
114                 std::bad_alloc);
115             mr.release();
116             (void)mr.allocate(10,1);
117         }
118     }
119 
120     void
run()121     run()
122     {
123         test();
124     }
125 };
126 
127 TEST_SUITE(static_resource_test, "boost.json.static_resource");
128 
129 BOOST_JSON_NS_END
130