summaryrefslogtreecommitdiff
path: root/gcc/testsuite/jit.dg/harness.h
blob: 0dc5ed1dad016d271be51d034569800222ae409e (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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
  Code shared between multiple testcases.

  This file contains "main" and support code.
  Each testcase should implement the following hooks:

    extern void
    create_code (gcc_jit_context *ctxt, void * user_data);

  and, #ifndef TEST_COMPILING_TO_FILE,

    extern void
    verify_code (gcc_jit_context *ctxt, gcc_jit_result *result);
 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

/* test-threads.c use threads, but dejagnu.h isn't thread-safe; there's a
   shared "buffer", and the counts of passed/failed etc are globals.

   The solution is to use macros to rename "pass" and "fail", replacing them
   with mutex-guarded alternatives.  */
#ifdef MAKE_DEJAGNU_H_THREADSAFE
#define pass dejagnu_pass
#define fail dejagnu_fail
#define note dejagnu_note
#endif

#include "jit-dejagnu.h"

#ifdef MAKE_DEJAGNU_H_THREADSAFE
#undef pass
#undef fail
#undef note
#endif

static char test[1024];

#define CHECK_NON_NULL(PTR) \
  do {                                       \
    if ((PTR) != NULL)                       \
      {                                      \
	pass ("%s: %s: %s is non-null",	     \
	      test, __func__, #PTR);	     \
      }                                      \
    else                                     \
      {                                      \
	fail ("%s: %s: %s is NULL",	     \
	      test, __func__, #PTR);	     \
	abort ();                            \
    }                                        \
  } while (0)

#define CHECK_VALUE(ACTUAL, EXPECTED) \
  do {                                       \
    if ((ACTUAL) == (EXPECTED))              \
      {                                      \
	pass ("%s: %s: actual: %s == expected: %s", \
	      test, __func__, #ACTUAL, #EXPECTED);  \
      }                                      \
    else                                     \
      {                                        \
	fail ("%s: %s: actual: %s != expected: %s", \
	      test, __func__, #ACTUAL, #EXPECTED);  \
	fprintf (stderr, "incorrect value\n"); \
	abort ();                              \
    }                                        \
  } while (0)

#define CHECK_DOUBLE_VALUE(ACTUAL, EXPECTED) \
  do {                                       \
    double expected = (EXPECTED);	     \
    double actual = (ACTUAL);		     \
    if (abs (actual - expected) < 0.00001)   \
      {                                      \
	pass ("%s: %s: actual: %s == expected: %s", \
	      __func__, test, #ACTUAL, #EXPECTED);  \
      }                                      \
    else                                     \
      {                                      \
	fail ("%s: %s: actual: %s != expected: %s", \
	      __func__, test, #ACTUAL, #EXPECTED);	   \
	fprintf (stderr, "incorrect value: %f\n", actual); \
	abort ();                            \
    }                                        \
  } while (0)

#define CHECK_STRING_VALUE(ACTUAL, EXPECTED) \
  check_string_value (__func__, (ACTUAL), (EXPECTED));

#define CHECK_STRING_STARTS_WITH(ACTUAL, EXPECTED_PREFIX) \
  check_string_starts_with (__func__, (ACTUAL), (EXPECTED_PREFIX));

#define CHECK_STRING_CONTAINS(ACTUAL, EXPECTED_SUBSTRING) \
  check_string_contains (__func__, #ACTUAL, (ACTUAL), (EXPECTED_SUBSTRING));

#define CHECK(COND) \
  do {					\
    if (COND)				\
      {				\
	pass ("%s: %s: %s", test, __func__, #COND);	\
      }				\
    else				\
      {				\
	fail ("%s: %s: %s", test, __func__, #COND);	\
	abort ();			\
      }				\
  } while (0)

#define CHECK_NO_ERRORS(CTXT) \
  do { \
    const char *err = gcc_jit_context_get_first_error (CTXT); \
    if (err) \
      fail ("%s: %s: error unexpectedly occurred: %s", test, __func__, err); \
    else \
      pass ("%s: %s: no errors occurred", test, __func__); \
  } while (0)

/* Hooks that testcases should provide.  */
extern void
create_code (gcc_jit_context *ctxt, void * user_data);

#ifndef TEST_COMPILING_TO_FILE
extern void
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result);
#endif

extern void check_string_value (const char *funcname,
				const char *actual, const char *expected);

extern void
check_string_starts_with (const char *funcname,
			  const char *actual,
			  const char *expected_prefix);

extern void
check_string_contains (const char *funcname,
		       const char *name,
		       const char *actual,
		       const char *expected_substring);

/* Implement framework needed for turning the testcase hooks into an
   executable.  test-combination.c and test-threads.c each combine multiple
   testcases into larger testcases, so we have COMBINED_TEST as a way of
   temporarily turning off this part of harness.h.  */
#ifndef COMBINED_TEST

void check_string_value (const char *funcname,
			 const char *actual, const char *expected)
{
  if (actual && !expected)
    {
      fail ("%s: %s: actual: \"%s\" != expected: NULL",
	    funcname, test, actual);
	fprintf (stderr, "incorrect value\n");
	abort ();
    }
    if (expected && !actual)
      {
	fail ("%s: %s: actual: NULL != expected: \"%s\"",
	      funcname, test, expected);
	fprintf (stderr, "incorrect value\n");
	abort ();
      }
    if (actual && expected)
      {
	if (strcmp (actual, expected))
	  {
	    fail ("%s: %s: actual: \"%s\" != expected: \"%s\"",
		  test, funcname, actual, expected);
	    fprintf (stderr, "incorrect valuen");
	    abort ();
	  }
	pass ("%s: %s: actual: \"%s\" == expected: \"%s\"",
	      test, funcname, actual, expected);
      }
    else
      pass ("%s: actual: NULL == expected: NULL");
}

void
check_string_starts_with (const char *funcname,
			  const char *actual,
			  const char *expected_prefix)
{
  if (!actual)
    {
      fail ("%s: %s: actual: NULL != expected prefix: \"%s\"",
	    test, funcname, expected_prefix);
      fprintf (stderr, "incorrect value\n");
      abort ();
    }

  if (strncmp (actual, expected_prefix, strlen (expected_prefix)))
    {
      fail ("%s: %s: actual: \"%s\" did not begin with expected prefix: \"%s\"",
	    test, funcname, actual, expected_prefix);
      fprintf (stderr, "incorrect value\n");
      abort ();
    }

  pass ("%s: actual: \"%s\" begins with expected prefix: \"%s\"",
	test, actual, expected_prefix);
}

void
check_string_contains (const char *funcname,
		       const char *name,
		       const char *actual,
		       const char *expected_substring)
{
  if (!actual)
    {
      fail ("%s: %s, %s: actual: NULL does not contain expected substring: \"%s\"",
	    test, funcname, name, expected_substring);
      fprintf (stderr, "incorrect value\n");
      abort ();
    }

  if (!strstr (actual, expected_substring))
    {
      fail ("%s: %s: %s: actual: \"%s\" did not contain expected substring: \"%s\"",
	    test, funcname, name, actual, expected_substring);
      fprintf (stderr, "incorrect value\n");
      abort ();
    }

  pass ("%s: %s: %s: found substring: \"%s\"",
	test, funcname, name, expected_substring);
}

#ifndef TEST_ESCHEWS_SET_OPTIONS
static void set_options (gcc_jit_context *ctxt, const char *argv0)
{
  /* Set up options.  */
  gcc_jit_context_set_str_option (
    ctxt,
    GCC_JIT_STR_OPTION_PROGNAME,
    argv0);
  gcc_jit_context_set_int_option (
    ctxt,
    GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL,
    3);
  gcc_jit_context_set_bool_option (
    ctxt,
    GCC_JIT_BOOL_OPTION_DEBUGINFO,
    1);
  gcc_jit_context_set_bool_option (
    ctxt,
    GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE,
    0);
  gcc_jit_context_set_bool_option (
    ctxt,
    GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE,
    0);
  gcc_jit_context_set_bool_option (
    ctxt,
    GCC_JIT_BOOL_OPTION_SELFCHECK_GC,
    1);
  gcc_jit_context_set_bool_option (
    ctxt,
    GCC_JIT_BOOL_OPTION_DUMP_SUMMARY,
    0);
}
#endif /* #ifndef TEST_ESCHEWS_SET_OPTIONS */

/* Concatenate two strings.  The result must be released using "free".  */

char *
concat_strings (const char *prefix, const char *suffix)
{
  char *result = (char *)malloc (strlen (prefix) + strlen (suffix) + 1);
  if (!result)
    {
      fail ("malloc failure");
      return NULL;
    }
  strcpy (result, prefix);
  strcpy (result + strlen (prefix), suffix);
  result[strlen (prefix) + strlen (suffix)] = '\0';
  return result;
}

#ifndef TEST_ESCHEWS_TEST_JIT
/* Set up logging to a logfile of the form "test-FOO.exe.log.txt".

   For example,
     SRCDIR/gcc/testsuite/jit.dg/test-hello-world.c
   is built as:
     BUILDDIR/gcc/testsuite/jit/test-hello-world.c.exe
   and is logged to
     BUILDDIR/gcc/testsuite/jit/test-hello-world.c.exe.log.txt

   The logfile must be closed by the caller.

   Note that not every testcase enables logging.  */
static FILE *
set_up_logging (gcc_jit_context *ctxt, const char *argv0)
{
  const char *logfile_name_suffix = ".log.txt";
  char *logfile_name = NULL;
  FILE *logfile = NULL;

  /* Build a logfile name of the form "test-FOO.exe.log.txt".  */
  logfile_name = concat_strings (argv0, logfile_name_suffix);
  if (!logfile_name)
    return NULL;
  logfile = fopen (logfile_name, "w");
  CHECK_NON_NULL (logfile);
  free (logfile_name);

  if (logfile)
    gcc_jit_context_set_logfile (ctxt, logfile, 0, 0);

  return logfile;
}

/* Exercise the API entrypoint:
     gcc_jit_context_dump_reproducer_to_file
   by calling it on the context, using the path expected by jit.exp.  */
static void
dump_reproducer (gcc_jit_context *ctxt, const char *argv0)
{
  char *reproducer_name;
  reproducer_name = concat_strings (argv0, ".reproducer.c");
  if (!reproducer_name)
    return;
  note ("%s: writing reproducer to %s", test, reproducer_name);
  gcc_jit_context_dump_reproducer_to_file (ctxt, reproducer_name);
  free (reproducer_name);
}

/* Run one iteration of the test.  */
static void
test_jit (const char *argv0, void *user_data)
{
  gcc_jit_context *ctxt;
  FILE *logfile;
#ifndef TEST_COMPILING_TO_FILE
  gcc_jit_result *result;
#endif

#ifdef TEST_COMPILING_TO_FILE
  unlink (OUTPUT_FILENAME);
#endif

  ctxt = gcc_jit_context_acquire ();
  if (!ctxt)
    {
      fail ("gcc_jit_context_acquire failed");
      return;
    }

  logfile = set_up_logging (ctxt, argv0);

  set_options (ctxt, argv0);

  create_code (ctxt, user_data);

  dump_reproducer (ctxt, argv0);

#ifdef TEST_COMPILING_TO_FILE
  gcc_jit_context_compile_to_file (ctxt,
				   (OUTPUT_KIND),
				   (OUTPUT_FILENAME));
  CHECK_NO_ERRORS (ctxt);
#else /* #ifdef TEST_COMPILING_TO_FILE */
  /* This actually calls into GCC and runs the build, all
     in a mutex for now.  */
  result = gcc_jit_context_compile (ctxt);

  verify_code (ctxt, result);
#endif

  gcc_jit_context_release (ctxt);

#ifndef TEST_COMPILING_TO_FILE
  /* Once we're done with the code, this unloads the built .so file: */
  gcc_jit_result_release (result);
#endif

  if (logfile)
    fclose (logfile);
}
#endif /* #ifndef TEST_ESCHEWS_TEST_JIT */

/* We want to prefix all unit test results with the test, but dejagnu.exp's
   host_execute appears to get confused by the leading "./" of argv0,
   leading to all tests simply reporting as a single period character ".".

   Hence strip out the final component of the path to the program name,
   so that we can use that in unittest reports.  */
const char*
extract_progname (const char *argv0)
{
  const char *p;

  p = argv0 + strlen (argv0);
  while (p != argv0 && p[-1] != '/')
    --p;
  return p;
}

#ifndef TEST_PROVIDES_MAIN
int
main (int argc, char **argv)
{
  int i;

  for (i = 1; i <= 5; i++)
    {
      snprintf (test, sizeof (test),
		"%s iteration %d of %d",
                extract_progname (argv[0]),
                i, 5);

      //printf ("ITERATION %d\n", i);
      test_jit (argv[0], NULL);
      //printf ("\n");
    }

  totals ();

  return 0;
}
#endif /* #ifndef TEST_PROVIDES_MAIN */

#endif /* #ifndef COMBINED_TEST */