-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestAPI.c
110 lines (74 loc) · 1.9 KB
/
testAPI.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "LinkedListAPI.h"
typedef struct car {
char *model;
char *serial;
int year;
}Car;
char* printCar(void *a) {
char *toReturn;
int length;
Car *toBePrinted;
toBePrinted = (Car *)a;
length = strlen(toBePrinted->model) + strlen(toBePrinted->serial) + 11 + 20 + 1;
toReturn = malloc(sizeof(char) * length);
sprintf(toReturn, "%s %d, serial: %s", toBePrinted->model, toBePrinted->year, toBePrinted->serial);
return toReturn;
}
int compareCar(const void *a, const void *b) {
Car *carOne;
Car *carTwo;
carOne = (Car *)a;
carTwo = (Car *)b;
return strcmp(carOne->serial, carTwo->serial);
}
void deleteCar(void *a) {
Car *toBeDeleted;
toBeDeleted = (Car*)a;
free(toBeDeleted->model);
free(toBeDeleted->serial);
free(toBeDeleted);
return;
}
int main(void) {
List *carList;
Car *tmpCar;
Car *checkCar;
char *j;
char *str;
ListIterator ptr;
j = malloc(sizeof(char)*2);
carList = initializeList(&printCar, &deleteCar, &compareCar);
for(int i = 1; i <= 7; i++) {
sprintf(j, "%c", i + 82);
tmpCar = malloc(sizeof(Car));
tmpCar->year = 2013 + i;
tmpCar->model = malloc(sizeof(char) * 8);
strcpy(tmpCar->model, "Model ");
strcat(tmpCar->model, j);
tmpCar->serial = malloc(sizeof(char) * 7);
strcpy(tmpCar->serial, "ABCDE");
strcat(tmpCar->serial, j);
insertSorted(carList, (void *)tmpCar);
tmpCar++;
}
ptr = createIterator(carList);
checkCar = (Car *)nextElement(&ptr);
while( checkCar != NULL ) {
str = carList->printData(checkCar);
printf("%s\n", str);
free(str);
checkCar = (Car *)nextElement(&ptr);
}
printf("TESTING: clearList\n");
clearList(carList);
if(carList->length != 0) {
printf("FAILED: length is not zero\n");
}
if(carList->head != NULL || carList->tail != NULL) {
printf("FAILED: list not cleared (nodes still in head/tail)\n");
}
free(checkCar);
free(j);
freeList(carList);
return 0;
}