1 /* Copyright (c) 2013, 2021, 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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /* See http://code.google.com/p/googletest/wiki/Primer */
24 
25 // First include (the generated) my_config.h, to get correct platform defines.
26 #include "my_config.h"
27 
28 #include <gtest/gtest.h>
29 
30 #include "univ.i"
31 
32 #include "ha_prototypes.h"
33 
34 namespace innodb_printf_unittest {
35 
36 static
37 void
test_snprintf(const char * res,char * buf,size_t bufsz,const char * fmt,...)38 test_snprintf(const char* res, char* buf, size_t bufsz, const char* fmt, ...)
39 {
40 	va_list args;
41 	size_t len;
42 
43 	EXPECT_STREQ(buf, res);
44 	va_start(args,fmt);
45 	len= my_vsnprintf(buf, bufsz-1, fmt, args);
46 	va_end(args);
47 	EXPECT_EQ(len, strlen(res));
48 	EXPECT_STREQ(buf, res);
49 }
50 
TEST(hainnodb,UtMySnprintf)51 TEST(hainnodb, UtMySnprintf)
52 {
53 	char	buf[72];
54 	size_t	bufsz;
55 
56 	bufsz = sizeof buf;
57 #define ARGS buf, bufsz, "foo %u " IB_ID_FMT ".", 1, (trx_id_t) -1
58 	ut_snprintf(ARGS);
59 	test_snprintf("foo 1 18446744073709551615.", ARGS);
60 
61 	bufsz = 25;
62 	ut_snprintf(ARGS); EXPECT_STREQ(buf, "foo 1 184467440737095516");
63 	my_snprintf(ARGS); EXPECT_STREQ(buf, "foo 1 .");
64 	bufsz = sizeof buf;
65 #undef ARGS
66 
67 	ib_uint32_t	a;
68 	ib_uint64_t	b;
69 	int64_t		c;
70 
71 #define ARGS buf, bufsz, UINT32PF "/" UINT64PF "/%" PRId64 "/" UINT64PFx "*", \
72 	a, b, c, b
73 
74 	a = 0, b = 1, c = 2;
75 	ut_snprintf(ARGS);
76 	test_snprintf(
77 		"0/1/2/0000000000000001*", ARGS);
78 	a = -1, b = -2, c = -3;
79 	ut_snprintf(ARGS);
80 	test_snprintf(
81 		"4294967295/18446744073709551614/-3/fffffffffffffffe*", ARGS);
82 
83 	a = 1234567890, b = 12345678901234567890ULL, c = static_cast<int64_t>(b);
84 	ut_snprintf(ARGS);
85 	test_snprintf(
86 		"1234567890/12345678901234567890/-6101065172474983726/"
87 		"ab54a98ceb1f0ad2*", ARGS);
88 	a = -1234567890, c = -8765432109876543210LL, b = ib_uint64_t (c);
89 	ut_snprintf(ARGS);
90 	test_snprintf(
91 		"3060399406/9681311963833008406/-8765432109876543210/"
92 		"865aedeff4018116*", ARGS);
93 #undef ARGS
94 }
95 
96 }
97