0f26b219af446dee299156ecf8d60e18a3189710
[deliverable/binutils-gdb.git] / gdb / unittests / common-utils-selftests.c
1 /* Self tests for general utility routines for GDB, the GNU debugger.
2
3 Copyright (C) 2016-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "common-defs.h"
21 #include "selftest.h"
22
23 namespace selftests {
24
25 static void
26 string_printf_tests ()
27 {
28 SELF_CHECK (string_printf ("%s", "") == "");
29 SELF_CHECK (string_printf ("%d comes before 2", 1) == "1 comes before 2");
30 SELF_CHECK (string_printf ("hello %s", "world") == "hello world");
31
32 #define X10 "0123456789"
33 #define X100 X10 X10 X10 X10 X10 X10 X10 X10 X10 X10
34 #define X1000 X100 X100 X100 X100 X100 X100 X100 X100 X100 X100
35 #define X10000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000
36 #define X100000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000
37 SELF_CHECK (string_printf ("%s", X10) == X10);
38 SELF_CHECK (string_printf ("%s", X100) == X100);
39 SELF_CHECK (string_printf ("%s", X1000) == X1000);
40 SELF_CHECK (string_printf ("%s", X10000) == X10000);
41 SELF_CHECK (string_printf ("%s", X100000) == X100000);
42 }
43
44 static std::string
45 format (const char *fmt, ...)
46 {
47 va_list vp;
48
49 va_start (vp, fmt);
50 std::string result = string_vprintf (fmt, vp);
51 va_end (vp);
52 return result;
53 }
54
55 static void
56 string_vprintf_tests ()
57 {
58 /* Basic smoke tests. */
59 SELF_CHECK (format ("%s", "test") == "test");
60 SELF_CHECK (format ("%d", 23) == "23");
61 SELF_CHECK (format ("%s %d %s", "test", 23, "done")
62 == "test 23 done");
63 SELF_CHECK (format ("nothing") == "nothing");
64 }
65
66 } /* namespace selftests */
67
68 void
69 _initialize_common_utils_selftests ()
70 {
71 selftests::register_test ("string_printf", selftests::string_printf_tests);
72 selftests::register_test ("string_vprintf", selftests::string_vprintf_tests);
73 }
This page took 0.030512 seconds and 4 git commands to generate.