-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.c
More file actions
207 lines (175 loc) · 4.86 KB
/
Copy pathTest.c
File metadata and controls
207 lines (175 loc) · 4.86 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include "DarrDouble.h"
#include "Logger.h"
#include "Test.h"
#define STRESS_DARR_SIZE 200000ULL
#define QUICK_SORT_DARR_SIZE 500000ULL
#define BUBBLE_SORT_DARR_SIZE 50000ULL
#define RECORD_TIME(ts) \
do { \
if (timespec_get(&(ts), TIME_UTC) == 0) { \
LOG_ERROR("Failed to get " #ts " time"); \
} \
} while (0)
static double get_elapsed_time(struct timespec start, struct timespec end)
{
return (double)(end.tv_sec - start.tv_sec) +
(double)(end.tv_nsec - start.tv_nsec) / 1e9;
}
static bool fill(DarrDouble* darr, size_t size, bool sorted)
{
if (darr == NULL)
return false;
LOG_INFO("Filling the darr");
for (size_t i = 1; i <= size; i++)
{
double value = (double)i;
if (!sorted && (i % 2 != 0))
value = -value;
if (!darr_add_last(darr, value))
{
LOG_ERROR("Couldn't add an element");
return false;
}
}
return true;
}
static double run_quick_sort_scenario(size_t size, bool sorted)
{
struct timespec start, end;
DarrDouble darr = { 0 };
if (!fill(&darr, size, sorted))
{
LOG_ERROR("Couldn't fill the darr");
darr_destroy(&darr);
return 0.0;
}
LOG_INFO("Start sorting");
const size_t current_size = darr_size(&darr);
RECORD_TIME(start);
if (current_size > 0)
{
darr_quick_sort(&darr, 0, current_size - 1);
}
RECORD_TIME(end);
LOG_INFO("Free the darr");
darr_destroy(&darr);
return get_elapsed_time(start, end);
}
static double run_bubble_sort_scenario(size_t size, bool sorted)
{
struct timespec start, end;
DarrDouble darr = { 0 };
if (!fill(&darr, size, sorted))
{
LOG_ERROR("Couldn't fill the darr");
darr_destroy(&darr);
return 0.0;
}
LOG_INFO("Start sorting");
RECORD_TIME(start);
darr_bubble_sort(&darr);
RECORD_TIME(end);
LOG_INFO("Free the darr");
darr_destroy(&darr);
return get_elapsed_time(start, end);
}
void run_test(const char* test_name, TestFunction test)
{
printf("--- %s ---\n", test_name);
double time_in_seconds = test();
printf("--- Ended in %.6f seconds ---\n\n", time_in_seconds);
}
double test_stress()
{
struct timespec start, end;
double pure_time = 0.0;
LOG_INFO("Testing size: %llu", STRESS_DARR_SIZE);
const size_t last_half = STRESS_DARR_SIZE - STRESS_DARR_SIZE / 2;
LOG_INFO("Creating a darr with capacity of 0");
RECORD_TIME(start);
DarrDouble darr = { 0 };
RECORD_TIME(end);
pure_time += get_elapsed_time(start, end);
LOG_INFO("Filling the darr via darr_add_first with %llu elements", STRESS_DARR_SIZE / 2);
RECORD_TIME(start);
for (size_t i = 0; i < STRESS_DARR_SIZE / 2; i++)
{
if (!darr_add_first(&darr, (double)i))
{
LOG_ERROR("Couldn't add an element");
darr_destroy(&darr);
return 0.0;
}
}
RECORD_TIME(end);
pure_time += get_elapsed_time(start, end);
LOG_INFO("Filling the darr via darr_add_last with %llu elements", last_half);
RECORD_TIME(start);
for (size_t i = 0; i < last_half; i++)
{
if (!darr_add_last(&darr, (double)i))
{
LOG_ERROR("Couldn't add an element");
darr_destroy(&darr);
return 0.0;
}
}
RECORD_TIME(end);
pure_time += get_elapsed_time(start, end);
LOG_INFO("Current size: %zu", darr_size(&darr));
LOG_INFO("Current capacity: %zu", darr_capacity(&darr));
LOG_INFO("Current memory usage: %.2f MB", darr_memory(&darr) / 1024.0 / 1024.0);
LOG_INFO("Getting first %llu elements", STRESS_DARR_SIZE / 2);
RECORD_TIME(start);
for (size_t i = 0; i < STRESS_DARR_SIZE / 2; i++)
{
double ignored;
if (!darr_get(&darr, &ignored, i))
{
LOG_ERROR("Couldn't get an element");
darr_destroy(&darr);
return 0.0;
}
}
RECORD_TIME(end);
pure_time += get_elapsed_time(start, end);
LOG_INFO("Setting last %llu elements", last_half);
RECORD_TIME(start);
for (size_t i = STRESS_DARR_SIZE / 2; i < STRESS_DARR_SIZE; i++)
{
if (!darr_set(&darr, 1.0, i))
{
LOG_ERROR("Couldn't set an element");
darr_destroy(&darr);
return 0.0;
}
}
RECORD_TIME(end);
pure_time += get_elapsed_time(start, end);
LOG_INFO("Removing %llu elements via darr_remove_last and darr_remove_first", last_half);
RECORD_TIME(start);
for (size_t i = 0; i < last_half / 2; i++)
{
if (!darr_remove_last(&darr, NULL) || !darr_remove_first(&darr, NULL))
{
LOG_ERROR("Couldn't remove an element");
darr_destroy(&darr);
return 0.0;
}
}
RECORD_TIME(end);
pure_time += get_elapsed_time(start, end);
LOG_INFO("Free the darr");
RECORD_TIME(start);
darr_destroy(&darr);
RECORD_TIME(end);
pure_time += get_elapsed_time(start, end);
return pure_time;
}
double test_quick_sort() { return run_quick_sort_scenario(QUICK_SORT_DARR_SIZE, false); }
double test_quick_sort_on_sorted() { return run_quick_sort_scenario(QUICK_SORT_DARR_SIZE, true); }
double test_bubble_sort() { return run_bubble_sort_scenario(BUBBLE_SORT_DARR_SIZE, false); }
double test_bubble_sort_on_sorted() { return run_bubble_sort_scenario(BUBBLE_SORT_DARR_SIZE, true); }