| 1 | /* |
| 2 | Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License, version 2.0, |
| 6 | as published by the Free Software Foundation. |
| 7 | |
| 8 | This program is designed to work with certain software (including |
| 9 | but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | as designated in a particular file or component or in included license |
| 11 | documentation. The authors of MySQL hereby grant you an additional |
| 12 | permission to link the program and your derivative works with the |
| 13 | separately licensed software that they have either included with |
| 14 | the program or referenced in the documentation. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License, version 2.0, for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 24 | |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include "sql/key.h" // KEY |
| 29 | #include "unittest/gunit/fake_key.h" // Fake_KEY |
| 30 | |
| 31 | namespace recperkey_unittest { |
| 32 | |
| 33 | /** |
| 34 | Test the API for setting and getting records per key values. |
| 35 | */ |
| 36 | |
| 37 | TEST(RecPerKeyTest, RecPerKeyAPI) { |
| 38 | const uint key_parts = 3; |
| 39 | |
| 40 | Fake_KEY key(key_parts, false); |
| 41 | |
| 42 | /* |
| 43 | Test that the rec_per_key values are default/unknown. |
| 44 | */ |
| 45 | for (uint kp = 0; kp < key_parts; kp++) { |
| 46 | EXPECT_FALSE(key.has_records_per_key(kp)); |
| 47 | EXPECT_EQ(key.records_per_key(kp), REC_PER_KEY_UNKNOWN); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | Test setting the values using the API |
| 52 | */ |
| 53 | |
| 54 | // Give one of the key parts a value |
| 55 | key.set_records_per_key(1, 2.0); |
| 56 | |
| 57 | // Validate that only one of the key parts has gotten a value |
| 58 | EXPECT_FALSE(key.has_records_per_key(0)); |
| 59 | EXPECT_TRUE(key.has_records_per_key(1)); |
| 60 | EXPECT_FALSE(key.has_records_per_key(2)); |
| 61 | |
| 62 | EXPECT_EQ(key.records_per_key(0), REC_PER_KEY_UNKNOWN); |
| 63 | EXPECT_EQ(key.records_per_key(1), 2.0); |
| 64 | EXPECT_EQ(key.records_per_key(2), REC_PER_KEY_UNKNOWN); |
| 65 | |
| 66 | // Reset the rec_per_key value to default/unkown |
| 67 | key.set_records_per_key(1, REC_PER_KEY_UNKNOWN); |
| 68 | EXPECT_FALSE(key.has_records_per_key(1)); |
| 69 | EXPECT_EQ(key.records_per_key(1), REC_PER_KEY_UNKNOWN); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | Test the old integer based rec_per_key implementation used |
| 74 | together with the records per key API. |
| 75 | */ |
| 76 | |
| 77 | TEST(RecPerKeyTest, RecPerKey) { |
| 78 | const uint key_parts = 3; |
| 79 | |
| 80 | Fake_KEY key(key_parts, false); |
| 81 | |
| 82 | // Set values directly in the integer based rec_per_key array |
| 83 | key.rec_per_key[0] = 0; // default/unknown value |
| 84 | key.rec_per_key[1] = 1; |
| 85 | key.rec_per_key[2] = 2; |
| 86 | |
| 87 | // Validate that this produces correct values using the API functions |
| 88 | EXPECT_FALSE(key.has_records_per_key(0)); |
| 89 | EXPECT_EQ(key.records_per_key(0), REC_PER_KEY_UNKNOWN); |
| 90 | EXPECT_TRUE(key.has_records_per_key(1)); |
| 91 | EXPECT_EQ(key.records_per_key(1), 1.0); |
| 92 | EXPECT_TRUE(key.has_records_per_key(2)); |
| 93 | EXPECT_EQ(key.records_per_key(2), 2.0); |
| 94 | |
| 95 | // Reset the values |
| 96 | key.rec_per_key[1] = 0; |
| 97 | key.rec_per_key[2] = 0; |
| 98 | EXPECT_FALSE(key.has_records_per_key(0)); |
| 99 | EXPECT_EQ(key.records_per_key(0), REC_PER_KEY_UNKNOWN); |
| 100 | EXPECT_FALSE(key.has_records_per_key(1)); |
| 101 | EXPECT_EQ(key.records_per_key(1), REC_PER_KEY_UNKNOWN); |
| 102 | EXPECT_FALSE(key.has_records_per_key(2)); |
| 103 | EXPECT_EQ(key.records_per_key(2), REC_PER_KEY_UNKNOWN); |
| 104 | |
| 105 | /* |
| 106 | Test that if both integer and float rec_per_key values are set, |
| 107 | the float values will be used. |
| 108 | */ |
| 109 | |
| 110 | // Give a value using the integer based rec_per_key |
| 111 | key.rec_per_key[1] = 1; |
| 112 | EXPECT_TRUE(key.has_records_per_key(1)); |
| 113 | EXPECT_EQ(key.records_per_key(1), 1.0); |
| 114 | |
| 115 | // Update the value using the API |
| 116 | key.set_records_per_key(1, 2.0); |
| 117 | EXPECT_TRUE(key.has_records_per_key(1)); |
| 118 | EXPECT_EQ(key.records_per_key(1), 2.0); |
| 119 | |
| 120 | // Set the value back to default/unknown |
| 121 | key.set_records_per_key(1, REC_PER_KEY_UNKNOWN); |
| 122 | |
| 123 | /* |
| 124 | This result might not be intuitive but when we set the value using |
| 125 | the API, only the float value will be updated. When the float value |
| 126 | is set to default/unknown, the integer value will be used instead. |
| 127 | In pratice this is not an issue, but can be solved by setting |
| 128 | the integer value to 0, when setting the float value. |
| 129 | */ |
| 130 | EXPECT_TRUE(key.has_records_per_key(1)); |
| 131 | EXPECT_EQ(key.records_per_key(1), 1.0); |
| 132 | |
| 133 | /* |
| 134 | Verify that we get default/unknown when also setting the integer |
| 135 | value to the default/unknown value. |
| 136 | */ |
| 137 | key.rec_per_key[1] = 0; |
| 138 | EXPECT_FALSE(key.has_records_per_key(1)); |
| 139 | EXPECT_EQ(key.records_per_key(1), REC_PER_KEY_UNKNOWN); |
| 140 | } |
| 141 | |
| 142 | } // namespace recperkey_unittest |