|
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * |
|
4 * This library is free software; you can redistribute it and/or |
|
5 * modify it under the terms of the GNU Lesser General Public |
|
6 * License as published by the Free Software Foundation; either |
|
7 * version 2 of the License, or (at your option) any later version. |
|
8 * |
|
9 * This library 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 GNU |
|
12 * Lesser General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Lesser General Public |
|
15 * License along with this library; if not, write to the |
|
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
17 * Boston, MA 02111-1307, USA. |
|
18 */ |
|
19 |
|
20 /* |
|
21 * Modified by the GLib Team 2003. See the AUTHORS |
|
22 * file for a list of people on the GLib Team. See the ChangeLog |
|
23 * files for a list of changes. These files are distributed with |
|
24 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
25 */ |
|
26 |
|
27 #include "glib.h" |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <string.h> |
|
31 |
|
32 #ifdef SYMBIAN |
|
33 #include "mrt2_glib2_test.h" |
|
34 #endif /*SYMBIAN*/ |
|
35 |
|
36 |
|
37 static gboolean any_failed = FALSE; |
|
38 static gboolean failed = FALSE; |
|
39 |
|
40 #define TEST(message,cond) G_STMT_START { failed = !(cond); \ |
|
41 if (failed) \ |
|
42 { assert_failed = TRUE; \ |
|
43 if (!message) \ |
|
44 g_print ("(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ |
|
45 else \ |
|
46 g_print ("(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), message ? (gchar*)message : ""); \ |
|
47 fflush (stdout); \ |
|
48 any_failed = TRUE; \ |
|
49 } \ |
|
50 } G_STMT_END |
|
51 |
|
52 #define TEST_FAILED(message) \ |
|
53 G_STMT_START { g_print ("Error: "); g_print message; g_print ("\n"); any_failed = TRUE; } G_STMT_END |
|
54 |
|
55 static gboolean |
|
56 same_value (const gchar *actual, |
|
57 const gchar *expected) |
|
58 { |
|
59 gdouble actual_value, expected_value; |
|
60 |
|
61 actual_value = g_ascii_strtod (actual, NULL); |
|
62 expected_value = g_ascii_strtod (expected, NULL); |
|
63 |
|
64 return actual_value == expected_value; |
|
65 } |
|
66 |
|
67 int |
|
68 main (int argc, |
|
69 char *argv[]) |
|
70 { |
|
71 gchar buf[128]; |
|
72 int i; |
|
73 long l; |
|
74 |
|
75 #ifdef SYMBIAN |
|
76 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
77 g_set_print_handler(mrtPrintHandler); |
|
78 #endif /*SYMBIAN*/ |
|
79 |
|
80 |
|
81 /* truncation and return value */ |
|
82 TEST (NULL, g_snprintf (buf, 0, "abc") == 3); |
|
83 TEST (NULL, g_snprintf (NULL, 0, "abc") == 3); |
|
84 TEST (NULL, g_snprintf (buf, 5, "abc") == 3); |
|
85 TEST (NULL, g_snprintf (buf, 1, "abc") == 3 && buf[0] == '\0' && !strcmp (buf, "")); |
|
86 TEST (NULL, g_snprintf (buf, 2, "abc") == 3 && buf[1] == '\0' && !strcmp (buf, "a")); |
|
87 TEST (NULL, g_snprintf (buf, 3, "abc") == 3 && buf[2] == '\0' && !strcmp (buf, "ab")); |
|
88 TEST (NULL, g_snprintf (buf, 4, "abc") == 3 && buf[3] == '\0' && !strcmp (buf, "abc")); |
|
89 TEST (NULL, g_snprintf (buf, 5, "abc") == 3 && buf[3] == '\0' && !strcmp (buf, "abc")); |
|
90 |
|
91 /* %d, basic formatting */ |
|
92 TEST (NULL, g_snprintf (buf, 128, "%d", 5) == 1 && !strcmp (buf, "5")); |
|
93 TEST (NULL, g_snprintf (buf, 128, "%d", 0) == 1 && !strcmp (buf, "0")); |
|
94 TEST (NULL, g_snprintf (buf, 128, "%.0d", 0) == 0 && !strcmp (buf, "")); |
|
95 TEST (NULL, g_snprintf (buf, 128, "%.0d", 1) == 1 && !strcmp (buf, "1")); |
|
96 TEST (NULL, g_snprintf (buf, 128, "%.d", 2) == 1 && !strcmp (buf, "2")); |
|
97 TEST (NULL, g_snprintf (buf, 128, "%d", -1) == 2 && !strcmp (buf, "-1")); |
|
98 TEST (NULL, g_snprintf (buf, 128, "%.3d", 5) == 3 && !strcmp (buf, "005")); |
|
99 TEST (NULL, g_snprintf (buf, 128, "%.3d", -5) == 4 && !strcmp (buf, "-005")); |
|
100 TEST (NULL, g_snprintf (buf, 128, "%5.3d", 5) == 5 && !strcmp (buf, " 005")); |
|
101 TEST (NULL, g_snprintf (buf, 128, "%-5.3d", -5) == 5 && !strcmp (buf, "-005 ")); |
|
102 /* %d, length modifiers */ |
|
103 TEST (NULL, g_snprintf (buf, 128, "%" G_GINT16_FORMAT, (gint16)-5) == 2 && !strcmp (buf, "-5")); |
|
104 TEST (NULL, g_snprintf (buf, 128, "%" G_GUINT16_FORMAT, (guint16)5) == 1 && !strcmp (buf, "5")); |
|
105 TEST (NULL, g_snprintf (buf, 128, "%" G_GINT32_FORMAT, (gint32)-5) == 2 && !strcmp (buf, "-5")); |
|
106 TEST (NULL, g_snprintf (buf, 128, "%" G_GUINT32_FORMAT, (guint32)5) == 1 && !strcmp (buf, "5")); |
|
107 TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-5) == 2 && !strcmp (buf, "-5")); |
|
108 TEST (NULL, g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)5) == 1 && !strcmp (buf, "5")); |
|
109 TEST (NULL, g_snprintf (buf, 128, "%" G_GSSIZE_FORMAT, (gssize)-5) == 2 && !strcmp (buf, "-5")); |
|
110 TEST (NULL, g_snprintf (buf, 128, "%" G_GSIZE_FORMAT, (gsize)5) == 1 && !strcmp (buf, "5")); |
|
111 /* %d, flags */ |
|
112 TEST (NULL, g_snprintf (buf, 128, "%-d", 5) == 1 && !strcmp (buf, "5")); |
|
113 TEST (NULL, g_snprintf (buf, 128, "%-+d", 5) == 2 && !strcmp (buf, "+5")); |
|
114 TEST (NULL, g_snprintf (buf, 128, "%+-d", 5) == 2 && !strcmp (buf, "+5")); |
|
115 TEST (NULL, g_snprintf (buf, 128, "%+d", -5) == 2 && !strcmp (buf, "-5")); |
|
116 TEST (NULL, g_snprintf (buf, 128, "% d", 5) == 2 && !strcmp (buf, " 5")); |
|
117 TEST (NULL, g_snprintf (buf, 128, "% .0d", 0) == 1 && !strcmp (buf, " ")); |
|
118 TEST (NULL, g_snprintf (buf, 128, "% +d", 5) == 2 && !strcmp (buf, "+5")); |
|
119 TEST (NULL, g_snprintf (buf, 128, "%03d", 5) == 3 && !strcmp (buf, "005")); |
|
120 TEST (NULL, g_snprintf (buf, 128, "%-03d", -5) == 3 && !strcmp (buf, "-5 ")); |
|
121 TEST (NULL, g_snprintf (buf, 128, "%03d", -5) == 3 && !strcmp (buf, "-05")); |
|
122 |
|
123 /* %o, basic formatting */ |
|
124 TEST (NULL, g_snprintf (buf, 128, "%o", 5) == 1 && !strcmp (buf, "5")); |
|
125 TEST (NULL, g_snprintf (buf, 128, "%o", 8) == 2 && !strcmp (buf, "10")); |
|
126 TEST (NULL, g_snprintf (buf, 128, "%o", 0) == 1 && !strcmp (buf, "0")); |
|
127 TEST (NULL, g_snprintf (buf, 128, "%.0o", 0) == 0 && !strcmp (buf, "")); |
|
128 TEST (NULL, g_snprintf (buf, 128, "%.0o", 1) == 1 && !strcmp (buf, "1")); |
|
129 TEST (NULL, g_snprintf (buf, 128, "%.3o", 5) == 3 && !strcmp (buf, "005")); |
|
130 TEST (NULL, g_snprintf (buf, 128, "%.3o", 8) == 3 && !strcmp (buf, "010")); |
|
131 TEST (NULL, g_snprintf (buf, 128, "%5.3o", 5) == 5 && !strcmp (buf, " 005")); |
|
132 |
|
133 /* %u, basic formatting */ |
|
134 TEST (NULL, g_snprintf (buf, 128, "%u", 5) == 1 && !strcmp (buf, "5")); |
|
135 TEST (NULL, g_snprintf (buf, 128, "%u", 0) == 1 && !strcmp (buf, "0")); |
|
136 TEST (NULL, g_snprintf (buf, 128, "%.0u", 0) == 0 && !strcmp (buf, "")); |
|
137 TEST (NULL, g_snprintf (buf, 128, "%.0u", 1) == 1 && !strcmp (buf, "1")); |
|
138 TEST (NULL, g_snprintf (buf, 128, "%.3u", 5) == 3 && !strcmp (buf, "005")); |
|
139 TEST (NULL, g_snprintf (buf, 128, "%5.3u", 5) == 5 && !strcmp (buf, " 005")); |
|
140 |
|
141 /* %x, basic formatting */ |
|
142 TEST (NULL, g_snprintf (buf, 128, "%x", 5) == 1 && !strcmp (buf, "5")); |
|
143 TEST (buf, g_snprintf (buf, 128, "%x", 31) == 2 && !strcmp (buf, "1f")); |
|
144 TEST (NULL, g_snprintf (buf, 128, "%x", 0) == 1 && !strcmp (buf, "0")); |
|
145 TEST (NULL, g_snprintf (buf, 128, "%.0x", 0) == 0 && !strcmp (buf, "")); |
|
146 TEST (NULL, g_snprintf (buf, 128, "%.0x", 1) == 1 && !strcmp (buf, "1")); |
|
147 TEST (NULL, g_snprintf (buf, 128, "%.3x", 5) == 3 && !strcmp (buf, "005")); |
|
148 TEST (NULL, g_snprintf (buf, 128, "%.3x", 31) == 3 && !strcmp (buf, "01f")); |
|
149 TEST (NULL, g_snprintf (buf, 128, "%5.3x", 5) == 5 && !strcmp (buf, " 005")); |
|
150 /* %x, flags */ |
|
151 TEST (NULL, g_snprintf (buf, 128, "%-x", 5) == 1 && !strcmp (buf, "5")); |
|
152 TEST (NULL, g_snprintf (buf, 128, "%03x", 5) == 3 && !strcmp (buf, "005")); |
|
153 TEST (NULL, g_snprintf (buf, 128, "%#x", 31) == 4 && !strcmp (buf, "0x1f")); |
|
154 TEST (NULL, g_snprintf (buf, 128, "%#x", 0) == 1 && !strcmp (buf, "0")); |
|
155 |
|
156 /* %X, basic formatting */ |
|
157 TEST (NULL, g_snprintf (buf, 128, "%X", 5) == 1 && !strcmp (buf, "5")); |
|
158 TEST (buf, g_snprintf (buf, 128, "%X", 31) == 2 && !strcmp (buf, "1F")); |
|
159 TEST (NULL, g_snprintf (buf, 128, "%X", 0) == 1 && !strcmp (buf, "0")); |
|
160 TEST (NULL, g_snprintf (buf, 128, "%.0X", 0) == 0 && !strcmp (buf, "")); |
|
161 TEST (NULL, g_snprintf (buf, 128, "%.0X", 1) == 1 && !strcmp (buf, "1")); |
|
162 TEST (NULL, g_snprintf (buf, 128, "%.3X", 5) == 3 && !strcmp (buf, "005")); |
|
163 TEST (NULL, g_snprintf (buf, 128, "%.3X", 31) == 3 && !strcmp (buf, "01F")); |
|
164 TEST (NULL, g_snprintf (buf, 128, "%5.3X", 5) == 5 && !strcmp (buf, " 005")); |
|
165 /* %X, flags */ |
|
166 TEST (NULL, g_snprintf (buf, 128, "%-X", 5) == 1 && !strcmp (buf, "5")); |
|
167 TEST (NULL, g_snprintf (buf, 128, "%03X", 5) == 3 && !strcmp (buf, "005")); |
|
168 TEST (NULL, g_snprintf (buf, 128, "%#X", 31) == 4 && !strcmp (buf, "0X1F")); |
|
169 TEST (NULL, g_snprintf (buf, 128, "%#X", 0) == 1 && !strcmp (buf, "0")); |
|
170 |
|
171 /* %f, basic formattting */ |
|
172 TEST (NULL, g_snprintf (buf, 128, "%f", G_PI) == 8 && !strncmp (buf, "3.14159", 7)); |
|
173 TEST (NULL, g_snprintf (buf, 128, "%.8f", G_PI) == 10 && !strncmp (buf, "3.1415926", 9)); |
|
174 TEST (NULL, g_snprintf (buf, 128, "%.0f", G_PI) == 1 && !strcmp (buf, "3")); |
|
175 TEST (NULL, g_snprintf (buf, 128, "%1.f", G_PI) == 1 && !strcmp (buf, "3")); |
|
176 TEST (NULL, g_snprintf (buf, 128, "%3.f", G_PI) == 3 && !strcmp (buf, " 3")); |
|
177 /* %f, flags */ |
|
178 TEST (NULL, g_snprintf (buf, 128, "%+f", G_PI) == 9 && !strncmp (buf, "+3.14159", 8)); |
|
179 TEST (NULL, g_snprintf (buf, 128, "% f", G_PI) == 9 && !strncmp (buf, " 3.14159", 8)); |
|
180 TEST (NULL, g_snprintf (buf, 128, "%#.0f", G_PI) == 2 && !strcmp (buf, "3.")); |
|
181 TEST (NULL, g_snprintf (buf, 128, "%05.2f", G_PI) == 5 && !strcmp (buf, "03.14")); |
|
182 |
|
183 /* %e, basic formatting */ |
|
184 /* for %e we can't expect to reproduce exact strings and lengths, since SUS |
|
185 * only guarantees that the exponent shall always contain at least two |
|
186 * digits. On Windows, it seems to be at least three digits long. |
|
187 * Therefore, we compare the results of parsing the expected result and the |
|
188 * actual result. |
|
189 */ |
|
190 TEST (buf, g_snprintf (buf, 128, "%e", G_PI) >= 12 && same_value (buf, "3.141593e+00")); |
|
191 TEST (buf, g_snprintf (buf, 128, "%.8e", G_PI) >= 14 && same_value (buf, "3.14159265e+00")); |
|
192 TEST (buf, g_snprintf (buf, 128, "%.0e", G_PI) >= 5 && same_value (buf, "3e+00")); |
|
193 TEST (buf, g_snprintf (buf, 128, "%.1e", 0.0) >= 7 && same_value (buf, "0.0e+00")); |
|
194 TEST (buf, g_snprintf (buf, 128, "%.1e", 0.00001) >= 7 && same_value (buf, "1.0e-05")); |
|
195 TEST (buf, g_snprintf (buf, 128, "%.1e", 10000.0) >= 7 && same_value (buf, "1.0e+04")); |
|
196 /* %e, flags */ |
|
197 TEST (buf, g_snprintf (buf, 128, "%+e", G_PI) >= 13 && same_value (buf, "+3.141593e+00")); |
|
198 TEST (buf, g_snprintf (buf, 128, "% e", G_PI) >= 13 && same_value (buf, " 3.141593e+00")); |
|
199 TEST (buf, g_snprintf (buf, 128, "%#.0e", G_PI) >= 6 && same_value (buf, "3.e+00")); |
|
200 TEST (buf, g_snprintf (buf, 128, "%09.2e", G_PI) >= 9 && same_value (buf, "03.14e+00")); |
|
201 |
|
202 /* %c */ |
|
203 TEST (NULL, g_snprintf (buf, 128, "%c", 'a') == 1 && !strcmp (buf, "a")); |
|
204 |
|
205 /* %s */ |
|
206 TEST (NULL, g_snprintf (buf, 128, "%.2s", "abc") == 2 && !strcmp (buf, "ab")); |
|
207 TEST (NULL, g_snprintf (buf, 128, "%.6s", "abc") == 3 && !strcmp (buf, "abc")); |
|
208 TEST (NULL, g_snprintf (buf, 128, "%5s", "abc") == 5 && !strcmp (buf, " abc")); |
|
209 TEST (NULL, g_snprintf (buf, 128, "%-5s", "abc") == 5 && !strcmp (buf, "abc ")); |
|
210 TEST (NULL, g_snprintf (buf, 128, "%5.2s", "abc") == 5 && !strcmp (buf, " ab")); |
|
211 TEST (NULL, g_snprintf (buf, 128, "%*s", 5, "abc") == 5 && !strcmp (buf, " abc")); |
|
212 #if 0 /* HP-UX doesn't get this right */ |
|
213 TEST (NULL, g_snprintf (buf, 128, "%*s", -5, "abc") == 5 && !strcmp (buf, "abc ")); |
|
214 #endif |
|
215 TEST (NULL, g_snprintf (buf, 128, "%*.*s", 5, 2, "abc") == 5 && !strcmp (buf, " ab")); |
|
216 |
|
217 /* %n */ |
|
218 TEST (NULL, g_snprintf (buf, 128, "abc%n", &i) == 3 && !strcmp (buf, "abc") && i == 3); |
|
219 TEST (NULL, g_snprintf (buf, 128, "abc%ln", &l) == 3 && !strcmp (buf, "abc") && l == 3); |
|
220 |
|
221 /* %% */ |
|
222 TEST (NULL, g_snprintf (buf, 128, "%%") == 1 && !strcmp (buf, "%")); |
|
223 |
|
224 /* positional parameters */ |
|
225 TEST (NULL, g_snprintf (buf, 128, "%2$c %1$c", 'b', 'a') == 3 && !strcmp (buf, "a b")); |
|
226 TEST (NULL, g_snprintf (buf, 128, "%1$*2$.*3$s", "abc", 5, 2) == 5 && !strcmp (buf, " ab")); |
|
227 TEST (NULL, g_snprintf (buf, 128, "%1$s%1$s", "abc") == 6 && !strcmp (buf, "abcabc")); |
|
228 |
|
229 /* 64 bit support */ |
|
230 TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)123456) == 6 && !strcmp (buf, "123456")); |
|
231 TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-123456) == 7 && !strcmp (buf, "-123456")); |
|
232 TEST (NULL, g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)123456) == 6 && !strcmp (buf, "123456")); |
|
233 TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "o", (gint64)123456) == 6 && !strcmp (buf, "361100")); |
|
234 TEST (NULL, g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "o", (gint64)123456) == 7 && !strcmp (buf, "0361100")); |
|
235 TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "x", (gint64)123456) == 5 && !strcmp (buf, "1e240")); |
|
236 TEST (NULL, g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "x", (gint64)123456) == 7 && !strcmp (buf, "0x1e240")); |
|
237 TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "X", (gint64)123456) == 5 && !strcmp (buf, "1E240")); |
|
238 #ifdef G_OS_WIN32 |
|
239 /* On Win32, test that the "ll" modifier also works, for backward |
|
240 * compatibility. One really should use the G_GINT64_MODIFIER (which |
|
241 * on Win32 is the "I64" that the (msvcrt) C library's printf uses), |
|
242 * but "ll" used to work with the "trio" g_printf implementation in |
|
243 * GLib 2.2, so it's best if it continues to work. |
|
244 */ |
|
245 TEST (NULL, g_snprintf (buf, 128, "%" "lli", (gint64)123456) == 6 && !strcmp (buf, "123456")); |
|
246 TEST (NULL, g_snprintf (buf, 128, "%" "lli", (gint64)-123456) == 7 && !strcmp (buf, "-123456")); |
|
247 TEST (NULL, g_snprintf (buf, 128, "%" "llu", (guint64)123456) == 6 && !strcmp (buf, "123456")); |
|
248 TEST (NULL, g_snprintf (buf, 128, "%" "ll" "o", (gint64)123456) == 6 && !strcmp (buf, "361100")); |
|
249 TEST (NULL, g_snprintf (buf, 128, "%#" "ll" "o", (gint64)123456) == 7 && !strcmp (buf, "0361100")); |
|
250 TEST (NULL, g_snprintf (buf, 128, "%" "ll" "x", (gint64)123456) == 5 && !strcmp (buf, "1e240")); |
|
251 TEST (NULL, g_snprintf (buf, 128, "%#" "ll" "x", (gint64)123456) == 7 && !strcmp (buf, "0x1e240")); |
|
252 TEST (NULL, g_snprintf (buf, 128, "%" "ll" "X", (gint64)123456) == 5 && !strcmp (buf, "1E240")); |
|
253 #endif |
|
254 #ifdef SYMBIAN |
|
255 testResultXml("printf-test"); |
|
256 #endif /* EMULATOR */ |
|
257 return any_failed; |
|
258 } |