ReactOS  0.4.14-dev-604-gcfdd483
CString.cpp
Go to the documentation of this file.
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 
19 {
20  const char* strA;
21  const wchar_t* strW;
22  int str_len;
24 };
25 
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 
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  {
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 
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 
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 
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));
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));
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 !
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 
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 
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 
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));
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));
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 // Allocation strategy seems to differ a bit between us and MS's atl.
121 // if someone cares enough to find out why, feel free to change the macro below.
122 #ifdef __GNUC__
123 #define ALLOC_EXPECT(a, b) b
124 #else
125 #define ALLOC_EXPECT(a, b) a
126 #endif
127 
128 
129 #undef ok
130 #undef _T
131 
132 #define TEST_NAMEX(name) void test_##name##W()
133 #define CStringX CStringW
134 #define _X(x) L ## x
135 #define XCHAR WCHAR
136 #define YCHAR CHAR
137 #define dbgstrx(x) wine_dbgstr_w(x)
138 #define ok ok_("CStringW:\n" __FILE__, __LINE__)
139 #define GetWindowsDirectoryX GetWindowsDirectoryW
140 #define MAKEINTRESOURCEX(x) MAKEINTRESOURCEW(x)
141 #define MAKEINTRESOURCEY(x) MAKEINTRESOURCEA(x)
142 #include "CString.inl"
143 
144 
145 #undef CStringX
146 #undef TEST_NAMEX
147 #undef _X
148 #undef XCHAR
149 #undef YCHAR
150 #undef dbgstrx
151 #undef ok
152 #undef GetWindowsDirectoryX
153 #undef MAKEINTRESOURCEX
154 #undef MAKEINTRESOURCEY
155 
156 #define TEST_NAMEX(name) void test_##name##A()
157 #define CStringX CStringA
158 #define _X(x) x
159 #define XCHAR CHAR
160 #define YCHAR WCHAR
161 #define dbgstrx(x) (const char*)x
162 #define ok ok_("CStringA:\n" __FILE__, __LINE__)
163 #define GetWindowsDirectoryX GetWindowsDirectoryA
164 #define MAKEINTRESOURCEX(x) MAKEINTRESOURCEA(x)
165 #define MAKEINTRESOURCEY(x) MAKEINTRESOURCEW(x)
166 #include "CString.inl"
167 
168 
170 {
171  test_basetypes();
172 
173  if ((ALLOC_EXPECT(1, 2)) == 2)
174  {
175  skip("Ignoring real GetAllocLength() lenght\n");
176  }
177 
178  test_operators_initW();
179  test_operators_initA();
180 
181  test_compareW();
182  test_compareA();
183 
184  test_findW();
185  test_findA();
186 
187  test_formatW();
188  test_formatA();
189 
190  test_substrW();
191  test_substrA();
192 
193  test_replaceW();
194  test_replaceA();
195 
196  test_trimW();
197  test_trimA();
198 
199  test_envW();
200  test_envA();
201 
202  test_load_strW();
203  test_load_strA();
204 
205  test_bstrW();
206  test_bstrA();
207 }
char strA[12]
Definition: clipboard.c:2028
const wchar_t * strW
Definition: CString.cpp:21
char * wine_dbgstr_w(const wchar_t *wstr)
Definition: atltest.h:87
#define _countof(array)
Definition: fontsub.cpp:30
#define str_len
Definition: treelist.c:89
GLdouble n
Definition: glext.h:7729
traits_test g_Tests[]
Definition: CString.cpp:26
WCHAR strW[12]
Definition: clipboard.c:2029
const char * strA
Definition: CString.cpp:20
smooth NULL
Definition: ftsmooth.c:416
#define ok
Definition: CString.cpp:162
int str_len
Definition: CString.cpp:22
static void test_basetypes()
Definition: CString.cpp:41
static const WCHAR L[]
Definition: oid.c:1250
#define ALLOC_EXPECT(a, b)
Definition: CString.cpp:125
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define skip(...)
Definition: atltest.h:64
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define memset(x, y, z)
Definition: compat.h:39
START_TEST(CString)
Definition: CString.cpp:169