Alternate e697dbe9c5997e35395fe158628dd8c5209481da
for Visual Studio 2022 and Windows 11.
読み取り中…
検索中…
一致する文字列を見つけられません
UtilityTest.cpp
[詳解]
1
5
6#include "pch.h"
7#include "Utility.h"
8#include "File.h"
9#include "FileUtility.h"
10#include "Array.hpp"
11
12using namespace Microsoft::VisualStudio::CppUnitTestFramework;
13
14namespace WindowsLibraryTest
15{
16 TEST_CLASS (UtilityTest)
17 {
18 public:
19 TEST_CLASS_INITIALIZE (ClassInitialize)
20 {
21 Logger::WriteMessage ("UtilityTest class initialize.\n");
22 }
23
24 TEST_CLASS_CLEANUP (ClassCleanup)
25 {
26 Logger::WriteMessage ("UtilityTest class cleanup.\n");
27 }
28
29 TEST_METHOD_INITIALIZE (MethodInitialize)
30 {
31 Logger::WriteMessage ("UtilityTest method initialize.\n");
32 }
33
34 TEST_METHOD_CLEANUP (MethodCleanup)
35 {
36 Logger::WriteMessage ("UtilityTest method cleanup.\n");
37 }
38
39 TEST_METHOD (CreateGUIDTest)
40 {
41 for (int i = 0; i < 10; i++)
42 {
43 auto guid = alt::Utility::MakeGUID ();
44 Logger::WriteMessage (guid.Ctr ());
45 Logger::WriteMessage (_T ("\n"));
46 }
47 }
48
49 TEST_METHOD (GetSystemTimeAndLocalTimeTest)
50 {
51 alt::TString message;
52 auto t1 = alt::Utility::GetSystemTime ();
53 auto t2 = alt::Utility::GetLocalTime ();
54
55 message.Format (_T ("SystemTime:%04d/%02d/%02dT%02d:%02d:%02d.%03d %d\n"),
56 t1.wYear, t1.wMonth, t1.wDay,
57 t1.wHour, t1.wMinute, t1.wSecond,
58 t1.wMilliseconds, t1.wDayOfWeek);
59 Logger::WriteMessage (message.Ctr ());
60
61 message.Format (_T (" LocalTime:%04d/%02d/%02dT%02d:%02d:%02d.%03d %d\n"),
62 t2.wYear, t2.wMonth, t2.wDay,
63 t2.wHour, t2.wMinute, t2.wSecond,
64 t2.wMilliseconds, t2.wDayOfWeek);
65 Logger::WriteMessage (message.Ctr ());
66 }
67
68 TEST_METHOD (GetFormatTimeTest)
69 {
70 Logger::WriteMessage (alt::Utility::GetFormatTime (alt::Utility::GetLocalTime ()).Ctr ());
71 }
72
73 TEST_METHOD (GetLocalTimeQuadTest)
74 {
76 alt::TString res2;
77 res2.Format (_T ("%lld"), res.QuadPart);
78 Logger::WriteMessage (res2.Ctr ());
79 }
80
81 TEST_METHOD (GetTimeByFileTimeTest1)
82 {
84 FILETIME t2{ 0, 0 };
85 t2.dwLowDateTime = t.LowPart;
86 t2.dwHighDateTime = t.HighPart;
87
89 auto t4 = alt::Utility::GetFormatTime (t3);
90 Logger::WriteMessage (t4.Ctr ());
91 }
92
93 TEST_METHOD (GetTimeByFileTimeTest2)
94 {
96 FILETIME ft;
97 ::SystemTimeToFileTime (&t, &ft);
98
100 auto t4 = alt::Utility::GetFormatTime (t3);
101 Logger::WriteMessage (t4.Ctr ());
102 }
103
104 TEST_METHOD (GetSystemInfoTest)
105 {
106 alt::TString message;
107 SYSTEM_INFO si;
109
110 message.Format (_T ("dwPageSize:%d\n"), si.dwPageSize);
111 Logger::WriteMessage (message.Ctr ());
112
113 message.Format (_T ("lpMinimumApplicationAddress:%p\n"), si.lpMinimumApplicationAddress);
114 Logger::WriteMessage (message.Ctr ());
115
116 message.Format (_T ("lpMaximumApplicationAddress:%p\n"), si.lpMaximumApplicationAddress);
117 Logger::WriteMessage (message.Ctr ());
118
119 message.Format (_T ("dwActiveProcessorMask:%d\n"), si.dwActiveProcessorMask);
120 Logger::WriteMessage (message.Ctr ());
121
122 message.Format (_T ("dwNumberOfProcessors:%d\n"), si.dwNumberOfProcessors);
123 Logger::WriteMessage (message.Ctr ());
124
125 message.Format (_T ("dwProcessorType:%d\n"), si.dwProcessorType);
126 Logger::WriteMessage (message.Ctr ());
127
128 message.Format (_T ("dwAllocationGranularity:%d\n"), si.dwAllocationGranularity);
129 Logger::WriteMessage (message.Ctr ());
130
131 message.Format (_T ("wProcessorLevel:%d\n"), si.wProcessorLevel);
132 Logger::WriteMessage (message.Ctr ());
133
134 message.Format (_T ("wProcessorRevision:%d\n"), si.wProcessorRevision);
135 Logger::WriteMessage (message.Ctr ());
136 }
137
138 TEST_METHOD (Base64EncodeAndDecodeTest1)
139 {
140 BYTE byBinary[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D' };
141 alt::TString response;
142
143 BOOL ret = alt::Utility::Base64Encode (byBinary, sizeof (byBinary), response);
144 Assert::IsTrue (ret);
145
146 Logger::WriteMessage (response.Ctr ());
147
148 DWORD dwBuffer;
149 ret = alt::Utility::Base64Decode (response, nullptr, dwBuffer);
150 Assert::IsTrue (ret);
151
152 LPBYTE lpbyBuffer = new BYTE[dwBuffer * sizeof (TCHAR)];
153 ret = alt::Utility::Base64Decode (response, lpbyBuffer, dwBuffer);
154 Assert::IsTrue (ret);
155 for (int i = 0; i < sizeof (byBinary); i++)
156 {
157 Assert::AreEqual<BYTE> (byBinary[i], lpbyBuffer[i]);
158 }
159 delete[] lpbyBuffer;
160 }
161
162 TEST_METHOD (Base64EncodeAndDecodeTest2)
163 {
164 alt::File binFile;
165 BOOL ret;
166
167 ret = binFile.Create (_T ("C:\\Windows\\Notepad.exe"), GENERIC_READ, 0, OPEN_EXISTING);
168 Assert::IsTrue (ret);
169
170 DWORD dwSize = (DWORD)binFile.GetSize ();
171 LPBYTE lpbyBinary = new BYTE[dwSize];
172
173 DWORD dwReadSize = binFile.Read (lpbyBinary, dwSize);
174 Assert::AreEqual<DWORD> (dwReadSize, dwSize);
175
176 alt::TString response;
177 ret = alt::Utility::Base64Encode (lpbyBinary, dwSize, response);
178 Assert::IsTrue (ret);
179 Logger::WriteMessage (response.Ctr ());
180
181 DWORD dwBuffer;
182 ret = alt::Utility::Base64Decode (response, nullptr, dwBuffer);
183 Assert::IsTrue (ret);
184
185 LPBYTE lpbyBuffer = new BYTE[dwBuffer];
186 ret = alt::Utility::Base64Decode (response, lpbyBuffer, dwBuffer);
187 Assert::IsTrue (ret);
188 for (DWORD i = 0; i < dwSize; i++)
189 {
190 Assert::AreEqual<BYTE> (lpbyBinary[i], lpbyBuffer[i]);
191 }
192
193 delete[] lpbyBuffer;
194 delete[] lpbyBinary;
195 }
196
197 TEST_METHOD (GetEnvironmentsTest)
198 {
199 auto ret = alt::Utility::GetEnvironments ();
200 for (auto item : ret)
201 {
202 Logger::WriteMessage (item.Ctr ());
203 Logger::WriteMessage (_T ("\n"));
204 }
205 }
206
207 TEST_METHOD (GetEnvTest)
208 {
209 auto ret = alt::Utility::GetEnv (_T ("PATH"));
210 auto ret2 = ret.Split (alt::TString (_T (";")));
211 for (auto item : ret2)
212 {
213 Logger::WriteMessage (item.Ctr ());
214 Logger::WriteMessage (_T ("\n"));
215 }
216
217 auto ret3 = alt::Utility::GetEnv (_T ("SolutionDir"));
218 Assert::IsTrue (ret3.Len () == 0);
219 }
220
221 TEST_METHOD (IniFileIOTest1)
222 {
223 BOOL ret;
224 LPCTSTR lpctszIniFile = _T (".\\UtilityTest.ini");
225 LPCTSTR lpctszSection = _T ("Section");
226 LPCTSTR lpctszKeyword = _T ("Keyword");
227 alt::TString response1 = _T ("日本語 の 値 を 設定");
228 alt::TString response2;
229
230 if (alt::FileUtility::IsExist (lpctszIniFile))
231 alt::FileUtility::Delete (lpctszIniFile);
232
234 lpctszIniFile, lpctszSection, lpctszKeyword, response1);
235 Assert::IsTrue (ret, _T ("Utility::WriteIniFile() write error.\n"));
236
238 lpctszIniFile, lpctszSection, lpctszKeyword, response2);
239 Assert::IsTrue (ret, _T ("Utility::ReadIniFile() read error.\n"));
240 Assert::IsTrue (
241 response1 == response2,
242 _T ("Utility::Read/WriteIniFile() value mismatched.\n"));
243 }
244
245 TEST_METHOD (IniFileIOTest2)
246 {
247 BOOL ret;
248 LPCTSTR lpctszIniFile = _T (".\\UtilityTest.ini");
249 LPCTSTR lpctszSection = _T ("Section");
250 LPCTSTR lpctszKeyword = _T ("Keyword");
251 INT iValue1 = 12345;
252 INT iValue2 = -1;
253
254 if (alt::FileUtility::IsExist (lpctszIniFile))
255 alt::FileUtility::Delete (lpctszIniFile);
256
258 lpctszIniFile, lpctszSection, lpctszKeyword, iValue1);
259 Assert::IsTrue (ret, _T ("Utility::WriteIniFile() write error.\n"));
260
261 iValue2 = alt::Utility::ReadIniFile (
262 lpctszIniFile, lpctszSection, lpctszKeyword, -1);
263 Assert::AreNotEqual (-1, ret, _T ("Utility::ReadIniFile() read error.\n"));
264 Assert::IsTrue (
265 iValue1 == iValue2,
266 _T ("Utility::Read/WriteIniFile() value mismatched.\n"));
267 }
268
269 TEST_METHOD (IniFileIOTest3)
270 {
271 BOOL ret;
272 LPCTSTR lpctszIniFile = _T (".\\UtilityTest.ini");
273 LPCTSTR lpctszSection = _T ("Section");
274 alt::TString keyword;
275
276 if (alt::FileUtility::IsExist (lpctszIniFile))
277 alt::FileUtility::Delete (lpctszIniFile);
278
279 int values[]{ -12345, -1234, -123, -12, -1, 0, 1, 22, 333, 4444, 55555, 666666 };
280 int loop = sizeof (values) / sizeof (int);
281
282 for (int i = 0; i < loop; i++)
283 {
284 keyword.Format (_T ("keyword_%02d"), i);
285 alt::TString response;
286 response << values[i];
287 ret = alt::Utility::WriteIniFile (lpctszIniFile, lpctszSection, keyword.Ctr (), response);
288 Assert::IsTrue (ret, _T ("Utility::WriteIniFile() write error.\n"));
289 }
290
291 for (int i = 0; i < loop; i++)
292 {
293 keyword.Format (_T ("keyword_%02d"), i);
294 alt::TString response;
295 ret = alt::Utility::ReadIniFile (lpctszIniFile, lpctszSection, keyword.Ctr (), response);
296 Assert::IsTrue (ret, _T ("Utility::WriteIniFile() write error.\n"));
297 Assert::AreEqual<int> (values[i], response.ParseInt ());
298 }
299 }
300
301 TEST_METHOD (CreateMD5Test1)
302 {
303 LPCSTR lpcszString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
304 BYTE byMD5[40];
305 LPSTR lpszResponse = "2AD372C377013BAA4EE32AB6649D2449";
306
307 Assert::IsTrue (alt::Utility::CreateMD5 ((LPBYTE)lpcszString, lstrlenA (lpcszString), byMD5), _T ("CreateMD5() failed.\n"));
308 Assert::IsTrue (_memicmp (lpszResponse, byMD5, 32) == 0);
309 }
310
311 TEST_METHOD (CreateMD5Test2)
312 {
313 LPCSTR lpcszResponse = "BA929713DC6AAEBFEDE06D7C6C96E54C";
314 LPCTSTR lpctszFileName = _T (".\\MD5_Check.txt");
315 alt::File testFile;
316 BYTE byMD5[40];
317 alt::TString message;
318 Assert::IsTrue (testFile.Create (lpctszFileName, GENERIC_WRITE, 0, CREATE_ALWAYS), _T ("Test file create failed.\n"));
319 for (int i = 0; i < 1024; i++)
320 {
321 message.Format (_T ("This line is No.%d\n"), i + 1);
322 Assert::IsTrue (-1 != testFile.Write (message.Ctr (), message.Byte ()), _T ("Test file write failed.\n"));
323 }
324 Assert::IsTrue (testFile.Close (), _T ("Test file close failed.\n"));
325
326 Assert::IsTrue (alt::Utility::CreateMD5 (lpctszFileName, byMD5), _T ("CreateMD5() failed.\n"));
327 Assert::IsTrue (_memicmp (lpcszResponse, byMD5, 32) == 0, _T ("MD5 response invalid.\n"));
328
329 Assert::IsTrue (alt::FileUtility::Delete (lpctszFileName), _T ("Test file delete failed.\n"));
330 }
331
332 TEST_METHOD (QuickSortTest1)
333 {
334 const INT count = 1000;
335 SHORT asRand[count];
336 srand (123);
337
338 for (int i = 0; i < count; i++)
339 {
340 asRand[i] = rand ();
341 }
342
343 alt::Utility::QuickSort<SHORT> (asRand, 0, count - 1);
344
345 for (int i = 0; i < count - 1; i++)
346 {
347 Assert::IsTrue (asRand[i] <= asRand[i + 1]);
348 }
349 }
350
351 class Container
352 {
353 public:
354 Container () { _wPrice = 0; };
355
356 Container (LPCTSTR lpctszKey, LPCTSTR lpctszValue, WORD wPrice) { Set (lpctszKey, lpctszValue, wPrice); };
357
358 Container (const Container& base) { _key = base._key; _value = base._value; _wPrice = base._wPrice; };
359
360 void Set (LPCTSTR lpctszKey, LPCTSTR lpctszValue, WORD wPrice)
361 {
362 _key = lpctszKey;
363 _value = lpctszValue;
364 _wPrice = wPrice;
365 };
366
367 void Get (alt::TString& key, alt::TString& value, WORD& wPrice)
368 {
369 key = _key;
370 value = _value;
371 wPrice = _wPrice;
372 };
373
374 bool operator < (const Container& base) const
375 {
376 bool ret;
377
378 // if (_wPrice < base._wPrice) ret = true;
379 // if (_key < base._key) ret = true;
380 if (_value < base._value) ret = true;
381 else ret = false;
382
383 return ret;
384 };
385
386 VOID operator = (const Container& base)
387 {
388 _key = base._key;
389 _value = base._value;
390 _wPrice = base._wPrice;
391 };
392
393 private:
394 alt::TString _key;
395 alt::TString _value;
396 WORD _wPrice;
397 };
398
399 TEST_METHOD (QuickSortTest2)
400 {
401 const INT count = 10;
402 Container container0 (_T ("Strawberry"), _T ("イチゴ"), 350);
403 Container container1 (_T ("Pineapple"), _T ("パイナップル"), 280);
404 Container container2 (_T ("Orange"), _T ("オレンジ"), 120);
405 Container container3 (_T ("Melon"), _T ("メロン"), 1350);
406 Container container4 (_T ("Mango"), _T ("マンゴー"), 720);
407 Container container5 (_T ("Lemon"), _T ("レモン"), 130);
408 Container container6 (_T ("Kiwifruit"), _T ("キウイフルーツ"),60);
409 Container container7 (_T ("Cherry"), _T ("サクランボ"), 20);
410 Container container8 (_T ("Banana"), _T ("バナナ"), 70);
411 Container container9 (_T ("Apple"), _T ("リンゴ"), 130);
412
413 Container fruits[count] {
414 container0, container1, container2, container3,
415 container4,container5,container6,container7,container8,
416 container9 };
417
418 for (auto item : fruits)
419 {
420 alt::TString key(40);
421 alt::TString value(40);
422 WORD wPrice;
423 alt::TString message;
424
425 item.Get (key, value, wPrice);
426 message.Format (_T ("%s\t%s\t%d\n"), key.Ctr(), value.Ctr(), wPrice);
427 Logger::WriteMessage (message.Ctr());
428 }
429 Logger::WriteMessage ("--------------------------------------------\n");
430
431 alt::Utility::QuickSort<Container> (fruits, 0, count - 1);
432
433 for (auto item : fruits)
434 {
435 alt::TString key(40);
436 alt::TString value(40);
437 WORD wPrice;
438 alt::TString message;
439
440 item.Get (key, value, wPrice);
441 message.Format (_T ("%s\t%s\t%d\n"), key.Ctr(), value.Ctr(), wPrice);
442 Logger::WriteMessage (message.Ctr());
443 }
444 }
445 };
446}
可変配列に関するクラス
ファイルIOに関するWindowsAPIを集約したクラス
ファイルハンドルを伴わないファイルIOに関するWindowsAPIを集約した クラス
struct _container Container
#define loop
汎用的に使えるユーティリティクラス
プリコンパイル済みヘッダー ファイルです。
ファイルIOに関するWindowsAPIを集約したクラス
Definition: File.h:16
BOOL APIENTRY Create(LPCTSTR lpctszFileName, DWORD dwDesiredAccess, DWORD dwShareMode, DWORD dwCreationDisposition)
ファイルを作成、オープンします。
Definition: File.cpp:12
LONGLONG APIENTRY GetSize() const
ファイルサイズを取得します。
Definition: File.cpp:51
static BOOL APIENTRY IsExist(LPCTSTR name)
ディレクトリ・ファイルの存在確認
static BOOL APIENTRY Delete(LPCTSTR name)
ファイルの削除
Definition: FileUtility.cpp:57
BOOL APIENTRY Close()
使用しなくなったハンドルはこれでクローズします。
DWORD APIENTRY Read(LPVOID lpvBuffer, DWORD dwSize) const
HANDLEを使ってデータを読み込みます。
DWORD APIENTRY Write(LPCVOID lpcvBuffer, DWORD dwSize) const
HANDLEを使ってデータを書き込みます。
文字列に関するWindowsAPIを集約したクラス
Definition: TString.h:17
TString &APIENTRY Format(LPCTSTR format,...)
フォーマットに従ってパラメータを文字列化します。
Definition: TString.cpp:333
INT APIENTRY Byte() const
内部で確保している文字列(バイト数)を取得します。
Definition: TString.cpp:44
LPCTSTR APIENTRY Ctr() const
内部で確保している文字列ポインタを取得します。
Definition: TString.h:46
int APIENTRY ParseInt() const
文字列の数値化
Definition: TString.cpp:370
static BOOL APIENTRY Base64Decode(TString &source, LPBYTE lpbyBuffer, DWORD &dwBuffer)
BASE64データをデコード
Definition: Utility.cpp:122
static const TString APIENTRY MakeGUID()
GUIDの作成
Definition: Utility.cpp:22
static skeleton::Array< TString > APIENTRY GetEnvironments()
環境変数の取得
Definition: Utility.cpp:143
static ULARGE_INTEGER APIENTRY GetLocalTimeQuad()
ローカル時間を64ビット値で取得
Definition: Utility.cpp:66
static BOOL APIENTRY CreateMD5(LPBYTE lpbyData, DWORD dwSize, LPBYTE lpbyMD5)
データからMD5を計算
Definition: Utility.cpp:218
static BOOL APIENTRY ReadIniFile(LPCTSTR lpctszIniFile, LPCTSTR lpctszSection, LPCTSTR lpctszKeyword, TString &response)
設定フィルの読み込み
Definition: Utility.cpp:178
static TString APIENTRY GetEnv(LPCTSTR lpctszKeyword)
環境変数の取得
Definition: Utility.cpp:164
static BOOL APIENTRY Base64Encode(LPBYTE lpbyString, DWORD dwLength, TString &response)
BASE64でデータをエンコード
Definition: Utility.cpp:98
static SYSTEMTIME APIENTRY GetLocalTime()
ローカル時間の取得
Definition: Utility.cpp:46
static VOID APIENTRY GetSystemInfo(SYSTEM_INFO &systemInfo)
システム情報を取得します。
Definition: Utility.cpp:93
static TString APIENTRY GetFormatTime(const SYSTEMTIME &systemTime)
SYSTEMTIME構造体の値を文字列化
Definition: Utility.cpp:55
static SYSTEMTIME APIENTRY GetTimeByFileTime(const FILETIME &fileTime)
FILETIME型からSYSTEMTIMEを取得
Definition: Utility.cpp:84
static BOOL APIENTRY WriteIniFile(LPCTSTR lpctszIniFile, LPCTSTR lpctszSection, LPCTSTR lpctszKeyword, TString &strValue)
設定フィルの書き込み
Definition: Utility.cpp:204
static SYSTEMTIME APIENTRY GetSystemTime()
システム時間の取得
Definition: Utility.cpp:37
TEST_CLASS(ArrayTest)
Definition: ArrayTest.cpp:19