Mercurial > hg > octave-kai > gnulib-hg
annotate tests/test-vasnprintf.c @ 17476:6057744acd2c default tip master
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Fri, 16 Aug 2013 06:32:22 -0700 |
parents | 3e765e2f4c46 |
children |
rev | line source |
---|---|
8329 | 1 /* Test of vasnprintf() and asnprintf() functions. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
2 Copyright (C) 2007-2013 Free Software Foundation, Inc. |
8329 | 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 |
8329 | 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. |
8329 | 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/>. */ |
8329 | 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:
8754
diff
changeset
|
19 #include <config.h> |
8329 | 20 |
21 #include "vasnprintf.h" | |
22 | |
23 #include <stdarg.h> | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
26 | |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
27 #include "macros.h" |
8329 | 28 |
29 static void | |
13195
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
30 test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) |
8329 | 31 { |
32 char buf[8]; | |
33 int size; | |
34 | |
35 for (size = 0; size <= 8; size++) | |
36 { | |
37 size_t length = size; | |
38 char *result = my_asnprintf (NULL, &length, "%d", 12345); | |
39 ASSERT (result != NULL); | |
40 ASSERT (strcmp (result, "12345") == 0); | |
41 ASSERT (length == 5); | |
42 free (result); | |
43 } | |
44 | |
45 for (size = 0; size <= 8; size++) | |
46 { | |
47 size_t length; | |
48 char *result; | |
49 | |
50 memcpy (buf, "DEADBEEF", 8); | |
51 length = size; | |
52 result = my_asnprintf (buf, &length, "%d", 12345); | |
53 ASSERT (result != NULL); | |
54 ASSERT (strcmp (result, "12345") == 0); | |
55 ASSERT (length == 5); | |
56 if (size < 6) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
57 ASSERT (result != buf); |
17417
3e765e2f4c46
parse-datetime, tests: don't use "string" + int
Paul Eggert <eggert@cs.ucla.edu>
parents:
17249
diff
changeset
|
58 ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0); |
8329 | 59 if (result != buf) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
60 free (result); |
8329 | 61 } |
62 } | |
63 | |
13195
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
64 static char * |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
65 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...) |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
66 { |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
67 va_list args; |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
68 char *ret; |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
69 |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
70 va_start (args, format); |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
71 ret = vasnprintf (resultbuf, lengthp, format, args); |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
72 va_end (args); |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
73 return ret; |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
74 } |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
75 |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
76 static void |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
77 test_vasnprintf () |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
78 { |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
79 test_function (my_asnprintf); |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
80 } |
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
81 |
8329 | 82 static void |
83 test_asnprintf () | |
84 { | |
13195
563239444f8f
vasnprintf test: Reduce code duplication.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
85 test_function (asnprintf); |
8329 | 86 } |
87 | |
88 int | |
89 main (int argc, char *argv[]) | |
90 { | |
91 test_vasnprintf (); | |
92 test_asnprintf (); | |
93 return 0; | |
94 } |