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

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