diff --git a/tests/stdlib.c b/tests/stdlib.c index 699d697b..b2a54454 100644 --- a/tests/stdlib.c +++ b/tests/stdlib.c @@ -267,6 +267,68 @@ void struct_with_define() is_eq(a.im, 12); } +void test_pointer_malloc1() { + int m = 3, n = 5; + int *p; + p = malloc(m * n * sizeof(int)); + int i, j, count = 0; + for (i = 0; i < m; i++) { + for (j = 0; j < n; j++) { + count++; + p[i+j*m] = count; + } + } + is_eq(p[0], 1); + is_eq(p[2+2*m], 13); + free(p); +} +void test_pointer_malloc2() { + int m = 3, n = 5; + int (*p)[n]; + p = malloc(m * n * sizeof(int)); + int i, j, count = 0; + for (i = 0; i < m; i++) { + for (j = 0; j < n; j++) { + count++; + p[i][j] = count; + } + } + is_eq(p[0][0], 1); + is_eq(p[2][2], 13); + free(p); +} + +void test_pointer_malloc2const() { + int (*p)[5]; + p = malloc(3 * 5 * sizeof(int)); + int i, j, count = 0; + for (i = 0; i < 3; i++) { + for (j = 0; j < 5; j++) { + count++; + p[i][j] = count; + } + } + is_eq(p[0][0], 1); + is_eq(p[2][2], 13); + free(p); +} + +void test_pointer_malloc3() { + int m = 3; + int (*p)[5]; + p = malloc(m * 5 * sizeof(int)); + int i, j, count = 0; + for (i = 0; i < m; i++) { + for (j = 0; j < 5; j++) { + count++; + p[i][j] = count; + } + } + is_eq(p[0][0], 1); + is_eq(p[2][2], 13); + free(p); +} + void test_atoi_post() { char * num[3] = {{"123"},{"987"},{"456"}}; @@ -287,7 +349,7 @@ void test_atoi_post() int main() { - plan(763); + plan(771); struct_with_define(); @@ -577,5 +639,11 @@ int main() diag("q_sort"); q_sort(); + diag("pointer array (*)[]"); + test_pointer_malloc1(); + test_pointer_malloc2(); + test_pointer_malloc2const(); + test_pointer_malloc3(); + done_testing(); }