1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #include "oneapi/tbb/cache_aligned_allocator.h"
18 #include "oneapi/tbb/tbb_allocator.h"
19 
20 // the real body of the test is there:
21 #include "common/allocator_test_common.h"
22 #include "common/allocator_stl_test_common.h"
23 
24 //! \file conformance_allocators.cpp
25 //! \brief Test for [memory_allocation.cache_aligned_allocator memory_allocation.tbb_allocator memory_allocation.cache_aligned_resource] specifications
26 
27 //! Testing ISO C++ allocator requirements
28 //! \brief \ref interface \ref requirement
29 TEST_CASE("Allocator concept") {
30     // allocate/deallocate
31     TestAllocator<oneapi::tbb::cache_aligned_allocator<void>>(Concept);
32     TestAllocator<oneapi::tbb::tbb_allocator<void>>(Concept);
33 
34     // max_size case for cache_aligned allocator
35     using Allocator = oneapi::tbb::cache_aligned_allocator<int>;
36     Allocator allocator;
37     AssertSameType(allocator.max_size(), typename std::allocator_traits<Allocator>::size_type(0));
38     // Following assertion catches case where max_size() is so large that computation of
39     // number of bytes for such an allocation would overflow size_type.
40     REQUIRE_MESSAGE((allocator.max_size() * typename std::allocator_traits<Allocator>::size_type(sizeof(int)) >= allocator.max_size()), "max_size larger than reasonable");
41 
42     // operator==
43     TestAllocator<oneapi::tbb::cache_aligned_allocator<void>>(Comparison);
44     TestAllocator<oneapi::tbb::tbb_allocator<void>>(Comparison);
45 }
46 
47 #if TBB_USE_EXCEPTIONS
48 //! Testing exception guarantees
49 //! \brief \ref requirement
50 TEST_CASE("Exceptions") {
51     TestAllocator<oneapi::tbb::cache_aligned_allocator<void>>(Exceptions);
52     TestAllocator<oneapi::tbb::tbb_allocator<void>>(Exceptions);
53 }
54 #endif /* TBB_USE_EXCEPTIONS */
55 
56 //! Testing allocators thread safety (should not introduce data races)
57 //! \brief \ref requirement
58 TEST_CASE("Thread safety") {
59     TestAllocator<oneapi::tbb::cache_aligned_allocator<void>>(ThreadSafety);
60     TestAllocator<oneapi::tbb::tbb_allocator<void>>(ThreadSafety);
61     oneapi::tbb::tbb_allocator<int> tbb_alloc;
62 #if _MSC_VER && _MSC_VER <= 1900 && !__INTEL_COMPILER
63     utils::suppress_unused_warning(tbb_alloc);
64 #endif
65     tbb_alloc.allocator_type();
66 }
67 
68 //! Testing tbb_allocator to return the type of allocation library used
69 //! \brief \ref requirement
70 TEST_CASE("tbb_allocator allocator type") {
71     using Allocator = oneapi::tbb::tbb_allocator<int>; Allocator tbb_alloc;
72 #if _MSC_VER && _MSC_VER <= 1900 && !__INTEL_COMPILER
73     utils::suppress_unused_warning(tbb_alloc);
74 #endif
75     Allocator::malloc_type a_type = tbb_alloc.allocator_type();
76     bool is_available_type = (a_type == Allocator::malloc_type::scalable) || (a_type == Allocator::standard);
77     REQUIRE(is_available_type);
78 }
79 
80 #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
81 //! Testing memory resources compatibility with STL containers through the
82 //! std::pmr::polymorphic_allocator
83 //! \brief \ref interface \ref requirement
84 TEST_CASE("polymorphic_allocator test") {
85     oneapi::tbb::cache_aligned_resource aligned_resource;
86     TestAllocator<std::pmr::polymorphic_allocator<void>>(Concept, std::pmr::polymorphic_allocator<void>(&aligned_resource));
87 }
88 #endif
89 
90