151c0b2f7Stbbdev /*
2*b15aabb3Stbbdev     Copyright (c) 2005-2021 Intel Corporation
351c0b2f7Stbbdev 
451c0b2f7Stbbdev     Licensed under the Apache License, Version 2.0 (the "License");
551c0b2f7Stbbdev     you may not use this file except in compliance with the License.
651c0b2f7Stbbdev     You may obtain a copy of the License at
751c0b2f7Stbbdev 
851c0b2f7Stbbdev         http://www.apache.org/licenses/LICENSE-2.0
951c0b2f7Stbbdev 
1051c0b2f7Stbbdev     Unless required by applicable law or agreed to in writing, software
1151c0b2f7Stbbdev     distributed under the License is distributed on an "AS IS" BASIS,
1251c0b2f7Stbbdev     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1351c0b2f7Stbbdev     See the License for the specific language governing permissions and
1451c0b2f7Stbbdev     limitations under the License.
1551c0b2f7Stbbdev */
1651c0b2f7Stbbdev 
1749e08aacStbbdev #include "oneapi/tbb/cache_aligned_allocator.h"
1849e08aacStbbdev #include "oneapi/tbb/tbb_allocator.h"
1951c0b2f7Stbbdev 
2051c0b2f7Stbbdev // the real body of the test is there:
2151c0b2f7Stbbdev #include "common/allocator_test_common.h"
2251c0b2f7Stbbdev #include "common/allocator_stl_test_common.h"
2351c0b2f7Stbbdev 
2451c0b2f7Stbbdev //! \file conformance_allocators.cpp
2551c0b2f7Stbbdev //! \brief Test for [memory_allocation.cache_aligned_allocator memory_allocation.tbb_allocator memory_allocation.cache_aligned_resource] specifications
2651c0b2f7Stbbdev 
2751c0b2f7Stbbdev //! Testing ISO C++ allocator requirements
2851c0b2f7Stbbdev //! \brief \ref interface \ref requirement
2951c0b2f7Stbbdev TEST_CASE("Allocator concept") {
3051c0b2f7Stbbdev     // allocate/deallocate
3149e08aacStbbdev     TestAllocator<oneapi::tbb::cache_aligned_allocator<void>>(Concept);
3249e08aacStbbdev     TestAllocator<oneapi::tbb::tbb_allocator<void>>(Concept);
3351c0b2f7Stbbdev 
3451c0b2f7Stbbdev     // max_size case for cache_aligned allocator
3549e08aacStbbdev     using Allocator = oneapi::tbb::cache_aligned_allocator<int>;
3651c0b2f7Stbbdev     Allocator allocator;
3751c0b2f7Stbbdev     AssertSameType(allocator.max_size(), typename std::allocator_traits<Allocator>::size_type(0));
3851c0b2f7Stbbdev     // Following assertion catches case where max_size() is so large that computation of
3951c0b2f7Stbbdev     // number of bytes for such an allocation would overflow size_type.
4051c0b2f7Stbbdev     REQUIRE_MESSAGE((allocator.max_size() * typename std::allocator_traits<Allocator>::size_type(sizeof(int)) >= allocator.max_size()), "max_size larger than reasonable");
4151c0b2f7Stbbdev 
4251c0b2f7Stbbdev     // operator==
4349e08aacStbbdev     TestAllocator<oneapi::tbb::cache_aligned_allocator<void>>(Comparison);
4449e08aacStbbdev     TestAllocator<oneapi::tbb::tbb_allocator<void>>(Comparison);
4551c0b2f7Stbbdev }
4651c0b2f7Stbbdev 
4751c0b2f7Stbbdev #if TBB_USE_EXCEPTIONS
4851c0b2f7Stbbdev //! Testing exception guarantees
4951c0b2f7Stbbdev //! \brief \ref requirement
5051c0b2f7Stbbdev TEST_CASE("Exceptions") {
5149e08aacStbbdev     TestAllocator<oneapi::tbb::cache_aligned_allocator<void>>(Exceptions);
5249e08aacStbbdev     TestAllocator<oneapi::tbb::tbb_allocator<void>>(Exceptions);
5351c0b2f7Stbbdev }
5451c0b2f7Stbbdev #endif /* TBB_USE_EXCEPTIONS */
5551c0b2f7Stbbdev 
5651c0b2f7Stbbdev //! Testing allocators thread safety (should not introduce data races)
5751c0b2f7Stbbdev //! \brief \ref requirement
5851c0b2f7Stbbdev TEST_CASE("Thread safety") {
5949e08aacStbbdev     TestAllocator<oneapi::tbb::cache_aligned_allocator<void>>(ThreadSafety);
6049e08aacStbbdev     TestAllocator<oneapi::tbb::tbb_allocator<void>>(ThreadSafety);
6149e08aacStbbdev     oneapi::tbb::tbb_allocator<int> tbb_alloc;
6251c0b2f7Stbbdev #if _MSC_VER && _MSC_VER <= 1900 && !__INTEL_COMPILER
6351c0b2f7Stbbdev     utils::suppress_unused_warning(tbb_alloc);
6451c0b2f7Stbbdev #endif
6551c0b2f7Stbbdev     tbb_alloc.allocator_type();
6651c0b2f7Stbbdev }
6751c0b2f7Stbbdev 
6851c0b2f7Stbbdev //! Testing tbb_allocator to return the type of allocation library used
6951c0b2f7Stbbdev //! \brief \ref requirement
7051c0b2f7Stbbdev TEST_CASE("tbb_allocator allocator type") {
7149e08aacStbbdev     using Allocator = oneapi::tbb::tbb_allocator<int>; Allocator tbb_alloc;
7251c0b2f7Stbbdev #if _MSC_VER && _MSC_VER <= 1900 && !__INTEL_COMPILER
7351c0b2f7Stbbdev     utils::suppress_unused_warning(tbb_alloc);
7451c0b2f7Stbbdev #endif
7551c0b2f7Stbbdev     Allocator::malloc_type a_type = tbb_alloc.allocator_type();
7651c0b2f7Stbbdev     bool is_available_type = (a_type == Allocator::malloc_type::scalable) || (a_type == Allocator::standard);
7751c0b2f7Stbbdev     REQUIRE(is_available_type);
7851c0b2f7Stbbdev }
7951c0b2f7Stbbdev 
8051c0b2f7Stbbdev #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
8151c0b2f7Stbbdev //! Testing memory resources compatibility with STL containers through the
8251c0b2f7Stbbdev //! std::pmr::polymorphic_allocator
8351c0b2f7Stbbdev //! \brief \ref interface \ref requirement
8451c0b2f7Stbbdev TEST_CASE("polymorphic_allocator test") {
8549e08aacStbbdev     oneapi::tbb::cache_aligned_resource aligned_resource;
8651c0b2f7Stbbdev     TestAllocator<std::pmr::polymorphic_allocator<void>>(Concept, std::pmr::polymorphic_allocator<void>(&aligned_resource));
8751c0b2f7Stbbdev }
8851c0b2f7Stbbdev #endif
8951c0b2f7Stbbdev 
90