-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList_test.c
112 lines (104 loc) · 3.22 KB
/
LinkedList_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
/*Tests for Linked List*/
#include "LinkedList.h"
boolean test_insertion(LinkedList l){
int original_length = l->length;
l->ops->add(5);
boolean amount_insertion = ((original_length + 1) == l->length);
boolean correct_value = (l->ops->find(5) != -1);
return (amount_insertion && correct_value);
}
boolean test_extraction(LinkedList l){
int original_length = l->length;
l->ops->add(10);
l->ops->add(11);
l->ops->add(12);
l->ops->add(14);
l->ops->add(15);
l->ops->add(16);
int ext1 = l->ops->get(0);
boolean extraction1 = (ext1 == 10);
boolean right_amount1 = ((original_length+5) == l->length);
int ext2 = l->ops->get(1);
boolean extraction2 = (ext2 == 12);
boolean right_amount2 = ((original_length+4) == l->length);
int ext3 = l->ops->get(3);
boolean extraction3 = (ext3 == 16);
boolean right_amount3 = ((original_length+3) == l->length);
int last = l->ops->getLast();
boolean extraction4 = (last == 15);
boolean right_amount4 = ((original_length+2) == l->length);
int first = l->ops->getFirst();
boolean extraction5 = (first == 11);
boolean right_amount5 = ((original_length+1) == l->length);
l->ops->add(10);
l->ops->clear();
boolean right_amount6 = (original_length == l->length);
/* Test exceptions */
boolean amounts = (right_amount1 && right_amount2 && right_amount3 && right_amount4 && right_amount5 && right_amount6);
boolean extractions = (extraction1 && extraction2 && extraction3 && extraction4 && extraction5);
return (amounts && extractions);
}
boolean test_remove(LinkedList l){
l->ops->add(10);
l->ops->add(11);
l->ops->add(12);
l->ops->add(13);
l->ops->add(14);
l->ops->add(15);
int original = l->size;
l->ops->removeByIndex(3);
boolean not_belongs1 = !l->ops->contains(13);
boolean right_amount1 = (original-1 == l->size);
l->ops->removeByValue(14);
boolean not_belongs2 = !l->ops->contains(14);
boolean right_amount2 = (original-2 == l->size);
return (right_amount1 && right_amount2 && not_belongs1 && not_belongs2);
}
boolean test_contains(LinkedList l){
l->ops->add(50);
boolean belongs = l->ops->contains(50);
boolean not_belongs = !l->ops->contains(51);
return (belongs && not_belongs);
}
boolean test_repetitions(LinkedList l){
int rep = 5;
for (int i = 0; i<rep; i++)
l->ops->add(100);
for (int k = 0; k<20; k++)
l->ops->add(k);
boolean repetitions = (l->ops->repetitions(100) == 5);
l->ops-clear();
return repetitions;
}
void main(){
LinkedList l=newLinkedList();
boolean insertion = test_insertion(l);
if (insertion)
printf("Insertion test succesfull\n");
else
printf("Insertion test failed\n");
boolean extraction = test_extraction(l);
if (extraction)
printf("Extraction test succesfull\n");
else
printf("Extraction test failed\n");
boolean remov = test_remove(l);
if (remov)
printf("Remove test succesfull\n");
else
printf("Remove test failed\n");
boolean contains = test_contains(l);
if (contains)
printf("Contains test succesfull\n");
else
printf("Contains test failed\n");
boolean repetitions = test_repetitions(l);
if (repetitions)
printf("Repetitions test succesfull\n");
else
printf("Repetitions test failed\n");
l->ops->destroy();
/* All test succesfull */
if (insertion && extraction && contains && repetitions)
printf("All test were succesfull!\n");
}