This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
72 lines (58 loc) · 1.52 KB
/
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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "test.h"
typedef struct TestCase TestCase;
#include "config.h"
static bool is_array_sorted(int *a, int len) {
for (int i = 0; i < len-1; ++i)
if (a[i] > a[i+1])
return false;
return true;
}
static void test(TestCase t) {
for (int i = 0; i < LENGTH(seqs); ++i) {
Sequence seq = seqs[i];
int repeat = repeattest;
seq.data = NULL;
while (repeat--) {
free(seq.data);
seq.data = malloc(seq.len * sizeof(int));
memcpy(seq.data, seqs[i].data, seq.len * sizeof(int));
Metric aux = {0};
clock_t t_beg, t_end;
t_beg = clock();
aux = t.sort(seq);
t_end = clock();
aux.ptime = (double)(t_end - t_beg) / (CLOCKS_PER_SEC/1000);
t.mets[i].comp += aux.comp;
t.mets[i].swap += aux.swap;
t.mets[i].ptime += aux.ptime;
}
t.mets[i].comp /= repeattest;
t.mets[i].swap /= repeattest;
t.mets[i].ptime /= repeattest;
printf("%s,%d,%s,%lld,%lld,%.3lf,%s\n",
t.name,
seqs[i].len,
seqs[i].name,
t.mets[i].comp,
t.mets[i].swap,
t.mets[i].ptime,
is_array_sorted(seq.data, seq.len) ? "sorted" : "UNSORTED");
free(seq.data);
}
}
int main(void) {
srand(time(0));
for (int i = 0; i < LENGTH(seqs); ++i)
seqs[i].data = seqs[i].initpop(seqs[i].len);
printf("Algorithm,Load number,Init sort,Comparisons,Swaps,CPU time (ms),Is sorted?\n");
for (int i = 0; i < LENGTH(tests); ++i)
test(tests[i]);
for (int i = 0; i < LENGTH(seqs); ++i)
free(seqs[i].data);
return 0;
}