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