2
0
mirror of https://github.com/boostorg/json.git synced 2026-02-01 08:32:13 +00:00
Files
json/test/static_resource.cpp
Vinnie Falco 591e2dc5a5 serializer refactor (API Change):
fix #275

* serialize() replaces to_string()
* serialize.hpp replaces to_string.hpp
* serializer works on all types
* serialize, operator<< work on all types
* reset() takes pointer arguments
* resetting constructor removed
2020-09-07 10:36:26 -07:00

95 lines
2.1 KiB
C++

//
// 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/cppalliance/json
//
// Test that header file is self-contained.
#include <boost/json/static_resource.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/serialize.hpp>
#include <iostream>
#include "test_suite.hpp"
BOOST_JSON_NS_BEGIN
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(void*, size_t)
{
unsigned char buf[1000];
static_resource mr(
&buf[0], sizeof(buf));
BOOST_TEST(serialize(parse(
"[1,2,3]", &mr)) == "[1,2,3]");
}
// 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);
}
// 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]");
}
// 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");
BOOST_JSON_NS_END