| 1 |
/* Copyright (C) 2000, 2001 Free Software Foundation. |
| 2 |
|
| 3 |
Ensure all expected transformations of builtin fputs occur and that |
| 4 |
we honor side effects in the stream argument. |
| 5 |
|
| 6 |
Written by Kaveh R. Ghazi, 10/30/2000. */ |
| 7 |
|
| 8 |
#define _GNU_SOURCE /* For fputs_unlocked. */ |
| 9 |
#include <stdio.h> |
| 10 |
extern void abort(void); |
| 11 |
|
| 12 |
/* Not all systems have fputs_unlocked. See fputs-lib.c. */ |
| 13 |
extern int (fputs_unlocked) (const char *, FILE *); |
| 14 |
|
| 15 |
int i; |
| 16 |
|
| 17 |
void |
| 18 |
main_test(void) |
| 19 |
{ |
| 20 |
FILE *s_array[] = {stdout, NULL}, **s_ptr = s_array; |
| 21 |
const char *const s1 = "hello world"; |
| 22 |
|
| 23 |
fputs ("", *s_ptr); |
| 24 |
fputs ("\n", *s_ptr); |
| 25 |
fputs ("bye", *s_ptr); |
| 26 |
fputs (s1, *s_ptr); |
| 27 |
fputs (s1+5, *s_ptr); |
| 28 |
fputs (s1+10, *s_ptr); |
| 29 |
fputs (s1+11, *s_ptr); |
| 30 |
|
| 31 |
/* Check side-effects when transforming fputs -> NOP. */ |
| 32 |
fputs ("", *s_ptr++); |
| 33 |
if (s_ptr != s_array+1 || *s_ptr != 0) |
| 34 |
abort(); |
| 35 |
|
| 36 |
/* Check side-effects when transforming fputs -> fputc. */ |
| 37 |
s_ptr = s_array; |
| 38 |
fputs ("\n", *s_ptr++); |
| 39 |
if (s_ptr != s_array+1 || *s_ptr != 0) |
| 40 |
abort(); |
| 41 |
|
| 42 |
/* Check side-effects when transforming fputs -> fwrite. */ |
| 43 |
s_ptr = s_array; |
| 44 |
fputs ("hello\n", *s_ptr++); |
| 45 |
if (s_ptr != s_array+1 || *s_ptr != 0) |
| 46 |
abort(); |
| 47 |
|
| 48 |
/* Test at least one instance of the __builtin_ style. We do this |
| 49 |
to ensure that it works and that the prototype is correct. */ |
| 50 |
s_ptr = s_array; |
| 51 |
__builtin_fputs ("", *s_ptr); |
| 52 |
/* These builtin stubs are called by __builtin_fputs, ensure their |
| 53 |
prototypes are set correctly too. */ |
| 54 |
__builtin_fputc ('\n', *s_ptr); |
| 55 |
__builtin_fwrite ("hello\n", 1, 6, *s_ptr); |
| 56 |
/* Check the unlocked style, these evaluate to nothing to avoid |
| 57 |
problems on systems without the unlocked functions. */ |
| 58 |
fputs_unlocked ("", *s_ptr); |
| 59 |
__builtin_fputs_unlocked ("", *s_ptr); |
| 60 |
|
| 61 |
/* Check side-effects in conditional expression. */ |
| 62 |
s_ptr = s_array; |
| 63 |
fputs (i++ ? "f" : "x", *s_ptr++); |
| 64 |
if (s_ptr != s_array+1 || *s_ptr != 0 || i != 1) |
| 65 |
abort(); |
| 66 |
fputs (--i ? "\n" : "\n", *--s_ptr); |
| 67 |
if (s_ptr != s_array || i != 0) |
| 68 |
abort(); |
| 69 |
} |