Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / condopptr.c
bloba4f72ef04de5093d67102d2a6931e3f2a952509f
1 /* Test conditional operator with pointer operands
2 */
3 /* In addition to the usual promotion rules that apply to the operands */
4 /* to all operators, the conditional operator has some special rules */
5 /* that apply to pointer types (as well as arrays, since an array */
6 /* reference is interchangeable with a pointer to the first element): */
7 /* 1. If the operands are a pointer and NULL, the resultant type */
8 /* is that of the non-NULL pointer. NULL can be defined as either */
9 /* 0 or (void *)0. */
10 /* 2. If the operands are a non-void pointer and a void (non-NULL) */
11 /* pointer, the resultant type is that of the void pointer. */
12 #include <testfwk.h>
14 int testarray[5] = {3,2,7,6,8};
16 unsigned char cond;
18 void
19 setcond(unsigned char state)
21 cond = state;
24 int
25 deref1(int *ip)
27 return *ip;
30 int
31 deref2(char *cp)
33 int *ip = (void *)cp;
34 return *ip;
38 void
39 testCondOpPtrTypes1(void)
41 #if !defined(__SDCC_pdk14) // Lack of memory
42 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
43 int *ip1;
44 int *ip2;
45 void *vp1;
46 void *vp2;
47 char *cp;
50 ip1 = &(testarray[2]);
52 setcond(1);
54 ip2 = cond ? testarray : ip1; /* int[], int * ==> int * */
55 ASSERT (*ip2 == 3);
56 ip2 = cond ? ip1 : testarray; /* int *, int[] ==> int * */
57 ASSERT (*ip2 == 7);
59 ip2 = cond ? testarray : 0; /* int[], 0 ==> int * */
60 ASSERT (*ip2 == 3);
61 ip2 = cond ? ip1 : 0; /* int *, 0 ==> int * */
62 ASSERT (*ip2 == 7);
64 ip2 = cond ? testarray : (void *)0; /* int[], NULL ==> int * */
65 ASSERT (*ip2 == 3);
66 ip2 = cond ? ip1 : (void *)0; /* int *, NULL ==> int * */
67 ASSERT (*ip2 == 7);
69 vp1 = &(testarray[1]);
70 ip2 = testarray;
71 vp2 = cond ? testarray : vp1; /* int[], void * ==> void * */
72 ASSERT (vp2 == ip2);
73 vp2 = cond ? ip1 : vp1; /* int *, void * ==> void * */
74 ASSERT (vp2 == ip1);
75 cp = cond ? testarray : vp1; /* int[], void * ==> void * */
76 ASSERT ((int *)cp == ip2);
77 cp = cond ? ip1 : vp1; /* int *, void * ==> void * */
78 ASSERT ((int *)cp == ip1);
80 ASSERT (deref1 (cond ? testarray : ip1) == 3);
81 ASSERT (deref1 (cond ? ip1 : testarray) == 7);
82 ASSERT (deref1 (cond ? testarray : 0) == 3);
83 ASSERT (deref1 (cond ? ip1 : 0) == 7);
84 ASSERT (deref1 (cond ? testarray : (void *)0) == 3);
85 ASSERT (deref1 (cond ? ip1 : (void *)0) == 7);
87 /* These resolve to type void * and so are compatible with */
88 /* the char * parameter */
89 ASSERT (deref2 (cond ? testarray : vp1) == 3);
90 ASSERT (deref2 (cond ? ip1 : vp1) == 7);
91 #endif
92 #endif
95 void
96 testCondOpPtrTypes2(void)
98 #if !defined(__SDCC_pdk14) // Lack of memory
99 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
100 int *ip1;
101 int *ip2;
102 void *vp1;
103 void *vp2;
104 char *cp;
106 ip1 = &(testarray[2]);
108 setcond(0);
110 ip2 = cond ? testarray : ip1; /* int[], int * ==> int * */
111 ASSERT (*ip2 == 7);
112 ip2 = cond ? ip1 : testarray; /* int *, int[] ==> int * */
113 ASSERT (*ip2 == 3);
115 ip2 = cond ? 0 : testarray; /* 0, int[] ==> int * */
116 ASSERT (*ip2 == 3);
117 ip2 = cond ? 0: ip1; /* 0, int * ==> int * */
118 ASSERT (*ip2 == 7);
120 ip2 = cond ? (void *)0 : testarray; /* NULL, int[] ==> int * */
121 ASSERT (*ip2 == 3);
122 ip2 = cond ? (void *)0 : ip1; /* NULL, int * ==> int * */
123 ASSERT (*ip2 == 7);
125 vp1 = &(testarray[1]);
126 ip2 = testarray;
127 vp2 = cond ? vp1 : testarray; /* void *, int[] ==> void * */
128 ASSERT (vp2 == ip2);
129 vp2 = cond ? vp1 : ip1; /* void *, int * ==> void * */
130 ASSERT (vp2 == ip1);
131 cp = cond ? vp1 : testarray; /* void *, int[] ==> void * */
132 ASSERT ((int *)cp == ip2);
133 cp = cond ? vp1 : ip1; /* void *, int * ==> void * */
134 ASSERT ((int *)cp == ip1);
136 ASSERT (deref1 (cond ? testarray : ip1) == 7);
137 ASSERT (deref1 (cond ? ip1 : testarray) == 3);
138 ASSERT (deref1 (cond ? 0 : testarray) == 3);
139 ASSERT (deref1 (cond ? 0 : ip1) == 7);
140 ASSERT (deref1 (cond ? (void *)0 : testarray) == 3);
141 ASSERT (deref1 (cond ? (void *)0 : ip1) == 7);
143 /* These resolve to type void * and so are compatible with */
144 /* the char * parameter */
145 ASSERT (deref2 (cond ? vp1 : testarray) == 3);
146 ASSERT (deref2 (cond ? vp1 : ip1) == 7);
147 #endif
148 #endif
152 foo0(int a, int b, int c)
154 return a > 10 ? ++b, ++c : b + c;
158 foo1(int a, int b, int c, int d)
160 return a > 20 ? ++b, ++c, ++d + b + c : b < 100 ? c + ++d : c - --d;
163 void
164 testBug2412(void)
166 ASSERT (foo0 (2, 4, 5) == 9);
167 ASSERT (foo0 (72, 84, 5) == 6);
168 ASSERT (foo0 (7, 84, 75) == 159);
170 ASSERT (foo1 (110, 12, 13, 15) == 43);
171 ASSERT (foo1 (18, 12, 13, 15) == 29);
172 ASSERT (foo1 (12, 129, 13, 13) == 1);