| 1 | /* Copyright (c) 2016, 2026, 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 designed to work 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 either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <algorithm> |
| 26 | #include <map> |
| 27 | #include <unordered_map> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "sql/dd/string_type.h" |
| 31 | |
| 32 | /* |
| 33 | Tests of dd::String_type |
| 34 | */ |
| 35 | |
| 36 | namespace dd_string_type_unit_test { |
| 37 | |
| 38 | typedef dd::String_type s_t; |
| 39 | |
| 40 | class DDStringTypeTest : public ::testing::Test {}; |
| 41 | |
| 42 | // Basic string usage |
| 43 | TEST(DDStringTypeTest, BasicTest) { |
| 44 | s_t x("foobar"); |
| 45 | x += "_tag"; |
| 46 | EXPECT_EQ(10U, x.size()); |
| 47 | } |
| 48 | |
| 49 | // Create string using stringstream |
| 50 | TEST(DDStringTypeTest, StreamTest) { |
| 51 | typedef dd::Stringstream_type ss_t; |
| 52 | |
| 53 | ss_t ss; |
| 54 | double d = 42.65; |
| 55 | ss << "The value of d is " << d << " this is an integer " << 42 << std::endl; |
| 56 | |
| 57 | s_t x("Stream result: "); |
| 58 | x += ss.str(); |
| 59 | EXPECT_EQ(61U, x.size()); |
| 60 | } |
| 61 | |
| 62 | // Strings in vector |
| 63 | TEST(DDStringTypeTest, VectorTest) { |
| 64 | typedef std::vector<s_t> sv_t; |
| 65 | |
| 66 | sv_t words = {"Mary", "had", "a", "little", "Lamb"}; |
| 67 | std::sort(words.begin(), words.end()); |
| 68 | ASSERT_TRUE(std::is_sorted(words.begin(), words.end())); |
| 69 | } |
| 70 | |
| 71 | // Strings as keys and values in maps |
| 72 | TEST(DDStringTypeTest, MapTest) { |
| 73 | typedef std::map<s_t, s_t> sm_t; |
| 74 | |
| 75 | sm_t dict = {{"large", "great"}, {"small", "little"}, {"medium", "average"}}; |
| 76 | |
| 77 | EXPECT_EQ(3U, dict.size()); |
| 78 | EXPECT_EQ("great", dict["large"]); |
| 79 | EXPECT_EQ("little", dict["small"]); |
| 80 | EXPECT_EQ("average", dict["medium"]); |
| 81 | } |
| 82 | |
| 83 | // Strings as keys and values in unordered (hash-based) maps |
| 84 | TEST(DDStringTypeTest, UnorderedMapTest) { |
| 85 | typedef std::unordered_map<s_t, s_t> sm_t; |
| 86 | |
| 87 | sm_t dict = {{"large", "great"}, {"small", "little"}, {"medium", "average"}}; |
| 88 | |
| 89 | EXPECT_EQ(3U, dict.size()); |
| 90 | EXPECT_EQ("great", dict["large"]); |
| 91 | EXPECT_EQ("little", dict["small"]); |
| 92 | EXPECT_EQ("average", dict["medium"]); |
| 93 | } |
| 94 | |
| 95 | struct Tracking_alloc { |
| 96 | static int allocations; |
| 97 | static int bytes_allocated; |
| 98 | |
| 99 | static int frees; |
| 100 | |
| 101 | void *operator()(size_t s) const { |
| 102 | ++allocations; |
| 103 | bytes_allocated += s; |
| 104 | return operator new(s); |
| 105 | } |
| 106 | |
| 107 | void operator()(void *p, size_t) const { |
| 108 | ++frees; |
| 109 | operator delete(p); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | int Tracking_alloc::allocations = 0; |
| 114 | int Tracking_alloc::bytes_allocated = 0; |
| 115 | int Tracking_alloc::frees = 0; |
| 116 | |
| 117 | template <class T> |
| 118 | using Tracking_allocator = |
| 119 | Stateless_allocator<T, Tracking_alloc, Tracking_alloc>; |
| 120 | |
| 121 | typedef dd::Char_string_template<Tracking_allocator<char>> Tracking_string; |
| 122 | typedef dd::Char_stringstream_template<Tracking_allocator<char>> |
| 123 | Tracking_stringstream; |
| 124 | |
| 125 | class TrackingStringTypeTest : public ::testing::Test { |
| 126 | protected: |
| 127 | void SetUp() override { |
| 128 | Tracking_alloc::allocations = 0; |
| 129 | Tracking_alloc::bytes_allocated = 0; |
| 130 | Tracking_alloc::frees = 0; |
| 131 | } |
| 132 | void TearDown() override { |
| 133 | std::cout << "allocations: " << Tracking_alloc::allocations |
| 134 | << ", frees: " << Tracking_alloc::frees |
| 135 | << ", bytes allocated: " << Tracking_alloc::bytes_allocated |
| 136 | << std::endl; |
| 137 | EXPECT_EQ(Tracking_alloc::allocations, Tracking_alloc::frees); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | TEST_F(TrackingStringTypeTest, TrackingString) { Tracking_string x = "foobar"; } |
| 142 | |
| 143 | TEST_F(TrackingStringTypeTest, TrackingStringstream) { |
| 144 | Tracking_stringstream x; |
| 145 | x << "This is a number " << 42 << " followed by a double " << 36.9 |
| 146 | << std::endl; |
| 147 | } |
| 148 | |
| 149 | TEST_F(TrackingStringTypeTest, TrackingStringstreamGetString) { |
| 150 | Tracking_stringstream x; |
| 151 | x << "This is a number " << 42 << " followed by a double " << 36.9 |
| 152 | << std::endl; |
| 153 | Tracking_string y = x.str(); |
| 154 | EXPECT_EQ(46u, y.size()); |
| 155 | } |
| 156 | |
| 157 | } // namespace dd_string_type_unit_test |