forked from arminbiere/kissat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestrank.c
112 lines (105 loc) · 2.67 KB
/
testrank.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
#include "../src/allocate.h"
#include "../src/rank.h"
#include "../src/stack.h"
#include "test.h"
static unsigned rank_unsigned (unsigned a) { return a; }
static void test_rank_unsigneds (void) {
#define M 2
srand (42);
printf ("running %d rounds\n", M);
for (unsigned i = 0; i < M; i++) {
#define N 80000
DECLARE_AND_INIT_SOLVER (solver);
unsigned found[N];
memset (found, 0, sizeof found);
unsigned A[N];
printf ("generating and ranking %d random numbers\n", N);
for (unsigned j = 0; j < N; j++)
A[j] = rand () % N;
for (unsigned j = 0; j < N; j++)
found[A[j]]++;
#if 0
printf ("before radix sorting");
for (unsigned j = 0; j < N; j++)
printf (" %u", A[j]);
printf ("\n");
fflush (stdout);
#endif
RADIX_SORT (unsigned, unsigned, N, A, rank_unsigned);
#if 0
printf ("after radix sorting");
for (unsigned j = 0; j < N; j++)
printf (" %u", A[j]);
printf ("\n");
fflush (stdout);
#endif
for (unsigned j = 1; j < N; j++)
assert (A[j - 1] <= A[j]);
for (unsigned j = 0; j < N; j++) {
assert (A[j] < N);
assert (found[A[j]] > 0);
found[A[j]]--;
}
for (unsigned j = 0; j < N; j++)
assert (!found[j]);
#ifndef QUIET
RELEASE_STACK (solver->profiles.stack);
#endif
#ifdef METRICS
assert (!solver->statistics.allocated_current);
#endif
#undef N
}
#undef M
}
#include <inttypes.h>
#include <string.h>
static uint64_t rank_string (const char *str) {
unsigned char ch;
uint64_t res = 0;
unsigned i = 64;
for (const char *p = str; (ch = *p); p++) {
assert (i);
i -= 8;
uint64_t tmp = ch;
res |= tmp << i;
}
return res;
}
static void test_rank_strings (void) {
#define N 10
DECLARE_AND_INIT_SOLVER (solver);
char S[N][9];
memset (S, 0, sizeof S);
char *A[N];
for (unsigned i = 0; i < N; i++)
A[i] = S[i];
srand (42);
for (unsigned i = 0; i < N; i++) {
unsigned len = (rand () % 8) + 1;
for (unsigned j = 0; j < len; j++)
S[i][j] = 'a' + (rand () % 26);
S[i][len] = 0;
}
printf ("before radix sorting:\n\n");
for (unsigned i = 0; i < N; i++)
printf ("A[%u] %s\n", i, A[i]);
fflush (stdout);
RADIX_SORT (char *, uint64_t, N, A, rank_string);
printf ("\nafter radix sorting:\n\n");
for (unsigned i = 0; i < N; i++)
printf ("A[%u] %s\n", i, A[i]);
fflush (stdout);
for (unsigned i = 1; i < N; i++)
assert (strcmp (A[i - 1], A[i]) <= 0);
#ifndef QUIET
RELEASE_STACK (solver->profiles.stack);
#endif
#ifdef METRICS
assert (!solver->statistics.allocated_current);
#endif
}
void tissat_schedule_rank (void) {
SCHEDULE_FUNCTION (test_rank_unsigneds);
SCHEDULE_FUNCTION (test_rank_strings);
}