summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/common/palsuite.h
blob: b77ca2ead5edb82616c3f0c17c2999e5c12e114d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================================
**
** Source:  palsuite.h
**
** Purpose: Define constants and implement functions that are useful to
**          multiple function categories. If common functions are useful
**          only amongst the test cases for a particular function, a separate
**          header file is placed in the root of those test cases.
**
**
**==========================================================================*/

#ifndef __PALSUITE_H__
#define __PALSUITE_H__

#include <pal_assert.h>
#include <pal.h>
#include <palprivate.h>

enum
{
    PASS = 0,
    FAIL = 1
};


void Trace(const char *format, ...)
{
    va_list arglist;
	
    va_start(arglist, format);

    vprintf(format, arglist);

    va_end(arglist);
}

void Fail(const char *format, ...)
{
    va_list arglist;
	
    va_start(arglist, format);

    vprintf(format, arglist);    

    va_end(arglist);
    printf("\n");

    // This will exit the test process
    PAL_TerminateEx(FAIL);
}

#ifdef PAL_PERF

int __cdecl Test_Main(int argc, char **argv);
int PAL_InitializeResult = 0;
static const char PALTEST_LOOP_ENV[]="PALTEST_LOOP_COUNT";

int __cdecl main(int argc, char **argv)
{
  int lastMainResult=0;

  int loopCount=1; // default: run the test's main once
  int loopIndex=0;
  char *szPerfLoopEnv = NULL;

  // Run PAL_Initialize once, save off the result. Any failures here
  // will be detected later by calls to PAL_Initialize in the test's main.
  PAL_InitializeResult = PAL_Initialize(argc, argv);

  // Check the environment to see if we need to run the test's main
  // multiple times. Ideally, we want to do this before PAL_Initialize so
  // that the overhead of checking the environment is not included in the
  // time between PAL_Initialize and PAL_Terminate. However, getenv in PAL
  // can be run only after PAL_Initialize.
  szPerfLoopEnv = getenv(PALTEST_LOOP_ENV);
  if (szPerfLoopEnv != NULL) 
  {
     loopCount = atoi(szPerfLoopEnv);
     if (loopCount <= 0) loopCount = 1;
  }

  // call the test's actual main in a loop
  for(loopIndex=0; loopIndex<loopCount; loopIndex++) {
      lastMainResult = Test_Main(argc, argv);
  }

  // call PAL_Terminate for real
  PAL_TerminateEx(lastMainResult);

  return lastMainResult;
}

// Test's calls to PAL_Initialize and PAL_Terminate are redirected
// to these bogus functions. These rely on PAL_Initialize and PAL_Terminate
// being called by the 'main' above.
#define PAL_Initialize(a, b) Bogus_PAL_Initialize(a, b)
#define PAL_Terminate()  Bogus_PAL_Terminate()
int Bogus_PAL_Initialize(int argc, char* argv[])
{
  // PAL_Initialize has already been called by the real main.
  // Just return the result.
  return PAL_InitializeResult;
}

void Bogus_PAL_Terminate()
{
  // Don't call PAL_Terminate. It will be called later by the
  // real main.
  return;
}

// Rename the main provided by the test case
#define main Test_Main

#endif // PAL_PERF

#ifdef BIGENDIAN
inline ULONG   VAL32(ULONG x)
{
    return( ((x & 0xFF000000L) >> 24) |               
            ((x & 0x00FF0000L) >>  8) |              
            ((x & 0x0000FF00L) <<  8) |              
            ((x & 0x000000FFL) << 24) );
}
#define th_htons(w)  (w)
#else   // BIGENDIAN
#define VAL32(x)    (x)
#define th_htons(w)  (((w) >> 8) | ((w) << 8))
#endif  // BIGENDIAN

#define _countof(_array) (sizeof(_array)/sizeof(_array[0]))

WCHAR* convert(const char * aString) 
{
    int size;
    WCHAR* wideBuffer;

    size = MultiByteToWideChar(CP_ACP,0,aString,-1,NULL,0);
    wideBuffer = (WCHAR*) malloc(size*sizeof(WCHAR));
    if (wideBuffer == NULL)
    {
        Fail("ERROR: Unable to allocate memory!\n");
    }
    MultiByteToWideChar(CP_ACP,0,aString,-1,wideBuffer,size);
    return wideBuffer;
}

char* convertC(const WCHAR * wString) 
{
    int size;
    char * MultiBuffer = NULL;

    size = WideCharToMultiByte(CP_ACP,0,wString,-1,MultiBuffer,0,NULL,NULL);
    MultiBuffer = (char*) malloc(size);
    if (MultiBuffer == NULL)
    {
        Fail("ERROR: Unable to allocate memory!\n");
    }
    WideCharToMultiByte(CP_ACP,0,wString,-1,MultiBuffer,size,NULL,NULL);
    return MultiBuffer;
}

UINT64 GetHighPrecisionTimeStamp(LARGE_INTEGER performanceFrequency)
{
    LARGE_INTEGER ts;
    if (!QueryPerformanceCounter(&ts))
    {
        Fail("ERROR: Unable to query performance counter!\n");      
    }
    
    return ts.QuadPart / (performanceFrequency.QuadPart / 1000);    
}

#endif