| 1 | /* Copyright (c) 2020, 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 | |
| 25 | #include "sql/memory/unique_ptr.h" |
| 26 | |
| 27 | #include <gmock/gmock.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | namespace memory { |
| 31 | namespace unittests { |
| 32 | |
| 33 | class Unique_ptr_test : public ::testing::Test { |
| 34 | protected: |
| 35 | Unique_ptr_test() = default; |
| 36 | virtual void SetUp() {} |
| 37 | virtual void TearDown() {} |
| 38 | }; |
| 39 | |
| 40 | TEST_F(Unique_ptr_test, Array_template_test) { |
| 41 | auto ptr = memory::make_unique<char[]>(10); |
| 42 | |
| 43 | ptr[0] = '0'; |
| 44 | ptr[1] = '1'; |
| 45 | ptr[2] = '2'; |
| 46 | ptr[3] = '3'; |
| 47 | ptr[4] = '4'; |
| 48 | ptr[5] = '5'; |
| 49 | ptr[6] = '6'; |
| 50 | ptr[7] = '7'; |
| 51 | ptr[8] = '8'; |
| 52 | ptr[9] = '\0'; |
| 53 | |
| 54 | EXPECT_EQ(ptr[2], '2'); |
| 55 | EXPECT_EQ(*ptr, '0'); |
| 56 | EXPECT_EQ(!ptr, false); |
| 57 | |
| 58 | EXPECT_EQ(*ptr.get(), '0'); |
| 59 | EXPECT_EQ(ptr.size(), 10); |
| 60 | |
| 61 | ptr.reserve(20); |
| 62 | EXPECT_EQ(ptr.size(), 20); |
| 63 | ptr[9] = '9'; |
| 64 | ptr[10] = '0'; |
| 65 | ptr[11] = '1'; |
| 66 | ptr[12] = '2'; |
| 67 | ptr[13] = '3'; |
| 68 | ptr[14] = '4'; |
| 69 | ptr[15] = '5'; |
| 70 | ptr[16] = '6'; |
| 71 | ptr[17] = '7'; |
| 72 | ptr[18] = '8'; |
| 73 | ptr[19] = '\0'; |
| 74 | |
| 75 | EXPECT_EQ(ptr[12], '2'); |
| 76 | EXPECT_EQ(*ptr.get(), '0'); |
| 77 | EXPECT_EQ(ptr.size(), 20); |
| 78 | |
| 79 | char *underlying = ptr.release(); |
| 80 | EXPECT_EQ(!ptr, true); |
| 81 | delete[] underlying; |
| 82 | |
| 83 | auto ptr2 = memory::make_unique<char[]>(10); |
| 84 | bool equal = (ptr == ptr2); |
| 85 | EXPECT_EQ(equal, false); |
| 86 | } |
| 87 | |
| 88 | TEST_F(Unique_ptr_test, Class_template_test) { |
| 89 | auto ptr = memory::make_unique<std::string>("012345678"); |
| 90 | |
| 91 | EXPECT_EQ(ptr->length(), 9); |
| 92 | EXPECT_EQ(*ptr, "012345678"); |
| 93 | EXPECT_EQ(!ptr, false); |
| 94 | |
| 95 | EXPECT_EQ(*ptr.get(), "012345678"); |
| 96 | EXPECT_EQ(ptr.size(), sizeof(std::string)); |
| 97 | |
| 98 | std::string *underlying = ptr.release(); |
| 99 | EXPECT_EQ(!ptr, true); |
| 100 | delete underlying; |
| 101 | |
| 102 | auto ptr2 = memory::make_unique<std::string>("012345678"); |
| 103 | bool equal = (ptr == ptr2); |
| 104 | EXPECT_EQ(equal, false); |
| 105 | } |
| 106 | |
| 107 | } // namespace unittests |
| 108 | } // namespace memory |