File: storage_ptr.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (218 lines) | stat: -rw-r--r-- 4,551 bytes parent folder | download | duplicates (2)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// Copyright (c) 2019 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/storage_ptr.hpp>

#include <boost/core/detail/static_assert.hpp>

#include <type_traits>

#include "test.hpp"
#include "test_suite.hpp"

//----------------------------------------------------------

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4702) // unreachable code
#endif

namespace boost {
namespace json {

BOOST_CORE_STATIC_ASSERT(
    std::is_nothrow_move_constructible<storage_ptr>::value);

class storage_ptr_test
{
public:
    static value jv1;
    static value jv2;

    struct throwing
        : container::pmr::memory_resource
    {
        throwing()
        {
            throw std::exception{};
        }

        void*
        do_allocate(
            std::size_t,
            std::size_t) override
        {
            return nullptr;
        }

        void
        do_deallocate(
            void*,
            std::size_t,
            std::size_t) noexcept override
        {
        }

        bool
        do_is_equal(
            memory_resource const& mr) const noexcept override
        {
            return this == &mr;
        }
    };

    void
    testMembers()
    {
        auto const dsp = storage_ptr{};
        auto const usp =
            make_shared_resource<unique_resource>();

        // ~storage_ptr()
        {
            // implied
        }

        // storage_ptr()
        {
            storage_ptr sp;
            BOOST_TEST(sp.get());
        }

        // storage_ptr(storage_ptr&&)
        {
            storage_ptr sp1 = dsp;
            storage_ptr sp2(std::move(sp1));
            BOOST_TEST(sp1.get());
            BOOST_TEST(*sp2 == *dsp);
        }

        // storage_ptr(storage_ptr const&)
        {
            storage_ptr sp1 = dsp;
            storage_ptr sp2(sp1);
            BOOST_TEST(sp1 == sp2);
        }

        // operator=(storage_ptr&&)
        {
            storage_ptr sp1(dsp);
            storage_ptr sp2(usp);
            sp2 = std::move(sp1);
            BOOST_TEST(*sp2 == *dsp);
        }

        // operator=(storage_ptr const&)
        {
            storage_ptr sp1(dsp);
            storage_ptr sp2(usp);
            sp2 = sp1;
            BOOST_TEST(*sp1 == *sp2);
        }

        // get()
        {
            {
                storage_ptr sp(dsp);
                BOOST_TEST(sp.get() == dsp.get());
            }
        }

        // operator->()
        {
            storage_ptr sp(dsp);
            BOOST_TEST(sp.operator->() == dsp.get());
        }

        // operator*()
        {
            storage_ptr sp(dsp);
            BOOST_TEST(&sp.operator*() == dsp.get());
        }

        // exception in make_storage
        {
            BOOST_TEST_THROWS(
                make_shared_resource<throwing>(),
                std::exception);
        }
    }

    // https://github.com/boostorg/json/pull/182
    void
    testPull182()
    {
        struct other
        {
            virtual void f() {}
        };

        struct my_resource
            : other
            , container::pmr::memory_resource
        {
            void*
            do_allocate(
                std::size_t,
                std::size_t) override
            {
                return nullptr;
            }

            void
            do_deallocate(
                void*,
                std::size_t,
                std::size_t) noexcept override
            {
            }

            bool
            do_is_equal(
                memory_resource const&) const noexcept override
            {
                return true;
            }
        };

        my_resource mr;
        BOOST_TEST(storage_ptr(&mr).get() == &mr);
    }

    void
    testInitOrder()
    {
        BOOST_ASSERT(jv1.storage() == jv2.storage());
        BOOST_TEST(! jv1.storage().is_deallocate_trivial());
        BOOST_TEST(! jv1.storage().is_shared());
    }

    void
    run()
    {
        storage_ptr sp;

        testMembers();
        testPull182();
        testInitOrder();
    }
};

value storage_ptr_test::jv1 = {1, 2, 3};
value storage_ptr_test::jv2 = {1, 2, 3};

TEST_SUITE(storage_ptr_test, "boost.json.storage_ptr");

} // namespace json
} // namespace boost

#ifdef _MSC_VER
#pragma warning(pop)
#endif