aboutsummaryrefslogtreecommitdiff
path: root/src/test/DUtilUnitTest/DictUtilTest.cpp
diff options
context:
space:
mode:
authorSean Hall <r.sean.hall@gmail.com>2020-07-11 21:11:30 +1000
committerSean Hall <r.sean.hall@gmail.com>2020-07-12 11:51:29 +1000
commit9c4b5559ccb55491fe68b0096d1be0496fd6fcc7 (patch)
treefaa51204fc2e6e3dfd1b00ae03c7184c4fb0b4bf /src/test/DUtilUnitTest/DictUtilTest.cpp
parentf651268309263fa67fa84caf9fb6dbebb5c900d9 (diff)
downloadwix-9c4b5559ccb55491fe68b0096d1be0496fd6fcc7.tar.gz
wix-9c4b5559ccb55491fe68b0096d1be0496fd6fcc7.tar.bz2
wix-9c4b5559ccb55491fe68b0096d1be0496fd6fcc7.zip
Import DUtilUnitTest from old wix4 repo.
Diffstat (limited to 'src/test/DUtilUnitTest/DictUtilTest.cpp')
-rw-r--r--src/test/DUtilUnitTest/DictUtilTest.cpp187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/test/DUtilUnitTest/DictUtilTest.cpp b/src/test/DUtilUnitTest/DictUtilTest.cpp
new file mode 100644
index 00000000..fd8a5953
--- /dev/null
+++ b/src/test/DUtilUnitTest/DictUtilTest.cpp
@@ -0,0 +1,187 @@
1// 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.
2
3#include "precomp.h"
4
5using namespace System;
6using namespace Xunit;
7using namespace WixTest;
8
9const DWORD numIterations = 100000;
10
11namespace DutilTests
12{
13 struct Value
14 {
15 DWORD dwNum;
16 LPWSTR sczKey;
17 };
18
19 public ref class DictUtil
20 {
21 public:
22 [Fact]
23 void DictUtilTest()
24 {
25 EmbeddedKeyTestHelper(DICT_FLAG_NONE, numIterations);
26
27 EmbeddedKeyTestHelper(DICT_FLAG_CASEINSENSITIVE, numIterations);
28
29 StringListTestHelper(DICT_FLAG_NONE, numIterations);
30
31 StringListTestHelper(DICT_FLAG_CASEINSENSITIVE, numIterations);
32 }
33
34 private:
35 void EmbeddedKeyTestHelper(DICT_FLAG dfFlags, DWORD dwNumIterations)
36 {
37 HRESULT hr = S_OK;
38 Value *rgValues = NULL;
39 Value *valueFound = NULL;
40 DWORD cValues = 0;
41 LPWSTR sczExpectedKey = NULL;
42 STRINGDICT_HANDLE sdValues = NULL;
43
44 try
45 {
46 hr = DictCreateWithEmbeddedKey(&sdValues, 0, (void **)&rgValues, offsetof(Value, sczKey), dfFlags);
47 NativeAssert::Succeeded(hr, "Failed to create dictionary of values");
48
49 for (DWORD i = 0; i < dwNumIterations; ++i)
50 {
51 cValues++;
52
53 hr = MemEnsureArraySize((void **)&rgValues, cValues, sizeof(Value), 5);
54 NativeAssert::Succeeded(hr, "Failed to grow value array");
55
56 hr = StrAllocFormatted(&rgValues[i].sczKey, L"%u_a_%u", i, i);
57 NativeAssert::Succeeded(hr, "Failed to allocate key for value {0}", i);
58
59 hr = DictAddValue(sdValues, rgValues + i);
60 NativeAssert::Succeeded(hr, "Failed to add item {0} to dict", i);
61 }
62
63 for (DWORD i = 0; i < dwNumIterations; ++i)
64 {
65 hr = StrAllocFormatted(&sczExpectedKey, L"%u_a_%u", i, i);
66 NativeAssert::Succeeded(hr, "Failed to allocate expected key {0}", i);
67
68 hr = DictGetValue(sdValues, sczExpectedKey, (void **)&valueFound);
69 NativeAssert::Succeeded(hr, "Failed to find value {0}", sczExpectedKey);
70
71 NativeAssert::StringEqual(sczExpectedKey, valueFound->sczKey);
72
73 hr = StrAllocFormatted(&sczExpectedKey, L"%u_A_%u", i, i);
74 NativeAssert::Succeeded(hr, "Failed to allocate uppercase expected key {0}", i);
75
76 hr = DictGetValue(sdValues, sczExpectedKey, (void **)&valueFound);
77
78 if (dfFlags & DICT_FLAG_CASEINSENSITIVE)
79 {
80 NativeAssert::Succeeded(hr, "Failed to find value {0}", sczExpectedKey);
81
82 NativeAssert::StringEqual(sczExpectedKey, valueFound->sczKey, TRUE);
83 }
84 else
85 {
86 if (E_NOTFOUND != hr)
87 {
88 hr = E_FAIL;
89 ExitOnFailure(hr, "This embedded key is case sensitive, but it seemed to have found something case using case insensitivity!: %ls", sczExpectedKey);
90 }
91 }
92
93 hr = StrAllocFormatted(&sczExpectedKey, L"%u_b_%u", i, i);
94 NativeAssert::Succeeded(hr, "Failed to allocate unexpected key {0}", i);
95
96 hr = DictGetValue(sdValues, sczExpectedKey, (void **)&valueFound);
97 if (E_NOTFOUND != hr)
98 {
99 hr = E_FAIL;
100 ExitOnFailure(hr, "Item shouldn't have been found in dictionary: %ls", sczExpectedKey);
101 }
102 }
103 }
104 finally
105 {
106 for (DWORD i = 0; i < cValues; ++i)
107 {
108 ReleaseStr(rgValues[i].sczKey);
109 }
110 ReleaseMem(rgValues);
111 ReleaseStr(sczExpectedKey);
112 ReleaseDict(sdValues);
113 }
114
115 LExit:
116 return;
117 }
118
119 void StringListTestHelper(DICT_FLAG dfFlags, DWORD dwNumIterations)
120 {
121 HRESULT hr = S_OK;
122 LPWSTR sczKey = NULL;
123 LPWSTR sczExpectedKey = NULL;
124 STRINGDICT_HANDLE sdValues = NULL;
125
126 try
127 {
128 hr = DictCreateStringList(&sdValues, 0, dfFlags);
129 NativeAssert::Succeeded(hr, "Failed to create dictionary of keys");
130
131 for (DWORD i = 0; i < dwNumIterations; ++i)
132 {
133 hr = StrAllocFormatted(&sczKey, L"%u_a_%u", i, i);
134 NativeAssert::Succeeded(hr, "Failed to allocate key for value {0}", i);
135
136 hr = DictAddKey(sdValues, sczKey);
137 NativeAssert::Succeeded(hr, "Failed to add key {0} to dict", i);
138 }
139
140 for (DWORD i = 0; i < dwNumIterations; ++i)
141 {
142 hr = StrAllocFormatted(&sczExpectedKey, L"%u_a_%u", i, i);
143 NativeAssert::Succeeded(hr, "Failed to allocate expected key {0}", i);
144
145 hr = DictKeyExists(sdValues, sczExpectedKey);
146 NativeAssert::Succeeded(hr, "Failed to find value {0}", sczExpectedKey);
147
148 hr = StrAllocFormatted(&sczExpectedKey, L"%u_A_%u", i, i);
149 NativeAssert::Succeeded(hr, "Failed to allocate uppercase expected key {0}", i);
150
151 hr = DictKeyExists(sdValues, sczExpectedKey);
152 if (dfFlags & DICT_FLAG_CASEINSENSITIVE)
153 {
154 NativeAssert::Succeeded(hr, "Failed to find value {0}", sczExpectedKey);
155 }
156 else
157 {
158 if (E_NOTFOUND != hr)
159 {
160 hr = E_FAIL;
161 ExitOnFailure(hr, "This stringlist dict is case sensitive, but it seemed to have found something case using case insensitivity!: %ls", sczExpectedKey);
162 }
163 }
164
165 hr = StrAllocFormatted(&sczExpectedKey, L"%u_b_%u", i, i);
166 NativeAssert::Succeeded(hr, "Failed to allocate unexpected key {0}", i);
167
168 hr = DictKeyExists(sdValues, sczExpectedKey);
169 if (E_NOTFOUND != hr)
170 {
171 hr = E_FAIL;
172 ExitOnFailure(hr, "Item shouldn't have been found in dictionary: %ls", sczExpectedKey);
173 }
174 }
175 }
176 finally
177 {
178 ReleaseStr(sczKey);
179 ReleaseStr(sczExpectedKey);
180 ReleaseDict(sdValues);
181 }
182
183 LExit:
184 return;
185 }
186 };
187}