-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.c
92 lines (81 loc) · 2.28 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "disjoint_set.h"
typedef int T;
void shuffle(T* array, size_t n) {
srand(time(NULL));
for (size_t i=0; i < n-1; i++) {
size_t j = i + rand() / (RAND_MAX / (n-1)) + 1;
T temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
typedef struct Q {
disj_elem elem;
int data;
} Q;
const char* check(bool actual, bool expected) {
return actual == expected ? "PASS" : "FAIL";
}
bool SAMESET(Q* arr, size_t a, size_t b, bool expected) {
bool actual = same_set(arr[a].elem, arr[b].elem);
printf("%s: same_set(%zu, %zu): %s\n", check(actual, expected), a, b, actual?"true":"false");
return actual == expected;
}
bool UNITE(Q* arr, size_t a, size_t b, bool before_expected) {
SAMESET(arr, a, b, before_expected);
bool actual = unite(arr[a].elem, arr[b].elem);
printf("%s: unite(%zu, %zu): %s\n", check(actual, before_expected), a, b, actual?"true":"false");
return (actual == before_expected) & SAMESET(arr, a, b, true);
}
typedef bool command(Q*, size_t, size_t, bool);
typedef struct test {
command* cmd;
int a, b;
bool expected;
} test;
int main() {
#define WORLD_SIZE 1000
int map[WORLD_SIZE];
for (int i=0; i < WORLD_SIZE; i++) {
map[i] = i;
}
shuffle(map, WORLD_SIZE);
disj_elem arena[WORLD_SIZE];
init_arena(arena, WORLD_SIZE);
Q arr[10];
for (int i=0; i < 10; i++) {
arr[i].elem = arena[map[i]];
arr[i].data = i;
}
test suite[] = {
{SAMESET, 0, 0, true},
{SAMESET, 0, 1, false},
{UNITE, 1, 0, false},
{SAMESET, 0, 1, true},
{SAMESET, 0, 2, false},
{UNITE, 2, 3, false},
{SAMESET, 3, 2, true},
{SAMESET, 0, 2, false},
{SAMESET, 0, 3, false},
{SAMESET, 1, 2, false},
{SAMESET, 1, 3, false},
{UNITE, 3, 0, false},
{SAMESET, 3, 2, true},
{SAMESET, 0, 2, true},
{SAMESET, 0, 3, true},
{SAMESET, 1, 2, true},
{SAMESET, 1, 3, true},
};
int passed = 0, total = 0;
for (size_t i=0; i < sizeof(suite) / sizeof(suite[0]); i++) {
test t = suite[i];
if (t.cmd(arr, t.a, t.b, t.expected))
passed++;
total++;
}
printf("Passed %d out of %d\n", passed, total);
return 0;
}