1 /* Test for _Atomic in C11. Basic execution tests for atomic loads
2 and stores. */
3 /* { dg-do run } */
4 /* { dg-options "-std=c11 -pedantic-errors" } */
5
6 extern void abort (void);
7 extern void exit (int);
8 extern int memcmp (const void *, const void *, __SIZE_TYPE__);
9
10 #define CMPLX(X, Y) __builtin_complex ((X), (Y))
11
12 #define TEST_SIMPLE_ASSIGN(TYPE, VALUE) \
13 do \
14 { \
15 static volatile _Atomic (TYPE) a, b = (TYPE) (VALUE); \
16 if (a != 0) \
17 abort (); \
18 if (b != ((TYPE) (VALUE))) \
19 abort (); \
20 if ((a = b) != ((TYPE) (VALUE))) \
21 abort (); \
22 if (a != ((TYPE) (VALUE))) \
23 abort (); \
24 } \
25 while (0)
26
27 #define TEST_SIMPLE_ASSIGN_ARITH(VALUE) \
28 do \
29 { \
30 TEST_SIMPLE_ASSIGN (_Bool, (VALUE)); \
31 TEST_SIMPLE_ASSIGN (char, (VALUE)); \
32 TEST_SIMPLE_ASSIGN (signed char, (VALUE)); \
33 TEST_SIMPLE_ASSIGN (unsigned char, (VALUE)); \
34 TEST_SIMPLE_ASSIGN (signed short, (VALUE)); \
35 TEST_SIMPLE_ASSIGN (unsigned short, (VALUE)); \
36 TEST_SIMPLE_ASSIGN (signed int, (VALUE)); \
37 TEST_SIMPLE_ASSIGN (unsigned int, (VALUE)); \
38 TEST_SIMPLE_ASSIGN (signed long, (VALUE)); \
39 TEST_SIMPLE_ASSIGN (unsigned long, (VALUE)); \
40 TEST_SIMPLE_ASSIGN (signed long long, (VALUE)); \
41 TEST_SIMPLE_ASSIGN (unsigned long long, (VALUE)); \
42 TEST_SIMPLE_ASSIGN (float, (VALUE)); \
43 TEST_SIMPLE_ASSIGN (double, (VALUE)); \
44 TEST_SIMPLE_ASSIGN (long double, (VALUE)); \
45 TEST_SIMPLE_ASSIGN (_Complex float, (VALUE)); \
46 TEST_SIMPLE_ASSIGN (_Complex double, (VALUE)); \
47 TEST_SIMPLE_ASSIGN (_Complex long double, (VALUE)); \
48 } \
49 while (0)
50
51 static void
test_simple_assign(void)52 test_simple_assign (void)
53 {
54 TEST_SIMPLE_ASSIGN_ARITH (0);
55 TEST_SIMPLE_ASSIGN_ARITH (1);
56 TEST_SIMPLE_ASSIGN_ARITH (2);
57 TEST_SIMPLE_ASSIGN_ARITH (-1);
58 TEST_SIMPLE_ASSIGN_ARITH (1ULL << 63);
59 TEST_SIMPLE_ASSIGN_ARITH (1.5);
60 TEST_SIMPLE_ASSIGN_ARITH (CMPLX (2.5, 3.5));
61 static int i;
62 TEST_SIMPLE_ASSIGN (int *, 0);
63 TEST_SIMPLE_ASSIGN (int *, &i);
64 struct s { short a[1024]; };
65 struct s init, copy;
66 _Atomic struct s s1, s2;
67 for (int j = 0; j < 1024; j++)
68 init.a[j] = j;
69 copy = (s1 = init);
70 if (memcmp (&init, ©, sizeof init) != 0)
71 abort ();
72 copy = (s2 = s1);
73 if (memcmp (&init, ©, sizeof init) != 0)
74 abort ();
75 copy = s1;
76 if (memcmp (&init, ©, sizeof init) != 0)
77 abort ();
78 copy = s2;
79 if (memcmp (&init, ©, sizeof init) != 0)
80 abort ();
81 }
82
83 int
main(void)84 main (void)
85 {
86 test_simple_assign ();
87 exit (0);
88 }
89