summaryrefslogtreecommitdiff
path: root/test/regression/initializers.c
blob: 31f73734743404bb5b18b9601e2d5652315d5e3f (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
#include <stdio.h>

int x0;

char x1 = 'x';

int x2 = 12345;

double x3 = 3.14159;

char x4[4] = { 'a', 'b', 'c', 'd' };

int x5[10] = { 1, 2, 3 };

struct { int y; int z; } x6 = { 4, 5 };

struct { int y; char z; } x7 = { 6, 'u' };

struct { char y; int z; } x8 = { 'v', 7 };

struct { char y[9]; double z; } x9 = { { 'a', 'b' }, 2.718 };

struct {
  struct { char y; int z; } u;
  double v;
} x10 = { { 'v', 7 }, 2.718 };

float x11 = 1 + 1 / 3.14159;

double x12 = 1 / 3.14159 + 1;

typedef enum { AAA , BBB } MyEnum;

const MyEnum x13[2] = { AAA, BBB };

int * x14 = &x2;

struct { char * y; int * z; float * u; double * v; } x15 = { x4, x5, &x11, &x12 };

unsigned char * x16[3] = {
  (unsigned char *) (x4 + 1),
  (unsigned char *) &x4[2],
  ((unsigned char *) x4) + 3 };

char x17[] = "Hello!";

char * x18 = "Hello!";

char * x19[2] = { "Hello", "world!" };

char x20[3] = "Hello!";

char x21[10] = "Hello!";

char * x22 = &(x10.u.y);

/* Initializer can refer to ident just declared */
struct list { int hd; struct list * tl; } x23 = { sizeof(x23), &x23 };

/* Watch out for aliases of char types */
typedef unsigned char byte;
byte x24[] = "/*B*/";

/* Another tricky case with string literals */
char * x25[] = { "/tmp" };

/* One more */
char x26[] = { "world" };

static void print_chars(char * s, int sz)
{
  int i;
  for (i = 0; i < sz; i++) {
    if (s[i] >= 32 && s[i] < 127)
      printf("'%c', ", s[i]);
    else
      printf("%d, ", s[i]);
  }
}

int main()
{
  int i;

  printf("x0 = %d\n", x0);
  printf("x1 = '%c'\n", x1);
  printf("x2 = %d\n", x2);
  printf("x3 = %.5f\n", x3);
  printf("x4 = { '%c', '%c', '%c', '%c' }\n",
         x4[0], x4[1], x4[2], x4[3]);
  printf("x5 = { ");
  for (i = 0; i < 10; i++) printf("%d, ", x5[i]);
  printf("}\n");
  printf("x6 = { %d, %d }\n", x6.y, x6.z);
  printf("x7 = { %d, '%c' }\n", x7.y, x7.z);
  printf("x8 = { '%c', %d }\n", x8.y, x8.z);
  printf("x9 = { { ");
  print_chars(x9.y, 9);
  printf("}, %.3f }\n", x9.z);
  printf("x10 = { { '%c', %d }, %.3f }\n",
         x10.u.y, x10.u.z, x10.v);
  printf("x11 = %.10f\n", x11);
  printf("x12 = %.10f\n", x12);
  printf("x13 = { %d, %d }\n", x13[0], x13[1]);
  if (x14 == &x2) printf("x14 ok\n"); else printf("x14 error\n");
  if (x15.y == x4 && x15.z == x5 && x15.u == &x11 && x15.v == &x12)
    printf("x15 ok\n");
  else
    printf("x15 error\n");
  if (x16[0] == (unsigned char *) x4 + 1
      && x16[1] == (unsigned char *) x4 + 2
      && x16[2] == (unsigned char *) x4 + 3)
    printf("x16 ok\n");
  else
    printf("x16 error\n");
  printf("x17[%d] = { ", (int) sizeof(x17));
  print_chars(x17, sizeof(x17));
  printf("}\n");
  printf("x18 = \"%s\"\n", x18);
  printf("x19 = { \"%s\", \"%s\" }\n", x19[0], x19[1]);
  printf("x20 = { ");
  print_chars(x20, sizeof(x20));
  printf("}\n");
  printf("x21 = { ");
  print_chars(x21, sizeof(x21));
  printf("}\n");
  if (x22 == &(x10.u.y))
    printf("x22 ok\n");
  else
    printf("x22 error\n");
  printf("x23 = { hd = %d, tl = %s }\n",
         x23.hd, x23.tl == &x23 ? "ok" : "ERROR");
  printf("x24[%d] = { ", (int) sizeof(x24));
  print_chars((char *) x24, sizeof(x24));
  printf("}\n");
  printf("x25[%d] = { \"%s\" }\n", (int) sizeof(x25), x25[0]);
  printf("x26[%d] = { ", (int) sizeof(x26));
  print_chars(x26, sizeof(x26));
  printf("}\n");
  return 0;
}