1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*============================================================================
6**
7** Source: swprintf.h
8**
9** Purpose: Containts common testing functions for swprintf.h
10**
11**
12**==========================================================================*/
13
14#ifndef __SWPRINTF_H__
15#define __SWPRINTF_H__
16
17void DoWStrTest(const WCHAR *formatstr, WCHAR *param, const WCHAR *checkstr)
18{
19 WCHAR buf[256] = { 0 };
20
21 swprintf_s(buf, _countof(buf), formatstr, param);
22
23 if (memcmp(buf, checkstr, wcslen(checkstr) * 2 + 2) != 0)
24 {
25 Fail("ERROR: failed to insert wide string \"%s\" into \"%s\".\n"
26 "Expected \"%s\", got \"%s\".\n",
27 convertC(param), convertC(formatstr),
28 convertC(checkstr), convertC(buf));
29 }
30}
31
32void DoStrTest(const WCHAR *formatstr, char *param, const WCHAR *checkstr)
33{
34 WCHAR buf[256] = { 0 };
35
36 swprintf_s(buf, _countof(buf), formatstr, param);
37
38 if (memcmp(buf, checkstr, wcslen(checkstr) * 2 + 2) != 0)
39 {
40 Fail("ERROR: failed to insert wide string \"%s\" into \"%s\".\n"
41 "Expected \"%s\", got \"%s\".\n",
42 param, convertC(formatstr), convertC(checkstr),
43 convertC(buf));
44 }
45}
46
47void DoPointerTest(const WCHAR *formatstr, void* param, const WCHAR *checkstr1)
48{
49 WCHAR buf[256] = { 0 };
50
51 swprintf_s(buf, _countof(buf), formatstr, param);
52 if (memcmp(buf, checkstr1, wcslen(checkstr1)*2 + 2) != 0)
53 {
54 Fail("ERROR: failed to insert pointer to %#p into \"%s\"\n"
55 "Expected \"%s\", got \"%s\".\n", param,
56 convertC(formatstr), convertC(checkstr1), convertC(buf));
57 }
58}
59
60void DoCharTest(const WCHAR *formatstr, char param, const WCHAR *checkstr)
61{
62 WCHAR buf[256] = { 0 };
63
64 swprintf_s(buf, _countof(buf), formatstr, param);
65 if (memcmp(buf, checkstr, wcslen(checkstr)*2 + 2) != 0)
66 {
67 Fail("ERROR: failed to insert char \'%c\' (%d) into \"%s\"\n"
68 "Expected \"%s\" got \"%s\".\n", param, param,
69 convertC(formatstr), convertC(checkstr), convertC(buf));
70 }
71}
72
73void DoWCharTest(const WCHAR *formatstr, WCHAR param, const WCHAR *checkstr)
74{
75 WCHAR buf[256] = { 0 };
76
77 swprintf_s(buf, _countof(buf), formatstr, param);
78 if (memcmp(buf, checkstr, wcslen(checkstr)*2 + 2) != 0)
79 {
80 Fail("ERROR: failed to insert wide char \'%c\' (%d) into \"%s\"\n"
81 "Expected \"%s\" got \"%s\".\n", (char) param, param,
82 convertC(formatstr), convertC(checkstr), convertC(buf));
83 }
84}
85
86void DoNumTest(const WCHAR *formatstr, int value, const WCHAR *checkstr)
87{
88 WCHAR buf[256] = { 0 };
89
90 swprintf_s(buf, _countof(buf), formatstr, value);
91 if (memcmp(buf, checkstr, wcslen(checkstr)* 2 + 2) != 0)
92 {
93 Fail("ERROR: failed to insert %#x into \"%s\"\n"
94 "Expected \"%s\" got \"%s\".\n", value, convertC(formatstr),
95 convertC(checkstr), convertC(buf));
96 }
97}
98
99void DoI64Test(const WCHAR *formatstr, INT64 param, char *paramdesc,
100 const WCHAR *checkstr1)
101{
102 WCHAR buf[256] = { 0 };
103
104 swprintf_s(buf, _countof(buf), formatstr, param);
105 if (memcmp(buf, checkstr1, wcslen(checkstr1)*2 + 2) != 0)
106 {
107 Fail("ERROR: failed to insert %s into \"%s\"\n"
108 "Expected \"%s\", got \"%s\".\n", paramdesc,
109 convertC(formatstr), convertC(checkstr1), convertC(buf));
110 }
111}
112
113void DoDoubleTest(const WCHAR *formatstr, double value, const WCHAR *checkstr1,
114 const WCHAR *checkstr2)
115{
116 WCHAR buf[256] = { 0 };
117
118 swprintf_s(buf, _countof(buf), formatstr, value);
119 if (memcmp(buf, checkstr1, wcslen(checkstr1)*2 + 2) != 0 &&
120 memcmp(buf, checkstr2, wcslen(checkstr2)*2 + 2) != 0)
121 {
122 Fail("ERROR: failed to insert %f into \"%s\"\n"
123 "Expected \"%s\" or \"%s\", got \"%s\".\n",
124 value, convertC(formatstr), convertC(checkstr1),
125 convertC(checkstr2), convertC(buf));
126 }
127}
128
129void DoArgumentPrecTest(const WCHAR *formatstr, int precision, void *param,
130 char *paramstr, const WCHAR *checkstr1, const WCHAR *checkstr2)
131{
132 WCHAR buf[256];
133
134 swprintf_s(buf, _countof(buf), formatstr, precision, param);
135 if (memcmp(buf, checkstr1, wcslen(checkstr1) + 2) != 0 &&
136 memcmp(buf, checkstr2, wcslen(checkstr2) + 2) != 0)
137 {
138 Fail("ERROR: failed to insert %s into \"%s\" with precision %d\n"
139 "Expected \"%s\" or \"%s\", got \"%s\".\n", paramstr,
140 convertC(formatstr), precision,
141 convertC(checkstr1), convertC(checkstr2), convertC(buf));
142 }
143}
144
145void DoArgumentPrecDoubleTest(const WCHAR *formatstr, int precision, double param,
146 const WCHAR *checkstr1, const WCHAR *checkstr2)
147{
148 WCHAR buf[256];
149
150 swprintf_s(buf, _countof(buf), formatstr, precision, param);
151 if (memcmp(buf, checkstr1, wcslen(checkstr1) + 2) != 0 &&
152 memcmp(buf, checkstr2, wcslen(checkstr2) + 2) != 0)
153 {
154 Fail("ERROR: failed to insert %f into \"%s\" with precision %d\n"
155 "Expected \"%s\" or \"%s\", got \"%s\".\n", param,
156 convertC(formatstr), precision,
157 convertC(checkstr1), convertC(checkstr2), convertC(buf));
158 }
159}
160
161#endif
162
163