Mercurial > hg > octave-lojdl > gnulib-hg
annotate tests/test-snprintf.c @ 15681:c21e706c88b2
Tests for module 'shutdown'.
* modules/shutdown-tests: New file.
* tests/test-shutdown.c: New file.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Tue, 20 Sep 2011 21:38:04 +0200 |
parents | 84ad6dbce2a8 |
children | 8250f2777afc |
rev | line source |
---|---|
8327 | 1 /* Test of snprintf() function. |
14079
97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents:
14003
diff
changeset
|
2 Copyright (C) 2007-2011 Free Software Foundation, Inc. |
8327 | 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 |
8327 | 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. |
8327 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
8327 | 16 |
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */ | |
18 | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8763
diff
changeset
|
19 #include <config.h> |
8327 | 20 |
21 #include <stdio.h> | |
22 | |
12489 | 23 #include "signature.h" |
24 SIGNATURE_CHECK (snprintf, int, (char *, size_t, char const *, ...)); | |
25 | |
8327 | 26 #include <string.h> |
27 | |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12489
diff
changeset
|
28 #include "macros.h" |
8327 | 29 |
30 int | |
31 main (int argc, char *argv[]) | |
32 { | |
33 char buf[8]; | |
34 int size; | |
35 int retval; | |
36 | |
14003
f82a977dcf0b
vsnprintf: make more consistent with snprintf; doc fixes
Paul Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
37 retval = snprintf (NULL, 0, "%d", 12345); |
f82a977dcf0b
vsnprintf: make more consistent with snprintf; doc fixes
Paul Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
38 ASSERT (retval == 5); |
f82a977dcf0b
vsnprintf: make more consistent with snprintf; doc fixes
Paul Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
39 |
8327 | 40 for (size = 0; size <= 8; size++) |
41 { | |
42 memcpy (buf, "DEADBEEF", 8); | |
43 retval = snprintf (buf, size, "%d", 12345); | |
14003
f82a977dcf0b
vsnprintf: make more consistent with snprintf; doc fixes
Paul Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
44 ASSERT (retval == 5); |
8327 | 45 if (size < 6) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
46 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
47 if (size > 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
48 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
49 ASSERT (memcmp (buf, "12345", size - 1) == 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
50 ASSERT (buf[size - 1] == '\0' || buf[size - 1] == '0' + size); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
51 } |
8763
39bb5430fc0e
Guard against vsnprintf implementations that mishandle a size=0 argument.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
52 #if !CHECK_SNPRINTF_POSIX |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
53 if (size > 0) |
8763
39bb5430fc0e
Guard against vsnprintf implementations that mishandle a size=0 argument.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
54 #endif |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
55 ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
56 } |
8327 | 57 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
58 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
59 ASSERT (memcmp (buf, "12345\0EF", 8) == 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
60 } |
8327 | 61 } |
62 | |
15340
03a50862ef09
snprintf: guarantee %1$d, for libintl
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
63 /* Test the support of the POSIX/XSI format strings with positions. */ |
03a50862ef09
snprintf: guarantee %1$d, for libintl
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
64 { |
03a50862ef09
snprintf: guarantee %1$d, for libintl
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
65 char result[100]; |
15380
84ad6dbce2a8
test-snprintf: avoid compiler warning
Eric Blake <eblake@redhat.com>
parents:
15340
diff
changeset
|
66 retval = snprintf (result, sizeof (result), "%2$d %1$d", 33, 55); |
15340
03a50862ef09
snprintf: guarantee %1$d, for libintl
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
67 ASSERT (strcmp (result, "55 33") == 0); |
03a50862ef09
snprintf: guarantee %1$d, for libintl
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
68 ASSERT (retval == strlen (result)); |
03a50862ef09
snprintf: guarantee %1$d, for libintl
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
69 } |
03a50862ef09
snprintf: guarantee %1$d, for libintl
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
70 |
8327 | 71 return 0; |
72 } |