FreeRDP
TestFormatSpecifiers.c
1 #include <stdio.h>
2 #include <winpr/wtypes.h>
3 #include <winpr/string.h>
4 
5 int TestFormatSpecifiers(int argc, char* argv[])
6 {
7  unsigned errors = 0;
8 
9  char fmt[4096];
10 
11  WINPR_UNUSED(argc);
12  WINPR_UNUSED(argv);
13 
14  /* size_t */
15  {
16  size_t arg = 0xabcd;
17  const char* chk = "uz:43981 oz:125715 xz:abcd Xz:ABCD";
18 
19  (void)sprintf_s(fmt, sizeof(fmt), "uz:%" PRIuz " oz:%" PRIoz " xz:%" PRIxz " Xz:%" PRIXz "",
20  arg, arg, arg, arg);
21 
22  if (strcmp(fmt, chk) != 0)
23  {
24  (void)fprintf(stderr, "%s failed size_t test: got [%s] instead of [%s]\n", __func__,
25  fmt, chk);
26  errors++;
27  }
28  }
29 
30  /* INT8 */
31  {
32  INT8 arg = -16;
33  const char* chk = "d8:-16 x8:f0 X8:F0";
34 
35  (void)sprintf_s(fmt, sizeof(fmt), "d8:%" PRId8 " x8:%" PRIx8 " X8:%" PRIX8 "", arg,
36  (UINT8)arg, (UINT8)arg);
37 
38  if (strcmp(fmt, chk) != 0)
39  {
40  (void)fprintf(stderr, "%s failed INT8 test: got [%s] instead of [%s]\n", __func__, fmt,
41  chk);
42  errors++;
43  }
44  }
45 
46  /* UINT8 */
47  {
48  UINT8 arg = 0xFE;
49  const char* chk = "u8:254 o8:376 x8:fe X8:FE";
50 
51  (void)sprintf_s(fmt, sizeof(fmt), "u8:%" PRIu8 " o8:%" PRIo8 " x8:%" PRIx8 " X8:%" PRIX8 "",
52  arg, arg, arg, arg);
53 
54  if (strcmp(fmt, chk) != 0)
55  {
56  (void)fprintf(stderr, "%s failed UINT8 test: got [%s] instead of [%s]\n", __func__, fmt,
57  chk);
58  errors++;
59  }
60  }
61 
62  /* INT16 */
63  {
64  INT16 arg = -16;
65  const char* chk = "d16:-16 x16:fff0 X16:FFF0";
66 
67  (void)sprintf_s(fmt, sizeof(fmt), "d16:%" PRId16 " x16:%" PRIx16 " X16:%" PRIX16 "", arg,
68  (UINT16)arg, (UINT16)arg);
69 
70  if (strcmp(fmt, chk) != 0)
71  {
72  (void)fprintf(stderr, "%s failed INT16 test: got [%s] instead of [%s]\n", __func__, fmt,
73  chk);
74  errors++;
75  }
76  }
77 
78  /* UINT16 */
79  {
80  UINT16 arg = 0xFFFE;
81  const char* chk = "u16:65534 o16:177776 x16:fffe X16:FFFE";
82 
83  (void)sprintf_s(fmt, sizeof(fmt),
84  "u16:%" PRIu16 " o16:%" PRIo16 " x16:%" PRIx16 " X16:%" PRIX16 "", arg, arg,
85  arg, arg);
86 
87  if (strcmp(fmt, chk) != 0)
88  {
89  (void)fprintf(stderr, "%s failed UINT16 test: got [%s] instead of [%s]\n", __func__,
90  fmt, chk);
91  errors++;
92  }
93  }
94 
95  /* INT32 */
96  {
97  INT32 arg = -16;
98  const char* chk = "d32:-16 x32:fffffff0 X32:FFFFFFF0";
99 
100  (void)sprintf_s(fmt, sizeof(fmt), "d32:%" PRId32 " x32:%" PRIx32 " X32:%" PRIX32 "", arg,
101  (UINT32)arg, (UINT32)arg);
102 
103  if (strcmp(fmt, chk) != 0)
104  {
105  (void)fprintf(stderr, "%s failed INT32 test: got [%s] instead of [%s]\n", __func__, fmt,
106  chk);
107  errors++;
108  }
109  }
110 
111  /* UINT32 */
112  {
113  UINT32 arg = 0xFFFFFFFE;
114  const char* chk = "u32:4294967294 o32:37777777776 x32:fffffffe X32:FFFFFFFE";
115 
116  (void)sprintf_s(fmt, sizeof(fmt),
117  "u32:%" PRIu32 " o32:%" PRIo32 " x32:%" PRIx32 " X32:%" PRIX32 "", arg, arg,
118  arg, arg);
119 
120  if (strcmp(fmt, chk) != 0)
121  {
122  (void)fprintf(stderr, "%s failed UINT16 test: got [%s] instead of [%s]\n", __func__,
123  fmt, chk);
124  errors++;
125  }
126  }
127 
128  /* INT64 */
129  {
130  INT64 arg = -16;
131  const char* chk = "d64:-16 x64:fffffffffffffff0 X64:FFFFFFFFFFFFFFF0";
132 
133  (void)sprintf_s(fmt, sizeof(fmt), "d64:%" PRId64 " x64:%" PRIx64 " X64:%" PRIX64 "", arg,
134  (UINT64)arg, (UINT64)arg);
135 
136  if (strcmp(fmt, chk) != 0)
137  {
138  (void)fprintf(stderr, "%s failed INT64 test: got [%s] instead of [%s]\n", __func__, fmt,
139  chk);
140  errors++;
141  }
142  }
143 
144  /* UINT64 */
145  {
146  UINT64 arg = 0xFFFFFFFFFFFFFFFE;
147  const char* chk = "u64:18446744073709551614 o64:1777777777777777777776 "
148  "x64:fffffffffffffffe X64:FFFFFFFFFFFFFFFE";
149 
150  (void)sprintf_s(fmt, sizeof(fmt),
151  "u64:%" PRIu64 " o64:%" PRIo64 " x64:%016" PRIx64 " X64:%016" PRIX64 "",
152  arg, arg, arg, arg);
153 
154  if (strcmp(fmt, chk) != 0)
155  {
156  (void)fprintf(stderr, "%s failed UINT64 test: got [%s] instead of [%s]\n", __func__,
157  fmt, chk);
158  errors++;
159  }
160  }
161 
162  if (errors)
163  {
164  (void)fprintf(stderr, "%s produced %u errors\n", __func__, errors);
165  return -1;
166  }
167 
168  return 0;
169 }