aboutsummaryrefslogtreecommitdiff
path: root/gdb/utils-selftests.c
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2016-11-08 15:26:42 +0000
committerPedro Alves <palves@redhat.com>2016-11-08 15:26:42 +0000
commitd4081a383e28db26c65298f7405554d4312b1342 (patch)
tree6a44bd0e08354fd50608dfe5240d28ec7e567e55 /gdb/utils-selftests.c
parentcb64e50d42a49bce61050c79c5ab0846905b6a82 (diff)
Introduce string_printf
This introduces the string_printf function. Like asprintf, but returns a std::string. gdb/ChangeLog: 2016-11-08 Pedro Alves <palves@redhat.com> * Makefile.in (COMMON_OBS): Add utils-selftests.o. * common/common-utils.c (string_printf): New function. * common/common-utils.h: Include <string>. (string_printf): Declare. * utils-selftests.c: New file.
Diffstat (limited to 'gdb/utils-selftests.c')
-rw-r--r--gdb/utils-selftests.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/gdb/utils-selftests.c b/gdb/utils-selftests.c
new file mode 100644
index 0000000000..f424655546
--- /dev/null
+++ b/gdb/utils-selftests.c
@@ -0,0 +1,60 @@
+/* Self tests for general utility routines for GDB, the GNU debugger.
+
+ Copyright (C) 2016 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "selftest.h"
+
+#if GDB_SELF_TEST
+
+namespace selftests {
+
+/* common-utils self tests. Defined here instead of in
+ common/common-utils.c because that file is shared with
+ gdbserver. */
+
+static void
+common_utils_tests (void)
+{
+ SELF_CHECK (string_printf ("%s", "") == "");
+ SELF_CHECK (string_printf ("%d comes before 2", 1) == "1 comes before 2");
+ SELF_CHECK (string_printf ("hello %s", "world") == "hello world");
+
+#define X10 "0123456789"
+#define X100 X10 X10 X10 X10 X10 X10 X10 X10 X10 X10
+#define X1000 X100 X100 X100 X100 X100 X100 X100 X100 X100 X100
+#define X10000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000
+#define X100000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000
+ SELF_CHECK (string_printf ("%s", X10) == X10);
+ SELF_CHECK (string_printf ("%s", X100) == X100);
+ SELF_CHECK (string_printf ("%s", X1000) == X1000);
+ SELF_CHECK (string_printf ("%s", X10000) == X10000);
+ SELF_CHECK (string_printf ("%s", X100000) == X100000);
+}
+
+} /* namespace selftests */
+
+#endif
+
+void
+_initialize_utils_selftests (void)
+{
+#if GDB_SELF_TEST
+ register_self_test (selftests::common_utils_tests);
+#endif
+}