Skip to content

Commit b4e339e

Browse files
committed
Some tests for _Generic
1 parent e44143a commit b4e339e

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

test/regression/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ TESTS=int32 int64 floats floats-basics floats-lit \
1616
funct3 expr5 struct7 struct8 struct11 struct12 casts1 casts2 char1 \
1717
sizeof1 sizeof2 binops bool for1 for2 switch switch2 compound \
1818
decl1 bitfields9 ptrs3 \
19-
parsing krfun ifconv
19+
parsing krfun ifconv generic
2020

2121
# Can run, but only in compiled mode, and have reference output in Results
2222

test/regression/Results/generic

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1
2+
-2
3+
42
4+
1.23
5+
4.57
6+
other
7+
other

test/regression/generic.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
/* Some tests from GCC, c11-generic-1.c */
5+
6+
void check (int n)
7+
{
8+
if (n) abort ();
9+
}
10+
11+
void test1(void)
12+
{
13+
int n = 0;
14+
15+
check (_Generic (n++, int: 0));
16+
/* _Generic should not evaluate its argument. */
17+
check (n);
18+
19+
check (_Generic (n, double: n++, default: 0));
20+
check (n);
21+
22+
/* Qualifiers are removed for the purpose of type matching. */
23+
const int cn = 0;
24+
check (_Generic (cn, int: 0, default: n++));
25+
check (n);
26+
check (_Generic ((const int) n, int: 0, default: n++));
27+
check (n);
28+
29+
/* Arrays decay to pointers. */
30+
int a[1];
31+
const int ca[1];
32+
check (_Generic (a, int *: 0, const int *: n++));
33+
check (n);
34+
check (_Generic (ca, const int *: 0, int *: n++));
35+
check (n);
36+
37+
/* Functions decay to pointers. */
38+
extern void f (void);
39+
check (_Generic (f, void (*) (void): 0, default: n++));
40+
check (n);
41+
42+
/* _Noreturn is not part of the function type. */
43+
check (_Generic (&abort, void (*) (void): 0, default: n++));
44+
check (n);
45+
46+
/* Integer promotions do not occur. */
47+
short s;
48+
check (_Generic (s, short: 0, int: n++));
49+
check (n);
50+
}
51+
52+
53+
/* Some tests from Clang, Sema/generic-selection.c */
54+
55+
void test2(int n)
56+
{
57+
int a1[_Generic(0, int: 1, short: 2, float: 3, default: 4) == 1 ? 1 : -1];
58+
int a2[_Generic(0, default: 1, short: 2, float: 3, int: 4) == 4 ? 1 : -1];
59+
int a3[_Generic(0L, int: 1, short: 2, float: 3, default: 4) == 4 ? 1 : -1];
60+
int a4[_Generic(0L, default: 1, short: 2, float: 3, int: 4) == 1 ? 1 : -1];
61+
int a5[_Generic(0, int: 1, short: 2, float: 3) == 1 ? 1 : -1];
62+
int a6[_Generic(0, short: 1, float: 2, int: 3) == 3 ? 1 : -1];
63+
int a7[_Generic("test", char *: 1, default: 2) == 1 ? 1 : -1];
64+
int a8[_Generic(test1, void (*)(void): 1, default: 2) == 1 ? 1 : -1];
65+
int b8[_Generic(test2, void (*)(void): 1, default: 2) == 2 ? 1 : -1];
66+
int c8[_Generic(test2, void (*)(int): 1, default: 2) == 1 ? 1 : -1];
67+
const int i = 12;
68+
int a9[_Generic(i, int: 1, default: 2) == 1 ? 1 : -1];
69+
(void)_Generic(*(int *)0, int: 1);
70+
}
71+
72+
/* Misc tests */
73+
74+
void print_int(long long x)
75+
{
76+
printf("%lld\n", x);
77+
}
78+
79+
void print_fp(double x)
80+
{
81+
printf("%.2f\n", x);
82+
}
83+
84+
void print_other(void * x)
85+
{
86+
printf("other\n");
87+
}
88+
89+
#define PRINT(x) \
90+
_Generic((x), \
91+
int: print_int, long: print_int, long long: print_int, \
92+
float: print_fp, double: print_fp, \
93+
default: print_other) (x)
94+
95+
int main()
96+
{
97+
test1();
98+
test2(5);
99+
PRINT(1);
100+
PRINT(-2L);
101+
PRINT(42LL);
102+
PRINT(1.23f);
103+
PRINT(4.56789);
104+
PRINT("hello!");
105+
PRINT(NULL);
106+
}
107+

0 commit comments

Comments
 (0)