1 // -*- C++ -*- 2 //===-- uninitialized_construct.pass.cpp ----------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 // Tests for uninitialized_default_construct, uninitialized_default_construct_n, 11 // uninitialized_value_construct, uninitialized_value_construct_n 12 13 #include "support/pstl_test_config.h" 14 15 #ifdef PSTL_STANDALONE_TESTS 16 #include "pstl/execution" 17 #include "pstl/memory" 18 #else 19 #include <execution> 20 #include <memory> 21 #endif // PSTL_STANDALONE_TESTS 22 23 #include "support/utils.h" 24 25 using namespace TestUtils; 26 27 // function of checking correctness for uninitialized.construct.value 28 template <typename T, typename Iterator> 29 bool 30 IsCheckValueCorrectness(Iterator begin, Iterator end) 31 { 32 for (; begin != end; ++begin) 33 { 34 if (*begin != T()) 35 { 36 return false; 37 } 38 } 39 return true; 40 } 41 42 struct test_uninit_construct 43 { 44 template <typename Policy, typename Iterator> 45 void 46 operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial<T>=*/std::false_type) 47 { 48 typedef typename std::iterator_traits<Iterator>::value_type T; 49 // it needs for cleaning memory that was filled by default constructors in unique_ptr<T[]> p(new T[n]) 50 // and for cleaning memory after last calling of uninitialized_value_construct_n. 51 // It is important for non-trivial types 52 std::destroy_n(exec, begin, n); 53 54 // reset counter of constructors 55 T::SetCount(0); 56 // run algorithm 57 std::uninitialized_default_construct(exec, begin, end); 58 // compare counter of constructors to length of container 59 EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct"); 60 // destroy objects for testing new algorithms on same memory 61 std::destroy(exec, begin, end); 62 63 std::uninitialized_default_construct_n(exec, begin, n); 64 EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct_n"); 65 std::destroy_n(exec, begin, n); 66 67 std::uninitialized_value_construct(exec, begin, end); 68 EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct"); 69 std::destroy(exec, begin, end); 70 71 std::uninitialized_value_construct_n(exec, begin, n); 72 EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct_n"); 73 } 74 75 template <typename Policy, typename Iterator> 76 void 77 operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial<T>=*/std::true_type) 78 { 79 typedef typename std::iterator_traits<Iterator>::value_type T; 80 81 std::uninitialized_default_construct(exec, begin, end); 82 std::destroy(exec, begin, end); 83 84 std::uninitialized_default_construct_n(exec, begin, n); 85 std::destroy_n(exec, begin, n); 86 87 std::uninitialized_value_construct(exec, begin, end); 88 // check correctness for uninitialized.construct.value 89 EXPECT_TRUE(IsCheckValueCorrectness<T>(begin, end), "wrong uninitialized_value_construct"); 90 std::destroy(exec, begin, end); 91 92 std::uninitialized_value_construct_n(exec, begin, n); 93 EXPECT_TRUE(IsCheckValueCorrectness<T>(begin, end), "wrong uninitialized_value_construct_n"); 94 std::destroy_n(exec, begin, n); 95 } 96 }; 97 98 template <typename T> 99 void 100 test_uninit_construct_by_type() 101 { 102 std::size_t N = 100000; 103 for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 104 { 105 std::unique_ptr<T[]> p(new T[n]); 106 invoke_on_all_policies(test_uninit_construct(), p.get(), std::next(p.get(), n), n, std::is_trivial<T>()); 107 } 108 } 109 110 int32_t 111 main() 112 { 113 114 // for user-defined types 115 #if !__PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN 116 test_uninit_construct_by_type<Wrapper<int32_t>>(); 117 test_uninit_construct_by_type<Wrapper<std::vector<std::string>>>(); 118 #endif 119 120 // for trivial types 121 test_uninit_construct_by_type<int8_t>(); 122 test_uninit_construct_by_type<float64_t>(); 123 124 std::cout << done() << std::endl; 125 return 0; 126 } 127