Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
8.0 KiB

  1. /***
  2. *test_out.c - test output.c
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * This file contains a small test suite for output.c. Although
  8. * it is certainly not comprehensive, it should catch most major bugs.
  9. *
  10. *Revision History:
  11. * 06-05-89 PHG Module created
  12. * 03-19-90 GJF Fixed copyright.
  13. * 10-03-90 GJF New-style function declarator.
  14. * 06-08-92 KRS Updated for 32-bit sizes and wchar_t specifiers.
  15. * 02-24-95 GJF Appended Mac version (probably just an old version)
  16. * 05-17-99 PML Remove all Macintosh support.
  17. *
  18. *******************************************************************************/
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdarg.h>
  22. void test (
  23. char *shouldbe,
  24. char *format,
  25. ...
  26. )
  27. {
  28. va_list argptr;
  29. char buffer[500];
  30. va_start(argptr, format);
  31. vsprintf(buffer, format, argptr);
  32. if (strcmp(shouldbe, buffer) != 0)
  33. printf("Was: \"%s\"\nShould be: \"%s\"\n", buffer, shouldbe);
  34. }
  35. main()
  36. {
  37. int i;
  38. long l;
  39. /* normal */
  40. test("Hello, world!", "Hello, world!");
  41. /* test %s */
  42. test("Hello, world!", "%s", "Hello, world!");
  43. test("Hello, world!", "%.20s", "Hello, world!");
  44. test("Hello", "%.5s", "Hello, world!");
  45. test("", "%.s", "Hello, world!");
  46. test("(null)", "%s", NULL);
  47. test(" Hello, world!", "%20s", "Hello, world!");
  48. test("Hello, world! ", "%-20s", "Hello, world!");
  49. test(" Hello", "%20.5s", "Hello, world!");
  50. test("Hello ", "%-20.5s", "Hello, world!");
  51. test("Hello", "%3.5s", "Hello, world!");
  52. test("Hello", "%-3.5s", "Hello, world!");
  53. test("Hello, world!", "%-8s", "Hello, world!");
  54. test("Hello, world!", "%8s", "Hello, world!");
  55. /* test %ws, %ls, %Zi (should be %S) */
  56. test("Hello, world!", "%ws", L"Hello, world!");
  57. test("Hello, world!", "%.20Z", L"Hello, world!");
  58. test("Hello", "%.5ls", L"Hello, world!");
  59. test("", "%.ws", L"Hello, world!");
  60. test("(null)", "%ls", NULL);
  61. test("(null)", "%ws", NULL);
  62. test("(null)", "%Z", NULL);
  63. test(" Hello, world!", "%20ls", L"Hello, world!");
  64. test("Hello, world! ", "%-20ws", L"Hello, world!");
  65. test(" Hello", "%20.5Z", L"Hello, world!");
  66. test("Hello ", "%-20.5ls", L"Hello, world!");
  67. test("Hello", "%3.5ws", L"Hello, world!");
  68. test("Hello", "%-3.5Z", L"Hello, world!");
  69. test("Hello, world!", "%-8ls", L"Hello, world!");
  70. test("Hello, world!", "%8ws", L"Hello, world!");
  71. /* test %wc, %lc, should also add %C */
  72. test("H", "%lc", L'H');
  73. test(" H", "%5lc", L'H');
  74. test("H ", "%-5lc", L'H');
  75. test("H", "%wc", L'H');
  76. test(" H", "%5wc", L'H');
  77. test("H ", "%-5wc", L'H');
  78. /* test %d/%u */
  79. test("54", "%d", 54);
  80. test("-42", "%i", -42);
  81. test(" -102", "%6d", -102);
  82. test("0 ", "%-6d", 0);
  83. test("00000", "%.5d", 0);
  84. test("-0345", "%.4d", -345);
  85. test("-345", "%.3d", -345);
  86. test("00345", "%.5d", 345);
  87. test("00345", "%05d", 345);
  88. test("345 ", "%-05d", 345);
  89. test(" -00055", "%10.5d", -55);
  90. test("-00055 ", "%-10.5d", -55);
  91. test(" ", "%5.0d", 0);
  92. test("12345678", "%ld", 12345678);
  93. test("+123", "%+d", 123);
  94. test("-123", "%+d", -123);
  95. test("+000123", "%+07d", 123);
  96. test(" 000123", "% 07d", 123);
  97. test("+123 ", "%+-7d", 123);
  98. test(" +0123", "%+7.4i", 123);
  99. test("-1", "%d", 0xFFFFFFFF);
  100. test("65535", "%u", 0xFFFF);
  101. /* test %x */
  102. test("DEF", "%X", 0xdef);
  103. test("14d ", "%-6x", 0x14d);
  104. test(" 0X14D", "%#6X", 0x14d);
  105. test("0x00014d ", "%#-10.6x", 0x14d);
  106. test("0", "%#x", 0);
  107. test("", "%.0x", 0);
  108. test(" ", "%#2.0X", 0);
  109. test("000000014d", "%010x", 0x14d);
  110. test("14d ", "%-010x", 0x14d);
  111. test("0x0000014d", "%#010x", 0x14d);
  112. test("FFFF", "%X", 0xffff);
  113. /* temp: %B/%C should go away */
  114. test("DEF", "%B", 0xdef);
  115. test("14D ", "%-6C", 0x14d);
  116. test(" 0X14D", "%#6B", 0x14d);
  117. test("0X00014D ", "%#-10.6C", 0x14d);
  118. test("0", "%#B", 0);
  119. test("", "%.0C", 0);
  120. test(" ", "%#2.0B", 0);
  121. test("000000014D", "%010C", 0x14d);
  122. test("14D ", "%-010B", 0x14d);
  123. test("0X0000014D", "%#010C", 0x14d);
  124. test("FFFF", "%B", 0xffff);
  125. /* test %o */
  126. test("703", "%o", 0703);
  127. test(" 703", "%6o", 0703);
  128. test(" 0703", "%#6o", 0703);
  129. test("0703 ", "%#-6o", 0703);
  130. test("000703", "%06o", 0703);
  131. test("00703 ", "%-#6.5o", 0703);
  132. test("0703 ", "%-#06o", 0703);
  133. test(" 00703", "%#6.5o", 0703);
  134. test("0", "%#.0o", 0);
  135. test("", "%.0o", 0);
  136. test("177777", "%o", 0xFFFF);
  137. /* test %p */
  138. test("000000FE", "%p", (char *)0x00fe);
  139. test("00003DFE ", "%-10p", (char *)0x3dfe);
  140. test(" 00009DF4", "%12p", (char *)0x9df4);
  141. /* test %n */
  142. test("Hello, world", "Hello, world%n", &i);
  143. test("12", "%i", i);
  144. test("This is 423 characters ", "This is %d characters %ln", 423, &l);
  145. test("23", "%ld", l);
  146. test("0x0003f", "%#.5x%n", 0x3f, &i);
  147. test("7", "%d", i);
  148. /* test multiple specifiers */
  149. test("24 43", "%d %d", 24, 43);
  150. test("24 43", "%ld %ld", 24l, 43L);
  151. test("00004ABC 43", "%p %d", (char *)0x4abc, 43);
  152. test("43", "%n%d", &i, 43);
  153. test("43", "%ln%d", &l, 43);
  154. printf("Completed all non-FP format specifiers\n");
  155. printf("Processing %%e\n");
  156. /* test %e */
  157. test("-4.000000e+000", "%e", -4.0);
  158. test("4.000000E+000", "%E", 4.0);
  159. test("-7E+001", "%.0E", -68.5);
  160. test("-7.E+001", "%#.0E", -74.5);
  161. test(" 6.78e+004 ", "% -12.2e", 67844.324);
  162. test(" +6.78e+004", "%+12.2e", 67844.324);
  163. test(" 0.000000e+000", "%17e", 0.0);
  164. test("-00007.467e-001", "%015.3e", -0.74673);
  165. printf("Processing %%f\n");
  166. /* test %f */
  167. test("-4.000000", "%f", -4.0);
  168. test("4.000000", "%f", 4.0);
  169. test("0.400000", "%f", 0.4);
  170. test("-69", "%.0f", -68.7);
  171. test("-74.", "%#.0f", -74.3);
  172. test(" 67844.32 ", "% -12.2f", 67844.324);
  173. test(" +67844.32", "%+12.2f", 67844.324);
  174. test(" 0.000000", "%17f", 0.0);
  175. test("-0000000000.747", "%015.3f", -0.74673);
  176. printf("Processing %%g\n");
  177. /* test %g */
  178. test("3.14159E-005", "%G", 0.000031415926535);
  179. test("0.000314159", "%g", 0.00031415926535);
  180. test("0.00314159", "%g", 0.0031415926535);
  181. test("0.0314159", "%g", 0.031415926535);
  182. test("0.314159", "%g", 0.31415926535);
  183. test("3.14159", "%g", 3.1415926535);
  184. test("31.4159", "%g", 31.415926535);
  185. test("314.159", "%g", 314.15926535);
  186. test("3141.59", "%g", 3141.5926535);
  187. test("31415.9", "%g", 31415.926535);
  188. test("314159", "%g", 314159.26535);
  189. test("3.14159e+006" , "%g", 3141592.6535);
  190. test("3", "%g", 3.0);
  191. test("3e+006", "%g", 3000000.0);
  192. test(" 3.14", "%7.4g", 3.1402);
  193. test(" 3.14 ", "% -7.4g", 3.1402);
  194. test("+0023.000", "%+#09.5g", 23.0);
  195. test("23.", "%#.2g", 23.0);
  196. test("23", "%.2g", 23.0);
  197. /* more multiples */
  198. test("3.1 43", "%g %d", 3.1, 43);
  199. test("3.1 43", "%Lg %d", (long double)3.1, 43);
  200. }