snprintf_2.2/test.c

Thu, 03 Aug 2006 23:09:14 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 03 Aug 2006 23:09:14 +0200
changeset 102
c00694dbcde8
parent 35
5a71d53d0228
permissions
-rw-r--r--

Updated locations of *.mk.

35
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
1 /*
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
2 * test.c - test a portable implementation of snprintf
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
3 *
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
4 * AUTHOR
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
5 * Mark Martinec <mark.martinec@ijs.si>, April 1999.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
6 *
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
7 * Copyright 1999, Mark Martinec. All rights reserved.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
8 *
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
9 * TERMS AND CONDITIONS
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
10 * This program is free software; you can redistribute it and/or modify
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
11 * it under the terms of the "Frontier Artistic License" which comes
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
12 * with this Kit.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
13 *
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
17 * See the Frontier Artistic License for more details.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
18 *
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
19 * You should have received a copy of the Frontier Artistic License
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
20 * with this Kit in the file named LICENSE.txt .
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
21 * If not, I'll be glad to provide one.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
22 *
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
23 * NOTE: This test program is a QUICK and DIRTY tool
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
24 * ===== used while testing and benchmarking my portable snprintf.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
25 * Certain data types are not fully supported, certain test
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
26 * cases were fabricated during testing by modifying the code
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
27 * or running it by specifying test parameters in the command line.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
28 *
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
29 * You are on your own if you want to use this test program!
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
30 */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
31
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
32 /* If no command arguments are specified do the exhaustive test.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
33 * This takes a long time. You may want to reduce the fw and fp
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
34 * upper limits in the for loops.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
35 * You may also reduce the number of test elements in the array iargs.
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
36 */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
37
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
38 #include <sys/types.h>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
39 #include <string.h>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
40 #include <stdlib.h>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
41 #include <stdio.h>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
42 #include <stdarg.h>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
43
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
44 #include <assert.h>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
45 #include <time.h>
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
46
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
47 #if defined(NEED_ASPRINTF) || defined(NEED_ASNPRINTF) || defined(NEED_VASPRINTF) || defined(NEED_VASNPRINTF)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
48 # if defined(NEED_SNPRINTF_ONLY)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
49 # undef NEED_SNPRINTF_ONLY
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
50 # endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
51 # if !defined(PREFER_PORTABLE_SNPRINTF)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
52 # define PREFER_PORTABLE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
53 # endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
54 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
55
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
56 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
57 extern int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
58 #else
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
59 extern int snprintf(char *, size_t, const char *, /*args*/ ...);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
60 extern int vsnprintf(char *, size_t, const char *, va_list);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
61 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
62
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
63 #ifndef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
64 #define portable_snprintf snprintf
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
65 #define portable_vsnprintf vsnprintf
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
66 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
67
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
68 #ifndef CLOCKS_PER_SEC
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
69 #define CLOCKS_PER_SEC 100
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
70 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
71
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
72 #define min(a,b) ((a)<(b) ? (a) : (b))
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
73 #define max(a,b) ((a)>(b) ? (a) : (b))
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
74
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
75 extern int asprintf (char **ptr, const char *fmt, /*args*/ ...);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
76 extern int vasprintf (char **ptr, const char *fmt, va_list ap);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
77 extern int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
78 extern int vasnprintf(char **ptr, size_t str_m, const char *fmt, va_list ap);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
79
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
80 #ifndef NEED_SNPRINTF_ONLY
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
81 int wrap_vsnprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
82 int wrap_vsnprintf(char *str, size_t str_m, const char *fmt, ...) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
83 va_list ap;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
84 int str_l;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
85
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
86 va_start(ap, fmt);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
87 str_l = vsnprintf(str, str_m, fmt, ap);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
88 va_end(ap);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
89 return str_l;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
90 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
91 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
92
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
93 #ifdef NEED_VASPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
94 int wrap_vasprintf(char **ptr, const char *fmt, /*args*/ ...);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
95 int wrap_vasprintf(char **ptr, const char *fmt, ...) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
96 va_list ap;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
97 int str_l;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
98
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
99 va_start(ap, fmt);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
100 str_l = vasprintf(ptr, fmt, ap);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
101 va_end(ap);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
102 return str_l;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
103 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
104 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
105
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
106 #ifdef NEED_VASNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
107 int wrap_vasnprintf(char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
108 int wrap_vasnprintf(char **ptr, size_t str_m, const char *fmt, ...) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
109 va_list ap;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
110 int str_l;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
111
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
112 va_start(ap, fmt);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
113 str_l = vasnprintf(ptr, str_m, fmt, ap);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
114 va_end(ap);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
115 return str_l;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
116 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
117 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
118
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
119 int main(int argc, char *argv[]) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
120 char str1[256], str2[256];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
121 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
122 char str3[256];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
123 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
124 int len1, len2, len3;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
125 int bad = 0;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
126 size_t str_m = 20; /* declared str size */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
127
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
128 if (0) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
129 /* benchmarking */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
130 const int cnt = 100000;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
131 size_t size;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
132 char str[40000];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
133 time_t t0,t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
134 int j,len,l1,l2;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
135 char *p;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
136 int breakpoint;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
137
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
138 size = 18000;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
139
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
140 printf("\n\nsize = %d\n", (int)size);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
141 p = malloc(size); assert(p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
142 memset(p,'h',size); p[size-1] = '\0';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
143 t0 = clock();
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
144
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
145 printf("\ndetermine breakeven point to see when it is worth\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
146 printf("calling memcpy and when to do inline string copy\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
147 printf("str_l, memcpy, inline\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
148 for (breakpoint=0; breakpoint<=35; breakpoint++) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
149 register size_t nnn = (size_t)breakpoint;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
150 printf("%5ld", nnn);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
151 for (j=10*cnt; j>0; j--) memcpy(str, p, nnn);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
152 t = clock(); printf(" %1.3f", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
153 for (j=10*cnt; j>0; j--) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
154 register size_t nn = (size_t)breakpoint;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
155 if (nn > 0) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
156 register char *dd; register const char *ss;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
157 for (ss=p, dd=str; nn>0; nn--) *dd++ = *ss++;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
158 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
159 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
160 t = clock(); printf(" %1.3f\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
161 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
162
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
163 printf("\nmeasuring time to SKIP a long format with no conversions\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
164 p[0] = '%'; p[1] = 's';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
165 for (j=cnt; j>0; j--) l1=portable_snprintf(NULL,(size_t)0,p,"1234567890");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
166 t = clock(); printf("t_port_nul = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
167 for (j=cnt; j>0; j--) l2=portable_snprintf(str,(size_t)8,p,"1234567890");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
168 t = clock(); printf("t_port = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
169 assert(l1==l2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
170 p[0] = p[1] = 'h';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
171
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
172 printf("\nmeasuring time to copy a long format with no conversions\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
173 for (j=cnt; j>0; j--) l1=portable_snprintf(NULL,(size_t)0,p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
174 t = clock(); printf("t_port_nul = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
175 for (j=cnt; j>0; j--) l2=portable_snprintf(str,sizeof(str),p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
176 t = clock(); printf("t_port = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
177 assert(l1==l2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
178 for (j=cnt; j>0; j--) sprintf(str,p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
179 t = clock(); printf("t_sys = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
180
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
181 printf("\nmeasuring time to copy a long format with one conversion\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
182 p[size-10] = '%';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
183 for (j=cnt; j>0; j--) l1=portable_snprintf(NULL,(size_t)0,p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
184 t = clock(); printf("t_port_nul = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
185 for (j=cnt; j>0; j--) l2=portable_snprintf(str,sizeof(str),p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
186 t = clock(); printf("t_port = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
187 assert(l1==l2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
188 for (j=cnt; j>0; j--) sprintf(str,p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
189 t = clock(); printf("t_sys = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
190
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
191 printf("\nmeasuring string argument copy speed\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
192 for (j=cnt; j>0; j--) l1=portable_snprintf(NULL,(size_t)0,"%.18000s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
193 t = clock(); printf("t_port_nul = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
194 for (j=cnt; j>0; j--) l2=portable_snprintf(str,sizeof(str),"%.18000s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
195 t = clock(); printf("t_port = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
196 assert(l1==l2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
197 for (j=cnt; j>0; j--) sprintf(str,"%.18000s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
198 t = clock(); printf("t_sys = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
199
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
200 printf("\nmeasuring left padding speed\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
201 p[0] = '\0';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
202 for (j=cnt; j>0; j--) l1=portable_snprintf(NULL,(size_t)0,"%-18000s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
203 t = clock(); printf("t_port_nul = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
204 for (j=cnt; j>0; j--) l2=portable_snprintf(str,sizeof(str),"%-18000s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
205 t = clock(); printf("t_port = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
206 assert(l1==l2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
207 for (j=cnt; j>0; j--) sprintf(str,"%-18000s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
208 t = clock(); printf("t_sys = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
209
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
210 printf("\nmeasuring right padding speed\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
211 p[0] = '\0';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
212 for (j=cnt; j>0; j--) l1=portable_snprintf(NULL,(size_t)0,"%18000s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
213 t = clock(); printf("t_port_nul = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
214 for (j=cnt; j>0; j--) l2=portable_snprintf(str,sizeof(str),"%18000s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
215 t = clock(); printf("t_port = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
216 assert(l1==l2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
217 for (j=cnt; j>0; j--) sprintf(str,"%18000s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
218 t = clock(); printf("t_sys = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
219
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
220 printf("\nmeasuring zero padding speed\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
221 for (j=cnt; j>0; j--) l1=portable_snprintf(NULL,(size_t)0,"%018000d",1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
222 t = clock(); printf("t_port_nul = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
223 for (j=cnt; j>0; j--) l2=portable_snprintf(str,sizeof(str),"%018000d",1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
224 t = clock(); printf("t_port = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
225 assert(l1==l2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
226 for (j=cnt; j>0; j--) sprintf(str,"%018000d",1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
227 t = clock(); printf("t_sys = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
228
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
229 printf("\nmeasuring system's sprintf to efficiently handle truncated strings\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
230 memset(p,'h',size); p[size-1] = '\0';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
231 t0 = clock();
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
232 for (j=cnt; j>0; j--) len = strlen(p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
233 printf("len = %d\n", len);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
234 t = clock(); printf("t_strlen = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
235 /* test if the system sprintf scans the whole string (e.g. by strlen)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
236 * before recognizing this was a bad idea since the format specified
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
237 * a truncated string precision, e.g. "%.8s" .
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
238 */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
239 for (j=cnt; j>0; j--) sprintf(str,"%.2s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
240 t = clock(); printf("t_sys = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
241 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
242 for (j=cnt; j>0; j--) snprintf(str,sizeof(str),"%.2s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
243 t = clock(); printf("t_sys = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
244 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
245 for (j=cnt; j>0; j--) portable_snprintf(str,sizeof(str),"%.2s",p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
246 t = clock(); printf("t_port = %1.3f s\n", (float)(t-t0)/CLOCKS_PER_SEC); t0 = t;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
247
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
248 free(p);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
249 return 0;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
250 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
251
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
252
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
253 /* preliminary halfhearted test */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
254 {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
255 const char fmt[] = "Bla%.4s%05iHE%%%-50sTail";
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
256 char *ptr4=0, *ptr5=0, *ptr6=0, *ptr7=0;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
257 int len1f;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
258 char str_full[256];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
259
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
260 printf("\npreliminary test: snprintf\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
261 len1 = snprintf(str1, str_m, fmt, "abcdef",-12,"str");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
262 len1f = snprintf(str_full, sizeof(str_full), fmt, "abcdef",-12,"str");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
263 assert(len1f==len1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
264 assert(memcmp(str1,str_full,min(len1,str_m-1)) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
265 assert(str1[str_m-1] == '\0');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
266 assert(str_full[sizeof(str_full)-1] == '\0');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
267
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
268 #ifndef NEED_SNPRINTF_ONLY
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
269 printf("preliminary test: vsnprintf\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
270 len2 = wrap_vsnprintf(str2, str_m, fmt, "abcdef",-12,"str");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
271 assert(len2==len1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
272 assert(memcmp(str1,str2,min(len1+1,str_m)) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
273 assert(str2[str_m-1] == '\0');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
274 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
275
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
276 #ifdef NEED_ASPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
277 printf("preliminary test: asprintf\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
278 len4 = asprintf(&ptr4, fmt, "abcdef",-12,"str");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
279 assert(ptr4);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
280 assert(len4==len1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
281 assert(memcmp(str_full,ptr4,min(len4+1,sizeof(str_full))) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
282 assert(ptr4[len4] == '\0');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
283 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
284
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
285 #ifdef NEED_ASNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
286 printf("preliminary test: asnprintf\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
287 len5 = asnprintf(&ptr5, str_m, fmt, "abcdef",-12,"str");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
288 assert(ptr5);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
289 assert(len5==len1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
290 assert(memcmp(str1,ptr5,min(len5+1,str_m)) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
291 assert(ptr5[len5] == '\0');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
292 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
293
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
294 #ifdef NEED_VASPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
295 printf("preliminary test: vasprintf\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
296 len6 = wrap_vasprintf(&ptr6, fmt, "abcdef",-12,"str");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
297 assert(ptr6);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
298 assert(len6==len1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
299 assert(memcmp(str_full,ptr6,min(len6+1,sizeof(str_full))) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
300 assert(ptr6[len6] == '\0');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
301 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
302
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
303 #ifdef NEED_VASNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
304 printf("preliminary test: vasnprintf\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
305 len7 = wrap_vasnprintf(&ptr7, str_m, fmt, "abcdef",-12,"str");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
306 assert(ptr7);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
307 assert(len7==len1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
308 assert(memcmp(str1,ptr7,min(len7+1,str_m)) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
309 assert(ptr7[len7] == '\0');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
310 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
311
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
312 if (ptr4) free(ptr4);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
313 if (ptr5) free(ptr5);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
314 if (ptr6) free(ptr6);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
315 if (ptr7) free(ptr7);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
316 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
317
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
318 /* second preliminary halfhearted test */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
319 {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
320 printf("\nsecond preliminary test:\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
321
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
322 printf("test 0a\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
323 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
324 len1 = snprintf(str1, sizeof(str1), "");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
325 len2 = sprintf (str2, "");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
326 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
327 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
328
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
329 printf("test 0b\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
330 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
331 len1 = snprintf(str1, sizeof(str1), "YK");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
332 len2 = sprintf (str2, "YK");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
333 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
334 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
335
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
336 printf("test 1\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
337 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
338 len1 = snprintf(str1, sizeof(str1), "%+d",0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
339 len2 = sprintf (str2, "%+d",0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
340 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
341 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
342
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
343 printf("test 2\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
344 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
345 len1 = snprintf(str1, sizeof(str1), "%.2147483647s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
346 len2 = sprintf (str2, "%.2147483647s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
347 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
348 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
349 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
350
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
351 printf("test 3a\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
352 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
353 len1 = snprintf(str1, sizeof(str1), "%.2147483648s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
354 len2 = sprintf (str2, "%.2147483648s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
355 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
356 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
357 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
358
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
359 printf("test 3b\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
360 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
361 len1 = snprintf(str1, sizeof(str1), "%.2147483649s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
362 len2 = sprintf (str2, "%.2147483649s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
363 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
364 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
365 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
366
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
367 printf("test 4\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
368 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
369 len1 = snprintf(str1, sizeof(str1), "%-.2147483647s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
370 len2 = sprintf (str2, "%-.2147483647s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
371 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
372 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
373 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
374
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
375 printf("test 5\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
376 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
377 len1 = snprintf(str1, sizeof(str1), "%-.2147483648s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
378 len2 = sprintf (str2, "%-.2147483648s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
379 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
380 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
381 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
382
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
383 printf("test 6\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
384 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
385 len1 = snprintf(str1, sizeof(str1), "%.4294967295s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
386 len2 = sprintf (str2, "%.4294967295s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
387 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
388 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
389 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
390
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
391 printf("test 7\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
392 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
393 len1 = snprintf(str1, sizeof(str1), "%.4294967296s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
394 len2 = sprintf (str2, "%.4294967296s", "13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
395 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
396 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
397 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
398
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
399
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
400 printf("test 12\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
401 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
402 len1 = snprintf(str1, sizeof(str1), "%.*s", 2147483647,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
403 len2 = sprintf (str2, "%.*s", 2147483647,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
404 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
405 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
406 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
407
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
408 printf("test 13\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
409 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
410 len1 = snprintf(str1, sizeof(str1), "%.*s", 2147483648U,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
411 len2 = sprintf (str2, "%.*s", 2147483648U,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
412 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
413 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
414 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
415
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
416 printf("test 14\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
417 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
418 len1 = snprintf(str1, sizeof(str1), "%-.*s", 2147483647,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
419 len2 = sprintf (str2, "%-.*s", 2147483647,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
420 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
421 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
422 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
423
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
424 printf("test 15\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
425 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
426 len1 = snprintf(str1, sizeof(str1), "%-.*s", 2147483648U,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
427 len2 = sprintf (str2, "%-.*s", 2147483648U,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
428 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
429 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
430 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
431
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
432 printf("test 16\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
433 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
434 len1 = snprintf(str1, sizeof(str1), "%.*s", 4294967295U,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
435 len2 = sprintf (str2, "%.*s", 4294967295U,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
436 printf("len1=%d, len2=%d\n", len1,len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
437 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
438 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
439
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
440 printf("test 17\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
441 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
442 len1 = snprintf(str1, sizeof(str1), "%.*s", 4294967296U,"13");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
443 /* len2 = sprintf (str2, "%.*s", 4294967296U,"13"); */ /* core dumps on HPUX */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
444 /* assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
445 * assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
446 */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
447
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
448
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
449 printf("test 95\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
450 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
451 len1 = snprintf(str1, sizeof(str1), "%c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
452 len2 = sprintf (str2, "%c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
453 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
454 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
455
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
456 printf("test 96\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
457 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
458 len1 = snprintf(str1, sizeof(str1), "%10c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
459 len2 = sprintf (str2, "%10c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
460 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
461 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
462
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
463 printf("test 97\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
464 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
465 len1 = snprintf(str1, sizeof(str1), "%-10c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
466 len2 = sprintf (str2, "%-10c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
467 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
468 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
469
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
470 printf("test 98\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
471 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
472 len1 = snprintf(str1, sizeof(str1), "%.10c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
473 len2 = sprintf (str2, "%.10c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
474 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
475 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
476
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
477 printf("test 99\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
478 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
479 len1 = snprintf(str1, sizeof(str1), "%-.10c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
480 len2 = sprintf (str2, "%-.10c",'A');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
481 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
482 assert(memcmp(str1,str2,(size_t)len1) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
483
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
484
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
485 printf("test 100\n");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
486 memset(str1,'x',sizeof(str1)); memset(str2,'x',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
487 len1 = snprintf(str1, (size_t)8, "blaBhb%shehe%cX","ABCD",'1');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
488 len2 = sprintf (str2, "blaBhb%shehe%cX","ABCD",'1');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
489 assert(len1==len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
490 assert(memcmp(str1,str2,(size_t)7) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
491 assert(str1[7] == '\0');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
492 assert(memcmp(str1+14,str2+16,(size_t)(len1-16)) == 0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
493
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
494 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
495
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
496 /* testing for correctness and compatibility */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
497 if (argc >= 3) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
498 char *c; int alldigits = 1;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
499 for (c=argv[2]; *c; c++)
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
500 if (! (*c == '-' || (*c >= '0' && *c <= '9'))) alldigits = 0;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
501 if (alldigits) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
502 int j = atoi(argv[2]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
503 len1 = portable_snprintf(str1, str_m, argv[1], j, 3);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
504 len2 = sprintf(str2, argv[1], j, 3);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
505 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
506 len3 = snprintf(str3, str_m, argv[1], j, 3);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
507 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
508 } else {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
509 len1 = portable_snprintf(str1, str_m, argv[1], argv[2], 3);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
510 len2 = sprintf(str2, argv[1], argv[2], 3);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
511 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
512 len3 = snprintf(str3, str_m, argv[1], argv[2], 3);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
513 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
514 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
515 printf("portable: |%s| len = %d\n", str1, len1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
516 printf("sys sprintf: |%s| len = %d\n", str2, len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
517 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
518 printf("sys snprintf: |%s| len = %d\n", str3, len3);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
519 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
520 } else { /* exhaustive testing */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
521
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
522 const char flags[] = "+- 0#"; /* set of test flags (including '\0')*/
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
523 int flags_l = strlen(flags);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
524
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
525 const char fspec[] = "scdpoxXuiy"; /* set of test formats (including '\0') */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
526 int fspec_l = strlen(fspec);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
527
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
528 #ifdef SNPRINTF_LONGLONG_SUPPORT
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
529 const char datatype[] = " hl2"; /* set of datatypes */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
530 #else
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
531 const char datatype[] = " hl"; /* set of datatypes */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
532 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
533 int datatype_l = strlen(datatype);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
534
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
535 const long int iargs[] = /* set of numeric test arguments */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
536 { 0,1,9,10,28,99,100,127,128,129,998,1000,32767,32768,32769,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
537 -1,-9,-10,-28,-99,-100,-127,-128,-129,
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
538 -998,-1000,-32767,-32768,-32769 };
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
539 int iargs_l = sizeof(iargs)/sizeof(iargs[0]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
540 const char *sargs[] = /* set of string test arguments */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
541 { "", "a", "0", "-ab", "abcde", "abcdefghijk mnopqrstuv" };
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
542 int sargs_l = sizeof(sargs)/sizeof(sargs[0]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
543
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
544 char fmt[256];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
545 int fmt_l;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
546 int a, fs, fl1, fl2, fl3, fl4, fl5, fw, fp, dt;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
547
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
548 for (fs=0; fs<=fspec_l; fs++) { /* format specifier */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
549 int strtype = (fspec[fs] == 's' || fspec[fs] == '%');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
550 int args_l = (strtype ? sargs_l : iargs_l);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
551
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
552 for (fw= -1; fw<=3; fw++) { /* minimal field width */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
553
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
554 printf("Trying format %%");
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
555 if (fw >= 0) printf("%d", fw);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
556 if (fspec[fs]) putchar(fspec[fs]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
557 putchar('\n');
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
558
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
559 for (fp= -2; fp<=3; fp++) { /* format field precision */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
560
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
561 /* data type modifiers */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
562 for (dt=0; dt < ((strtype||fspec[fs]=='c') ? 1 : datatype_l); dt++) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
563
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
564 int dataty = datatype[dt];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
565
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
566 if (fspec[fs] == 'D' || fspec[fs] == 'U' || fspec[fs] == 'O')
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
567 dataty = 'l';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
568
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
569 if (fspec[fs] == 'p' && dataty == '2') continue;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
570
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
571 for (fl1=0; fl1<=flags_l; fl1++) { /* flags */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
572 for (fl2=0; fl2<=flags_l; fl2++) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
573 for (fl3=0; fl3<=flags_l; fl3++) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
574 for (fl4=0; fl4<=flags_l; fl4++) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
575 for (fl5=0; fl5<=flags_l; fl5++) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
576
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
577 for (a=0; a<args_l; a++) { /* test arguments */
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
578
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
579 fmt_l = 0; fmt[fmt_l++] = '%';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
580 if (flags[fl1]) fmt[fmt_l++] = flags[fl1];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
581 if (flags[fl2]) fmt[fmt_l++] = flags[fl2];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
582 if (flags[fl3]) fmt[fmt_l++] = flags[fl3];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
583 if (flags[fl4]) fmt[fmt_l++] = flags[fl4];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
584 if (flags[fl5]) fmt[fmt_l++] = flags[fl5];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
585 if (fw >= 0) fmt_l += sprintf(fmt+fmt_l, "%d", fw);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
586 if (fp >= -1) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
587 fmt[fmt_l++] = '.';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
588 if (fp >= 0) fmt_l += sprintf(fmt+fmt_l, "%d", fp);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
589 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
590 if (dataty == '2')
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
591 { fmt[fmt_l++] = 'l'; fmt[fmt_l++] = 'l'; }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
592 else if (dataty != ' ')
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
593 { fmt[fmt_l++] = dataty; }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
594
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
595 if (fspec[fs]) fmt[fmt_l++] = fspec[fs];
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
596 fmt[fmt_l++] = '\0';
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
597
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
598 if (a==0 && fl1==flags_l && fl2==flags_l && fl3==flags_l
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
599 && fl4==flags_l && fl5==flags_l) printf("%s\n", fmt);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
600
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
601 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
602 memset(str1,'G',sizeof(str1));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
603 memset(str2,'G',sizeof(str2));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
604 memset(str3,'G',sizeof(str3));
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
605 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
606 len1 = len2 = len3 = 0;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
607 if (strtype) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
608 len1 = portable_snprintf(str1, str_m, fmt, sargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
609 len2 = sprintf(str2, fmt, sargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
610 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
611 len3 = snprintf(str3, str_m, fmt, sargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
612 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
613 } else if (fspec[fs] == 'p') {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
614 len1 = portable_snprintf(str1, str_m, fmt, (void *)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
615 len2 = sprintf(str2, fmt, (void *)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
616 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
617 len3 = snprintf(str3, str_m, fmt, (void *)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
618 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
619 } else {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
620 switch (dataty) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
621 case '\0':
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
622 len1 = portable_snprintf(str1, str_m, fmt, (int) iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
623 len2 = sprintf(str2, fmt, (int) iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
624 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
625 len3 = snprintf(str3, str_m, fmt, (int) iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
626 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
627 break;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
628 case 'h':
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
629 len1 = portable_snprintf(str1, str_m, fmt, (short int)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
630 len2 = sprintf(str2, fmt, (short int)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
631 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
632 len3 = snprintf(str3, str_m, fmt, (short int)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
633 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
634 break;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
635 case 'l':
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
636 len1 = portable_snprintf(str1, str_m, fmt, (long int)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
637 len2 = sprintf(str2, fmt, (long int)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
638 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
639 len3 = snprintf(str3, str_m, fmt, (long int)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
640 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
641 break;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
642 #ifdef SNPRINTF_LONGLONG_SUPPORT
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
643 case '2':
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
644 len1 = portable_snprintf(str1, str_m, fmt, (long long int)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
645 len2 = sprintf(str2, fmt, (long long int)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
646 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
647 len3 = snprintf(str3, str_m, fmt, (long long int)iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
648 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
649 break;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
650 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
651 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
652 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
653
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
654 if (0) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
655 #ifdef HAVE_SNPRINTF
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
656 } else if (len1 != len3 ||
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
657 memcmp(str1,str3,min(len1+20,sizeof(str1))) != 0) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
658 bad = 1;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
659 if (strtype) printf("\n2: %s, <%s>\n", fmt, sargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
660 else printf("\n2: %s, %ld\n", fmt, iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
661 printf("portable: |%s| len = %d\n", str1, len1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
662 printf("sys sprintf: |%s| len = %d\n", str2, len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
663 printf("sys snprintf: |%s| len = %d\n", str3, len3);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
664 #else
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
665 } else if (len1 != len2 ||
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
666 (len1>0 && memcmp(str1,str2,min(len1,str_m)-1) != 0)) {
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
667 bad = 1;
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
668 if (strtype) printf("\n1: %s, <%s>\n", fmt, sargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
669 else printf("\n1: %s, %ld\n", fmt, iargs[a]);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
670 printf("portable: |%s| len = %d\n", str1, len1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
671 printf("sys sprintf: |%s| len = %d\n", str2, len2);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
672 #endif
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
673 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
674 if (bad) return(1);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
675 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
676
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
677 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
678 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
679 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
680 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
681
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
682 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
683 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
684 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
685 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
686 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
687 }
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
688 return (bad?1:0);
5a71d53d0228 trunk: changeset 38
tuomov
parents:
diff changeset
689 }

mercurial