-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Slightly simplify max_size and add new tests for vector #119990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// <vector> | ||
// vector<bool> | ||
|
||
// size_type max_size() const; | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#include <climits> | ||
#include <cstdint> | ||
#include <limits> | ||
#include <memory> | ||
#include <type_traits> | ||
#include <vector> | ||
|
||
#include "min_allocator.h" | ||
#include "sized_allocator.h" | ||
#include "test_allocator.h" | ||
#include "test_macros.h" | ||
|
||
#if TEST_STD_VER >= 11 | ||
|
||
template <typename Alloc> | ||
TEST_CONSTEXPR_CXX20 void test(const std::vector<bool, Alloc>& v) { | ||
using Vector = std::vector<bool, Alloc>; | ||
using size_type = typename Vector::size_type; | ||
using difference_type = typename Vector::difference_type; | ||
const size_type max_dist = static_cast<size_type>(std::numeric_limits<difference_type>::max()); | ||
assert(v.max_size() <= max_dist); | ||
|
||
// The following check is specific to libc++ implementation details and is not portable to libstdc++ | ||
// and MSVC STL, as they use different types for the underlying word storage. | ||
# if defined(_LIBCPP_VERSION) | ||
using storage_type = typename Vector::__storage_type; | ||
using storage_alloc = typename std::allocator_traits<Alloc>::template rebind_alloc<storage_type>; | ||
using storage_traits = typename std::allocator_traits<Alloc>::template rebind_traits<storage_type>; | ||
const size_type max_alloc = storage_traits::max_size(storage_alloc(v.get_allocator())); | ||
std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT; | ||
const size_type max_size = max_dist / bits_per_word < max_alloc ? max_dist : max_alloc * bits_per_word; | ||
assert(v.max_size() / bits_per_word <= max_alloc); // max_alloc * bits_per_word may overflow | ||
assert(v.max_size() == max_size); | ||
# endif // defined(_LIBCPP_VERSION) | ||
} | ||
|
||
#endif // TEST_STD_VER >= 11 | ||
|
||
TEST_CONSTEXPR_CXX20 bool tests() { | ||
// The following check is specific to libc++ implementation details and is not portable to libstdc++ | ||
// and MSVC STL, as they use different types for the underlying word storage. | ||
#if defined(_LIBCPP_VERSION) | ||
// Test cases where v.max_size() is determined by allocator::max_size() | ||
{ | ||
using Alloc = limited_allocator<bool, 10>; | ||
using Vector = std::vector<bool, Alloc>; | ||
using storage_type = Vector::__storage_type; | ||
Vector v; | ||
std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT; | ||
assert(v.max_size() == 10 * bits_per_word); | ||
} | ||
#endif // defined(_LIBCPP_VERSION) | ||
|
||
#if TEST_STD_VER >= 11 | ||
|
||
// Test with various allocators and different `size_type`s | ||
{ | ||
test(std::vector<bool>()); | ||
test(std::vector<bool, std::allocator<int> >()); | ||
test(std::vector<bool, min_allocator<bool> >()); | ||
test(std::vector<bool, test_allocator<bool> >(test_allocator<bool>(1))); | ||
test(std::vector<bool, other_allocator<bool> >(other_allocator<bool>(5))); | ||
test(std::vector<bool, sized_allocator<bool, std::uint8_t, std::int8_t> >()); | ||
test(std::vector<bool, sized_allocator<bool, std::uint16_t, std::int16_t> >()); | ||
test(std::vector<bool, sized_allocator<bool, std::uint32_t, std::int32_t> >()); | ||
test(std::vector<bool, sized_allocator<bool, std::uint64_t, std::int64_t> >()); | ||
test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1)> >()); | ||
} | ||
|
||
// Test cases to identify incorrect implementations that unconditionally compute an internal-to-external | ||
// capacity in a way that can overflow, leading to incorrect results. | ||
{ | ||
test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 61> >()); | ||
test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 63> >()); | ||
} | ||
|
||
#endif // TEST_STD_VER >= 11 | ||
|
||
return true; | ||
} | ||
|
||
int main(int, char**) { | ||
tests(); | ||
|
||
#if TEST_STD_VER >= 20 | ||
static_assert(tests()); | ||
#endif | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This alias only exists in libc++, and our test suite must be portable. I suspect this means you will need to significantly weaken the tests for non-libc++ implementations, and that's okay. But the tests should still work on non-libc++ implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made part of the test libc++ only because libstdc++'s implementation is slightly different from libc++. The return values in the two implementations are, in math form (assuming no overflow)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another reason why this test is not fully portable to other standard implementations is that libc++, libstdc++, and MSVC-STL all use different word types for
vector<bool>
underlying storage:unsigned int
(fixed)unsigned long
(fixed)alloc_traits::size_type
(configurable through custom allocator)