| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <deque> |
| 10 | |
| 11 | // void resize(size_type n); |
| 12 | |
| 13 | #include "asan_testing.h" |
| 14 | #include <deque> |
| 15 | #include <algorithm> |
| 16 | #include <iterator> |
| 17 | #include <cassert> |
| 18 | #include <cstddef> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | #include "min_allocator.h" |
| 22 | |
| 23 | template <class C> |
| 24 | C make(int size, int start = 0) { |
| 25 | const int b = 4096 / sizeof(int); |
| 26 | int init = 0; |
| 27 | if (start > 0) { |
| 28 | init = (start + 1) / b + ((start + 1) % b != 0); |
| 29 | init *= b; |
| 30 | --init; |
| 31 | } |
| 32 | C c(init, 0); |
| 33 | for (int i = 0; i < init - start; ++i) |
| 34 | c.pop_back(); |
| 35 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c)); |
| 36 | for (int i = 0; i < size; ++i) |
| 37 | c.push_back(i); |
| 38 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c)); |
| 39 | for (int i = 0; i < start; ++i) |
| 40 | c.pop_front(); |
| 41 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c)); |
| 42 | return c; |
| 43 | } |
| 44 | |
| 45 | template <class C> |
| 46 | void test(C& c1, int size) { |
| 47 | typedef typename C::const_iterator CI; |
| 48 | typename C::size_type c1_osize = c1.size(); |
| 49 | c1.resize(size); |
| 50 | assert(c1.size() == static_cast<std::size_t>(size)); |
| 51 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 52 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1)); |
| 53 | CI i = c1.begin(); |
| 54 | for (int j = 0; static_cast<std::size_t>(j) < std::min(c1_osize, c1.size()); ++j, ++i) |
| 55 | assert(*i == j); |
| 56 | for (std::size_t j = c1_osize; j < c1.size(); ++j, ++i) |
| 57 | assert(*i == 0); |
| 58 | } |
| 59 | |
| 60 | template <class C> |
| 61 | void testN(int start, int N, int M) { |
| 62 | C c1 = make<C>(N, start); |
| 63 | test(c1, M); |
| 64 | } |
| 65 | |
| 66 | int main(int, char**) { |
| 67 | { |
| 68 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
| 69 | const int N = sizeof(rng) / sizeof(rng[0]); |
| 70 | for (int i = 0; i < N; ++i) |
| 71 | for (int j = 0; j < N; ++j) |
| 72 | for (int k = 0; k < N; ++k) |
| 73 | testN<std::deque<int> >(start: rng[i], N: rng[j], M: rng[k]); |
| 74 | } |
| 75 | #if TEST_STD_VER >= 11 |
| 76 | { |
| 77 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
| 78 | const int N = sizeof(rng) / sizeof(rng[0]); |
| 79 | for (int i = 0; i < N; ++i) |
| 80 | for (int j = 0; j < N; ++j) |
| 81 | for (int k = 0; k < N; ++k) |
| 82 | testN<std::deque<int, min_allocator<int>>>(rng[i], rng[j], rng[k]); |
| 83 | } |
| 84 | { |
| 85 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
| 86 | const int N = sizeof(rng) / sizeof(rng[0]); |
| 87 | for (int i = 0; i < N; ++i) |
| 88 | for (int j = 0; j < N; ++j) |
| 89 | for (int k = 0; k < N; ++k) |
| 90 | testN<std::deque<int, safe_allocator<int>>>(rng[i], rng[j], rng[k]); |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |