1 void testBrackets(int* ar, int len) { 2 int i; 3 int errors = 0; 4 for (i = 0; i < len; i++) { 5 ar[i] = i; 6 } 7 for (i = 0; i < len; i++) { 8 if (ar[i] != i) { 9 printf("error: [%d] %d != %d\n", i, ar[i], i); 10 errors++; 11 } 12 } 13 printf("Errors: %d\n", errors); 14 } 15 16 void testBrackets2D(int** ar2D, int lenX, int lenY) { 17 int x, y; 18 int errors = 0; 19 for (x = 0; x < lenX; x++) { 20 for (y = 0; y < lenY; y++) { 21 ar2D[x][y] = x * lenY + y; 22 } 23 } 24 for (x = 0; x < lenX; x++) { 25 for (y = 0; y < lenY; y++) { 26 int expected = x * lenY + y; 27 int val = ar2D[x][y]; 28 if (val != expected) { 29 printf("error: [%d][%d] %d != %d\n", x, y, val, expected); 30 errors++; 31 } 32 } 33 } 34 printf("2D Errors: %d\n", errors); 35 } 36 37 void testHeap() { 38 int* ar = (int*) malloc(100); 39 testBrackets(ar, 25); 40 free(ar); 41 } 42 43 void testHeap2D() { 44 int lenX = 10; 45 int lenY = 5; 46 int* ar = (int*) malloc(lenX * lenY * 4); 47 int** ar2D = (int**) malloc(lenX * 4); 48 int i; 49 for(i = 0; i < lenX; i++) { 50 ar2D[i] = ar + lenY * i; 51 } 52 testBrackets2D(ar2D, lenX, lenY); 53 free(ar); 54 free(ar2D); 55 } 56 57 int main() { 58 testHeap(); 59 testHeap2D(); 60 return 0; 61 } 62