1 /*
2  * PROJECT:     ReactOS api tests
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Test for CString
5  * COPYRIGHT:   Copyright 2016-2017 Mark Jansen (mark.jansen@reactos.org)
6  *              Copyright 2017 Katayama Hirofumi MZ
7  */
8 
9 #include <atlstr.h>
10 #include "resource.h"
11 
12 #ifdef HAVE_APITEST
13     #include <apitest.h>
14 #else
15     #include "atltest.h"
16 #endif
17 
18 struct traits_test
19 {
20     const char* strA;
21     const wchar_t* strW;
22     int str_len;
23     int exp_1, exp_2, exp_3, exp_4;
24 };
25 
26 traits_test g_Tests[] = {
27     // inputs                   outputs
28     { NULL, NULL, 0,            0,  0, -1,  0 },
29     { NULL, NULL, -1,           0, -1, -1,  0 },
30     { NULL, NULL, 1,            0,  1, -1,  0 },
31 
32     { "", L"", 0,               0,  0,  0,  0 },
33     { "", L"", -1,              0, -1,  0,  1 },
34     { "", L"", 1,               0,  1,  0,  1 },
35 
36     { "AAABBB", L"AAABBB", 0,   6,  0,  6,  0 },
37     { "AAABBB", L"AAABBB", 3,   6,  3,  6,  3 },
38     { "AAABBB", L"AAABBB", -1,  6, -1,  6,  7 },
39 };
40 
test_basetypes()41 static void test_basetypes()
42 {
43     int len;
44     char bufA[10];
45     wchar_t bufW[10];
46 
47     for (size_t n = 0; n < _countof(g_Tests); ++n)
48     {
49         len = ChTraitsCRT<char>::GetBaseTypeLength(g_Tests[n].strA);
50         ok(len == g_Tests[n].exp_1, "Expected len to be %i, was %i for %u (A)\n", g_Tests[n].exp_1, len, n);
51 
52         len = ChTraitsCRT<char>::GetBaseTypeLength(g_Tests[n].strA, g_Tests[n].str_len);
53         ok(len == g_Tests[n].exp_2, "Expected len to be %i, was %i for %u (A,len)\n", g_Tests[n].exp_2, len, n);
54 
55         len = ChTraitsCRT<char>::GetBaseTypeLength(g_Tests[n].strW);
56         ok(len == g_Tests[n].exp_3, "Expected len to be %i, was %i for %u (W)\n", g_Tests[n].exp_3, len, n);
57 
58         len = ChTraitsCRT<char>::GetBaseTypeLength(g_Tests[n].strW, g_Tests[n].str_len);
59         ok(len == g_Tests[n].exp_4, "Expected len to be %i, was %i for %u (W,len)\n", g_Tests[n].exp_4, len, n);
60 
61         if (g_Tests[n].strA && g_Tests[n].strW)
62         {
63             memset(bufA, 'x', sizeof(bufA));
64             ChTraitsCRT<char>::ConvertToBaseType(bufA, g_Tests[n].exp_1+1, g_Tests[n].strA);
65             char ch = bufA[g_Tests[n].exp_1];
66             ok(ch == '\0', "Expected %i to be \\0, was: %c (%i) for %u\n", g_Tests[n].exp_1, ch, (int)ch, n);
67             ok(!strcmp(bufA, g_Tests[n].strA), "Expected bufA to be %s, was: %s for %u\n", g_Tests[n].strA, bufA, n);
68             ch = bufA[g_Tests[n].exp_1+1];
69             ok(ch == 'x', "Expected %i to be 'x', was: %c (%i) for %u\n", g_Tests[n].exp_1+1, ch, (int)ch, n);
70         }
71 
72         if (g_Tests[n].strA && g_Tests[n].strW)
73         {
74             memset(bufA, 'x', sizeof(bufA));
75             ChTraitsCRT<char>::ConvertToBaseType(bufA, g_Tests[n].exp_1+1, g_Tests[n].strW);
76             char ch = bufA[g_Tests[n].exp_1];
77             ok(ch == '\0', "Expected %i to be \\0, was: %c (%i) for %u\n", g_Tests[n].exp_1, ch, (int)ch, n);
78             ok(!strcmp(bufA, g_Tests[n].strA), "Expected bufA to be %s, was: %s for %u\n", g_Tests[n].strA, bufA, n);
79             ch = bufA[g_Tests[n].exp_1+1];
80             ok(ch == 'x', "Expected %i to be 'x', was: %c (%i) for %u\n", g_Tests[n].exp_1+1, ch, (int)ch, n);
81         }
82 
83         // wchar_t --> please note, swapped the expectations from 2 and 4 !
84         len = ChTraitsCRT<wchar_t>::GetBaseTypeLength(g_Tests[n].strA);
85         ok(len == g_Tests[n].exp_1, "Expected len to be %i, was %i for %u (A)\n", g_Tests[n].exp_1, len, n);
86 
87         len = ChTraitsCRT<wchar_t>::GetBaseTypeLength(g_Tests[n].strA, g_Tests[n].str_len);
88         ok(len == g_Tests[n].exp_4, "Expected len to be %i, was %i for %u (A,len)\n", g_Tests[n].exp_4, len, n);
89 
90         len = ChTraitsCRT<wchar_t>::GetBaseTypeLength(g_Tests[n].strW);
91         ok(len == g_Tests[n].exp_3, "Expected len to be %i, was %i for %u (W)\n", g_Tests[n].exp_3, len, n);
92 
93         len = ChTraitsCRT<wchar_t>::GetBaseTypeLength(g_Tests[n].strW, g_Tests[n].str_len);
94         ok(len == g_Tests[n].exp_2, "Expected len to be %i, was %i for %u (W,len)\n", g_Tests[n].exp_2, len, n);
95 
96         if (g_Tests[n].strA && g_Tests[n].strW)
97         {
98             memset(bufW, 'x', sizeof(bufW));
99             ChTraitsCRT<wchar_t>::ConvertToBaseType(bufW, g_Tests[n].exp_1+1, g_Tests[n].strA);
100             wchar_t ch = bufW[g_Tests[n].exp_1];
101             ok(ch == L'\0', "Expected %i to be \\0, was: %c (%i) for %u\n", g_Tests[n].exp_1, ch, (int)ch, n);
102             ok(!wcscmp(bufW, g_Tests[n].strW), "Expected bufW to be %s, was: %s for %u\n", wine_dbgstr_w(g_Tests[n].strW), wine_dbgstr_w(bufW), n);
103             ch = bufW[g_Tests[n].exp_1+1];
104             ok(ch == 30840, "Expected %i to be %i for %u\n", g_Tests[n].exp_1+1, (int)ch, n);
105         }
106 
107         if (g_Tests[n].strA && g_Tests[n].strW)
108         {
109             memset(bufW, 'x', sizeof(bufW));
110             ChTraitsCRT<wchar_t>::ConvertToBaseType(bufW, g_Tests[n].exp_1+1, g_Tests[n].strW);
111             wchar_t ch = bufW[g_Tests[n].exp_1];
112             ok(ch == '\0', "Expected %i to be \\0, was: %c (%i) for %u\n", g_Tests[n].exp_1, ch, (int)ch, n);
113             ok(!wcscmp(bufW, g_Tests[n].strW), "Expected bufW to be %s, was: %s for %u\n", wine_dbgstr_w(g_Tests[n].strW), wine_dbgstr_w(bufW), n);
114             ch = bufW[g_Tests[n].exp_1+1];
115             ok(ch == 30840, "Expected %i to be %i for %u\n", g_Tests[n].exp_1+1, (int)ch, n);
116         }
117     }
118 }
119 
120 #undef ok
121 #undef _T
122 
123 #define TEST_NAMEX(name)        void test_##name##W()
124 #define CStringX                CStringW
125 #define _X(x)                   L ## x
126 #define XCHAR                   WCHAR
127 #define YCHAR                   CHAR
128 #define dbgstrx(x)              wine_dbgstr_w(x)
129 #define ok                      ok_("CStringW:\n" __FILE__, __LINE__)
130 #define GetWindowsDirectoryX    GetWindowsDirectoryW
131 #define MAKEINTRESOURCEX(x)     MAKEINTRESOURCEW(x)
132 #define MAKEINTRESOURCEY(x)     MAKEINTRESOURCEA(x)
133 #include "CString.inl"
134 
135 
136 #undef CStringX
137 #undef TEST_NAMEX
138 #undef _X
139 #undef XCHAR
140 #undef YCHAR
141 #undef dbgstrx
142 #undef ok
143 #undef GetWindowsDirectoryX
144 #undef MAKEINTRESOURCEX
145 #undef MAKEINTRESOURCEY
146 
147 #define TEST_NAMEX(name)        void test_##name##A()
148 #define CStringX                CStringA
149 #define _X(x)                   x
150 #define XCHAR                   CHAR
151 #define YCHAR                   WCHAR
152 #define dbgstrx(x)              (const char*)x
153 #define ok                      ok_("CStringA:\n" __FILE__, __LINE__)
154 #define GetWindowsDirectoryX    GetWindowsDirectoryA
155 #define MAKEINTRESOURCEX(x)     MAKEINTRESOURCEA(x)
156 #define MAKEINTRESOURCEY(x)     MAKEINTRESOURCEW(x)
157 #include "CString.inl"
158 
159 
START_TEST(CString)160 START_TEST(CString)
161 {
162     test_basetypes();
163 
164     test_operators_initW();
165     test_operators_initA();
166 
167     test_compareW();
168     test_compareA();
169 
170     test_findW();
171     test_findA();
172 
173     test_formatW();
174     test_formatA();
175 
176     test_substrW();
177     test_substrA();
178 
179     test_replaceW();
180     test_replaceA();
181 
182     test_trimW();
183     test_trimA();
184 
185     test_envW();
186     test_envA();
187 
188     test_load_strW();
189     test_load_strA();
190 
191     test_bstrW();
192     test_bstrA();
193 
194     test_tokenizeW();
195     test_tokenizeA();
196 }
197