-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTests.c
131 lines (100 loc) · 3.25 KB
/
Tests.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "Gtree.h"
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
typedef struct SPerson
{
char *id;
char *name;
int age;
}StPerson, *PtrPerson;
int compare(void* a, void* b)
{
PtrPerson p1= (PtrPerson) a, p2= (PtrPerson) b;
return atoi(p1->id) - atoi(p2->id);
}
char* get_key_value(void* data)
{
PtrPerson p = (PtrPerson) data;
return p->id;
}
char* to_string(void* a)
{
PtrPerson person = (PtrPerson) a;
char age[3];
char * personInfo = (char*) malloc(48);
strcpy(personInfo,"\nID: ");
strncat(personInfo, person->id,strlen(person->id));
strcpy(personInfo,"\nName: ");
strncat(personInfo, person->name,strlen(person->name));
strcat(personInfo, ", Age: ");
sprintf(age, "%d", person->age);
strcat(personInfo,age);
strcat(personInfo,"\n");
return personInfo;
}
char* to_string_only_id(void * a)
{
PtrPerson person = (PtrPerson) a;
char * personInfo = (char*) malloc(48);
strncat(personInfo, person->id,strlen(person->id));
strcat(personInfo,"-");
return personInfo;
}
PtrPerson createPerson(char *id, char * name, int age)
{
PtrPerson person = (PtrPerson) malloc (sizeof (StPerson));
person->id = id;
person->name = name;
person->age=age;
return person;
}
int main(int argc, char const *argv[])
{
int retrievedResult=-1;
PtrPerson test;
PbinTree tree;
(void)init_g_tree(&tree,sizeof(StPerson), compare,get_key_value, to_string_only_id);
test = createPerson("15","Test",12);
(void)insert(tree,test);
test = createPerson("10","Test2",14);
(void)insert(tree,test);
test = createPerson("20","Test3",15);
(void)insert(tree,test);
test = createPerson("5","Test4",15);
(void)insert(tree,test);
test = createPerson("12","Test4",15);
(void)insert(tree,test);
test = createPerson("16","Test5",15);
(void)insert(tree,test);
test = createPerson("25","Test6",15);
(void)insert(tree,test);
printf("\n###Pre-order###\nExpect: 15-10-5-12-20-16-25\nResult: ");
list_pre_order(tree);
printf("\n###Pos-order###\nExpect: 5-12-10-16-25-20-15\nResult: ");
list_pos_order(tree);
printf("\n###In-order ###\nExpect: 10-12-15-16-20-25\nResult: ");
list_in_order(tree);
PtrPerson retrievedPerson = (PtrPerson) malloc (sizeof(StPerson)),
searchPerson = (PtrPerson) malloc (sizeof(StPerson));
searchPerson->id="25";
retrievedResult = get_element(tree, searchPerson, retrievedPerson);
printf("\nSearch for %s id.Retrieved Person Result -> %d \n", searchPerson->id,retrievedResult);
if(retrievedResult == 0)
printf("\nRetrieved person: %s\n", to_string(retrievedPerson));
//Remove leaf.
printf("\nDeleting 25. Delete response: %d \n",delete_element(tree,test));
printf("[Leaf removing] After remove %s Pre-order: ", test->id);
list_pre_order(tree);
//Remove non-leaf
test->id="20";
printf("\nDeleting 20. Delete response: %d \n",delete_element(tree,test));
printf("[Non leaf removing] After remove %s Pre-order: ", test->id);
list_pre_order(tree);
//Remove root
test->id="15";
printf("\nDeleting 15. Delete response: %d \n",delete_element(tree,test));
printf("[Root] After remove %s Pre-order: ", test->id);
list_pre_order(tree);
//Remove non existent element
printf("\nDeleting 15. Delete response: %d \n",delete_element(tree,test));
return 0;
}