-
Notifications
You must be signed in to change notification settings - Fork 38
/
std_testcase.h
349 lines (287 loc) · 9.08 KB
/
std_testcase.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#ifndef _STD_TESTCASE_H
#define _STD_TESTCASE_H
/* This file exists in order to:
* 1) Include lots of standardized headers in one place
* 2) To avoid #include-ing things in the middle of your code
* #include-ing in the middle of a C/C++ file is apt to cause compiler errors
* 3) To get good #define's in place
*
* In reality you need a complex interaction of scripts of build processes to do
* this correctly (i.e., autoconf)
*/
#ifdef _WIN32
/* Ensure the CRT does not disable the "insecure functions"
* Ensure not to generate warnings about ANSI C functions.
*/
#define _CRT_SECURE_NO_DEPRECATE 1
#define _CRT_SECURE_NO_WARNING 1
/* We do not use _malloca as it sometimes allocates memory on the heap and we
* do not want this to happen in test cases that expect stack-allocated memory
* Also, _alloca_s cannot be used as it is deprecated and has been replaced
* with _malloca */
#define ALLOCA _alloca
/* disable warnings about use of POSIX names for functions like execl()
Visual Studio wants you to use the ISO C++ name, such as _execl() */
#pragma warning(disable:4996)
#else
/* Linux/GNU wants this macro, otherwise stdint.h and limits.h are mostly useless */
# define __STDC_LIMIT_MACROS 1
#define ALLOCA alloca
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <time.h>
#include <limits.h>
#include <string.h>
#include <stdint.h>
#ifndef _WIN32
/* SIZE_MAX, int64_t, etc. are in this file on Linux */
# include <stdint.h>
#endif
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <io.h> /* for open/close etc */
#endif
#include <inttypes.h> // for PRId64
#include <wctype.h>
#ifndef _WIN32
#include <wchar.h>
#endif
#ifdef __cplusplus
#include <new> // for placement new
/* classes used in some test cases as a custom type */
class TwoIntsClass
{
public: // Needed to access variables from label files
int intOne;
int intTwo;
};
class OneIntClass
{
public: // Needed to access variables from label files
int intOne;
};
#endif
#ifndef __cplusplus
/* Define true and false, which are included in C++, but not in C */
#define true 1
#define false 0
#endif /* end ifndef __cplusplus */
/* rand only returns 15 bits, so we xor 3 calls together to get the full result (13 bits overflow, but that is okay) */
// shifting signed values might overflow and be undefined
#define URAND31() (((unsigned)rand()<<30) ^ ((unsigned)rand()<<15) ^ rand())
// choose to produce a positive or a negative number. Note: conditional only evaluates one URAND31
#define RAND32() ((int)(rand() & 1 ? URAND31() : -URAND31() - 1))
/* rand only returns 15 bits, so we xor 5 calls together to get the full result (11 bits overflow, but that is okay) */
// shifting signed values might overflow and be undefined
#define URAND63() (((uint64_t)rand()<<60) ^ ((uint64_t)rand()<<45) ^ ((uint64_t)rand()<<30) ^ ((uint64_t)rand()<<15) ^ rand())
// choose to produce a positive or a negative number. Note: conditional only evaluates one URAND63
#define RAND64() ((int64_t)(rand() & 1 ? URAND63() : -URAND63() - 1))
/* struct used in some test cases as a custom type */
typedef struct _twoIntsStruct
{
int intOne;
int intTwo;
} twoIntsStruct;
#ifdef __cplusplus
extern "C" {
#endif
/* The variables below are declared "const", so a tool should
be able to identify that reads of these will always return their
initialized values. */
extern const int GLOBAL_CONST_TRUE; /* true */
extern const int GLOBAL_CONST_FALSE; /* false */
extern const int GLOBAL_CONST_FIVE; /* 5 */
/* The variables below are not defined as "const", but are never
assigned any other value, so a tool should be able to identify that
reads of these will always return their initialized values. */
extern int globalTrue; /* true */
extern int globalFalse; /* false */
extern int globalFive; /* 5 */
#ifdef __cplusplus
}
#endif
/* header file to define functions in io.c. Not named io.h
because that name is already taken by a system header on
Windows */
#ifdef __cplusplus
extern "C" {
#endif
void printLine (const char * line)
{
if(line != NULL)
{
printf("%s\n", line);
}
}
void printWLine (const wchar_t * line)
{
if(line != NULL)
{
wprintf(L"%ls\n", line);
}
}
void printIntLine (int intNumber)
{
printf("%d\n", intNumber);
}
void printShortLine (short shortNumber)
{
printf("%hd\n", shortNumber);
}
void printFloatLine (float floatNumber)
{
printf("%f\n", floatNumber);
}
void printLongLine (long longNumber)
{
printf("%ld\n", longNumber);
}
void printLongLongLine (int64_t longLongIntNumber)
{
printf("%" PRId64 "\n", longLongIntNumber);
}
void printSizeTLine (size_t sizeTNumber)
{
printf("%zu\n", sizeTNumber);
}
void printHexCharLine (char charHex)
{
printf("%02x\n", charHex);
}
void printWcharLine(wchar_t wideChar)
{
/* ISO standard dictates wchar_t can be ref'd only with %ls, so we must make a
* string to print a wchar */
wchar_t s[2];
s[0] = wideChar;
s[1] = L'\0';
printf("%ls\n", s);
}
void printUnsignedLine(unsigned unsignedNumber)
{
printf("%u\n", unsignedNumber);
}
void printHexUnsignedCharLine(unsigned char unsignedCharacter)
{
printf("%02x\n", unsignedCharacter);
}
void printDoubleLine(double doubleNumber)
{
printf("%g\n", doubleNumber);
}
void printStructLine (const twoIntsStruct * structTwoIntsStruct)
{
printf("%d -- %d\n", structTwoIntsStruct->intOne, structTwoIntsStruct->intTwo);
}
void printBytesLine(const unsigned char * bytes, size_t numBytes)
{
size_t i;
for (i = 0; i < numBytes; ++i)
{
printf("%02x", bytes[i]);
}
puts(""); /* output newline */
}
/* Decode a string of hex characters into the bytes they represent. The second
* parameter specifies the length of the output buffer. The number of bytes
* actually written to the output buffer is returned. */
size_t decodeHexChars(unsigned char * bytes, size_t numBytes, const char * hex)
{
size_t numWritten = 0;
/* We can't sscanf directly into the byte array since %02x expects a pointer to int,
* not a pointer to unsigned char. Also, since we expect an unbroken string of hex
* characters, we check for that before calling sscanf; otherwise we would get a
* framing error if there's whitespace in the input string. */
while (numWritten < numBytes && isxdigit(hex[2 * numWritten]) && isxdigit(hex[2 * numWritten + 1]))
{
int byte;
sscanf(&hex[2 * numWritten], "%02x", &byte);
bytes[numWritten] = (unsigned char) byte;
++numWritten;
}
return numWritten;
}
/* Decode a string of hex characters into the bytes they represent. The second
* parameter specifies the length of the output buffer. The number of bytes
* actually written to the output buffer is returned. */
size_t decodeHexWChars(unsigned char * bytes, size_t numBytes, const wchar_t * hex)
{
size_t numWritten = 0;
/* We can't swscanf directly into the byte array since %02x expects a pointer to int,
* not a pointer to unsigned char. Also, since we expect an unbroken string of hex
* characters, we check for that before calling swscanf; otherwise we would get a
* framing error if there's whitespace in the input string. */
while (numWritten < numBytes && iswxdigit(hex[2 * numWritten]) && iswxdigit(hex[2 * numWritten + 1]))
{
int byte;
swscanf(&hex[2 * numWritten], L"%02x", &byte);
bytes[numWritten] = (unsigned char) byte;
++numWritten;
}
return numWritten;
}
/* The two functions always return 1 or 0, so a tool should be able to
identify that uses of these functions will always return these values */
int globalReturnsTrue()
{
return 1;
}
int globalReturnsFalse()
{
return 0;
}
int globalReturnsTrueOrFalse()
{
return (rand() % 2);
}
/* The variables below are declared "const", so a tool should
be able to identify that reads of these will always return their
initialized values. */
const int GLOBAL_CONST_TRUE = 1; /* true */
const int GLOBAL_CONST_FALSE = 0; /* false */
const int GLOBAL_CONST_FIVE = 5;
/* The variables below are not defined as "const", but are never
assigned any other value, so a tool should be able to identify that
reads of these will always return their initialized values. */
int globalTrue = 1; /* true */
int globalFalse = 0; /* false */
int globalFive = 5;
/* define a bunch of these as empty functions so that if a test case forgets
to make their's statically scoped, we'll get a linker error */
void good1() { }
void good2() { }
void good3() { }
void good4() { }
void good5() { }
void good6() { }
void good7() { }
void good8() { }
void good9() { }
/* shouldn't be used, but just in case */
void bad1() { }
void bad2() { }
void bad3() { }
void bad4() { }
void bad5() { }
void bad6() { }
void bad7() { }
void bad8() { }
void bad9() { }
/* define global argc and argv */
#ifdef __cplusplus
extern "C" {
#endif
int globalArgc = 0;
char** globalArgv = NULL;
/* Define some global variables that will get argc and argv */
extern int globalArgc;
extern char** globalArgv;
#ifdef __cplusplus
}
#endif
#endif