| 1 | /* Copyright (c) 2017, 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 | |
| 26 | #include "my_sys.h" |
| 27 | |
| 28 | namespace my_b_vprintf_unittest { |
| 29 | |
| 30 | void test1(const char *res, const char *fmt, ...) { |
| 31 | IO_CACHE info; |
| 32 | va_list args; |
| 33 | size_t len; |
| 34 | init_io_cache(&info, -1, 0, WRITE_CACHE, 0, true, MYF(0)); |
| 35 | memset(info.write_buffer, 0, 64); /* RECORD_CACHE_SIZE is 64K */ |
| 36 | va_start(args, fmt); |
| 37 | len = my_b_vprintf(&info, fmt, args); |
| 38 | va_end(args); |
| 39 | EXPECT_EQ(len, strlen(res)); |
| 40 | EXPECT_STREQ((char *)(info.write_buffer), res); |
| 41 | end_io_cache(&info); |
| 42 | } |
| 43 | |
| 44 | TEST(Mysys, Vprintf) { |
| 45 | test1("Format specifier s works", "Format specifier s %s", "works"); |
| 46 | test1("Format specifier b works", "Format specifier b %.5b", "works!!!"); |
| 47 | test1("Format specifier b with * works", "Format specifier b with * %.*b", 5, |
| 48 | "workss!!!"); |
| 49 | test1("Format specifier c !", "Format specifier c %c", '!'); |
| 50 | test1("Format specifier d 1", "Format specifier d %d", 1); |
| 51 | test1("Format specifier u 2", "Format specifier u %u", 2); |
| 52 | test1("Format specifier ld -3", "Format specifier ld %ld", -3L); |
| 53 | test1("Format specifier lu 4", "Format specifier lu %lu", 4L); |
| 54 | test1("Format specifier llu 5", "Format specifier llu %llu", 5LL); |
| 55 | |
| 56 | test1("Width is ignored for strings <x> <y>", |
| 57 | "Width is ignored for strings <%04s> <%5s>", "x", "y"); |
| 58 | |
| 59 | test1("Hello", "Hello"); |
| 60 | test1("Hello int, 1", "Hello int, %d", 1); |
| 61 | test1("Hello int, -1", "Hello int, %d", -1); |
| 62 | test1("Hello string 'I am a string'", "Hello string '%s'", "I am a string"); |
| 63 | test1("Hello hack hack 1", "Hello hack hack %d", 1); |
| 64 | test1("Hello 1 hack 4", "Hello %d hack %d", 1, 4); |
| 65 | test1("Hello 1 hack hack hack hack hack 4", |
| 66 | "Hello %d hack hack hack hack hack %d", 1, 4); |
| 67 | test1("Hello 'hack' hhhhhhhhhhhhhhh", "Hello '%s' hhhhhhhhhhhhhhh", "hack"); |
| 68 | test1("Hello hhhhhhhhh 1 sssssssss", "Hello hhhhhhhhh %d sssssssss", 1); |
| 69 | test1("Hello 1", "Hello %u", 1); |
| 70 | test1("Hello 4294967295", "Hello %u", -1); |
| 71 | test1("Hello TEST", "Hello %05s", "TEST"); |
| 72 | } |
| 73 | |
| 74 | } // namespace my_b_vprintf_unittest |