-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLLTests.c
96 lines (89 loc) · 2.44 KB
/
LLTests.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
#include "NormalLL.h"
#include "ArrayLL.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *vals;
int SIZE = 100000000;
char *arg0;
void errorExit(char *message) {
fprintf(stderr, "%s\n", message);
fprintf(stderr, "Usage: %s [size]\n", arg0);
exit(EXIT_FAILURE);
}
void normalTest() {
printf("---Normal Linked List---\n");
time_t start = time(NULL);
NormalLL *l = normalLLCreate();
for (int i = 0; i < SIZE; i++) {
normalLLAppend(l, vals + i);
}
printf("Appended in %lu seconds.\n", time(NULL) - start);
start = time(NULL);
for (int i = 0; i < SIZE / 2; i++) {
if (*(int *) normalLLDeleteBack(l) != SIZE - i - 1) {
fprintf(stderr, "DeleteBack does not match.\n");
exit(EXIT_FAILURE);
}
if (*(int *) normalLLDeleteFront(l) != i) {
fprintf(stderr, "DeleteFront does not match.\n");
exit(EXIT_FAILURE);
}
}
if (SIZE % 2 == 1) {
normalLLDeleteBack(l);
}
if (normalLLLength(l) != 0) {
exit(EXIT_FAILURE);
}
printf("Deleted in %lu seconds.\n", time(NULL) - start);
freeNormalLL(&l);
}
void arrayTest() {
printf("---Array Linked List---\n");
time_t start = time(NULL);
ArrayLL *l = arrayLLCreate(SIZE);
for (int i = 0; i < SIZE; i++) {
arrayLLAppend(l, vals + i);
}
printf("Appended in %lu seconds.\n", time(NULL) - start);
start = time(NULL);
for (int i = 0; i < SIZE / 2; i++) {
if (*(int *) arrayLLDeleteBack(l) != SIZE - i - 1) {
fprintf(stderr, "DeleteBack does not match.\n");
exit(EXIT_FAILURE);
}
if (*(int *) arrayLLDeleteFront(l) != i) {
fprintf(stderr, "DeleteFront does not match.\n");
exit(EXIT_FAILURE);
}
}
if (SIZE % 2 == 1) {
arrayLLDeleteBack(l);
}
if (arrayLLLength(l) != 0) {
exit(EXIT_FAILURE);
}
printf("Deleted in %lu seconds.\n", time(NULL) - start);
freeArrayLL(&l);
}
int main(int argc, char *argv[]) {
arg0 = argv[0];
if (argc > 2) {
errorExit("Too many arguments.");
}
if (argc == 2) {
SIZE = atoi(argv[1]);
if (SIZE <= 0) {
errorExit("Please input a positive number.");
}
}
vals = malloc(SIZE * sizeof(*vals));
for (int i = 0; i < SIZE; i++) {
vals[i] = i;
}
normalTest();
arrayTest();
free(vals);
return EXIT_SUCCESS;
}