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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8327
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
ce1d4fb435ff Tests for module 'snprintf'.
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
8327
ce1d4fb435ff Tests for module 'snprintf'.
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.
8327
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
8891
633babea5f62 Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents: 8763
diff changeset
19 #include <config.h>
8327
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <stdio.h>
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
12489
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
23 #include "signature.h"
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
24 SIGNATURE_CHECK (snprintf, int, (char *, size_t, char const *, ...));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
25
8327
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <string.h>
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12489
diff changeset
28 #include "macros.h"
8327
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 int
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 main (int argc, char *argv[])
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 {
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 char buf[8];
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 int size;
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 int retval;
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 for (size = 0; size <= 8; size++)
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 {
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 memcpy (buf, "DEADBEEF", 8);
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 }
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 return 0;
ce1d4fb435ff Tests for module 'snprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 }