File: uninitialized_construct.cc

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (131 lines) | stat: -rw-r--r-- 4,409 bytes parent folder | download
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
// -*- C++ -*-
// { dg-options "-ltbb" }
// { dg-do run { target c++17 } }
// { dg-timeout-factor 3 }
// { dg-require-effective-target tbb_backend }

//===-- uninitialized_construct.pass.cpp ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// Tests for uninitialized_default_construct, uninitialized_default_construct_n,
//           uninitialized_value_construct,   uninitialized_value_construct_n

#include "pstl/pstl_test_config.h"

#ifdef PSTL_STANDALONE_TESTS
#include "pstl/execution"
#include "pstl/memory"
#else
#include <execution>
#include <memory>
#endif // PSTL_STANDALONE_TESTS

#include "pstl/test_utils.h"

using namespace TestUtils;

// function of checking correctness for uninitialized.construct.value
template <typename T, typename Iterator>
bool
IsCheckValueCorrectness(Iterator begin, Iterator end)
{
    for (; begin != end; ++begin)
    {
        if (*begin != T())
        {
            return false;
        }
    }
    return true;
}

struct test_uninit_construct
{
    template <typename Policy, typename Iterator>
    void
    operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial<T>=*/std::false_type)
    {
        typedef typename std::iterator_traits<Iterator>::value_type T;
        // it needs for cleaning memory that was filled by default constructors in unique_ptr<T[]> p(new T[n])
        // and for cleaning memory after last calling of uninitialized_value_construct_n.
        // It is important for non-trivial types
        std::destroy_n(exec, begin, n);

        // reset counter of constructors
        T::SetCount(0);
        // run algorithm
        std::uninitialized_default_construct(exec, begin, end);
        // compare counter of constructors to length of container
        EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct");
        // destroy objects for testing new algorithms on same memory
        std::destroy(exec, begin, end);

        std::uninitialized_default_construct_n(exec, begin, n);
        EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct_n");
        std::destroy_n(exec, begin, n);

        std::uninitialized_value_construct(exec, begin, end);
        EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct");
        std::destroy(exec, begin, end);

        std::uninitialized_value_construct_n(exec, begin, n);
        EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct_n");
    }

    template <typename Policy, typename Iterator>
    void
    operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial<T>=*/std::true_type)
    {
        typedef typename std::iterator_traits<Iterator>::value_type T;

        std::uninitialized_default_construct(exec, begin, end);
        std::destroy(exec, begin, end);

        std::uninitialized_default_construct_n(exec, begin, n);
        std::destroy_n(exec, begin, n);

        std::uninitialized_value_construct(exec, begin, end);
        // check correctness for uninitialized.construct.value
        EXPECT_TRUE(IsCheckValueCorrectness<T>(begin, end), "wrong uninitialized_value_construct");
        std::destroy(exec, begin, end);

        std::uninitialized_value_construct_n(exec, begin, n);
        EXPECT_TRUE(IsCheckValueCorrectness<T>(begin, end), "wrong uninitialized_value_construct_n");
        std::destroy_n(exec, begin, n);
    }
};

template <typename T>
void
test_uninit_construct_by_type()
{
    std::size_t N = 100000;
    for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
    {
        std::unique_ptr<T[]> p(new T[n]);
        invoke_on_all_policies(test_uninit_construct(), p.get(), std::next(p.get(), n), n, std::is_trivial<T>());
    }
}

int
main()
{

    // for user-defined types
#if !defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN)
    test_uninit_construct_by_type<Wrapper<int32_t>>();
    test_uninit_construct_by_type<Wrapper<std::vector<std::string>>>();
#endif

    // for trivial types
    test_uninit_construct_by_type<int8_t>();
    test_uninit_construct_by_type<float64_t>();

    std::cout << done() << std::endl;
    return 0;
}