File: alloc_construct_test.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (135 lines) | stat: -rw-r--r-- 3,177 bytes parent folder | download | duplicates (15)
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
/*
Copyright 2019 Glen Joseph Fernandes
(glenjofe@gmail.com)

Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/alloc_construct.hpp>
#include <boost/core/default_allocator.hpp>
#include <boost/core/lightweight_test.hpp>

class type {
public:
    explicit type(int x = 0, int y = 0)
        : value_(x + y) {
        ++count;
    }

    type(const type& other)
        : value_(other.value_) {
        ++count;
    }

    ~type() {
        --count;
    }

    int value() const {
        return value_;
    }

    static int count;

private:
    int value_;
};

int type::count = 0;

void test_construct()
{
    boost::default_allocator<type> a;
    type* p = a.allocate(1);
    boost::alloc_construct(a, p);
    BOOST_TEST_EQ(type::count, 1);
    BOOST_TEST_EQ(p->value(), 0);
    boost::alloc_destroy(a, p);
    BOOST_TEST_EQ(type::count, 0);
    a.deallocate(p, 1);
}

void test_construct_value()
{
    boost::default_allocator<type> a;
    type* p = a.allocate(1);
    boost::alloc_construct(a, p, 1);
    BOOST_TEST_EQ(type::count, 1);
    BOOST_TEST_EQ(p->value(), 1);
    boost::alloc_destroy(a, p);
    BOOST_TEST_EQ(type::count, 0);
    a.deallocate(p, 1);
}

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
    !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
void test_construct_args()
{
    boost::default_allocator<type> a;
    type* p = a.allocate(1);
    boost::alloc_construct(a, p, 1, 2);
    BOOST_TEST_EQ(type::count, 1);
    BOOST_TEST_EQ(p->value(), 3);
    boost::alloc_destroy(a, p);
    BOOST_TEST_EQ(type::count, 0);
    a.deallocate(p, 1);
}
#endif

void test_construct_n()
{
    boost::default_allocator<type> a;
    type* p = a.allocate(3);
    boost::alloc_construct_n(a, p, 3);
    BOOST_TEST_EQ(type::count, 3);
    BOOST_TEST_EQ(p[0].value(), 0);
    BOOST_TEST_EQ(p[1].value(), 0);
    BOOST_TEST_EQ(p[2].value(), 0);
    boost::alloc_destroy_n(a, p, 3);
    BOOST_TEST_EQ(type::count, 0);
    a.deallocate(p, 3);
}

void test_construct_n_list()
{
    boost::default_allocator<type> a;
    type* p = a.allocate(3);
    type q(1);
    boost::alloc_construct_n(a, p, 3, &q, 1);
    BOOST_TEST_EQ(type::count, 4);
    BOOST_TEST_EQ(p[0].value(), 1);
    BOOST_TEST_EQ(p[1].value(), 1);
    BOOST_TEST_EQ(p[2].value(), 1);
    boost::alloc_destroy_n(a, p, 3);
    BOOST_TEST_EQ(type::count, 1);
    a.deallocate(p, 3);
}

void test_construct_n_iterator()
{
    boost::default_allocator<type> a;
    type* p = a.allocate(3);
    type l[] = { type(1), type(2), type(3) };
    boost::alloc_construct_n(a, p, 3, &l[0]);
    BOOST_TEST_EQ(type::count, 6);
    BOOST_TEST_EQ(p[0].value(), 1);
    BOOST_TEST_EQ(p[1].value(), 2);
    BOOST_TEST_EQ(p[2].value(), 3);
    boost::alloc_destroy_n(a, p, 3);
    BOOST_TEST_EQ(type::count, 3);
    a.deallocate(p, 3);
}

int main()
{
    test_construct();
    test_construct_value();
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
    !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
    test_construct_args();
#endif
    test_construct_n();
    test_construct_n_list();
    test_construct_n_iterator();
    return boost::report_errors();
}