1 /* Copyright (c) 2013, 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 St, Fifth Floor, Boston, MA  02110-1301  USA */
22 
23 /*
24   Bug#68477    Suboptimal code in skip_trailing_space()
25   Bug#16395778 SUBOPTIMAL CODE IN SKIP_TRAILING_SPACE()
26 
27   Below we test some alternative implementations for skip_trailing_space.
28   In order to do benchmarking, configure in optimized mode, and
29   generate a separate executable for this file:
30     cmake -DMERGE_UNITTESTS=0
31   You may want to tweak some constants below:
32    - experiment with num_iterations
33    - experiment with inserting something in front of the whitespace
34    - experiment with different test_values
35   run 'strings-t --disable-tap-output' to see timing reports for your platform.
36  */
37 
38 // First include (the generated) my_config.h, to get correct platform defines.
39 #include "my_config.h"
40 #include <gtest/gtest.h>
41 #include <string>
42 
43 #include "skip_trailing.h"
44 
45 namespace skip_trailing_space_unittest {
46 
47 #if defined(GTEST_HAS_PARAM_TEST)
48 
49 #if !defined(NDEBUG)
50 // There is no point in benchmarking anything in debug mode.
51 const size_t num_iterations= 1ULL;
52 #else
53 // Set this so that each test case takes a few seconds.
54 // And set it back to a small value before pushing!!
55 // const size_t num_iterations= 200000000ULL;
56 const size_t num_iterations= 2ULL;
57 #endif
58 
59 class SkipTrailingSpaceTest : public ::testing::TestWithParam<int>
60 {
61 protected:
SetUp()62   virtual void SetUp()
63   {
64     int num_spaces= GetParam();
65     // Insert something else (or nothing) here,
66     //   to see effects of alignment of data:
67     m_string.append("1");
68     for (int ix= 0 ; ix < num_spaces; ++ix)
69       m_string.append(" ");
70     m_length= m_string.length();
71   }
72   size_t m_length;
73   std::string m_string;
74 };
75 
76 int test_values[]= {0, 24, 100, 150};
77 
78 INSTANTIATE_TEST_CASE_P(Skip, SkipTrailingSpaceTest,
79                         ::testing::ValuesIn(test_values));
80 
TEST_P(SkipTrailingSpaceTest,Unaligned)81 TEST_P(SkipTrailingSpaceTest, Unaligned)
82 {
83   for (size_t ix= 0; ix < num_iterations; ++ix)
84     skip_trailing_unalgn(reinterpret_cast<const uchar*>(m_string.c_str()),
85                          m_length);
86 }
87 
TEST_P(SkipTrailingSpaceTest,Original)88 TEST_P(SkipTrailingSpaceTest, Original)
89 {
90   for (size_t ix= 0; ix < num_iterations; ++ix)
91     skip_trailing_orig(reinterpret_cast<const uchar*>(m_string.c_str()),
92                        m_length);
93 }
94 
TEST_P(SkipTrailingSpaceTest,FourByte)95 TEST_P(SkipTrailingSpaceTest, FourByte)
96 {
97   for (size_t ix= 0; ix < num_iterations; ++ix)
98     skip_trailing_4byte(reinterpret_cast<const uchar*>(m_string.c_str()),
99                         m_length);
100 }
101 
TEST_P(SkipTrailingSpaceTest,EightByte)102 TEST_P(SkipTrailingSpaceTest, EightByte)
103 {
104   for (size_t ix= 0; ix < num_iterations; ++ix)
105     skip_trailing_8byte(reinterpret_cast<const uchar*>(m_string.c_str()),
106                         m_length);
107 }
108 
109 #endif  // GTEST_HAS_PARAM_TEST
110 
111 }
112