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