ReactOS 0.4.15-dev-8219-ge8b88cf
CComBSTR.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 CComBSTR
5 * COPYRIGHT: Copyright 2015-2017 Mark Jansen (mark.jansen@reactos.org)
6 */
7
8#include <apitest.h>
9#include <atlbase.h>
10
11#include "resource.h"
12
13#define verify_str (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : verify_str_imp
14#define verify_str2 (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : verify_str_imp2
15
16
17static void verify_str_imp2(const CComBSTR& comstr, PCWSTR expected, size_t ExpectedLength)
18{
19 BSTR str = (BSTR)comstr;
20 if (expected || ExpectedLength)
21 {
22 winetest_ok(str != NULL, "Expected str to be a valid pointer\n");
23 if (str)
24 {
25 if (expected)
26 {
27 winetest_ok(!wcscmp(str, expected), "Expected the string to be '%s', was '%s'\n", wine_dbgstr_w(expected), wine_dbgstr_w(str));
28 }
29 size_t Length = comstr.Length();
30 winetest_ok(Length == ExpectedLength, "Expected Length to be %u, was: %u\n", ExpectedLength, Length);
31 Length = comstr.ByteLength();
32 ExpectedLength *= sizeof(WCHAR);
33 winetest_ok(Length == ExpectedLength, "Expected ByteLength to be %u, was: %u\n", ExpectedLength, Length);
34 }
35 }
36 else
37 {
38 winetest_ok(str == NULL || str[0] == '\0', "Expected str to be empty, was: '%s'\n", wine_dbgstr_w(str));
39 }
40}
41
42static void verify_str_imp(const CComBSTR& comstr, PCWSTR expected)
43{
45}
46
48{
49 CComBSTR empty1, empty2;
50 CComBSTR happyW(L"I am a happy BSTR");
51 CComBSTR happyA("I am a happy BSTR");
52 CComBSTR happyW4(4, L"I am a happy BSTR");
53 CComBSTR fromlen1(1), fromlen10(10);
54 CComBSTR fromBSTRW(happyW), fromBSTRA(happyA), fromBSTRW4(happyW4);
55 CComBSTR fromBSTRlen1(fromlen1), fromBSTRlen10(fromlen10);
56
57 verify_str(empty1, NULL);
58 verify_str(empty2, NULL);
59 verify_str(happyW, L"I am a happy BSTR");
60 verify_str(happyA, L"I am a happy BSTR");
61 verify_str(happyW4, L"I am");
62 verify_str2(fromlen1, NULL, 1);
63 verify_str2(fromlen10, NULL, 10);
64 verify_str(fromBSTRW, L"I am a happy BSTR");
65 verify_str(fromBSTRA, L"I am a happy BSTR");
66 verify_str(fromBSTRW4, L"I am");
67 verify_str2(fromBSTRlen1, NULL, 1);
68 verify_str2(fromBSTRlen10, NULL, 10);
69}
70
72{
73 CComBSTR happy(L"I am a happy BSTR"), empty, odd;
74 CComBSTR happyCopy1, happyCopy2, emptyCopy, oddCopy;
75
76 odd = ::SysAllocStringByteLen("aaaaa", 3);
77
78 happyCopy1 = happy.Copy();
79 happyCopy2 = happy; // Calls happyW.Copy()
80 emptyCopy = empty.Copy();
81 oddCopy = odd.Copy();
82
83 verify_str(happy, L"I am a happy BSTR");
85 verify_str2(odd, L"\u6161a", 2);
86 verify_str(happyCopy1, L"I am a happy BSTR");
87 verify_str(happyCopy2, L"I am a happy BSTR");
88 verify_str(emptyCopy, NULL);
89 verify_str2(oddCopy, L"\u6161a", 2);
90 ok((BSTR)happy != (BSTR)happyCopy1, "Expected pointers to be different\n");
91 ok((BSTR)happy != (BSTR)happyCopy2, "Expected pointers to be different\n");
92
93
94 happyCopy1 = (LPCOLESTR)NULL;
95 happyCopy2 = (LPCSTR)NULL;
96
97 verify_str(happyCopy1, NULL);
98 verify_str(happyCopy2, NULL);
99
100 HRESULT hr = happy.CopyTo(&happyCopy1);
101 ok(hr == S_OK, "Expected hr to be E_POINTER, was: %08lx\n", hr);
102
103#if 0
104 // This asserts
105 hr = happy.CopyTo((BSTR*)NULL);
106 ok(hr == E_POINTER, "Expected hr to be E_POINTER, was: %u\n");
107#endif
108
109 BSTR RawPtr = ::SysAllocString(L"TEST--");
110 happy.Attach(RawPtr);
111 ok_ptr(happy.m_str, RawPtr);
112 verify_str(happy, L"TEST--");
113 happyCopy1.Attach(happy.Detach());
114 ok_ptr(happyCopy1.m_str, RawPtr);
115 verify_str(happyCopy1, L"TEST--");
116 ok_ptr(happy.m_str, NULL);
117}
118
120{
121 GUID guid = { 0x12345678, 0x9abc, 0xdef0, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0} };
122 CComBSTR fromGuid(guid), empty;
123 verify_str(fromGuid, L"{12345678-9ABC-DEF0-1234-56789ABCDEF0}");
125 empty = fromGuid;
126 verify_str(empty, L"{12345678-9ABC-DEF0-1234-56789ABCDEF0}");
127}
128
130{
131 CComBSTR test1, test2, test3;
133
134 ok(true == test1.LoadString(mod, IDS_TEST1), "Expected LoadString to succeed\n");
135 ok(true == test2.LoadString(mod, IDS_TEST2), "Expected LoadString to succeed\n");
136 ok(false == test3.LoadString(mod, IDS_TEST2 + 1), "Expected LoadString to fail\n");
137
138 verify_str(test1, L"Test string one.");
139 verify_str(test2, L"I am a happy BSTR");
141}
142
143START_TEST(CComBSTR)
144{
148 test_loadres();
149}
static void verify_str_imp(const CComBSTR &comstr, PCWSTR expected)
Definition: CComBSTR.cpp:42
static void verify_str_imp2(const CComBSTR &comstr, PCWSTR expected, size_t ExpectedLength)
Definition: CComBSTR.cpp:17
void test_copyassignment()
Definition: CComBSTR.cpp:71
void test_fromguid()
Definition: CComBSTR.cpp:119
#define verify_str2
Definition: CComBSTR.cpp:14
#define verify_str
Definition: CComBSTR.cpp:13
void test_loadres()
Definition: CComBSTR.cpp:129
void test_construction()
Definition: CComBSTR.cpp:47
#define ok(value,...)
Definition: atltest.h:57
#define START_TEST(x)
Definition: atltest.h:75
#define ok_ptr(expression, result)
Definition: atltest.h:108
#define NULL
Definition: types.h:112
static const WCHAR empty[]
Definition: main.c:47
OLECHAR * BSTR
Definition: compat.h:2293
void test2()
Definition: ehthrow.cxx:284
void test1()
Definition: ehthrow.cxx:277
void test3()
Definition: ehthrow.cxx:291
static int mod
Definition: i386-dis.c:1288
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define S_OK
Definition: intsafe.h:52
#define wine_dbgstr_w
Definition: kernel32.h:34
#define odd(x)
Definition: bidi.c:51
const GUID * guid
#define IDS_TEST1
Definition: resource.h:2
#define IDS_TEST2
Definition: resource.h:3
BOOL expected
Definition: store.c:2063
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define L(x)
Definition: ntvdm.h:50
BSTR WINAPI SysAllocString(LPCOLESTR str)
Definition: oleaut.c:238
BSTR WINAPI DECLSPEC_HOTPATCH SysAllocStringByteLen(LPCSTR str, UINT len)
Definition: oleaut.c:428
const WCHAR * str
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
void __winetest_cdecl winetest_ok(int condition, const char *msg,...)
HRESULT hr
Definition: shlfolder.c:183
Definition: unary.h:11
const uint16_t * PCWSTR
Definition: typedefs.h:57
#define GetModuleHandle
Definition: winbase.h:3827
#define E_POINTER
Definition: winerror.h:2365
const char * LPCSTR
Definition: xmlstorage.h:183
__wchar_t WCHAR
Definition: xmlstorage.h:180