1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
using namespace System;
using namespace Xunit;
using namespace WixInternal::TestSupport;
const DWORD numIterations = 100000;
namespace DutilTests
{
struct Value
{
DWORD dwNum;
LPWSTR sczKey;
};
public ref class DictUtil
{
public:
[Fact]
void DictUtilTest()
{
DutilInitialize(&DutilTestTraceError);
EmbeddedKeyTestHelper(DICT_FLAG_NONE, numIterations);
EmbeddedKeyTestHelper(DICT_FLAG_CASEINSENSITIVE, numIterations);
StringListTestHelper(DICT_FLAG_NONE, numIterations);
StringListTestHelper(DICT_FLAG_CASEINSENSITIVE, numIterations);
DutilUninitialize();
}
private:
void EmbeddedKeyTestHelper(DICT_FLAG dfFlags, DWORD dwNumIterations)
{
HRESULT hr = S_OK;
Value *rgValues = NULL;
Value *valueFound = NULL;
DWORD cValues = 0;
LPWSTR sczExpectedKey = NULL;
STRINGDICT_HANDLE sdValues = NULL;
try
{
hr = DictCreateWithEmbeddedKey(&sdValues, 0, (void **)&rgValues, offsetof(Value, sczKey), dfFlags);
NativeAssert::Succeeded(hr, "Failed to create dictionary of values");
for (DWORD i = 0; i < dwNumIterations; ++i)
{
cValues++;
hr = MemEnsureArraySize((void **)&rgValues, cValues, sizeof(Value), 5);
NativeAssert::Succeeded(hr, "Failed to grow value array");
hr = StrAllocFormatted(&rgValues[i].sczKey, L"%u_a_%u", i, i);
NativeAssert::Succeeded(hr, "Failed to allocate key for value {0}", i);
hr = DictAddValue(sdValues, rgValues + i);
NativeAssert::Succeeded(hr, "Failed to add item {0} to dict", i);
}
for (DWORD i = 0; i < dwNumIterations; ++i)
{
hr = StrAllocFormatted(&sczExpectedKey, L"%u_a_%u", i, i);
NativeAssert::Succeeded(hr, "Failed to allocate expected key {0}", i);
hr = DictGetValue(sdValues, sczExpectedKey, (void **)&valueFound);
NativeAssert::Succeeded(hr, "Failed to find value {0}", sczExpectedKey);
NativeAssert::StringEqual(sczExpectedKey, valueFound->sczKey);
hr = StrAllocFormatted(&sczExpectedKey, L"%u_A_%u", i, i);
NativeAssert::Succeeded(hr, "Failed to allocate uppercase expected key {0}", i);
hr = DictGetValue(sdValues, sczExpectedKey, (void **)&valueFound);
if (dfFlags & DICT_FLAG_CASEINSENSITIVE)
{
NativeAssert::Succeeded(hr, "Failed to find value {0}", sczExpectedKey);
NativeAssert::StringEqual(sczExpectedKey, valueFound->sczKey, TRUE);
}
else
{
if (E_NOTFOUND != hr)
{
hr = E_FAIL;
ExitOnFailure(hr, "This embedded key is case sensitive, but it seemed to have found something case using case insensitivity!: %ls", sczExpectedKey);
}
}
hr = StrAllocFormatted(&sczExpectedKey, L"%u_b_%u", i, i);
NativeAssert::Succeeded(hr, "Failed to allocate unexpected key {0}", i);
hr = DictGetValue(sdValues, sczExpectedKey, (void **)&valueFound);
if (E_NOTFOUND != hr)
{
hr = E_FAIL;
ExitOnFailure(hr, "Item shouldn't have been found in dictionary: %ls", sczExpectedKey);
}
}
}
finally
{
for (DWORD i = 0; i < cValues; ++i)
{
ReleaseStr(rgValues[i].sczKey);
}
ReleaseMem(rgValues);
ReleaseStr(sczExpectedKey);
ReleaseDict(sdValues);
}
LExit:
return;
}
void StringListTestHelper(DICT_FLAG dfFlags, DWORD dwNumIterations)
{
HRESULT hr = S_OK;
LPWSTR sczKey = NULL;
LPWSTR sczExpectedKey = NULL;
STRINGDICT_HANDLE sdValues = NULL;
try
{
hr = DictCreateStringList(&sdValues, 0, dfFlags);
NativeAssert::Succeeded(hr, "Failed to create dictionary of keys");
for (DWORD i = 0; i < dwNumIterations; ++i)
{
hr = StrAllocFormatted(&sczKey, L"%u_a_%u", i, i);
NativeAssert::Succeeded(hr, "Failed to allocate key for value {0}", i);
hr = DictAddKey(sdValues, sczKey);
NativeAssert::Succeeded(hr, "Failed to add key {0} to dict", i);
}
for (DWORD i = 0; i < dwNumIterations; ++i)
{
hr = StrAllocFormatted(&sczExpectedKey, L"%u_a_%u", i, i);
NativeAssert::Succeeded(hr, "Failed to allocate expected key {0}", i);
hr = DictKeyExists(sdValues, sczExpectedKey);
NativeAssert::Succeeded(hr, "Failed to find value {0}", sczExpectedKey);
hr = StrAllocFormatted(&sczExpectedKey, L"%u_A_%u", i, i);
NativeAssert::Succeeded(hr, "Failed to allocate uppercase expected key {0}", i);
hr = DictKeyExists(sdValues, sczExpectedKey);
if (dfFlags & DICT_FLAG_CASEINSENSITIVE)
{
NativeAssert::Succeeded(hr, "Failed to find value {0}", sczExpectedKey);
}
else
{
if (E_NOTFOUND != hr)
{
hr = E_FAIL;
ExitOnFailure(hr, "This stringlist dict is case sensitive, but it seemed to have found something case using case insensitivity!: %ls", sczExpectedKey);
}
}
hr = StrAllocFormatted(&sczExpectedKey, L"%u_b_%u", i, i);
NativeAssert::Succeeded(hr, "Failed to allocate unexpected key {0}", i);
hr = DictKeyExists(sdValues, sczExpectedKey);
if (E_NOTFOUND != hr)
{
hr = E_FAIL;
ExitOnFailure(hr, "Item shouldn't have been found in dictionary: %ls", sczExpectedKey);
}
}
}
finally
{
ReleaseStr(sczKey);
ReleaseStr(sczExpectedKey);
ReleaseDict(sdValues);
}
LExit:
return;
}
};
}
|