| 1 | /** \file |
| 2 | * \brief Tests for ogdf::PoolMemoryAllocator, ogdf::MallocMemoryAllocator and the respective macros. |
| 3 | * |
| 4 | * \author Tilo Wiedera |
| 5 | * |
| 6 | * \par License: |
| 7 | * This file is part of the Open Graph Drawing Framework (OGDF). |
| 8 | * |
| 9 | * \par |
| 10 | * Copyright (C)<br> |
| 11 | * See README.md in the OGDF root directory for details. |
| 12 | * |
| 13 | * \par |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * Version 2 or 3 as published by the Free Software Foundation; |
| 17 | * see the file LICENSE.txt included in the packaging of this file |
| 18 | * for details. |
| 19 | * |
| 20 | * \par |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * \par |
| 27 | * You should have received a copy of the GNU General Public |
| 28 | * License along with this program; if not, see |
| 29 | * http://www.gnu.org/copyleft/gpl.html |
| 30 | */ |
| 31 | |
| 32 | #include <cmath> |
| 33 | #include <iostream> |
| 34 | #include <ogdf/basic/memory.h> |
| 35 | #include <ogdf/basic/System.h> |
| 36 | #include <testing.h> |
| 37 | |
| 38 | template<size_t size> |
| 39 | class OGDFObject { |
| 40 | char x[size]; |
| 41 | |
| 42 | OGDF_NEW_DELETE |
| 43 | }; |
| 44 | |
| 45 | template<size_t size> |
| 46 | class MallocObject { |
| 47 | char x[size]; |
| 48 | |
| 49 | OGDF_MALLOC_NEW_DELETE |
| 50 | }; |
| 51 | |
| 52 | template<template<size_t> class ObjectOfSize> |
| 53 | void describeMemoryManager(const std::string &name) { |
| 54 | describe(name + " allocator" , [] { |
| 55 | after_each([] { |
| 56 | size_t managerAllocated = System::memoryAllocatedByMemoryManager(); |
| 57 | size_t mallocAllocated = System::memoryAllocatedByMalloc(); |
| 58 | AssertThat(managerAllocated, IsLessThanOrEqualTo(mallocAllocated)); |
| 59 | }); |
| 60 | |
| 61 | it("allocates objects that need exactly 1 byte" , [] { |
| 62 | delete new ObjectOfSize<1>; |
| 63 | }); |
| 64 | |
| 65 | it("does not deallocate nullptr" , [] { |
| 66 | ObjectOfSize<1> *ptr = nullptr; |
| 67 | delete ptr; |
| 68 | }); |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | go_bandit([] { |
| 73 | describeMemoryManager<OGDFObject>("OGDF" ); |
| 74 | describeMemoryManager<MallocObject>("Malloc" ); |
| 75 | }); |
| 76 | |