1 #undef BIG_ENDIAN
2 #undef LITTLE_ENDIAN
3
4 #include "StdAfx.h"
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <dirent.h>
11 #include <unistd.h>
12
13 #ifdef HAVE_WCHAR__H
14 #include <wchar.h>
15 #endif
16 #ifdef HAVE_LOCALE
17 #include <locale.h>
18 #endif
19
20 #include <windows.h>
21
22 #define NEED_NAME_WINDOWS_TO_UNIX
23 // #include "myPrivate.h"
24
25 #include "Common/StringConvert.h"
26 #include "Common/StdOutStream.h"
27
28 #undef NDEBUG
29 #include <assert.h>
30
31 #include "Common/StringConvert.cpp"
32 #include "Common/StdOutStream.cpp"
33 #include "Common/IntToString.cpp"
34
35 #include "Windows/Synchronization.cpp"
36 #include "Windows/FileFind.cpp"
37 #include "Windows/Time.cpp"
38 #include "../C/Threads.c"
39
40 using namespace NWindows;
41
42 #if defined(HAVE_WCHAR__H) && defined(HAVE_MBSTOWCS) && defined(HAVE_WCSTOMBS)
test_mbs(void)43 void test_mbs(void) {
44 wchar_t wstr1[256] = {
45 L'e',
46 0xE8, // latin small letter e with grave
47 0xE9, // latin small letter e with acute
48 L'a',
49 0xE0, // latin small letter a with grave
50 0x20AC, // euro sign
51 L'b',
52 0 };
53 wchar_t wstr2[256];
54 char astr[256];
55
56 global_use_utf16_conversion = 1;
57
58 size_t len1 = wcslen(wstr1);
59
60 printf("wstr1 - %d - '%ls'\n",(int)len1,wstr1);
61
62 size_t len0 = wcstombs(astr,wstr1,sizeof(astr));
63 printf("astr - %d - '%s'\n",(int)len0,astr);
64
65 size_t len2 = mbstowcs(wstr2,astr,sizeof(wstr2)/sizeof(*wstr2));
66 printf("wstr - %d - '%ls'\n",(int)len2,wstr2);
67
68 if (wcscmp(wstr1,wstr2) != 0) {
69 printf("ERROR during conversions wcs -> mbs -> wcs\n");
70 exit(EXIT_FAILURE);
71 }
72
73 char *ptr = astr;
74 size_t len = 0;
75 while (*ptr) {
76 ptr = CharNextA(ptr);
77 len += 1;
78 }
79 if ((len != len1) && (len != 12)) { // 12 = when locale is UTF8 instead of ISO8859-15
80 printf("ERROR CharNextA : len=%d, len1=%d\n",(int)len,(int)len1);
81 exit(EXIT_FAILURE);
82 }
83
84 UString ustr(wstr1);
85 assert(ustr.Length() == (int)len1);
86
87 AString ansistr(astr);
88 assert(ansistr.Length() == (int)len0);
89
90 ansistr = UnicodeStringToMultiByte(ustr);
91 assert(ansistr.Length() == (int)len0);
92
93 assert(strcmp(ansistr,astr) == 0);
94 assert(wcscmp(ustr,wstr1) == 0);
95
96 UString ustr2 = MultiByteToUnicodeString(astr);
97 assert(ustr2.Length() == (int)len1);
98 assert(wcscmp(ustr2,wstr1) == 0);
99 }
100 #endif
101
test_astring(int num)102 static void test_astring(int num) {
103 AString strResult;
104
105 strResult = "first part : ";
106 char number[256];
107 sprintf(number,"%d",num);
108 strResult += AString(number);
109
110 strResult += " : last part";
111
112 printf("strResult -%s-\n",(const char *)strResult);
113
114 }
115
116
117 extern void my_windows_split_path(const AString &p_path, AString &dir , AString &base);
118
119 static struct {
120 const char *path;
121 const char *dir;
122 const char *base;
123 }
124 tabSplit[]=
125 {
126 { "",".","." },
127 { "/","/","/" },
128 { ".",".","." },
129 { "//","/","/" },
130 { "///","/","/" },
131 { "dir",".","dir" },
132 { "/dir","/","dir" },
133 { "/dir/","/","dir" },
134 { "/dir/base","/dir","base" },
135 { "/dir//base","/dir","base" },
136 { "/dir///base","/dir","base" },
137 { "//dir/base","//dir","base" },
138 { "///dir/base","///dir","base" },
139 { "/dir/base/","/dir","base" },
140 { 0,0,0 }
141 };
142
test_split_astring()143 static void test_split_astring() {
144 int ind = 0;
145 while (tabSplit[ind].path) {
146 AString path(tabSplit[ind].path);
147 AString dir;
148 AString base;
149
150 my_windows_split_path(path,dir,base);
151
152 if ((dir != tabSplit[ind].dir) || (base != tabSplit[ind].base)) {
153 printf("ERROR : '%s' '%s' '%s'\n",(const char *)path,(const char *)dir,(const char *)base);
154 }
155 ind++;
156 }
157 printf("test_split_astring : done\n");
158 }
159
160 // Number of 100 nanosecond units from 1/1/1601 to 1/1/1970
161 #define EPOCH_BIAS 116444736000000000LL
UnixTimeToUL(time_t tps_unx)162 static LARGE_INTEGER UnixTimeToUL(time_t tps_unx)
163 {
164 LARGE_INTEGER ul;
165 ul.QuadPart = tps_unx * 10000000LL + EPOCH_BIAS;
166 return ul;
167 }
168
FileTimeToUL(FILETIME fileTime)169 static LARGE_INTEGER FileTimeToUL(FILETIME fileTime)
170 {
171 LARGE_INTEGER lFileTime;
172 lFileTime.QuadPart = fileTime.dwHighDateTime;
173 lFileTime.QuadPart = (lFileTime.QuadPart << 32) | fileTime.dwLowDateTime;
174 return lFileTime;
175 }
176
display(const char * txt,SYSTEMTIME systime)177 static void display(const char *txt,SYSTEMTIME systime)
178 {
179 FILETIME fileTime;
180 BOOL ret = SystemTimeToFileTime(&systime,&fileTime);
181 assert(ret == TRUE);
182 LARGE_INTEGER ulFileTime = FileTimeToUL(fileTime);
183
184 const char * day="";
185 switch (systime.wDayOfWeek)
186 {
187 case 0:day = "Sunday";break;
188 case 1:day = "Monday";break;
189 case 2:day = "Tuesday";break;
190 case 3:day = "Wednesday";break;
191 case 4:day = "Thursday";break;
192 case 5:day = "Friday";break;
193 case 6:day = "Saturday";break;
194 }
195 g_StdOut<< txt << day << " "
196 << (int)systime.wYear << "/" << (int)systime.wMonth << "/" << (int)systime.wDay << " "
197 << (int)systime.wHour << ":" << (int)systime.wMinute << ":" << (int)systime.wSecond << ":"
198 << (int)systime.wMilliseconds
199 << " (" << (UInt64)ulFileTime.QuadPart << ")\n";
200 }
201
test_time()202 static void test_time()
203 {
204 time_t tps_unx = time(0);
205
206 printf("Test Time (1):\n");
207 printf("===========\n");
208 SYSTEMTIME systimeGM;
209 GetSystemTime(&systimeGM);
210
211 LARGE_INTEGER ul = UnixTimeToUL(tps_unx);
212 g_StdOut<<" unix time = " << (UInt64)tps_unx << " (" << (UInt64)ul.QuadPart << ")\n";
213
214 g_StdOut<<" gmtime : " << asctime(gmtime(&tps_unx))<<"\n";
215 g_StdOut<<" localtime : " << asctime(localtime(&tps_unx))<<"\n";
216
217 display(" GetSystemTime : ", systimeGM);
218 }
219
test_time2()220 static void test_time2()
221 {
222 printf("Test Time (2):\n");
223 printf("===========\n");
224 /* DosTime To utcFileTime */
225 UInt32 dosTime = 0x30d0094C;
226 FILETIME utcFileTime;
227 FILETIME localFileTime;
228
229 if (NTime::DosTimeToFileTime(dosTime, localFileTime))
230 {
231 if (!LocalFileTimeToFileTime(&localFileTime, &utcFileTime))
232 utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0;
233 }
234
235 printf(" - 0x%x => 0x%x 0x%x => 0x%x 0x%x\n",(unsigned)dosTime,
236 (unsigned)localFileTime.dwHighDateTime,(unsigned)localFileTime.dwLowDateTime,
237 (unsigned)utcFileTime.dwHighDateTime,(unsigned)utcFileTime.dwLowDateTime);
238
239
240 /* utcFileTime to DosTime */
241 FILETIME localFileTime2 = { 0, 0 };
242 UInt32 dosTime2 = 0;
243 FileTimeToLocalFileTime(&utcFileTime, &localFileTime2);
244 NTime::FileTimeToDosTime(localFileTime2, dosTime2);
245
246 printf(" - 0x%x <= 0x%x 0x%x <= 0x%x 0x%x\n",(unsigned)dosTime2,
247 (unsigned)localFileTime2.dwHighDateTime,(unsigned)localFileTime2.dwLowDateTime,
248 (unsigned)utcFileTime.dwHighDateTime,(unsigned)utcFileTime.dwLowDateTime);
249
250 assert(dosTime == dosTime2);
251 assert(localFileTime.dwHighDateTime == localFileTime2.dwHighDateTime);
252 assert(localFileTime.dwLowDateTime == localFileTime2.dwLowDateTime);
253 }
254
test_semaphore()255 static void test_semaphore()
256 {
257 g_StdOut << "\nTEST SEMAPHORE :\n";
258
259 NWindows::NSynchronization::CSynchro sync;
260 NWindows::NSynchronization::CSemaphoreWFMO sema;
261 bool bres;
262 DWORD waitResult;
263 int i;
264
265 sync.Create();
266 sema.Create(&sync,2,10);
267
268 g_StdOut << " - Release(1)\n";
269 for(i = 0 ;i < 8;i++)
270 {
271 // g_StdOut << " - Release(1) : "<< i << "\n";
272 bres = sema.Release(1);
273 assert(bres == S_OK);
274 }
275 // g_StdOut << " - Release(1) : done\n";
276 bres = sema.Release(1);
277 assert(bres == S_FALSE);
278
279 g_StdOut << " - WaitForMultipleObjects(INFINITE)\n";
280 HANDLE events[1] = { sema };
281 for(i=0;i<10;i++)
282 {
283 waitResult = ::WaitForMultipleObjects(1, events, FALSE, INFINITE);
284 assert(waitResult == WAIT_OBJECT_0);
285 }
286
287 g_StdOut << " Done\n";
288 }
289
290
291 /****************************************************************************************/
292
293
294 static int threads_count = 0;
295
thread_fct(void * param)296 static THREAD_FUNC_RET_TYPE thread_fct(void *param) {
297 threads_count++;
298 return 0;
299 }
300
301 #define MAX_THREADS 100000
302
test_thread(void)303 int test_thread(void) {
304 ::CThread thread;
305
306 Thread_Construct(&thread);
307
308 threads_count = 0;
309
310 printf("test_thread : %d threads\n",MAX_THREADS);
311
312 for(int i=0;i<MAX_THREADS;i++) {
313 Thread_Create(&thread, thread_fct, 0);
314
315 Thread_Wait(&thread);
316
317 Thread_Close(&thread);
318 }
319
320 assert(threads_count == MAX_THREADS);
321
322 return 0;
323 }
324
325 /****************************************************************************************/
main()326 int main() {
327
328 // return test_thread();
329
330
331 #ifdef HAVE_LOCALE
332 setlocale(LC_ALL,"");
333 #endif
334
335 #if defined(BIG_ENDIAN)
336 printf("BIG_ENDIAN : %d\n",(int)BIG_ENDIAN);
337 #endif
338 #if defined(LITTLE_ENDIAN)
339 printf("LITTLE_ENDIAN : %d\n",(int)LITTLE_ENDIAN);
340 #endif
341
342 printf("sizeof(Byte) : %d\n",(int)sizeof(Byte));
343 printf("sizeof(UInt16) : %d\n",(int)sizeof(UInt16));
344 printf("sizeof(UInt32) : %d\n",(int)sizeof(UInt32));
345 printf("sizeof(UINT32) : %d\n",(int)sizeof(UINT32));
346 printf("sizeof(UInt64) : %d\n",(int)sizeof(UInt64));
347 printf("sizeof(UINT64) : %d\n",(int)sizeof(UINT64));
348 printf("sizeof(void *) : %d\n",(int)sizeof(void *));
349 printf("sizeof(size_t) : %d\n",(int)sizeof(size_t));
350 printf("sizeof(ptrdiff_t) : %d\n",(int)sizeof(ptrdiff_t));
351 printf("sizeof(off_t) : %d\n",(int)sizeof(off_t));
352
353 union {
354 Byte b[2];
355 UInt16 s;
356 } u;
357 u.s = 0x1234;
358
359 if ((u.b[0] == 0x12) && (u.b[1] == 0x34)) {
360 printf("CPU : big endian\n");
361 } else if ((u.b[0] == 0x34) && (u.b[1] == 0x12)) {
362 printf("CPU : little endian\n");
363 } else {
364 printf("CPU : unknown endianess\n");
365 }
366
367 #if defined(HAVE_WCHAR__H) && defined(HAVE_MBSTOWCS) && defined(HAVE_WCSTOMBS)
368 test_mbs();
369 #endif
370
371 test_astring(12345);
372 test_split_astring();
373
374
375 test_time();
376
377 test_time2();
378
379 test_semaphore();
380
381 printf("\n### All Done ###\n\n");
382
383 return 0;
384 }
385
386