@@ -2088,6 +2088,209 @@ int main() {
20882088}
20892089EOF
20902090
2091+ # Test const local variables
2092+ try_ 50 << EOF
2093+ int main() {
2094+ const int x = 50;
2095+ return x;
2096+ }
2097+ EOF
2098+
2099+ # Test const local variable initialization
2100+ try_ 75 << EOF
2101+ int main() {
2102+ int y = 25;
2103+ const int x = y + 50;
2104+ return x;
2105+ }
2106+ EOF
2107+
2108+ # Test const pointer to non-const data (pointer itself is const)
2109+ try_ 42 << EOF
2110+ int main() {
2111+ int x = 42;
2112+ int * const p = &x;
2113+ return *p;
2114+ }
2115+ EOF
2116+
2117+ # Test function returning const pointer (using global instead of static)
2118+ try_ 100 << EOF
2119+ int global_value = 100;
2120+
2121+ const int *get_ptr() {
2122+ return &global_value;
2123+ }
2124+
2125+ int main() {
2126+ const int *p = get_ptr();
2127+ return *p;
2128+ }
2129+ EOF
2130+
2131+ # Test const in nested expressions
2132+ try_ 30 << EOF
2133+ int compute(const int *a, const int *b) {
2134+ return (*a) * 2 + (*b) * 3;
2135+ }
2136+
2137+ int main() {
2138+ int x = 6;
2139+ int y = 6;
2140+ return compute(&x, &y); /* 6*2 + 6*3 = 30 */
2141+ }
2142+ EOF
2143+
2144+ # Test const with conditional operator
2145+ try_ 25 << EOF
2146+ int choose_value(int cond, const int *a, const int *b) {
2147+ return cond ? *a : *b;
2148+ }
2149+
2150+ int main() {
2151+ int x = 25;
2152+ int y = 50;
2153+ return choose_value(1, &x, &y);
2154+ }
2155+ EOF
2156+
2157+ # Test const cast from non-const
2158+ try_ 88 << EOF
2159+ int use_const(const int *p) {
2160+ return *p;
2161+ }
2162+
2163+ int main() {
2164+ int val = 88;
2165+ int *p = &val;
2166+ return use_const((const int *)p);
2167+ }
2168+ EOF
2169+
2170+ # Test const with multiple indirection levels
2171+ try_ 33 << EOF
2172+ int get_deep_value(const int * const * const *ppp) {
2173+ return ***ppp;
2174+ }
2175+
2176+ int main() {
2177+ int val = 33;
2178+ int *p = &val;
2179+ int **pp = &p;
2180+ int ***ppp = &pp;
2181+ return get_deep_value(ppp);
2182+ }
2183+ EOF
2184+
2185+ # Test const parameter with pointer arithmetic
2186+ try_ 15 << EOF
2187+ int sum_range(const int *start, const int *end) {
2188+ int sum = 0;
2189+ const int *p = start;
2190+ while (p < end) {
2191+ sum += *p;
2192+ p++;
2193+ }
2194+ return sum;
2195+ }
2196+
2197+ int main() {
2198+ int arr[3] = {5, 5, 5};
2199+ return sum_range(arr, arr + 3);
2200+ }
2201+ EOF
2202+
2203+ # Test const with simple function call
2204+ try_ 10 << EOF
2205+ int double_value(int n) {
2206+ return n * 2;
2207+ }
2208+
2209+ int apply_double(const int *x) {
2210+ return double_value(*x);
2211+ }
2212+
2213+ int main() {
2214+ int val = 5;
2215+ return apply_double(&val);
2216+ }
2217+ EOF
2218+
2219+ # Test const struct pointer parameter
2220+ try_ 150 << EOF
2221+ typedef struct {
2222+ int x;
2223+ int y;
2224+ } Point;
2225+
2226+ int point_sum(const Point *p) {
2227+ return p->x + p->y;
2228+ }
2229+
2230+ int main() {
2231+ Point pt = {75, 75};
2232+ return point_sum(&pt);
2233+ }
2234+ EOF
2235+
2236+ # Test const array elements access
2237+ try_ 21 << EOF
2238+ int sum_const_array(const int arr[3]) {
2239+ return arr[0] + arr[1] + arr[2];
2240+ }
2241+
2242+ int main() {
2243+ int data[3] = {7, 7, 7};
2244+ return sum_const_array(data);
2245+ }
2246+ EOF
2247+
2248+ # Test const pointer with multiple dereferences
2249+ try_ 40 << EOF
2250+ int process(const int *x, const int *y) {
2251+ int val1 = *x;
2252+ int val2 = *y;
2253+ return val2; /* returns second value */
2254+ }
2255+
2256+ int main() {
2257+ int a = 30;
2258+ int b = 40;
2259+ return process(&a, &b);
2260+ }
2261+ EOF
2262+
2263+ # Test const pointer increment in loop
2264+ try_ 3 << EOF
2265+ int count_until(const int *arr, int target) {
2266+ const int *p = arr;
2267+ int count = 0;
2268+ while (*p != target) {
2269+ count++;
2270+ p++;
2271+ }
2272+ return count;
2273+ }
2274+
2275+ int main() {
2276+ int data[5] = {1, 2, 3, 10, 5};
2277+ return count_until(data, 10); /* returns 3 */
2278+ }
2279+ EOF
2280+
2281+ # Test const pointer reference passing
2282+ try_ 7 << EOF
2283+ int sum_via_ptr(const int *a, const int *b) {
2284+ return *a + *b;
2285+ }
2286+
2287+ int main() {
2288+ int x = 3;
2289+ int y = 4;
2290+ return sum_via_ptr(&x, &y);
2291+ }
2292+ EOF
2293+
20912294# Category: Ternary Operator
20922295begin_category " Ternary Operator" " Testing conditional ?: operator"
20932296
0 commit comments