2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // Official repository: https://github.com/boostorg/json
11 // Test that header file is self-contained.
12 #include <boost/json/monotonic_resource.hpp>
14 #include <boost/json/null_resource.hpp>
15 #include <boost/json/parse.hpp>
16 #include <boost/json/serialize.hpp>
17 #include <boost/json/detail/align.hpp>
20 #include "test_suite.hpp"
24 BOOST_STATIC_ASSERT( std::is_nothrow_destructible
<monotonic_resource
>::value
);
26 class monotonic_resource_test
33 std::size_t buffer_size
)
35 using ptr_t
= const volatile unsigned char*;
36 return reinterpret_cast<ptr_t
>(ptr
) >= reinterpret_cast<ptr_t
>(buffer
) &&
37 reinterpret_cast<ptr_t
>(ptr
) < reinterpret_cast<ptr_t
>(buffer
) + buffer_size
;
41 all_alloc_in_same_block(
42 monotonic_resource
& mr
,
47 auto first
= mr
.allocate(align
, align
);
49 for (auto allocs
= (bytes
- align
) / align
; allocs
; --allocs
)
50 result
&= in_buffer(mr
.allocate(align
, align
), first
, bytes
);
58 //--------------------------------------
60 unsigned char buf
[4000];
61 monotonic_resource
mr( buf
);
63 // Parse the string, using our memory resource
64 auto const jv
= parse( "[1,2,3]", &mr
);
69 //--------------------------------------
75 // ~monotonic_resource
80 // monotonic_resource(size_t, storage_ptr)
83 monotonic_resource
mr(1);
86 monotonic_resource
mr(5000);
89 monotonic_resource
mr(
90 1, get_null_resource());
93 monotonic_resource
mr(
94 5000, get_null_resource());
98 // monotonic_resource(unsigned char*, size_t, storage_ptr)
101 unsigned char buf
[2000];
102 monotonic_resource
mr(
103 &buf
[0], sizeof(buf
));
106 unsigned char buf
[2000];
107 monotonic_resource
mr(
108 &buf
[0], sizeof(buf
),
109 get_null_resource());
113 // monotonic_resource(unsigned char[N], storage_ptr)
116 unsigned char buf
[2000];
117 monotonic_resource
mr(buf
);
120 unsigned char buf
[2000];
121 monotonic_resource
mr(buf
,
122 get_null_resource());
126 #ifdef __cpp_lib_byte
127 // monotonic_resource(std::byte[N], storage_ptr)
131 monotonic_resource
mr(buf
);
136 monotonic_resource
mr(buf
,
137 get_null_resource());
142 // monotonic_resource(unsigned char[N], std::size_t, storage_ptr)
145 unsigned char buf
[2000];
146 monotonic_resource
mr(buf
, 1000);
149 unsigned char buf
[2000];
150 monotonic_resource
mr(buf
, 1000,
151 get_null_resource());
155 #ifdef __cpp_lib_byte
156 // monotonic_resource(unsigned char[N], std::size_t, storage_ptr)
160 monotonic_resource
mr(buf
, 1000);
164 monotonic_resource
mr(buf
, 1000,
165 get_null_resource());
172 unsigned char buf
[10];
173 monotonic_resource
mr(
175 (void)mr
.allocate(10,1);
177 (void)mr
.allocate(10,1);
182 monotonic_resource
mr(std::size_t(-1)-2);
189 // test that each block gets filled to capacity
190 // and if the growth factor is correct
192 monotonic_resource mr
;
193 BOOST_TEST(all_alloc_in_same_block(mr
, 1024, 1));
194 BOOST_TEST(all_alloc_in_same_block(mr
, 2048, 2));
195 BOOST_TEST(all_alloc_in_same_block(mr
, 4096, 1));
196 BOOST_TEST(all_alloc_in_same_block(mr
, 8192, 4));
197 BOOST_TEST(all_alloc_in_same_block(mr
, 16384, 1));
198 BOOST_TEST(all_alloc_in_same_block(mr
, 32768, 8));
199 BOOST_TEST(all_alloc_in_same_block(mr
, 65536, 1));
201 // test if each allocation is aligned correctly
203 monotonic_resource mr
;
204 for (std::size_t i
= 0; i
< 4096; ++i
)
206 const auto size
= ((i
* 3) % 32) + 1;
207 std::size_t next
= 1;
208 for (auto mod
= i
% alignof(detail::max_align_t
);
209 mod
; mod
>>= 1, next
<<= 1);
210 const auto align
= (std::max
)(next
,
212 BOOST_TEST(!(reinterpret_cast<std::uintptr_t>(mr
.allocate(size
, align
)) % align
));
215 // test if user provided sizes are correctly rounded
218 monotonic_resource
mr(10);
219 BOOST_TEST(all_alloc_in_same_block(mr
, 1024, 1));
222 monotonic_resource
mr(1025);
223 BOOST_TEST(all_alloc_in_same_block(mr
, 2048, 1));
226 monotonic_resource
mr(4000);
227 BOOST_TEST(all_alloc_in_same_block(mr
, 4096, 1));
230 // test if sizes are correctly determined from initial buffers
233 unsigned char buf
[512];
234 monotonic_resource
mr(buf
, 512);
235 BOOST_TEST(all_alloc_in_same_block(mr
, 512, 1));
236 BOOST_TEST(all_alloc_in_same_block(mr
, 1024, 1));
239 unsigned char buf
[2048];
240 monotonic_resource
mr(buf
, 2048);
241 BOOST_TEST(all_alloc_in_same_block(mr
, 2048, 1));
242 BOOST_TEST(all_alloc_in_same_block(mr
, 4096, 1));
245 unsigned char buf
[4000];
246 monotonic_resource
mr(buf
, 4000);
247 BOOST_TEST(all_alloc_in_same_block(mr
, 4000, 1));
248 BOOST_TEST(all_alloc_in_same_block(mr
, 4096, 1));
251 // test if allocations that exceed the block size cause rounding to occur
254 monotonic_resource mr
;
255 auto p
= mr
.allocate(2048);
257 BOOST_TEST(all_alloc_in_same_block(mr
, 4096, 1));
260 monotonic_resource mr
;
262 p
= mr
.allocate(2000, 1);
263 p
= mr
.allocate(48, 1);
265 BOOST_TEST(all_alloc_in_same_block(mr
, 4096, 1));
276 "title
": "example glossary
",
283 "GlossTerm
": "Standard Generalized Markup Language
",
285 "Abbrev
": "ISO
8879:1986",
287 "para
": "A meta
-markup language
, used to create markup languages such as DocBook
.",
288 "GlossSeeAlso
": ["GML
", "XML
"]
296 , make_shared_resource
<monotonic_resource
>());
309 TEST_SUITE(monotonic_resource_test
, "boost.json.monotonic_resource");