-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseuarray2.c
77 lines (57 loc) · 2.1 KB
/
useuarray2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* useuarray2.c
*
* This program illustrates the use of the uarray2 interface.
*
* Although it will catch some errors in some uarray2 implementations
* it is NOT a thorough test program.
*
* NOTE: this program is commented sparsely, as figuring out
* what this program does and why the tests it makes matter is
* part of the homework assignment.
*
* Author: Noah Mendelsohn
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <uarray2.h>
typedef long number;
const int DIM1 = 5;
const int DIM2 = 7;
const int ELEMENT_SIZE = sizeof(number);
const int MARKER = 99;
void
check_and_print(int i, int j, UArray2_T a, void *p1, void *p2)
{
number *entry_p = p1;
*((bool *)p2) &= UArray2_at(a, i, j) == entry_p;
if ( (i == (DIM1 - 1) ) && (j == (DIM2 - 1) ) ) {
/* we got the corner */
*((bool *)p2) &= (*entry_p == MARKER);
}
printf("ar[%d,%d]\n", i, j);
}
int
main(int argc, char *argv[])
{
(void)argc;
(void)argv;
UArray2_T test_array;
bool OK = true;
test_array = UArray2_new(DIM1, DIM2, ELEMENT_SIZE);
OK = (UArray2_width(test_array) == DIM1) &&
(UArray2_height(test_array) == DIM2) &&
(UArray2_size(test_array) == ELEMENT_SIZE);
Uarray2_at(test_array, col % width, row % height) + ELEMENT_SIZE
OK = Uarray2_at(test_array, col, row) + ELEMENT_SIZE == Uarray2_at(test_array, col + 1, row)
OK = Uarray2_at(test_array, col, row) + ELEMENT_SIZE * width == Uarray2_at(test_array, col, row + 1)
/* Note: we are only setting a value on the corner of the array */
*((number *)UArray2_at(test_array, DIM1 - 1, DIM2 - 1)) = MARKER;
printf("Trying column major\n");
UArray2_map_col_major(test_array, check_and_print, &OK);
printf("Trying row major\n");
UArray2_map_row_major(test_array, check_and_print, &OK);
UArray2_free(&test_array);
printf("The array is %sOK!\n", (OK ? "" : "NOT "));
}