1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/* Test conditional operator with pointer operands
*/
/* In addition to the usual promotion rules that apply to the operands */
/* to all operators, the conditional operator has some special rules */
/* that apply to pointer types (as well as arrays, since an array */
/* reference is interchangable with a pointer to the first element): */
/* 1. If the operands are a pointer and NULL, the resultant type */
/* is that of the non-NULL pointer. NULL can be defined as either */
/* 0 or (void *)0. */
/* 2. If the operands are a non-void pointer and a void (non-NULL) */
/* pointer, the resultant type is that of the void pointer. */
#include <testfwk.h>
int testarray[5] = {3,2,7,6,8};
unsigned char cond;
void
setcond(unsigned char state)
{
cond = state;
}
int
deref1(int *ip)
{
return *ip;
}
int
deref2(char *cp)
{
int *ip = (void *)cp;
return *ip;
}
void
testCondOpPtrTypes1(void)
{
#if !defined(__SDCC_pdk14) // Lack of memory
int *ip1;
int *ip2;
void *vp1;
void *vp2;
char *cp;
ip1 = &(testarray[2]);
setcond(1);
ip2 = cond ? testarray : ip1; /* int[], int * ==> int * */
ASSERT (*ip2 == 3);
ip2 = cond ? ip1 : testarray; /* int *, int[] ==> int * */
ASSERT (*ip2 == 7);
ip2 = cond ? testarray : 0; /* int[], 0 ==> int * */
ASSERT (*ip2 == 3);
ip2 = cond ? ip1 : 0; /* int *, 0 ==> int * */
ASSERT (*ip2 == 7);
ip2 = cond ? testarray : (void *)0; /* int[], NULL ==> int * */
ASSERT (*ip2 == 3);
ip2 = cond ? ip1 : (void *)0; /* int *, NULL ==> int * */
ASSERT (*ip2 == 7);
vp1 = &(testarray[1]);
ip2 = testarray;
vp2 = cond ? testarray : vp1; /* int[], void * ==> void * */
ASSERT (vp2 == ip2);
vp2 = cond ? ip1 : vp1; /* int *, void * ==> void * */
ASSERT (vp2 == ip1);
cp = cond ? testarray : vp1; /* int[], void * ==> void * */
ASSERT ((int *)cp == ip2);
cp = cond ? ip1 : vp1; /* int *, void * ==> void * */
ASSERT ((int *)cp == ip1);
ASSERT (deref1 (cond ? testarray : ip1) == 3);
ASSERT (deref1 (cond ? ip1 : testarray) == 7);
ASSERT (deref1 (cond ? testarray : 0) == 3);
ASSERT (deref1 (cond ? ip1 : 0) == 7);
ASSERT (deref1 (cond ? testarray : (void *)0) == 3);
ASSERT (deref1 (cond ? ip1 : (void *)0) == 7);
/* These resolve to type void * and so are compatible with */
/* the char * parameter */
ASSERT (deref2 (cond ? testarray : vp1) == 3);
ASSERT (deref2 (cond ? ip1 : vp1) == 7);
#endif
}
void
testCondOpPtrTypes2(void)
{
#if !defined(__SDCC_pdk14) // Lack of memory
int *ip1;
int *ip2;
void *vp1;
void *vp2;
char *cp;
ip1 = &(testarray[2]);
setcond(0);
ip2 = cond ? testarray : ip1; /* int[], int * ==> int * */
ASSERT (*ip2 == 7);
ip2 = cond ? ip1 : testarray; /* int *, int[] ==> int * */
ASSERT (*ip2 == 3);
ip2 = cond ? 0 : testarray; /* 0, int[] ==> int * */
ASSERT (*ip2 == 3);
ip2 = cond ? 0: ip1; /* 0, int * ==> int * */
ASSERT (*ip2 == 7);
ip2 = cond ? (void *)0 : testarray; /* NULL, int[] ==> int * */
ASSERT (*ip2 == 3);
ip2 = cond ? (void *)0 : ip1; /* NULL, int * ==> int * */
ASSERT (*ip2 == 7);
vp1 = &(testarray[1]);
ip2 = testarray;
vp2 = cond ? vp1 : testarray; /* void *, int[] ==> void * */
ASSERT (vp2 == ip2);
vp2 = cond ? vp1 : ip1; /* void *, int * ==> void * */
ASSERT (vp2 == ip1);
cp = cond ? vp1 : testarray; /* void *, int[] ==> void * */
ASSERT ((int *)cp == ip2);
cp = cond ? vp1 : ip1; /* void *, int * ==> void * */
ASSERT ((int *)cp == ip1);
ASSERT (deref1 (cond ? testarray : ip1) == 7);
ASSERT (deref1 (cond ? ip1 : testarray) == 3);
ASSERT (deref1 (cond ? 0 : testarray) == 3);
ASSERT (deref1 (cond ? 0 : ip1) == 7);
ASSERT (deref1 (cond ? (void *)0 : testarray) == 3);
ASSERT (deref1 (cond ? (void *)0 : ip1) == 7);
/* These resolve to type void * and so are compatible with */
/* the char * parameter */
ASSERT (deref2 (cond ? vp1 : testarray) == 3);
ASSERT (deref2 (cond ? vp1 : ip1) == 7);
#endif
}
int
foo0(int a, int b, int c)
{
return a > 10 ? ++b, ++c : b + c;
}
int
foo1(int a, int b, int c, int d)
{
return a > 20 ? ++b, ++c, ++d + b + c : b < 100 ? c + ++d : c - --d;
}
void
testBug2412(void)
{
ASSERT (foo0 (2, 4, 5) == 9);
ASSERT (foo0 (72, 84, 5) == 6);
ASSERT (foo0 (7, 84, 75) == 159);
ASSERT (foo1 (110, 12, 13, 15) == 43);
ASSERT (foo1 (18, 12, 13, 15) == 29);
ASSERT (foo1 (12, 129, 13, 13) == 1);
}
|