-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_queue_tests.c
86 lines (75 loc) · 2.2 KB
/
priority_queue_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
#include "action.h"
#include "priority_queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define RED "\x1b[0;31m"
#define GRN "\x1b[0;32m"
#define reset "\x1b[0m"
void test_case();
void test_case_string();
int main() {
printf("---TESTS---\n");
init_queue();
test_case(0, action_queue_size);
// SCREW THE MEMORY LEAKS
parent_action_t* p1 = calloc(1, sizeof(parent_action_t));
p1->time_last = 100;
p1->name = "p1";
parent_action_t* p2 = calloc(1, sizeof(parent_action_t));
p2->time_last = 200;
p2->name = "p2";
parent_action_t* p3 = calloc(1, sizeof(parent_action_t));
p3->time_last = 300;
p3->name = "p3";
parent_action_t* p4 = calloc(1, sizeof(parent_action_t));
p4->time_last = 400;
p4->name = "p4";
parent_action_t* p5 = calloc(1, sizeof(parent_action_t));
p5->time_last = 500;
p5->name = "p5";
enqueue(p5);
enqueue(p3);
enqueue(p4);
enqueue(p1);
enqueue(p2);
test_case(5, action_queue_size);
test_case(100, dequeue()->time_last);
test_case(200, dequeue()->time_last);
test_case(300, dequeue()->time_last);
test_case(400, dequeue()->time_last);
test_case(500, dequeue()->time_last);
// realloc tests
int num_tests = 500;
for(int i = 0; i < num_tests; i++) {
parent_action_t *temp = calloc(1, sizeof(parent_action_t));
temp->time_last = rand() % 100;
enqueue(temp);
}
test_case(500, action_queue_size);
int previous = dequeue()->time_last;
for (int i = 0; i < num_tests - 1; i++) {
clock_t curr = dequeue()->time_last;
if(curr < previous) {
printf(RED "FAILED\n" reset);
}
previous = curr;
}
}
void test_case(int expected, int result) {
if (expected == result) {
printf(GRN "PASSED TEST\n" reset);
} else {
printf(RED "FAILED TEST, expected %d, got %d\n" reset, expected,
result);
}
}
void test_case_string(char* expected, char* result) {
if (strcmp(expected, result)) {
printf(GRN "PASSED TEST\n" reset);
} else {
printf(RED "FAILED TEST, expected %s, got %s\n" reset, expected,
result);
}
}