1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*============================================================================
6**
7** Source: palsuite.h
8**
9** Purpose: Define constants and implement functions that are useful to
10** multiple function categories. If common functions are useful
11** only amongst the test cases for a particular function, a separate
12** header file is placed in the root of those test cases.
13**
14**
15**==========================================================================*/
16
17#ifndef __PALSUITE_H__
18#define __PALSUITE_H__
19
20#include <pal_assert.h>
21#include <pal.h>
22#include <palprivate.h>
23
24enum
25{
26 PASS = 0,
27 FAIL = 1
28};
29
30
31void Trace(const char *format, ...)
32{
33 va_list arglist;
34
35 va_start(arglist, format);
36
37 vprintf(format, arglist);
38
39 va_end(arglist);
40}
41
42void Fail(const char *format, ...)
43{
44 va_list arglist;
45
46 va_start(arglist, format);
47
48 vprintf(format, arglist);
49
50 va_end(arglist);
51 printf("\n");
52
53 // This will exit the test process
54 PAL_TerminateEx(FAIL);
55}
56
57#ifdef PAL_PERF
58
59int __cdecl Test_Main(int argc, char **argv);
60int PAL_InitializeResult = 0;
61static const char PALTEST_LOOP_ENV[]="PALTEST_LOOP_COUNT";
62
63int __cdecl main(int argc, char **argv)
64{
65 int lastMainResult=0;
66
67 int loopCount=1; // default: run the test's main once
68 int loopIndex=0;
69 char *szPerfLoopEnv = NULL;
70
71 // Run PAL_Initialize once, save off the result. Any failures here
72 // will be detected later by calls to PAL_Initialize in the test's main.
73 PAL_InitializeResult = PAL_Initialize(argc, argv);
74
75 // Check the environment to see if we need to run the test's main
76 // multiple times. Ideally, we want to do this before PAL_Initialize so
77 // that the overhead of checking the environment is not included in the
78 // time between PAL_Initialize and PAL_Terminate. However, getenv in PAL
79 // can be run only after PAL_Initialize.
80 szPerfLoopEnv = getenv(PALTEST_LOOP_ENV);
81 if (szPerfLoopEnv != NULL)
82 {
83 loopCount = atoi(szPerfLoopEnv);
84 if (loopCount <= 0) loopCount = 1;
85 }
86
87 // call the test's actual main in a loop
88 for(loopIndex=0; loopIndex<loopCount; loopIndex++) {
89 lastMainResult = Test_Main(argc, argv);
90 }
91
92 // call PAL_Terminate for real
93 PAL_TerminateEx(lastMainResult);
94
95 return lastMainResult;
96}
97
98// Test's calls to PAL_Initialize and PAL_Terminate are redirected
99// to these bogus functions. These rely on PAL_Initialize and PAL_Terminate
100// being called by the 'main' above.
101#define PAL_Initialize(a, b) Bogus_PAL_Initialize(a, b)
102#define PAL_Terminate() Bogus_PAL_Terminate()
103int Bogus_PAL_Initialize(int argc, char* argv[])
104{
105 // PAL_Initialize has already been called by the real main.
106 // Just return the result.
107 return PAL_InitializeResult;
108}
109
110void Bogus_PAL_Terminate()
111{
112 // Don't call PAL_Terminate. It will be called later by the
113 // real main.
114 return;
115}
116
117// Rename the main provided by the test case
118#define main Test_Main
119
120#endif // PAL_PERF
121
122#ifdef BIGENDIAN
123inline ULONG VAL32(ULONG x)
124{
125 return( ((x & 0xFF000000L) >> 24) |
126 ((x & 0x00FF0000L) >> 8) |
127 ((x & 0x0000FF00L) << 8) |
128 ((x & 0x000000FFL) << 24) );
129}
130#define th_htons(w) (w)
131#else // BIGENDIAN
132#define VAL32(x) (x)
133#define th_htons(w) (((w) >> 8) | ((w) << 8))
134#endif // BIGENDIAN
135
136#define _countof(_array) (sizeof(_array)/sizeof(_array[0]))
137
138WCHAR* convert(const char * aString)
139{
140 int size;
141 WCHAR* wideBuffer;
142
143 size = MultiByteToWideChar(CP_ACP,0,aString,-1,NULL,0);
144 wideBuffer = (WCHAR*) malloc(size*sizeof(WCHAR));
145 if (wideBuffer == NULL)
146 {
147 Fail("ERROR: Unable to allocate memory!\n");
148 }
149 MultiByteToWideChar(CP_ACP,0,aString,-1,wideBuffer,size);
150 return wideBuffer;
151}
152
153char* convertC(const WCHAR * wString)
154{
155 int size;
156 char * MultiBuffer = NULL;
157
158 size = WideCharToMultiByte(CP_ACP,0,wString,-1,MultiBuffer,0,NULL,NULL);
159 MultiBuffer = (char*) malloc(size);
160 if (MultiBuffer == NULL)
161 {
162 Fail("ERROR: Unable to allocate memory!\n");
163 }
164 WideCharToMultiByte(CP_ACP,0,wString,-1,MultiBuffer,size,NULL,NULL);
165 return MultiBuffer;
166}
167
168UINT64 GetHighPrecisionTimeStamp(LARGE_INTEGER performanceFrequency)
169{
170 LARGE_INTEGER ts;
171 if (!QueryPerformanceCounter(&ts))
172 {
173 Fail("ERROR: Unable to query performance counter!\n");
174 }
175
176 return ts.QuadPart / (performanceFrequency.QuadPart / 1000);
177}
178
179#endif
180
181
182
183