@@ -2024,6 +2024,209 @@ int main() {
20242024}
20252025EOF
20262026
2027+ # Test const local variables
2028+ try_ 50 << EOF
2029+ int main() {
2030+ const int x = 50;
2031+ return x;
2032+ }
2033+ EOF
2034+
2035+ # Test const local variable initialization
2036+ try_ 75 << EOF
2037+ int main() {
2038+ int y = 25;
2039+ const int x = y + 50;
2040+ return x;
2041+ }
2042+ EOF
2043+
2044+ # Test const pointer to non-const data (pointer itself is const)
2045+ try_ 42 << EOF
2046+ int main() {
2047+ int x = 42;
2048+ int * const p = &x;
2049+ return *p;
2050+ }
2051+ EOF
2052+
2053+ # Test function returning const pointer (using global instead of static)
2054+ try_ 100 << EOF
2055+ int global_value = 100;
2056+
2057+ const int *get_ptr() {
2058+ return &global_value;
2059+ }
2060+
2061+ int main() {
2062+ const int *p = get_ptr();
2063+ return *p;
2064+ }
2065+ EOF
2066+
2067+ # Test const in nested expressions
2068+ try_ 30 << EOF
2069+ int compute(const int *a, const int *b) {
2070+ return (*a) * 2 + (*b) * 3;
2071+ }
2072+
2073+ int main() {
2074+ int x = 6;
2075+ int y = 6;
2076+ return compute(&x, &y); /* 6*2 + 6*3 = 30 */
2077+ }
2078+ EOF
2079+
2080+ # Test const with conditional operator
2081+ try_ 25 << EOF
2082+ int choose_value(int cond, const int *a, const int *b) {
2083+ return cond ? *a : *b;
2084+ }
2085+
2086+ int main() {
2087+ int x = 25;
2088+ int y = 50;
2089+ return choose_value(1, &x, &y);
2090+ }
2091+ EOF
2092+
2093+ # Test const cast from non-const
2094+ try_ 88 << EOF
2095+ int use_const(const int *p) {
2096+ return *p;
2097+ }
2098+
2099+ int main() {
2100+ int val = 88;
2101+ int *p = &val;
2102+ return use_const((const int *)p);
2103+ }
2104+ EOF
2105+
2106+ # Test const with multiple indirection levels
2107+ try_ 33 << EOF
2108+ int get_deep_value(const int * const * const *ppp) {
2109+ return ***ppp;
2110+ }
2111+
2112+ int main() {
2113+ int val = 33;
2114+ int *p = &val;
2115+ int **pp = &p;
2116+ int ***ppp = &pp;
2117+ return get_deep_value(ppp);
2118+ }
2119+ EOF
2120+
2121+ # Test const parameter with pointer arithmetic
2122+ try_ 15 << EOF
2123+ int sum_range(const int *start, const int *end) {
2124+ int sum = 0;
2125+ const int *p = start;
2126+ while (p < end) {
2127+ sum += *p;
2128+ p++;
2129+ }
2130+ return sum;
2131+ }
2132+
2133+ int main() {
2134+ int arr[3] = {5, 5, 5};
2135+ return sum_range(arr, arr + 3);
2136+ }
2137+ EOF
2138+
2139+ # Test const with simple function call
2140+ try_ 10 << EOF
2141+ int double_value(int n) {
2142+ return n * 2;
2143+ }
2144+
2145+ int apply_double(const int *x) {
2146+ return double_value(*x);
2147+ }
2148+
2149+ int main() {
2150+ int val = 5;
2151+ return apply_double(&val);
2152+ }
2153+ EOF
2154+
2155+ # Test const struct pointer parameter
2156+ try_ 150 << EOF
2157+ typedef struct {
2158+ int x;
2159+ int y;
2160+ } Point;
2161+
2162+ int point_sum(const Point *p) {
2163+ return p->x + p->y;
2164+ }
2165+
2166+ int main() {
2167+ Point pt = {75, 75};
2168+ return point_sum(&pt);
2169+ }
2170+ EOF
2171+
2172+ # Test const array elements access
2173+ try_ 21 << EOF
2174+ int sum_const_array(const int arr[3]) {
2175+ return arr[0] + arr[1] + arr[2];
2176+ }
2177+
2178+ int main() {
2179+ int data[3] = {7, 7, 7};
2180+ return sum_const_array(data);
2181+ }
2182+ EOF
2183+
2184+ # Test const pointer with multiple dereferences
2185+ try_ 40 << EOF
2186+ int process(const int *x, const int *y) {
2187+ int val1 = *x;
2188+ int val2 = *y;
2189+ return val2; /* returns second value */
2190+ }
2191+
2192+ int main() {
2193+ int a = 30;
2194+ int b = 40;
2195+ return process(&a, &b);
2196+ }
2197+ EOF
2198+
2199+ # Test const pointer increment in loop
2200+ try_ 3 << EOF
2201+ int count_until(const int *arr, int target) {
2202+ const int *p = arr;
2203+ int count = 0;
2204+ while (*p != target) {
2205+ count++;
2206+ p++;
2207+ }
2208+ return count;
2209+ }
2210+
2211+ int main() {
2212+ int data[5] = {1, 2, 3, 10, 5};
2213+ return count_until(data, 10); /* returns 3 */
2214+ }
2215+ EOF
2216+
2217+ # Test const pointer reference passing
2218+ try_ 7 << EOF
2219+ int sum_via_ptr(const int *a, const int *b) {
2220+ return *a + *b;
2221+ }
2222+
2223+ int main() {
2224+ int x = 3;
2225+ int y = 4;
2226+ return sum_via_ptr(&x, &y);
2227+ }
2228+ EOF
2229+
20272230# Category: Ternary Operator
20282231begin_category " Ternary Operator" " Testing conditional ?: operator"
20292232
0 commit comments