aboutsummaryrefslogtreecommitdiff
path: root/src/test/DUtilUnitTest/StrUtilTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/DUtilUnitTest/StrUtilTest.cpp')
-rw-r--r--src/test/DUtilUnitTest/StrUtilTest.cpp184
1 files changed, 184 insertions, 0 deletions
diff --git a/src/test/DUtilUnitTest/StrUtilTest.cpp b/src/test/DUtilUnitTest/StrUtilTest.cpp
new file mode 100644
index 00000000..406f2f23
--- /dev/null
+++ b/src/test/DUtilUnitTest/StrUtilTest.cpp
@@ -0,0 +1,184 @@
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
9namespace DutilTests
10{
11 public ref class StrUtil
12 {
13 public:
14 [Fact]
15 void StrUtilFormattedTest()
16 {
17 HRESULT hr = S_OK;
18 LPWSTR sczText = NULL;
19
20 try
21 {
22 hr = StrAllocFormatted(&sczText, L"%hs - %ls - %u", "ansi string", L"unicode string", 1234);
23 NativeAssert::Succeeded(hr, "Failed to format string.");
24 NativeAssert::StringEqual(L"ansi string - unicode string - 1234", sczText);
25
26 ReleaseNullStr(sczText);
27
28 hr = StrAllocString(&sczText, L"repeat", 0);
29 NativeAssert::Succeeded(hr, "Failed to allocate string.");
30
31 hr = StrAllocFormatted(&sczText, L"%ls and %ls", sczText, sczText);
32 NativeAssert::Succeeded(hr, "Failed to format string unto itself.");
33 NativeAssert::StringEqual(L"repeat and repeat", sczText);
34 }
35 finally
36 {
37 ReleaseStr(sczText);
38 }
39 }
40
41 [Fact]
42 void StrUtilTrimTest()
43 {
44 TestTrim(L"", L"");
45 TestTrim(L"Blah", L"Blah");
46 TestTrim(L"\t\t\tBlah", L"Blah");
47 TestTrim(L"\t Blah ", L"Blah");
48 TestTrim(L"Blah ", L"Blah");
49 TestTrim(L"\t Spaces \t Between \t", L"Spaces \t Between");
50 TestTrim(L" \t\t\t ", L"");
51
52 TestTrimAnsi("", "");
53 TestTrimAnsi("Blah", "Blah");
54 TestTrimAnsi("\t\t\tBlah", "Blah");
55 TestTrimAnsi(" Blah ", "Blah");
56 TestTrimAnsi("Blah ", "Blah");
57 TestTrimAnsi("\t Spaces \t Between \t", "Spaces \t Between");
58 TestTrimAnsi(" \t\t\t ", "");
59 }
60
61 [Fact]
62 void StrUtilConvertTest()
63 {
64 char a[] = { 'a', 'b', 'C', 'd', '\0', '\0' };
65
66 TestStrAllocStringAnsi(a, 5, L"abCd");
67 TestStrAllocStringAnsi(a, 4, L"abCd");
68 TestStrAllocStringAnsi(a, 3, L"abC");
69 TestStrAllocStringAnsi(a, 2, L"ab");
70 TestStrAllocStringAnsi(a, 1, L"a");
71 TestStrAllocStringAnsi(a, 0, L"abCd");
72
73 wchar_t b[] = { L'a', L'b', L'C', L'd', L'\0', L'\0' };
74
75 TestStrAnsiAllocString(b, 5, "abCd");
76 TestStrAnsiAllocString(b, 4, "abCd");
77 TestStrAnsiAllocString(b, 3, "abC");
78 TestStrAnsiAllocString(b, 2, "ab");
79 TestStrAnsiAllocString(b, 1, "a");
80 TestStrAnsiAllocString(b, 0, "abCd");
81 }
82
83 private:
84 void TestTrim(LPCWSTR wzInput, LPCWSTR wzExpectedResult)
85 {
86 HRESULT hr = S_OK;
87 LPWSTR sczOutput = NULL;
88
89 try
90 {
91 hr = StrTrimWhitespace(&sczOutput, wzInput);
92 NativeAssert::Succeeded(hr, "Failed to trim whitespace from string: {0}", wzInput);
93
94 if (0 != wcscmp(wzExpectedResult, sczOutput))
95 {
96 hr = E_FAIL;
97 ExitOnFailure(hr, "Trimmed string \"%ls\", expected result \"%ls\", actual result \"%ls\"", wzInput, wzExpectedResult, sczOutput);
98 }
99 }
100 finally
101 {
102 ReleaseStr(sczOutput);
103 }
104
105 LExit:
106 return;
107 }
108
109 void TestTrimAnsi(LPCSTR szInput, LPCSTR szExpectedResult)
110 {
111 HRESULT hr = S_OK;
112 LPSTR sczOutput = NULL;
113
114 try
115 {
116 hr = StrAnsiTrimWhitespace(&sczOutput, szInput);
117 NativeAssert::Succeeded(hr, "Failed to trim whitespace from string: \"{0}\"", szInput);
118
119 if (0 != strcmp(szExpectedResult, sczOutput))
120 {
121 hr = E_FAIL;
122 ExitOnFailure(hr, "Trimmed string \"%hs\", expected result \"%hs\", actual result \"%hs\"", szInput, szExpectedResult, sczOutput);
123 }
124 }
125 finally
126 {
127 ReleaseStr(sczOutput);
128 }
129
130 LExit:
131 return;
132 }
133
134 void TestStrAllocStringAnsi(LPCSTR szSource, DWORD cchSource, LPCWSTR wzExpectedResult)
135 {
136 HRESULT hr = S_OK;
137 LPWSTR sczOutput = NULL;
138
139 try
140 {
141 hr = StrAllocStringAnsi(&sczOutput, szSource, cchSource, CP_UTF8);
142 NativeAssert::Succeeded(hr, "Failed to call StrAllocStringAnsi on string: \"{0}\"", szSource);
143
144 if (0 != wcscmp(sczOutput, wzExpectedResult))
145 {
146 hr = E_FAIL;
147 ExitOnFailure(hr, "String doesn't match, expected result \"%ls\", actual result \"%ls\"", wzExpectedResult, sczOutput);
148 }
149 }
150 finally
151 {
152 ReleaseStr(sczOutput);
153 }
154
155 LExit:
156 return;
157 }
158
159 void TestStrAnsiAllocString(LPWSTR wzSource, DWORD cchSource, LPCSTR szExpectedResult)
160 {
161 HRESULT hr = S_OK;
162 LPSTR sczOutput = NULL;
163
164 try
165 {
166 hr = StrAnsiAllocString(&sczOutput, wzSource, cchSource, CP_UTF8);
167 NativeAssert::Succeeded(hr, "Failed to call StrAllocStringAnsi on string: \"{0}\"", wzSource);
168
169 if (0 != strcmp(sczOutput, szExpectedResult))
170 {
171 hr = E_FAIL;
172 ExitOnFailure(hr, "String doesn't match, expected result \"%hs\", actual result \"%hs\"", szExpectedResult, sczOutput);
173 }
174 }
175 finally
176 {
177 ReleaseStr(sczOutput);
178 }
179
180 LExit:
181 return;
182 }
183 };
184}