This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathtest_new.cpp
More file actions
120 lines (91 loc) · 2.57 KB
/
test_new.cpp
File metadata and controls
120 lines (91 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/*
LiveCode does not use exceptions and the codebase assumes that `new`
returns NULL if the allocation fails. This is not the default
behaviour of modern C++ compilers so we need to check that the
compiler has the correct flags to get the desired behaviour.
We need to check that:
- If an allocation fails then new returns a null pointer and
doesn't (try) to throw an exception.
- If an allocation fails then the constructor is not called.
*/
#include "gtest/gtest.h"
#include "prefix.h"
enum {
kSize =
// MSVC borks if a declared array is > 0x7fffffff bytes long, so make sure
// VeryBig doesn't exceed this.
#if defined(__32_BIT__) || defined(__WINDOWS__)
1 << 15
#else
1 << 20
#endif
};
class Big {
public:
Big() {
// Check that the constructor is not called after the
// allocation failed. If the compiler flags are not set
// correctly it may be assuming that `new` never returns
// NULL and so calls the constructor without checking it's
// not NULL. Writing to `m_big[0]` will cause a seg fault
// if `this` is NULL.
m_big[0] = 0;
}
char m_big[kSize];
};
struct VeryBig {
Big m_bigger[kSize];
};
#if defined(__EMSCRIPTEN__) || defined(__MAC__)
TEST(new, DISABLED_new)
#else
TEST(new, new)
#endif
//
// Checks that new returns NULL on error.
//
{
VeryBig* pointers[1000];
int i = 0;
VeryBig* p = new (nothrow) VeryBig;
for (; p != NULL && i < 1000; i++) {
pointers[i] = p;
p = new (nothrow) VeryBig;
}
ASSERT_NE(1000, i) << "All alocations succeed!";
for (i--; i >= 0; i--) {
delete pointers[i];
}
}
#if defined(__EMSCRIPTEN__) || defined(__MAC__)
TEST(new, DISABLED_array)
#else
TEST(new, array)
#endif
//
// Checks that new[] returns NULL on error.
//
{
VeryBig* pointers[1000];
int i = 0;
VeryBig* p = new (nothrow) VeryBig[1];
for (; p != NULL && i < 1000; i++) {
pointers[i] = p;
p = new (nothrow) VeryBig[1];
}
ASSERT_NE(1000, i) << "All alocations succeed!";
for (i--; i >= 0; i--) {
delete [] pointers[i];
}
}