This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
146 lines (126 loc) · 3.81 KB
/
test.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "list.h"
#include <stdlib.h>
#include <stdio.h>
/* Here we are going to write some functions to support a linked list that stores points (x, y) */
/* Here is the data the user in this case us will be storing into our linked list */
typedef struct point
{
int x;
int y;
} point;
point* create_point(int x, int y)
{
point *p;
p = malloc(sizeof(point));
p->x = x;
p->y = y;
return p;
}
void print_point(void* data)
{
if (data != NULL) {
point *p = (point *)data;
printf("(%d, %d)", p->x, p->y);
}
}
void free_point(void* data)
{
free_data(data);
}
int compare_point_data(const void* a, const void* b)
{
point *pa = (point *)a;
point *pb = (point *)b;
int pasum = pa->x + pa->y;
int pbsum = pb->x + pb->y;
if (pasum == pbsum)
return 1;
else
return 0;
}
/* This main function does a little testing
Like all good CS Majors you are required to test
your code here. There is no substitute for testing
and you should be sure to test for all edge cases
e.g., calling remove_front on an empty list.
*/
int main(void)
{
/* Now to make use of all of this stuff */
list* llist = create_list();
/* What does an empty list contain? Lets use our handy traversal function */
printf("TEST CASE 1\nAn Empty list should print nothing here:");
traverse(llist, print_point);
printf("\n");
/* Lets add a point and then print */
push_front(llist, create_point(3, 5));
printf("TEST CASE 2\nA List with (3, 5) should print (3, 5):");
traverse(llist, print_point);
printf("\n");
/* Lets remove a point and then print */
pop_front(llist, free_point);
printf("TEST CASE 3\nAnother Empty list should print nothing here:");
traverse(llist, print_point);
printf("\n");
/* Lets add two points and then print */
push_front(llist, create_point(3, 5));
push_front(llist, create_point(9, 16));
printf("TEST CASE 4\nA List with two nodes should print those two nodes:");
traverse(llist, print_point);
printf("\n");
/* Lets kill the list */
empty_list(llist, free_point);
printf("TEST CASE 5\nAfter freeing all nodes the list should be empty:");
traverse(llist, print_point);
printf("\n");
/* Add from back */
push_back(llist, create_point(3, 5));
push_back(llist, create_point(9, 16));
printf("TEST CASE 6\nA List with two nodes should print those two nodes:");
traverse(llist, print_point);
printf("\n");
/* Remove from back */
pop_back(llist, free_point);
pop_back(llist, free_point);
printf("TEST CASE 7\nAfter freeing all nodes the list should be empty:");
traverse(llist, print_point);
printf("\n");
/* Test remove_data */
point *p8 = create_point(3, 5);
push_front(llist, p8);
push_front(llist, create_point(9, 16));
remove_data(llist, p8, compare_point_data, free_point);
printf("TEST CASE 8\nAfter removal only one should remain:");
traverse(llist, print_point);
printf("\n");
pop_back(llist, free_point);
/* List querying */
push_front(llist, create_point(3, 5));
push_front(llist, create_point(9, 16));
printf("TEST CASE 9");
printf("\nFront:");
print_point(front(llist));
printf("\nBack:");
print_point(back(llist));
printf("\nIs empty: %d", is_empty(llist));
printf("\nSize: %d", size(llist));
printf("\n");
pop_front(llist, free_point);
pop_front(llist, free_point);
/* Searching */
point *p10 = create_point(3, 5);
push_front(llist, p10);
push_front(llist, create_point(9, 16));
int result = find_occurrence(llist, p10, compare_point_data);
printf("TEST CASE 10\nShould be true: %d", result);
p10 = create_point(4, 5);
result = find_occurrence(llist, p10, compare_point_data);
printf("\nShould be false: %d", result);
printf("\n");
free_point(p10);
pop_front(llist, free_point);
pop_front(llist, free_point);
/* Testing over clean up*/
free(llist);
return 0;
}