PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/selp_glibc-2.5.90-19.0.46/stdio-common/tst-printf.c

https://bitbucket.org/Mali_Laurent/ps50c550
C | 392 lines | 308 code | 48 blank | 36 comment | 60 complexity | a3e0c8b615976b23b233ebe626a8927d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, AGPL-1.0, CC-BY-SA-3.0, BSD-3-Clause
  1. /* Copyright (C) 1991,92,93,95,96,97,98,99, 2000, 2002, 2006
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C 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.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifdef BSD
  17. #include </usr/include/stdio.h>
  18. #define EXIT_SUCCESS 0
  19. #else
  20. #include <limits.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #endif
  25. #include <float.h>
  26. static void rfg1 (void);
  27. static void rfg2 (void);
  28. static void rfg3 (void);
  29. static void
  30. fmtchk (const char *fmt)
  31. {
  32. (void) fputs(fmt, stdout);
  33. (void) printf(":\t`");
  34. (void) printf(fmt, 0x12);
  35. (void) printf("'\n");
  36. }
  37. static void
  38. fmtst1chk (const char *fmt)
  39. {
  40. (void) fputs(fmt, stdout);
  41. (void) printf(":\t`");
  42. (void) printf(fmt, 4, 0x12);
  43. (void) printf("'\n");
  44. }
  45. static void
  46. fmtst2chk (const char *fmt)
  47. {
  48. (void) fputs(fmt, stdout);
  49. (void) printf(":\t`");
  50. (void) printf(fmt, 4, 4, 0x12);
  51. (void) printf("'\n");
  52. }
  53. /* This page is covered by the following copyright: */
  54. /* (C) Copyright C E Chew
  55. *
  56. * Feel free to copy, use and distribute this software provided:
  57. *
  58. * 1. you do not pretend that you wrote it
  59. * 2. you leave this copyright notice intact.
  60. */
  61. /*
  62. * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
  63. */
  64. #define DEC -123
  65. #define INT 255
  66. #define UNS (~0)
  67. /* Formatted Output Test
  68. *
  69. * This exercises the output formatting code.
  70. */
  71. static void
  72. fp_test (void)
  73. {
  74. int i, j, k, l;
  75. char buf[7];
  76. char *prefix = buf;
  77. char tp[20];
  78. puts("\nFormatted output test");
  79. printf("prefix 6d 6o 6x 6X 6u\n");
  80. strcpy(prefix, "%");
  81. for (i = 0; i < 2; i++) {
  82. for (j = 0; j < 2; j++) {
  83. for (k = 0; k < 2; k++) {
  84. for (l = 0; l < 2; l++) {
  85. strcpy(prefix, "%");
  86. if (i == 0) strcat(prefix, "-");
  87. if (j == 0) strcat(prefix, "+");
  88. if (k == 0) strcat(prefix, "#");
  89. if (l == 0) strcat(prefix, "0");
  90. printf("%5s |", prefix);
  91. strcpy(tp, prefix);
  92. strcat(tp, "6d |");
  93. printf(tp, DEC);
  94. strcpy(tp, prefix);
  95. strcat(tp, "6o |");
  96. printf(tp, INT);
  97. strcpy(tp, prefix);
  98. strcat(tp, "6x |");
  99. printf(tp, INT);
  100. strcpy(tp, prefix);
  101. strcat(tp, "6X |");
  102. printf(tp, INT);
  103. strcpy(tp, prefix);
  104. strcat(tp, "6u |");
  105. printf(tp, UNS);
  106. printf("\n");
  107. }
  108. }
  109. }
  110. }
  111. printf("%10s\n", (char *) NULL);
  112. printf("%-10s\n", (char *) NULL);
  113. }
  114. int
  115. main (int argc, char *argv[])
  116. {
  117. static char shortstr[] = "Hi, Z.";
  118. static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
  119. I am ready for my first lesson today.";
  120. int result = 0;
  121. fmtchk("%.4x");
  122. fmtchk("%04x");
  123. fmtchk("%4.4x");
  124. fmtchk("%04.4x");
  125. fmtchk("%4.3x");
  126. fmtchk("%04.3x");
  127. fmtst1chk("%.*x");
  128. fmtst1chk("%0*x");
  129. fmtst2chk("%*.*x");
  130. fmtst2chk("%0*.*x");
  131. #ifndef BSD
  132. printf("bad format:\t\"%b\"\n");
  133. printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
  134. #endif
  135. printf("decimal negative:\t\"%d\"\n", -2345);
  136. printf("octal negative:\t\"%o\"\n", -2345);
  137. printf("hex negative:\t\"%x\"\n", -2345);
  138. printf("long decimal number:\t\"%ld\"\n", -123456L);
  139. printf("long octal negative:\t\"%lo\"\n", -2345L);
  140. printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
  141. printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
  142. printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
  143. printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
  144. printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
  145. printf("zero-padded string:\t\"%010s\"\n", shortstr);
  146. printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
  147. printf("space-padded string:\t\"%10s\"\n", shortstr);
  148. printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
  149. printf("null string:\t\"%s\"\n", (char *)NULL);
  150. printf("limited string:\t\"%.22s\"\n", longstr);
  151. printf("e-style >= 1:\t\"%e\"\n", 12.34);
  152. printf("e-style >= .1:\t\"%e\"\n", 0.1234);
  153. printf("e-style < .1:\t\"%e\"\n", 0.001234);
  154. printf("e-style big:\t\"%.60e\"\n", 1e20);
  155. printf ("e-style == .1:\t\"%e\"\n", 0.1);
  156. printf("f-style >= 1:\t\"%f\"\n", 12.34);
  157. printf("f-style >= .1:\t\"%f\"\n", 0.1234);
  158. printf("f-style < .1:\t\"%f\"\n", 0.001234);
  159. printf("g-style >= 1:\t\"%g\"\n", 12.34);
  160. printf("g-style >= .1:\t\"%g\"\n", 0.1234);
  161. printf("g-style < .1:\t\"%g\"\n", 0.001234);
  162. printf("g-style big:\t\"%.60g\"\n", 1e20);
  163. printf (" %6.5f\n", .099999999860301614);
  164. printf (" %6.5f\n", .1);
  165. printf ("x%5.4fx\n", .5);
  166. printf ("%#03x\n", 1);
  167. printf ("something really insane: %.10000f\n", 1.0);
  168. {
  169. double d = FLT_MIN;
  170. int niter = 17;
  171. while (niter-- != 0)
  172. printf ("%.17e\n", d / 2);
  173. fflush (stdout);
  174. }
  175. printf ("%15.5e\n", 4.9406564584124654e-324);
  176. #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
  177. printf (FORMAT, 0.0, 0.0, 0.0);
  178. printf (FORMAT, 1.0, 1.0, 1.0);
  179. printf (FORMAT, -1.0, -1.0, -1.0);
  180. printf (FORMAT, 100.0, 100.0, 100.0);
  181. printf (FORMAT, 1000.0, 1000.0, 1000.0);
  182. printf (FORMAT, 10000.0, 10000.0, 10000.0);
  183. printf (FORMAT, 12345.0, 12345.0, 12345.0);
  184. printf (FORMAT, 100000.0, 100000.0, 100000.0);
  185. printf (FORMAT, 123456.0, 123456.0, 123456.0);
  186. #undef FORMAT
  187. {
  188. char buf[20];
  189. char buf2[512];
  190. printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
  191. snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
  192. buf);
  193. printf ("snprintf (\"%%.999999u\", 10) == %d\n",
  194. snprintf(buf2, sizeof(buf2), "%.999999u", 10));
  195. }
  196. fp_test ();
  197. printf ("%e should be 1.234568e+06\n", 1234567.8);
  198. printf ("%f should be 1234567.800000\n", 1234567.8);
  199. printf ("%g should be 1.23457e+06\n", 1234567.8);
  200. printf ("%g should be 123.456\n", 123.456);
  201. printf ("%g should be 1e+06\n", 1000000.0);
  202. printf ("%g should be 10\n", 10.0);
  203. printf ("%g should be 0.02\n", 0.02);
  204. #if 0
  205. /* This test rather checks the way the compiler handles constant
  206. folding. gcc behavior wrt to this changed in 3.2 so it is not a
  207. portable test. */
  208. {
  209. double x=1.0;
  210. printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
  211. }
  212. #endif
  213. {
  214. char buf[200];
  215. sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
  216. result |= strcmp (buf,
  217. "onetwo three ");
  218. puts (result != 0 ? "Test failed!" : "Test ok.");
  219. }
  220. {
  221. char buf[200];
  222. sprintf (buf, "%07Lo", 040000000000ll);
  223. printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf);
  224. if (strcmp (buf, "40000000000") != 0)
  225. {
  226. result = 1;
  227. fputs ("\tFAILED", stdout);
  228. }
  229. puts ("");
  230. }
  231. printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
  232. printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
  233. printf ("printf (\"%%hhi\", %i) = %hhi\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
  234. printf ("printf (\"%%hi\", %i) = %hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
  235. printf ("printf (\"%%1$hhu\", %2$u) = %1$hhu\n",
  236. UCHAR_MAX + 2, UCHAR_MAX + 2);
  237. printf ("printf (\"%%1$hu\", %2$u) = %1$hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
  238. printf ("printf (\"%%1$hhi\", %2$i) = %1$hhi\n",
  239. UCHAR_MAX + 2, UCHAR_MAX + 2);
  240. printf ("printf (\"%%1$hi\", %2$i) = %1$hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
  241. puts ("--- Should be no further output. ---");
  242. rfg1 ();
  243. rfg2 ();
  244. rfg3 ();
  245. {
  246. char bytes[7];
  247. char buf[20];
  248. memset (bytes, '\xff', sizeof bytes);
  249. sprintf (buf, "foo%hhn\n", &bytes[3]);
  250. if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
  251. || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
  252. {
  253. puts ("%hhn overwrite more bytes");
  254. result = 1;
  255. }
  256. if (bytes[3] != 3)
  257. {
  258. puts ("%hhn wrote incorrect value");
  259. result = 1;
  260. }
  261. }
  262. return result != 0;
  263. }
  264. static void
  265. rfg1 (void)
  266. {
  267. char buf[100];
  268. sprintf (buf, "%5.s", "xyz");
  269. if (strcmp (buf, " ") != 0)
  270. printf ("got: '%s', expected: '%s'\n", buf, " ");
  271. sprintf (buf, "%5.f", 33.3);
  272. if (strcmp (buf, " 33") != 0)
  273. printf ("got: '%s', expected: '%s'\n", buf, " 33");
  274. sprintf (buf, "%8.e", 33.3e7);
  275. if (strcmp (buf, " 3e+08") != 0)
  276. printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
  277. sprintf (buf, "%8.E", 33.3e7);
  278. if (strcmp (buf, " 3E+08") != 0)
  279. printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
  280. sprintf (buf, "%.g", 33.3);
  281. if (strcmp (buf, "3e+01") != 0)
  282. printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
  283. sprintf (buf, "%.G", 33.3);
  284. if (strcmp (buf, "3E+01") != 0)
  285. printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
  286. }
  287. static void
  288. rfg2 (void)
  289. {
  290. int prec;
  291. char buf[100];
  292. prec = 0;
  293. sprintf (buf, "%.*g", prec, 3.3);
  294. if (strcmp (buf, "3") != 0)
  295. printf ("got: '%s', expected: '%s'\n", buf, "3");
  296. prec = 0;
  297. sprintf (buf, "%.*G", prec, 3.3);
  298. if (strcmp (buf, "3") != 0)
  299. printf ("got: '%s', expected: '%s'\n", buf, "3");
  300. prec = 0;
  301. sprintf (buf, "%7.*G", prec, 3.33);
  302. if (strcmp (buf, " 3") != 0)
  303. printf ("got: '%s', expected: '%s'\n", buf, " 3");
  304. prec = 3;
  305. sprintf (buf, "%04.*o", prec, 33);
  306. if (strcmp (buf, " 041") != 0)
  307. printf ("got: '%s', expected: '%s'\n", buf, " 041");
  308. prec = 7;
  309. sprintf (buf, "%09.*u", prec, 33);
  310. if (strcmp (buf, " 0000033") != 0)
  311. printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
  312. prec = 3;
  313. sprintf (buf, "%04.*x", prec, 33);
  314. if (strcmp (buf, " 021") != 0)
  315. printf ("got: '%s', expected: '%s'\n", buf, " 021");
  316. prec = 3;
  317. sprintf (buf, "%04.*X", prec, 33);
  318. if (strcmp (buf, " 021") != 0)
  319. printf ("got: '%s', expected: '%s'\n", buf, " 021");
  320. }
  321. static void
  322. rfg3 (void)
  323. {
  324. char buf[100];
  325. double g = 5.0000001;
  326. unsigned long l = 1234567890;
  327. double d = 321.7654321;
  328. const char s[] = "test-string";
  329. int i = 12345;
  330. int h = 1234;
  331. sprintf (buf,
  332. "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
  333. i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
  334. if (strcmp (buf,
  335. " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string") != 0)
  336. printf ("got: '%s', expected: '%s'\n", buf,
  337. " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string");
  338. }