Skip to content

Commit b619761

Browse files
committed
Add tests for array literal pointer semantics
The tests added cover: - decay of int[] compound literals to int* during initialization - passing an int[] literal to a function expecting int* - pointer-typed ternary expressions involving array literals - char[] and short[] literal sums to confirm correct pointer behavior These tests ensure the correctness of the unified decay and pointer semantics introduced in the previous commit.
1 parent 28d954b commit b619761

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

tests/driver.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,28 @@ int main() {
733733
}
734734
EOF
735735

736+
# Test: Array compound literal decay to pointer in initializer
737+
try_ 0 << EOF
738+
int main(void) {
739+
int *arr = (int[]){1, 2, 3, 4, 5};
740+
return arr[0] != 1 || arr[4] != 5;
741+
}
742+
EOF
743+
744+
# Test: Passing array compound literal as pointer argument
745+
try_ 0 << EOF
746+
int sum(int *p, int n) {
747+
int s = 0;
748+
for (int i = 0; i < n; i++)
749+
s += p[i];
750+
return s;
751+
}
752+
int main(void) {
753+
int s = sum((int[]){1, 2, 3, 0, 0}, 3);
754+
return s != 6;
755+
}
756+
EOF
757+
736758
# Test: Complex expression with compound literals
737759
try_ 77 << EOF
738760
int main() {
@@ -4737,6 +4759,36 @@ int main() {
47374759
}
47384760
EOF
47394761

4762+
try_ 200 << EOF
4763+
int main(void) {
4764+
char *s = (char[]){'A', 'B', 'C', 'D', 'E'};
4765+
return s[0] + s[1] + s[4]; /* 65 + 66 + 69 */
4766+
}
4767+
EOF
4768+
4769+
try_ 6 << EOF
4770+
int main(void) {
4771+
short *s = (short[]){1, 2, 3, 4, 5};
4772+
return s[0] + s[4];
4773+
}
4774+
EOF
4775+
4776+
try_ 60 << EOF
4777+
int main(void) {
4778+
int arr[] = {10, 20, 30, 40, 50};
4779+
int *selected = 1 ? arr : (int[]){1, 2, 3, 4, 5};
4780+
return selected[0] + selected[4];
4781+
}
4782+
EOF
4783+
4784+
try_ 6 << EOF
4785+
int main(void) {
4786+
int arr[] = {10, 20, 30, 40, 50};
4787+
int *selected = 0 ? arr : (int[]){1, 2, 3, 4, 5};
4788+
return selected[0] + selected[4];
4789+
}
4790+
EOF
4791+
47404792
try_ 120 << EOF
47414793
int main() {
47424794
/* Complex expression with mixed compound literals */

tests/snapshots/fib-arm-static.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/fib-riscv-static.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/hello-arm-static.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/hello-riscv-static.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)