|
| 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