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: test18.c
8**
9** Purpose: Test #18 for the vswprintf function.
10**
11**
12**===================================================================*/
13
14#include <palsuite.h>
15#include "../vswprintf.h"
16
17/* memcmp is used to verify the results, so this test is dependent on it. */
18/* ditto with wcslen */
19
20#define DOTEST(a,b,c,d,e) DoTest(a,b,(void*)c,d,e)
21
22void DoArgumentPrecTest(WCHAR *formatstr, int precision, void *param,
23 WCHAR *paramstr, WCHAR *checkstr1, WCHAR *checkstr2)
24{
25 WCHAR buf[256];
26
27 testvswp(buf, _countof(buf), formatstr, precision, param);
28 if (memcmp(buf, checkstr1, wcslen(checkstr1) + 2) != 0 &&
29 memcmp(buf, checkstr2, wcslen(checkstr2) + 2) != 0)
30 {
31 Fail("ERROR: failed to insert %s into \"%s\" with precision %d\n"
32 "Expected \"%s\" or \"%s\", got \"%s\".\n",
33 paramstr,
34 convertC(formatstr),
35 precision,
36 convertC(checkstr1),
37 convertC(checkstr2),
38 convertC(buf));
39 }
40}
41void DoArgumentPrecDoubleTest(WCHAR *formatstr, int precision, double param,
42 WCHAR *checkstr1, WCHAR *checkstr2)
43{
44 WCHAR buf[256];
45
46 testvswp(buf, _countof(buf), formatstr, precision, param);
47 if (memcmp(buf, checkstr1, wcslen(checkstr1) + 2) != 0 &&
48 memcmp(buf, checkstr2, wcslen(checkstr2) + 2) != 0)
49 {
50 Fail("ERROR: failed to insert %f into \"%s\" with precision %d\n"
51 "Expected \"%s\" or \"%s\", got \"%s\".\n",
52 param, convertC(formatstr),
53 precision,
54 convertC(checkstr1),
55 convertC(checkstr2),
56 convertC(buf));
57 }
58}
59
60/*
61 * Uses memcmp & wcslen
62 */
63
64int __cdecl main(int argc, char *argv[])
65{
66
67 if (PAL_Initialize(argc, argv) != 0)
68 return(FAIL);
69
70 DoArgumentPrecTest(convert("%.*s"), 2, (void*)convert("bar"), convert("bar"),
71 convert("ba"), convert("ba"));
72 DoArgumentPrecTest(convert("%.*c"), 0, (void*)'a', convert("a"),
73 convert("a"), convert("a"));
74 DoArgumentPrecTest(convert("%.*c"), 4, (void*)'a', convert("a"),
75 convert("a"), convert("a"));
76 DoArgumentPrecTest(convert("%.*C"), 0, (void*)'a', convert("a"),
77 convert("a"), convert("a"));
78 DoArgumentPrecTest(convert("%.*C"), 4, (void*)'a', convert("a"),
79 convert("a"), convert("a"));
80 DoArgumentPrecTest(convert("%.*d"), 1, (void*)42, convert("42"),
81 convert("42"), convert("42"));
82 DoArgumentPrecTest(convert("%.*d"), 3, (void*)42, convert("42"),
83 convert("042"), convert("042"));
84 DoArgumentPrecTest(convert("%.*i"), 1, (void*)42, convert("42"),
85 convert("42"), convert("42"));
86 DoArgumentPrecTest(convert("%.*i"), 3, (void*)42, convert("42"),
87 convert("042"), convert("042"));
88 DoArgumentPrecTest(convert("%.*o"), 1, (void*)42, convert("42"),
89 convert("52"), convert("52"));
90 DoArgumentPrecTest(convert("%.*o"), 3, (void*)42, convert("42"),
91 convert("052"), convert("052"));
92 DoArgumentPrecTest(convert("%.*u"), 1, (void*)42, convert("42"),
93 convert("42"), convert("42"));
94 DoArgumentPrecTest(convert("%.*u"), 3, (void*)42, convert("42"),
95 convert("042"), convert("042"));
96 DoArgumentPrecTest(convert("%.*x"), 1, (void*)0x42, convert("0x42"),
97 convert("42"), convert("42"));
98 DoArgumentPrecTest(convert("%.*x"), 3, (void*)0x42, convert("0x42"),
99 convert("042"), convert("042"));
100 DoArgumentPrecTest(convert("%.*X"), 1, (void*)0x42, convert("0x42"),
101 convert("42"), convert("42"));
102 DoArgumentPrecTest(convert("%.*X"), 3, (void*)0x42, convert("0x42"),
103 convert("042"), convert("042"));
104
105
106 DoArgumentPrecDoubleTest(convert("%.*e"), 1, 2.01, convert("2.0e+000"),
107 convert("2.0e+00"));
108 DoArgumentPrecDoubleTest(convert("%.*e"), 3, 2.01, convert("2.010e+000"),
109 convert("2.010e+00"));
110 DoArgumentPrecDoubleTest(convert("%.*E"), 1, 2.01, convert("2.0E+000"),
111 convert("2.0E+00"));
112 DoArgumentPrecDoubleTest(convert("%.*E"), 3, 2.01, convert("2.010E+000"),
113 convert("2.010E+00"));
114 DoArgumentPrecDoubleTest(convert("%.*f"), 1, 2.01, convert("2.0"),
115 convert("2.0"));
116 DoArgumentPrecDoubleTest(convert("%.*f"), 3, 2.01, convert("2.010"),
117 convert("2.010"));
118 DoArgumentPrecDoubleTest(convert("%.*g"), 1, 256.01, convert("3e+002"),
119 convert("3e+02"));
120 DoArgumentPrecDoubleTest(convert("%.*g"), 3, 256.01, convert("256"),
121 convert("256"));
122 DoArgumentPrecDoubleTest(convert("%.*g"), 4, 256.01, convert("256"),
123 convert("256"));
124 DoArgumentPrecDoubleTest(convert("%.*g"), 6, 256.01, convert("256.01"),
125 convert("256.01"));
126 DoArgumentPrecDoubleTest(convert("%.*G"), 1, 256.01, convert("3E+002"),
127 convert("3E+02"));
128 DoArgumentPrecDoubleTest(convert("%.*G"), 3, 256.01, convert("256"),
129 convert("256"));
130 DoArgumentPrecDoubleTest(convert("%.*G"), 4, 256.01, convert("256"),
131 convert("256"));
132 DoArgumentPrecDoubleTest(convert("%.*G"), 6, 256.01, convert("256.01"),
133 convert("256.01"));
134
135 PAL_Terminate();
136 return PASS;
137}
138