|
| 1 | +/* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
| 2 | +
|
| 3 | + This program is free software; you can redistribute it and/or modify |
| 4 | + it under the terms of the GNU General Public License as published by |
| 5 | + the Free Software Foundation; version 2 of the License. |
| 6 | +
|
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with this program; if not, write to the Free Software |
| 14 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ |
| 15 | + |
| 16 | +// First include (the generated) my_config.h, to get correct platform defines. |
| 17 | +#include "my_config.h" |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include <stdlib.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +namespace calloc_unittest { |
| 24 | + |
| 25 | +/* |
| 26 | + Set num_iterations to a reasonable value (e.g. 200000), build release |
| 27 | + and run with 'calloc-t --disable-tap-output' to see the time taken. |
| 28 | +*/ |
| 29 | +#if !defined(DBUG_OFF) |
| 30 | +// There is no point in benchmarking anything in debug mode. |
| 31 | +static int num_iterations= 2; |
| 32 | +#else |
| 33 | +// Set this so that each test case takes a few seconds. |
| 34 | +// And set it back to a small value before pushing!! |
| 35 | +static int num_iterations= 2000; |
| 36 | +#endif |
| 37 | + |
| 38 | + |
| 39 | +class CallocTest : public ::testing::Test |
| 40 | +{ |
| 41 | +protected: |
| 42 | + CallocTest() { }; |
| 43 | + |
| 44 | + void malloc_test(int count); |
| 45 | + void calloc_test(int count); |
| 46 | +}; |
| 47 | + |
| 48 | + |
| 49 | +void CallocTest::malloc_test(int count) |
| 50 | +{ |
| 51 | + for (int i= 1; i <= count; i++) |
| 52 | + { |
| 53 | + size_t size= i * 10; |
| 54 | + void *rawmem1= malloc(size); |
| 55 | + memset(rawmem1, 0, size); |
| 56 | + void *rawmem2= malloc(size); |
| 57 | + memset(rawmem2, 0, size); |
| 58 | + // We need to prevent the optimizer from removing the whole loop. |
| 59 | + EXPECT_FALSE(memcmp(rawmem1, rawmem2, 1)); |
| 60 | + free(rawmem1); |
| 61 | + free(rawmem2); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +void CallocTest::calloc_test(int count) |
| 67 | +{ |
| 68 | + for (int i= 1; i <= count; i++) |
| 69 | + { |
| 70 | + size_t size= i * 10; |
| 71 | + void *rawmem1= calloc(size, 1); |
| 72 | + void *rawmem2= calloc(size, 1); |
| 73 | + // We need to prevent the optimizer from removing the whole loop. |
| 74 | + EXPECT_FALSE(memcmp(rawmem1, rawmem2, 1)); |
| 75 | + free(rawmem1); |
| 76 | + free(rawmem2); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +TEST_F(CallocTest, WarmupTest) |
| 82 | +{ |
| 83 | + calloc_test(num_iterations); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(CallocTest, MallocTest) |
| 87 | +{ |
| 88 | + malloc_test(num_iterations); |
| 89 | +} |
| 90 | + |
| 91 | +TEST_F(CallocTest, CallocTest) |
| 92 | +{ |
| 93 | + calloc_test(num_iterations); |
| 94 | +} |
| 95 | + |
| 96 | +} |
0 commit comments