-
Notifications
You must be signed in to change notification settings - Fork 89
/
stresstest.c
413 lines (345 loc) · 9.15 KB
/
stresstest.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Copyright (c) 2010-2014 Christopher Swenson. */
/* Copyright (c) 2012 Google Inc. All Rights Reserved. */
#define _XOPEN_SOURCE
#define SORT_NAME sorter
#define SORT_TYPE int64_t
#define SORT_CMP(x, y) ((x) - (y))
#ifdef SET_SORT_EXTRA
#define SORT_EXTRA
#endif
#include "sort.h"
#define SORT_NAME stable
#define SORT_TYPE int*
#define SORT_CMP(x, y) (*(x) - *(y))
#ifdef SET_SORT_EXTRA
#define SORT_EXTRA
#endif
#include "sort.h"
/* Used to control the stress test */
#define SEED 123
#define MAXSIZE 45000
#define TESTS 1000
#define RAND_RANGE(__n, __min, __max) \
(__n) = (__min) + (long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / (0x7fffffff + 1.0)))
enum {
FILL_RANDOM,
FILL_SAME,
FILL_SORTED,
FILL_SORTED_10,
FILL_SORTED_100,
FILL_SORTED_10000,
FILL_SWAPPED_N2,
FILL_SWAPPED_N8,
FILL_EVIL,
FILL_LAST_ELEMENT
};
char *test_names[FILL_LAST_ELEMENT] = {
"random numbers",
"same number",
"sorted numbers",
"sorted blocks of length 10",
"sorted blocks of length 100",
"sorted blocks of length 10000",
"swapped size/2 pairs",
"swapped size/8 pairs",
"known evil data"
};
/* used for stdlib */
static __inline int simple_cmp(const void *a, const void *b) {
const int64_t da = *((const int64_t *) a);
const int64_t db = *((const int64_t *) b);
return (da < db) ? -1 : (da == db) ? 0 : 1;
}
#ifdef _WIN32
#include <time.h>
static __inline void srand48(long seed) {
srand(seed);
}
static __inline long lrand48(void) {
int x;
rand_s(&x);
return x & 0x7fffffff;
}
static __inline double utime(void) {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
return 1000000.0 * ts.tv_sec + ts.tv_nsec / 1000.0;
}
#else
#include <sys/time.h>
static __inline double utime(void) {
struct timeval t;
gettimeofday(&t, NULL);
return (1000000.0 * t.tv_sec + t.tv_usec);
}
#endif
/* helper functions */
int verify(int64_t *dst, const int size) {
int i;
for (i = 1; i < size; i++) {
if (dst[i - 1] > dst[i]) {
printf("Verify failed! at %d", i);
return 0;
}
}
return 1;
}
static void fill_random(int64_t *dst, const int size) {
int i;
for (i = 0; i < size; i++) {
dst[i] = lrand48();
}
}
static void fill_same(int64_t *dst, const int size) {
int i;
for (i = 0; i < size; i++) {
dst[i] = 0;
}
}
static void fill_sorted(int64_t *dst, const int size) {
int i;
for (i = 0; i < size; i++) {
dst[i] = i;
}
}
static void fill_sorted_blocks(int64_t *dst, const int size, const int block_size) {
int i, filled, this_block_size;
filled = 0;
for (i = 0; i < size; i += block_size) {
this_block_size = (filled + block_size) < size ? block_size : (size - filled);
fill_random(dst + filled, this_block_size);
qsort(dst + filled, this_block_size, sizeof(int64_t), simple_cmp);
filled += this_block_size;
}
}
static void fill_swapped(int64_t *dst, const int size, const int swapped_cnt) {
int i, tmp;
size_t ind1 = 0;
size_t ind2 = 0;
fill_sorted(dst, size);
for (i = 0; i < swapped_cnt; i++) {
ind1 = lrand48();
RAND_RANGE(ind1, 0, size - 1);
ind2 = lrand48();
RAND_RANGE(ind2, 0, size - 1);
tmp = dst[ind1];
dst[ind1] = dst[ind2];
dst[ind2] = tmp;
}
}
static void fill_evil(int64_t *dst, const int size) {
int i;
for (i = 0; i < size; i++) {
dst[i] = i ^ 1;
}
}
static void fill(int64_t *dst, const int size, int type) {
switch (type) {
case FILL_SORTED:
fill_sorted(dst, size);
break;
case FILL_SORTED_10:
fill_sorted_blocks(dst, size, 10);
break;
case FILL_SORTED_100:
fill_sorted_blocks(dst, size, 100);
break;
case FILL_SORTED_10000:
fill_sorted_blocks(dst, size, 10000);
break;
case FILL_SWAPPED_N2:
fill_swapped(dst, size, size / 2);
break;
case FILL_SWAPPED_N8:
fill_swapped(dst, size, size / 8);
break;
case FILL_SAME:
fill_same(dst, size);
break;
case FILL_EVIL:
fill_evil(dst, size);
break;
case FILL_RANDOM:
default:
fill_random(dst, size);
break;
}
}
#define TEST_STDLIB(name) do { \
res = 0; \
diff = 0; \
printf("%-29s", "stdlib " #name ); \
for (test = 0; test < sizes_cnt; test++) { \
int64_t size = sizes[test]; \
fill(dst, size, type); \
usec1 = utime(); \
name (dst, size, sizeof(int64_t), simple_cmp); \
usec2 = utime(); \
res = verify(dst, size); \
if (!res) { \
break; \
} \
diff += usec2 - usec1; \
} \
printf(" - %s, %10.1f usec\n", res ? "ok" : "FAILED", diff); \
if (!res) return 0; \
} while (0)
#define TEST_SORT_H(name) do { \
res = 0; \
diff = 0; \
printf("%-29s", "sort.h " #name); \
for (test = 0; test < sizes_cnt; test++) { \
int64_t size = sizes[test]; \
fill(dst, size, type); \
usec1 = utime(); \
sorter_ ## name (dst, size); \
usec2 = utime(); \
res = verify(dst, size); \
if (!res) { \
break; \
} \
diff += usec2 - usec1; \
} \
printf(" - %s, %10.1f usec\n", res ? "ok" : "FAILED", diff); \
if (!res) return 0; \
} while (0)
int run_tests(int64_t *sizes, int sizes_cnt, int type) {
int test, res;
double usec1, usec2, diff;
int64_t * dst = (int64_t *)malloc(MAXSIZE * sizeof(int64_t));
printf("-------\nRunning tests with %s:\n-------\n", test_names[type]);
TEST_STDLIB(qsort);
#if !defined(__linux__) && !defined(__CYGWIN__) && !defined(_WIN32)
TEST_STDLIB(heapsort);
TEST_STDLIB(mergesort);
#endif
if (MAXSIZE < 10000) {
#ifdef SET_SORT_EXTRA
TEST_SORT_H(selection_sort);
TEST_SORT_H(bubble_sort);
#endif
TEST_SORT_H(binary_insertion_sort);
TEST_SORT_H(bitonic_sort);
}
TEST_SORT_H(quick_sort);
TEST_SORT_H(merge_sort);
TEST_SORT_H(heap_sort);
TEST_SORT_H(shell_sort);
TEST_SORT_H(tim_sort);
TEST_SORT_H(merge_sort_in_place);
#ifdef SET_SORT_EXTRA
TEST_SORT_H(grail_sort);
TEST_SORT_H(sqrt_sort);
TEST_SORT_H(rec_stable_sort);
TEST_SORT_H(grail_sort_dyn_buffer);
#endif
free(dst);
return 0;
}
/* stability testing functions */
/* cheap hack to keep a copy to compare against */
static int **original;
static int first_original = 0;
/* make a int* array */
void make_intp_array(int **array, int64_t size, int num_values) {
int64_t i;
if (first_original == 0) {
first_original = 1;
original = malloc(sizeof(int *) * size);
}
for (i = 0; i < size; i++) {
array[i] = original[i] = malloc(sizeof(int));
*(array[i]) = lrand48() % num_values;
}
}
/* free all the pointers */
void clean_intp_array(int **array, int64_t size) {
int64_t i;
for (i = 0; i < size; i++) {
free(array[i]);
}
}
/* find the first instance of an element in an array of pointers */
int64_t find_next(int **array, int64_t start, int64_t last, int value) {
int64_t i;
for (i = start; i < last; i++) {
if (*(array[i]) == value) {
break;
}
}
return i;
}
/* verify that the given list is stable */
int verify_stable(int **array, int64_t size, int num_values) {
int value;
int64_t i = 0;
int64_t j = 0;
for (value = 0; value < num_values; value++) {
while (1) {
i = find_next(original, i, size, value);
/* unlikely, but possible */
if (i == size) {
break;
}
j = find_next(array, j, size, value);
if (j == size) {
return 0;
}
if (original[i] != array[j]) {
return 0;
}
i++;
j++;
}
}
return 1;
}
/* Checks that given sort function is stable. */
void check_stable(char *name, void (*sort_fun)(int **arr, size_t size), int size, int num_values) {
int **array = malloc(sizeof(int *) * size);
make_intp_array(array, size, num_values);
sort_fun(array, size);
printf("%21s -- %s\n", name, verify_stable(array, size, num_values) ? "stable" : "UNSTABLE");
clean_intp_array(array, size);
free(array);
}
/* Check which sorts are stable. */
void stable_tests(void) {
int size = 100000;
int num_values = 1000;
check_stable("binary insertion sort", stable_binary_insertion_sort, size, num_values);
#ifdef SET_SORT_EXTRA
check_stable("selection sort", stable_selection_sort, size, num_values);
check_stable("bubble sort", stable_bubble_sort, size, num_values);
#endif
check_stable("quick sort", stable_quick_sort, size, num_values);
check_stable("merge sort", stable_merge_sort, size, num_values);
check_stable("heap sort", stable_heap_sort, size, num_values);
check_stable("shell sort", stable_shell_sort, size, num_values);
check_stable("tim sort", stable_tim_sort, size, num_values);
check_stable("merge (in-place) sort", stable_merge_sort_in_place, size, num_values);
#ifdef SET_SORT_EXTRA
check_stable("grail sort", stable_grail_sort, size, num_values);
check_stable("sqrt sort", stable_sqrt_sort, size, num_values);
check_stable("rec stable sort", stable_rec_stable_sort, size, num_values);
check_stable("grail sort dyn byffer", stable_grail_sort_dyn_buffer, size, num_values);
#endif
}
int main(void) {
int i = 0;
int64_t sizes[TESTS];
srand48(SEED);
stable_tests();
fill_random(sizes, TESTS);
for (i = 0; i < TESTS; i++) {
RAND_RANGE(sizes[i], 1, MAXSIZE);
}
sizes[TESTS - 1] = MAXSIZE;
for (i = 0; i < FILL_LAST_ELEMENT; i++) {
int result = run_tests(sizes, TESTS, i);
if (result) {
return 1;
}
}
return 0;
}