1 /* Test conditional operator with pointer operands
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 */
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. */
14 int testarray
[5] = {3,2,7,6,8};
19 setcond(unsigned char state
)
39 testCondOpPtrTypes1(void)
41 #if !defined(__SDCC_pdk14) // Lack of memory
42 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
50 ip1
= &(testarray
[2]);
54 ip2
= cond
? testarray
: ip1
; /* int[], int * ==> int * */
56 ip2
= cond
? ip1
: testarray
; /* int *, int[] ==> int * */
59 ip2
= cond
? testarray
: 0; /* int[], 0 ==> int * */
61 ip2
= cond
? ip1
: 0; /* int *, 0 ==> int * */
64 ip2
= cond
? testarray
: (void *)0; /* int[], NULL ==> int * */
66 ip2
= cond
? ip1
: (void *)0; /* int *, NULL ==> int * */
69 vp1
= &(testarray
[1]);
71 vp2
= cond
? testarray
: vp1
; /* int[], void * ==> void * */
73 vp2
= cond
? ip1
: vp1
; /* int *, void * ==> void * */
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);
96 testCondOpPtrTypes2(void)
98 #if !defined(__SDCC_pdk14) // Lack of memory
99 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
106 ip1
= &(testarray
[2]);
110 ip2
= cond
? testarray
: ip1
; /* int[], int * ==> int * */
112 ip2
= cond
? ip1
: testarray
; /* int *, int[] ==> int * */
115 ip2
= cond
? 0 : testarray
; /* 0, int[] ==> int * */
117 ip2
= cond
? 0: ip1
; /* 0, int * ==> int * */
120 ip2
= cond
? (void *)0 : testarray
; /* NULL, int[] ==> int * */
122 ip2
= cond
? (void *)0 : ip1
; /* NULL, int * ==> int * */
125 vp1
= &(testarray
[1]);
127 vp2
= cond
? vp1
: testarray
; /* void *, int[] ==> void * */
129 vp2
= cond
? vp1
: ip1
; /* void *, int * ==> void * */
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);
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
;
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);