1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
//
// Copyright (c) 2020 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/json
//
// Test that header file is self-contained.
#include <boost/json/static_resource.hpp>
#include <boost/core/detail/static_assert.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/serialize.hpp>
#include <iostream>
#include "test_suite.hpp"
namespace boost {
namespace json {
BOOST_CORE_STATIC_ASSERT(
std::is_nothrow_destructible<static_resource>::value);
class static_resource_test
{
public:
void
testJavadocs()
{
//--------------------------------------
unsigned char buf[ 4000 ];
static_resource mr( buf );
// Parse the string, using our memory resource
value jv = parse( "[1,2,3]", &mr );
// Print the JSON
std::cout << jv;
//--------------------------------------
}
void
test()
{
// static_resource(unsigned char*, size_t)
{
unsigned char buf[1000];
static_resource mr(
&buf[0], sizeof(buf));
BOOST_TEST(serialize(parse(
"[1,2,3]", &mr)) == "[1,2,3]");
}
#if defined(__cpp_lib_byte)
// static_resource(std::byte*, size_t)
{
std::byte buf[1000];
static_resource mr(
&buf[0], sizeof(buf));
BOOST_TEST(serialize(parse(
"[1,2,3]", &mr)) == "[1,2,3]");
}
#endif
// static_resource(unsigned char[N])
{
unsigned char buf[10];
static_resource mr(buf);
BOOST_TEST_THROWS(
serialize(parse("[1,2,3]", &mr)),
std::bad_alloc);
}
#if defined(__cpp_lib_byte)
// static_resource(std::byte[N])
{
std::byte buf[10];
static_resource mr(buf);
BOOST_TEST_THROWS(
serialize(parse("[1,2,3]", &mr)),
std::bad_alloc);
}
#endif
// static_resource(unsigned char[N], size_t)
{
unsigned char buf[1000];
static_resource mr(
buf, 500);
BOOST_TEST(serialize(parse(
"[1,2,3]", &mr)) == "[1,2,3]");
}
#if defined(__cpp_lib_byte)
// static_resource(std::byte[N])
{
std::byte buf[1000];
static_resource mr(
buf, 500);
BOOST_TEST(serialize(parse(
"[1,2,3]", &mr)) == "[1,2,3]");
}
#endif
// release()
{
unsigned char buf[10];
static_resource mr(
buf, sizeof(buf));
(void)mr.allocate(10,1);
BOOST_TEST_THROWS(
mr.allocate(10,1),
std::bad_alloc);
mr.release();
(void)mr.allocate(10,1);
}
}
void
run()
{
test();
}
};
TEST_SUITE(static_resource_test, "boost.json.static_resource");
} // namespace json
} // namespace boost
|