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