-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_t.c
executable file
·57 lines (45 loc) · 1003 Bytes
/
array_t.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
#include "array.h"
#include <stdio.h>
#include <string.h>
char *
ok(int test) {
return test ? "ok" : "not ok";
}
void
print_ok(char *name, int test) {
printf("%-20s %s\n", name, ok(test));
}
#define NELTS 103
#define ELTSZ 81
#define BLOCKSZ 1
int
main() {
array_t a;
int i, t;
char buf[4096], *p;
array_init(&a, ELTSZ, BLOCKSZ);
p = (char*)array_add(&a, NELTS);
for(i=0; i<NELTS; i++) {
sprintf(p, "%d", i);
p += ELTSZ;
}
print_ok("count", array_count(&a)==NELTS);
t = 1;
for(i=0; i<array_count(&a); i++) {
p = (char*)array_get(&a, i);
sprintf(buf, "%d", i);
if( strcmp(p, buf) ) {
fprintf(stderr, "error: a(%d)=%s", i, p);
t = 0;
}
}
print_ok("get", t && i==NELTS);
array_clear(&a);
print_ok("clear", array_count(&a)==0);
t = 1;
for(i=0; i<NELTS; i++) {
if( array_get(&a, i) ) t = 0;
}
print_ok("get_null", t);
return 0;
}