struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / gcc-torture-execute-memchr-1.c
blob88089d591f3d81d25de7ceb6b553c0897d196392
1 /*
2 memchr-1.c from the execute part of the gcc torture tests.
3 */
5 #include <testfwk.h>
7 #include <stddef.h>
9 #ifdef __SDCC
10 #pragma disable_warning 147
11 #endif
13 /* PR tree-optimization/86711 - wrong folding of memchr
15 Verify that memchr() of arrays initialized with string literals
16 where the nul doesn't fit in the array doesn't find the nul. */
18 extern void* memchr (const void*, int, size_t);
20 static const char c = '1';
21 static const char s1[1] = "1";
22 static const char s4[4] = "1234";
24 static const char s4_2[2][4] = { "1234", "5678" };
25 static const char s5_3[3][5] = { "12345", "6789", "01234" };
27 volatile int v0 = 0;
28 volatile int v1 = 1;
29 volatile int v2 = 2;
30 volatile int v3 = 3;
31 volatile int v4 = 3;
33 void test_narrow (void)
36 int i0 = 0;
37 int i1 = i0 + 1;
38 int i2 = i1 + 1;
39 int i3 = i2 + 1;
40 int i4 = i3 + 1;
42 ASSERT (memchr ("" + 1, 0, 0) == 0);
44 ASSERT (memchr (&c, 0, sizeof c) == 0);
45 ASSERT (memchr (&c + 1, 0, sizeof c - 1) == 0);
46 ASSERT (memchr (&c + i1, 0, sizeof c - i1) == 0);
47 ASSERT (memchr (&c + v1, 0, sizeof c - v1) == 0);
49 ASSERT (memchr (s1, 0, sizeof s1) == 0);
50 ASSERT (memchr (s1 + 1, 0, sizeof s1 - 1) == 0);
51 ASSERT (memchr (s1 + i1, 0, sizeof s1 - i1) == 0);
52 ASSERT (memchr (s1 + v1, 0, sizeof s1 - v1) == 0);
54 ASSERT (memchr (&s1, 0, sizeof s1) == 0);
55 ASSERT (memchr (&s1 + 1, 0, sizeof s1 - 1) == 0);
56 ASSERT (memchr (&s1 + i1, 0, sizeof s1 - i1) == 0);
57 ASSERT (memchr (&s1 + v1, 0, sizeof s1 - v1) == 0);
58 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
59 ASSERT (memchr (&s1[0], 0, sizeof s1) == 0);
60 ASSERT (memchr (&s1[0] + 1, 0, sizeof s1 - 1) == 0);
61 ASSERT (memchr (&s1[0] + i1, 0, sizeof s1 - i1) == 0);
62 ASSERT (memchr (&s1[0] + v1, 0, sizeof s1 - v1) == 0);
64 ASSERT (memchr (&s1[i0], 0, sizeof s1) == 0);
65 ASSERT (memchr (&s1[i0] + 1, 0, sizeof s1 - 1) == 0);
66 ASSERT (memchr (&s1[i0] + i1, 0, sizeof s1 - i1) == 0);
67 ASSERT (memchr (&s1[i0] + v1, 0, sizeof s1 - v1) == 0);
69 ASSERT (memchr (&s1[v0], 0, sizeof s1) == 0);
70 ASSERT (memchr (&s1[v0] + 1, 0, sizeof s1 - 1) == 0);
71 ASSERT (memchr (&s1[v0] + i1, 0, sizeof s1 - i1) == 0);
72 ASSERT (memchr (&s1[v0] + v1, 0, sizeof s1 - v1) == 0);
75 ASSERT (memchr (s4 + i0, 0, sizeof s4 - i0) == 0);
76 ASSERT (memchr (s4 + i1, 0, sizeof s4 - i1) == 0);
77 ASSERT (memchr (s4 + i2, 0, sizeof s4 - i2) == 0);
78 ASSERT (memchr (s4 + i3, 0, sizeof s4 - i3) == 0);
79 ASSERT (memchr (s4 + i4, 0, sizeof s4 - i4) == 0);
81 ASSERT (memchr (s4 + v0, 0, sizeof s4 - v0) == 0);
82 ASSERT (memchr (s4 + v1, 0, sizeof s4 - v1) == 0);
83 ASSERT (memchr (s4 + v2, 0, sizeof s4 - v2) == 0);
84 ASSERT (memchr (s4 + v3, 0, sizeof s4 - v3) == 0);
85 ASSERT (memchr (s4 + v4, 0, sizeof s4 - v4) == 0);
87 ASSERT (memchr (s4_2, 0, sizeof s4_2) == 0);
89 ASSERT (memchr (s4_2[0], 0, sizeof s4_2[0]) == 0);
90 ASSERT (memchr (s4_2[1], 0, sizeof s4_2[1]) == 0);
92 ASSERT (memchr (s4_2[0] + 1, 0, sizeof s4_2[0] - 1) == 0);
93 ASSERT (memchr (s4_2[1] + 2, 0, sizeof s4_2[1] - 2) == 0);
94 ASSERT (memchr (s4_2[1] + 3, 0, sizeof s4_2[1] - 3) == 0);
96 ASSERT (memchr (s4_2[v0], 0, sizeof s4_2[v0]) == 0);
97 ASSERT (memchr (s4_2[v0] + 1, 0, sizeof s4_2[v0] - 1) == 0);
100 /* The following calls must find the nul. */
101 ASSERT (memchr ("", 0, 1) != 0);
102 ASSERT (memchr (s5_3, 0, sizeof s5_3) == &s5_3[1][4]);
104 ASSERT (memchr (&s5_3[0][0] + i0, 0, sizeof s5_3 - i0) == &s5_3[1][4]);
105 ASSERT (memchr (&s5_3[0][0] + i1, 0, sizeof s5_3 - i1) == &s5_3[1][4]);
106 ASSERT (memchr (&s5_3[0][0] + i2, 0, sizeof s5_3 - i2) == &s5_3[1][4]);
107 ASSERT (memchr (&s5_3[0][0] + i4, 0, sizeof s5_3 - i4) == &s5_3[1][4]);
109 ASSERT (memchr (&s5_3[1][i0], 0, sizeof s5_3[1] - i0) == &s5_3[1][4]);
110 #endif
113 void test_wide (void) { }
115 void
116 testTortureExecute (void)
118 test_narrow ();
119 test_wide ();