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// UNSUPPORTED: no-exceptions
10// <memory>
11
12// allocator:
13// constexpr T* allocate(size_t n);
14
15#include <memory>
16#include <cassert>
17#include <new>
18
19#include "test_macros.h"
20
21template <typename T>
22void test_max(std::size_t count)
23{
24 std::allocator<T> a;
25 try {
26 TEST_IGNORE_NODISCARD a.allocate(count);
27 assert(false);
28 } catch (const std::bad_array_new_length &) {
29 }
30}
31
32template <typename T>
33void test()
34{
35 // Bug 26812 -- allocating too large
36 typedef std::allocator<T> A;
37 typedef std::allocator_traits<A> AT;
38 A a;
39 test_max<T> (AT::max_size(a) + 1); // just barely too large
40 test_max<T> (AT::max_size(a) * 2); // significantly too large
41 test_max<T> (((std::size_t) -1) / sizeof(T) + 1); // multiply will overflow
42 test_max<T> ((std::size_t) -1); // way too large
43}
44
45int main(int, char**)
46{
47 test<double>();
48 LIBCPP_ONLY(test<const double>());
49
50 return 0;
51}
52

source code of libcxx/test/std/utilities/memory/default.allocator/allocator.members/allocate.size.pass.cpp