Skip to content

Commit c06d564

Browse files
committed
feat(tests): add increment operation tests for arrays, pointers, and struct members
Signed-off-by: Praneeth Sarode <praneethsarode@gmail.com>
1 parent 05b69e2 commit c06d564

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
int main() {
2+
int arr[5] = {1, 2, 3, 4, 5};
3+
arr[0]++;
4+
++arr[1];
5+
if (arr[0] != 2) return 1;
6+
if (arr[1] != 3) return 2;
7+
8+
int x = arr[2]++;
9+
if (x != 3) return 3;
10+
if (arr[2] != 4) return 4;
11+
12+
int y = ++arr[3];
13+
if (y != 5) return 5;
14+
if (arr[3] != 5) return 6;
15+
16+
return 0;
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
int main() {
2+
int a = 5;
3+
int* p = &a;
4+
5+
// Test post-increment on dereference
6+
(*p)++;
7+
if (a != 6) { return 1; }
8+
9+
// Test pre-increment on dereference
10+
++(*p);
11+
if (a != 7) { return 2; }
12+
13+
// Test with pointer to pointer
14+
int arr[2];
15+
arr[0] = 10;
16+
arr[1] = 20;
17+
18+
int* ptr = &arr[0];
19+
int** pptr = &ptr;
20+
21+
// (*pptr) is 'ptr'. Incrementing it should move 'ptr' to &arr[1]
22+
// Since ptr is int*, it should move by sizeof(int) = 4 bytes.
23+
(*pptr)++;
24+
25+
if (*ptr != 20) { return 3; }
26+
27+
return 0;
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct Point {
2+
int x;
3+
int y;
4+
};
5+
6+
int main() {
7+
struct Point p;
8+
p.x = 10;
9+
p.y = 20;
10+
11+
p.x++;
12+
if (p.x != 11) return 1;
13+
14+
struct Point* ptr = &p;
15+
ptr->y++;
16+
if (p.y != 21) return 2;
17+
18+
return 0;
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
struct Point {
2+
int x;
3+
int y;
4+
};
5+
6+
int main() {
7+
int arr[2] = {10, 20};
8+
int* ptr = &arr[0];
9+
10+
if (*ptr != 10) return 1;
11+
12+
ptr++;
13+
if (*ptr != 20) return 2;
14+
15+
ptr--;
16+
if (*ptr != 10) return 3;
17+
18+
++ptr;
19+
if (*ptr != 20) return 4;
20+
21+
--ptr;
22+
if (*ptr != 10) return 5;
23+
24+
// Test with struct (size should be 8)
25+
struct Point points[2];
26+
points[0].x = 1;
27+
points[0].y = 2;
28+
points[1].x = 3;
29+
points[1].y = 4;
30+
struct Point* p_ptr = &points[0];
31+
32+
if (p_ptr->x != 1) return 6;
33+
34+
p_ptr++;
35+
if (p_ptr->x != 3) return 7;
36+
37+
p_ptr--;
38+
if (p_ptr->x != 1) return 8;
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)