Mercurial > gnulib
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 |
rev | line source |
---|---|
8331 | 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 | 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 | 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 | 8 |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
8331 | 16 |
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */ | |
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 | 27 |
28 #include "xvasprintf.h" | |
29 | |
30 #include <stdarg.h> | |
31 #include <stdlib.h> | |
32 #include <string.h> | |
33 | |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12482
diff
changeset
|
34 #include "macros.h" |
8331 | 35 |
36 static char * | |
37 my_xasprintf (const char *format, ...) | |
38 { | |
39 va_list args; | |
40 char *ret; | |
41 | |
42 va_start (args, format); | |
43 ret = xvasprintf (format, args); | |
44 va_end (args); | |
45 return ret; | |
46 } | |
47 | |
48 static void | |
12210
441d8820b5a5
tests: avoid some compiler warnings
Eric Blake <ebb9@byu.net>
parents:
9889
diff
changeset
|
49 test_xvasprintf (void) |
8331 | 50 { |
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 | 53 |
54 for (repeat = 0; repeat <= 8; repeat++) | |
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 | 57 ASSERT (result != NULL); |
58 ASSERT (strcmp (result, "12345") == 0); | |
59 free (result); | |
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 | 85 } |
86 | |
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 | 89 { |
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 | 92 |
93 for (repeat = 0; repeat <= 8; repeat++) | |
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 | 96 ASSERT (result != NULL); |
97 ASSERT (strcmp (result, "12345") == 0); | |
98 free (result); | |
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 | 126 } |
127 | |
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 | 130 { |
131 test_xvasprintf (); | |
132 test_xasprintf (); | |
133 | |
134 return 0; | |
135 } |