annotate tests/test-xvasprintf.c @ 40196:e63f5d3edab5

relocatable-prog: Update documentation. * doc/relocatable-maint.texi (Supporting Relocation): Update to match the recent changes.
author Bruno Haible <bruno@clisp.org>
date Sun, 24 Feb 2019 01:49:15 +0100
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of xvasprintf() and xasprintf() functions.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2007-2019 Free Software Foundation, Inc.
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8891
diff changeset
4 This program is free software: you can redistribute it and/or modify
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8891
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8891
diff changeset
7 (at your option) any later version.
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
17556
23a34520ac8b test-xvasprintf: disable some -Wformat-security diagnostics
Ivailo <xakepa10@gmail.com>
parents: 17459
diff changeset
19 /* Tell GCC not to warn about the specific edge cases tested here. */
23a34520ac8b test-xvasprintf: disable some -Wformat-security diagnostics
Ivailo <xakepa10@gmail.com>
parents: 17459
diff changeset
20 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
23a34520ac8b test-xvasprintf: disable some -Wformat-security diagnostics
Ivailo <xakepa10@gmail.com>
parents: 17459
diff changeset
21 # pragma GCC diagnostic ignored "-Wformat-zero-length"
23a34520ac8b test-xvasprintf: disable some -Wformat-security diagnostics
Ivailo <xakepa10@gmail.com>
parents: 17459
diff changeset
22 # pragma GCC diagnostic ignored "-Wformat-nonliteral"
23a34520ac8b test-xvasprintf: disable some -Wformat-security diagnostics
Ivailo <xakepa10@gmail.com>
parents: 17459
diff changeset
23 # pragma GCC diagnostic ignored "-Wformat-security"
23a34520ac8b test-xvasprintf: disable some -Wformat-security diagnostics
Ivailo <xakepa10@gmail.com>
parents: 17459
diff changeset
24 #endif
23a34520ac8b test-xvasprintf: disable some -Wformat-security diagnostics
Ivailo <xakepa10@gmail.com>
parents: 17459
diff changeset
25
8891
633babea5f62 Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents: 8754
diff changeset
26 #include <config.h>
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include "xvasprintf.h"
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <stdarg.h>
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include <stdlib.h>
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #include <string.h>
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12482
diff changeset
34 #include "macros.h"
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 static char *
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 my_xasprintf (const char *format, ...)
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 {
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 va_list args;
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 char *ret;
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 va_start (args, format);
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 ret = xvasprintf (format, args);
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 va_end (args);
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 return ret;
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 }
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 static void
12210
441d8820b5a5 tests: avoid some compiler warnings
Eric Blake <ebb9@byu.net>
parents: 9889
diff changeset
49 test_xvasprintf (void)
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 {
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 int repeat;
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
52 char *result;
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 for (repeat = 0; repeat <= 8; repeat++)
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 {
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
56 result = my_xasprintf ("%d", 12345);
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 ASSERT (result != NULL);
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 ASSERT (strcmp (result, "12345") == 0);
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 free (result);
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 }
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
61
12323
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
62 {
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
63 /* Silence gcc warning about zero-length format string. */
13410
78e99fa31037 test-xvasprintf: avoid 'const' discard warnings
Jim Meyering <meyering@redhat.com>
parents: 12559
diff changeset
64 const char *empty = "";
12323
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
65 result = my_xasprintf (empty);
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
66 ASSERT (result != NULL);
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
67 ASSERT (strcmp (result, "") == 0);
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
68 free (result);
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
69 }
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
70
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
71 result = my_xasprintf ("%s", "foo");
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
72 ASSERT (result != NULL);
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
73 ASSERT (strcmp (result, "foo") == 0);
12274
88fe10a9db6a tests/test-xvasprintf.c: Fix memory leak.
Simon Josefsson <simon@josefsson.org>
parents: 12273
diff changeset
74 free (result);
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
75
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
76 result = my_xasprintf ("%s%s", "foo", "bar");
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
77 ASSERT (result != NULL);
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
78 ASSERT (strcmp (result, "foobar") == 0);
12274
88fe10a9db6a tests/test-xvasprintf.c: Fix memory leak.
Simon Josefsson <simon@josefsson.org>
parents: 12273
diff changeset
79 free (result);
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
80
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
81 result = my_xasprintf ("%s%sbaz", "foo", "bar");
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
82 ASSERT (result != NULL);
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
83 ASSERT (strcmp (result, "foobarbaz") == 0);
12274
88fe10a9db6a tests/test-xvasprintf.c: Fix memory leak.
Simon Josefsson <simon@josefsson.org>
parents: 12273
diff changeset
84 free (result);
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 }
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 static void
13410
78e99fa31037 test-xvasprintf: avoid 'const' discard warnings
Jim Meyering <meyering@redhat.com>
parents: 12559
diff changeset
88 test_xasprintf (void)
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 {
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 int repeat;
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
91 char *result;
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 for (repeat = 0; repeat <= 8; repeat++)
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 {
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
95 result = xasprintf ("%d", 12345);
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 ASSERT (result != NULL);
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 ASSERT (strcmp (result, "12345") == 0);
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 free (result);
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 }
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
100
12323
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
101 {
17459
9c2a21bcb268 xvasprintf-tests: port to GCC with hardening flags
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
102 /* Silence gcc warning about zero-length format string,
9c2a21bcb268 xvasprintf-tests: port to GCC with hardening flags
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
103 and about "format not a string literal and no format"
9c2a21bcb268 xvasprintf-tests: port to GCC with hardening flags
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
104 (whatever that means) . */
13410
78e99fa31037 test-xvasprintf: avoid 'const' discard warnings
Jim Meyering <meyering@redhat.com>
parents: 12559
diff changeset
105 const char *empty = "";
17459
9c2a21bcb268 xvasprintf-tests: port to GCC with hardening flags
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
106 result = xasprintf (empty, empty);
12323
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
107 ASSERT (result != NULL);
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
108 ASSERT (strcmp (result, "") == 0);
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
109 free (result);
dd9a1c6c56b4 test-xvasprintf: silence compiler warnings
Eric Blake <ebb9@byu.net>
parents: 12274
diff changeset
110 }
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
111
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
112 result = xasprintf ("%s", "foo");
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
113 ASSERT (result != NULL);
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
114 ASSERT (strcmp (result, "foo") == 0);
12274
88fe10a9db6a tests/test-xvasprintf.c: Fix memory leak.
Simon Josefsson <simon@josefsson.org>
parents: 12273
diff changeset
115 free (result);
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
116
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
117 result = xasprintf ("%s%s", "foo", "bar");
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
118 ASSERT (result != NULL);
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
119 ASSERT (strcmp (result, "foobar") == 0);
12274
88fe10a9db6a tests/test-xvasprintf.c: Fix memory leak.
Simon Josefsson <simon@josefsson.org>
parents: 12273
diff changeset
120 free (result);
12273
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
121
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
122 result = my_xasprintf ("%s%sbaz", "foo", "bar");
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
123 ASSERT (result != NULL);
0c24082ae7d9 test-xvasprintf: Add %s%s related checks.
Simon Josefsson <simon@josefsson.org>
parents: 12210
diff changeset
124 ASSERT (strcmp (result, "foobarbaz") == 0);
12274
88fe10a9db6a tests/test-xvasprintf.c: Fix memory leak.
Simon Josefsson <simon@josefsson.org>
parents: 12273
diff changeset
125 free (result);
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 }
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 int
12482
11bc92a9e2bc gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
129 main (int argc _GL_UNUSED, char *argv[])
8331
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 {
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 test_xvasprintf ();
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 test_xasprintf ();
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 return 0;
0a462a560fcd Tests for module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 }