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