-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_run.c
61 lines (54 loc) · 1.53 KB
/
test_run.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "list.h"
void list_with_ints();
void list_with_strings();
boolean iterate_int(void *data);
boolean iterate_string(void *data);
void free_string(void *data);
//int main(int argc, char *argv[]) {
// printf("Loading int demo...\n");
// list_with_ints();
// list_with_strings();
// return 0;
//}
void list_with_ints() {
int numbers = 10;
printf("Generating list with the first %d positive numbers...\n", numbers);
int i;
list list;
list_new(&list, sizeof (int), NULL);
for (i = 1; i <= numbers; i++) {
list_append(&list, &i);
}
list_for_each(&list, iterate_int);
list_destroy(&list);
printf("Successfully freed %d numbers...\n", numbers);
}
void list_with_strings() {
int numNames = 5;
const char *names[] = {"David", "Kevin", "Michael", "Craig", "Jimi"};
int i;
list list;
list_new(&list, sizeof (char *), free_string);
char *name;
for (i = 0; i < numNames; i++) {
name = strdup(names[i]);
list_append(&list, &name);
}
list_for_each(&list, iterate_string);
list_destroy(&list);
printf("Successfully freed %d strings...\n", numNames);
}
boolean iterate_int(void *data) {
printf("Found value: %d\n", *(int *) data);
return TRUE;
}
boolean iterate_string(void *data) {
printf("Found string value: %s\n", *(char **) data);
return TRUE;
}
void free_string(void *data) {
free(*(char **) data);
}