| 1 | /* Copyright (c) 2013, 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 | /* See http://code.google.com/p/googletest/wiki/Primer */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "univ.i" |
| 29 | |
| 30 | #include "ha_prototypes.h" |
| 31 | |
| 32 | namespace innodb_printf_unittest { |
| 33 | |
| 34 | static void test_snprintf(const char *res, char *buf, size_t bufsz, |
| 35 | const char *fmt, ...) { |
| 36 | va_list args; |
| 37 | size_t len; |
| 38 | |
| 39 | EXPECT_STREQ(buf, res); |
| 40 | va_start(args, fmt); |
| 41 | len = vsnprintf(buf, bufsz - 1, fmt, args); |
| 42 | va_end(args); |
| 43 | EXPECT_EQ(len, strlen(res)); |
| 44 | EXPECT_STREQ(buf, res); |
| 45 | } |
| 46 | |
| 47 | TEST(hainnodb, UtMySnprintf) { |
| 48 | char buf[72]; |
| 49 | size_t bufsz; |
| 50 | |
| 51 | bufsz = sizeof buf; |
| 52 | #define ARGS buf, bufsz, "foo %u " IB_ID_FMT ".", 1, (trx_id_t)-1 |
| 53 | snprintf(ARGS); |
| 54 | test_snprintf("foo 1 18446744073709551615.", ARGS); |
| 55 | |
| 56 | bufsz = 25; |
| 57 | snprintf(ARGS); |
| 58 | EXPECT_STREQ(buf, "foo 1 184467440737095516"); |
| 59 | snprintf(ARGS); |
| 60 | EXPECT_STREQ(buf, "foo 1 ."); |
| 61 | bufsz = sizeof buf; |
| 62 | #undef ARGS |
| 63 | |
| 64 | ib_uint32_t a; |
| 65 | ib_uint64_t b; |
| 66 | int64_t c; |
| 67 | |
| 68 | #define ARGS \ |
| 69 | buf, bufsz, UINT32PF "/" UINT64PF "/%" PRId64 "/" UINT64PFx "*", a, b, c, b |
| 70 | |
| 71 | a = 0, b = 1, c = 2; |
| 72 | snprintf(ARGS); |
| 73 | test_snprintf("0/1/2/0000000000000001*", ARGS); |
| 74 | a = -1, b = -2, c = -3; |
| 75 | snprintf(ARGS); |
| 76 | test_snprintf("4294967295/18446744073709551614/-3/fffffffffffffffe*", ARGS); |
| 77 | |
| 78 | a = 1234567890, b = 12345678901234567890ULL, c = static_cast<int64_t>(b); |
| 79 | snprintf(ARGS); |
| 80 | test_snprintf( |
| 81 | "1234567890/12345678901234567890/-6101065172474983726/" |
| 82 | "ab54a98ceb1f0ad2*", |
| 83 | ARGS); |
| 84 | a = -1234567890, c = -8765432109876543210LL, b = ib_uint64_t(c); |
| 85 | snprintf(ARGS); |
| 86 | test_snprintf( |
| 87 | "3060399406/9681311963833008406/-8765432109876543210/" |
| 88 | "865aedeff4018116*", |
| 89 | ARGS); |
| 90 | #undef ARGS |
| 91 | } |
| 92 | |
| 93 | } // namespace innodb_printf_unittest |