Abort on OOM by default in FX_Alloc().
[pdfium.git] / core / src / fxcrt / fx_basic_memmgr_unittest.cpp
1 // Copyright 2015 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <limits>
6
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "../../include/fxcrt/fx_memory.h"
9
10 namespace {
11
12 const size_t kMaxByteAlloc = std::numeric_limits<size_t>::max();
13 const size_t kMaxIntAlloc = kMaxByteAlloc / sizeof(int);
14 const size_t kOverflowIntAlloc = kMaxIntAlloc + 100;
15
16 }  // namespace
17
18 TEST(fxcrt, FX_AllocOOM) {
19     EXPECT_DEATH_IF_SUPPORTED(FX_Alloc(int, kMaxIntAlloc), "");
20     EXPECT_DEATH_IF_SUPPORTED(FX_Alloc(int, kOverflowIntAlloc), "");
21
22     int* ptr = FX_Alloc(int, 1);
23     EXPECT_TRUE(ptr);
24     EXPECT_DEATH_IF_SUPPORTED(FX_Realloc(int, ptr, kMaxIntAlloc), "");
25     EXPECT_DEATH_IF_SUPPORTED(FX_Realloc(int, ptr, kOverflowIntAlloc), "");
26     FX_Free(ptr);
27 }
28
29 TEST(fxcrt, FX_TryAllocOOM) {
30     EXPECT_FALSE(FX_TryAlloc(int, kMaxIntAlloc));
31     EXPECT_FALSE(FX_TryAlloc(int, kOverflowIntAlloc));
32
33     int* ptr = FX_Alloc(int, 1);
34     EXPECT_TRUE(ptr);
35     EXPECT_FALSE(FX_TryRealloc(int, ptr, kMaxIntAlloc));
36     EXPECT_FALSE(FX_TryRealloc(int, ptr, kOverflowIntAlloc));
37     FX_Free(ptr);
38 }
39
40 TEST(fxcrt, FXMEM_DefaultOOM) {
41     EXPECT_FALSE(FXMEM_DefaultAlloc(kMaxByteAlloc, 0));
42
43     void* ptr = FXMEM_DefaultAlloc(1, 0);
44     EXPECT_TRUE(ptr);
45     EXPECT_FALSE(FXMEM_DefaultRealloc(ptr, kMaxByteAlloc, 0));
46     FXMEM_DefaultFree(ptr, 0);
47 }