cosmopolitan/third_party/chibicc/test/union_test.c
Justine Tunney 8da931a7f6 Add chibicc
This program popped up on Hacker News recently. It's the only modern
compiler I've ever seen that doesn't have dependencies and is easily
modified. So I added all of the missing GNU extensions I like to use
which means it might be possible soon to build on non-Linux and have
third party not vendor gcc binaries.
2020-12-06 16:20:21 -08:00

132 lines
2.4 KiB
C

#include "third_party/chibicc/test/test.h"
int main() {
ASSERT(8, ({
union {
int a;
char b[6];
} x;
sizeof(x);
}));
ASSERT(3, ({
union {
int a;
char b[4];
} x;
x.a = 515;
x.b[0];
}));
ASSERT(2, ({
union {
int a;
char b[4];
} x;
x.a = 515;
x.b[1];
}));
ASSERT(0, ({
union {
int a;
char b[4];
} x;
x.a = 515;
x.b[2];
}));
ASSERT(0, ({
union {
int a;
char b[4];
} x;
x.a = 515;
x.b[3];
}));
ASSERT(3, ({
union {
int a, b;
} x, y;
x.a = 3;
y.a = 5;
y = x;
y.a;
}));
ASSERT(3, ({
union {
struct {
int a, b;
} c;
} x, y;
x.c.b = 3;
y.c.b = 5;
y = x;
y.c.b;
}));
ASSERT(0xef, ({
union {
struct {
unsigned char a, b, c, d;
};
long e;
} x;
x.e = 0xdeadbeef;
x.a;
}));
ASSERT(0xbe, ({
union {
struct {
unsigned char a, b, c, d;
};
long e;
} x;
x.e = 0xdeadbeef;
x.b;
}));
ASSERT(0xad, ({
union {
struct {
unsigned char a, b, c, d;
};
long e;
} x;
x.e = 0xdeadbeef;
x.c;
}));
ASSERT(0xde, ({
union {
struct {
unsigned char a, b, c, d;
};
long e;
} x;
x.e = 0xdeadbeef;
x.d;
}));
ASSERT(3, ({
struct {
union {
int a, b;
};
union {
int c, d;
};
} x;
x.a = 3;
x.b;
}));
ASSERT(5, ({
struct {
union {
int a, b;
};
union {
int c, d;
};
} x;
x.d = 5;
x.c;
}));
return 0;
}