Add 'deps/cmph/' from commit 'a250982ade093f4eed0552bbdd22dd7b0432007f'
git-subtree-dir: deps/cmph git-subtree-mainline:5040f4007bgit-subtree-split:a250982ade
This commit is contained in:
26
deps/cmph/tests/Makefile.am
vendored
Normal file
26
deps/cmph/tests/Makefile.am
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
TESTS = $(check_PROGRAMS)
|
||||
check_PROGRAMS = graph_tests select_tests compressed_seq_tests compressed_rank_tests cmph_benchmark_test
|
||||
noinst_PROGRAMS = packed_mphf_tests mphf_tests
|
||||
|
||||
AM_CPPFLAGS = -I../src/
|
||||
|
||||
graph_tests_SOURCES = graph_tests.c
|
||||
graph_tests_LDADD = ../src/libcmph.la
|
||||
|
||||
packed_mphf_tests_SOURCES = packed_mphf_tests.c
|
||||
packed_mphf_tests_LDADD = ../src/libcmph.la
|
||||
|
||||
mphf_tests_SOURCES = mphf_tests.c
|
||||
mphf_tests_LDADD = ../src/libcmph.la
|
||||
|
||||
select_tests_SOURCES = select_tests.c
|
||||
select_tests_LDADD = ../src/libcmph.la
|
||||
|
||||
compressed_seq_tests_SOURCES = compressed_seq_tests.c
|
||||
compressed_seq_tests_LDADD = ../src/libcmph.la
|
||||
|
||||
compressed_rank_tests_SOURCES = compressed_rank_tests.c
|
||||
compressed_rank_tests_LDADD = ../src/libcmph.la
|
||||
|
||||
cmph_benchmark_test_SOURCES = cmph_benchmark_test.c
|
||||
cmph_benchmark_test_LDADD = ../src/libcmph.la
|
||||
23
deps/cmph/tests/cmph_benchmark_test.c
vendored
Normal file
23
deps/cmph/tests/cmph_benchmark_test.c
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <unistd.h> // for sleep
|
||||
#include <limits.h>
|
||||
|
||||
#include "cmph_benchmark.h"
|
||||
|
||||
void bm_sleep(int iters) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
void bm_increment(int iters) {
|
||||
int i, v = 0;
|
||||
for (i = 0; i < INT_MAX; ++i) {
|
||||
v += i;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
BM_REGISTER(bm_sleep, 1);
|
||||
BM_REGISTER(bm_increment, 1);
|
||||
run_benchmarks(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
78
deps/cmph/tests/compressed_rank_tests.c
vendored
Normal file
78
deps/cmph/tests/compressed_rank_tests.c
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "../src/compressed_rank.h"
|
||||
|
||||
#define DEBUG
|
||||
#include "../src/debug.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void print_values(compressed_rank_t * cr, cmph_uint32 idx)
|
||||
{
|
||||
register cmph_uint32 index;
|
||||
|
||||
index = compressed_rank_query(cr, idx);
|
||||
fprintf(stderr, "Index[%u]\t= %u\n", idx, index);
|
||||
}
|
||||
|
||||
|
||||
static inline void print_values_packed(char * cr_packed, cmph_uint32 idx)
|
||||
{
|
||||
register cmph_uint32 index;
|
||||
|
||||
index = compressed_rank_query_packed(cr_packed, idx);
|
||||
fprintf(stderr, "Index[%u]\t= %u\n", idx, index);
|
||||
}
|
||||
|
||||
/*
|
||||
n = 20
|
||||
Indices: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
||||
vector[] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1}
|
||||
nzeros = 12
|
||||
zeroIndices[] = {0, 1, 2, 5, 7, 9, 11, 12, 13, 16, 17, 18}
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
compressed_rank_t cr;
|
||||
cmph_uint32 i = 0;
|
||||
cmph_uint32 n = 12;
|
||||
cmph_uint32 nIndices = 20;
|
||||
cmph_uint32 keys_vec[] = {0, 1, 2, 5, 7, 9, 11, 12, 13, 16, 17, 18};
|
||||
char *buf = NULL;
|
||||
cmph_uint32 buflen = 0;
|
||||
char * cr_packed = NULL;
|
||||
cmph_uint32 cr_pack_size = 0;
|
||||
|
||||
compressed_rank_init(&cr);
|
||||
compressed_rank_generate(&cr, keys_vec, n);
|
||||
fprintf(stderr, "Space usage = %u\n", compressed_rank_get_space_usage(&cr));
|
||||
for(i = 0; i < nIndices; i++)
|
||||
{
|
||||
print_values(&cr, i);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Dumping compressed rank structure\n");
|
||||
compressed_rank_dump(&cr, &buf, &buflen);
|
||||
|
||||
compressed_rank_destroy(&cr);
|
||||
fprintf(stderr, "Loading compressed rank structure\n");
|
||||
|
||||
compressed_rank_load(&cr, buf, buflen);
|
||||
for(i = 0; i < nIndices; i++)
|
||||
{
|
||||
print_values(&cr, i);
|
||||
}
|
||||
free(buf);
|
||||
|
||||
cr_pack_size = compressed_rank_packed_size(&cr);
|
||||
|
||||
cr_packed = (char *) calloc(cr_pack_size, sizeof(char));
|
||||
compressed_rank_pack(&cr, cr_packed);
|
||||
compressed_rank_destroy(&cr);
|
||||
|
||||
fprintf(stderr, "Querying the packed compressed rank structure\n");
|
||||
for(i = 0; i < nIndices; i++)
|
||||
{
|
||||
print_values_packed(cr_packed, i);
|
||||
}
|
||||
|
||||
free(cr_packed);
|
||||
return 0;
|
||||
}
|
||||
71
deps/cmph/tests/compressed_seq_tests.c
vendored
Normal file
71
deps/cmph/tests/compressed_seq_tests.c
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "../src/compressed_seq.h"
|
||||
|
||||
#define DEBUG
|
||||
#include "../src/debug.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void print_values(compressed_seq_t * cs, cmph_uint32 idx)
|
||||
{
|
||||
register cmph_uint32 index;
|
||||
|
||||
index = compressed_seq_query(cs, idx);
|
||||
fprintf(stderr, "Index[%u]\t= %u\n", idx, index);
|
||||
}
|
||||
|
||||
|
||||
static inline void print_values_packed(char * cs_packed, cmph_uint32 idx)
|
||||
{
|
||||
register cmph_uint32 index;
|
||||
|
||||
index = compressed_seq_query_packed(cs_packed, idx);
|
||||
fprintf(stderr, "Index[%u]\t= %u\n", idx, index);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
compressed_seq_t cs;
|
||||
cmph_uint32 i = 0;
|
||||
cmph_uint32 n = 20;
|
||||
cmph_uint32 keys_vec[] = { 0, 1, 1, 1, 2, 2, 2, 3, 5, 5,
|
||||
6, 6, 9, 9, 9, 12, 12, 13, 17, 1077};
|
||||
char *buf = NULL;
|
||||
cmph_uint32 buflen = 0;
|
||||
char * cs_packed = NULL;
|
||||
cmph_uint32 cs_pack_size = 0;
|
||||
|
||||
compressed_seq_init(&cs);
|
||||
compressed_seq_generate(&cs, keys_vec, n);
|
||||
fprintf(stderr, "Space usage = %u\n", compressed_seq_get_space_usage(&cs));
|
||||
for(i = 0; i < n; i++)
|
||||
{
|
||||
print_values(&cs, i);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Dumping compressed seq structure\n");
|
||||
compressed_seq_dump(&cs, &buf, &buflen);
|
||||
|
||||
compressed_seq_destroy(&cs);
|
||||
fprintf(stderr, "Loading compressed seq structure\n");
|
||||
|
||||
compressed_seq_load(&cs, buf, buflen);
|
||||
for(i = 0; i < n; i++)
|
||||
{
|
||||
print_values(&cs, i);
|
||||
}
|
||||
free(buf);
|
||||
|
||||
cs_pack_size = compressed_seq_packed_size(&cs);
|
||||
|
||||
cs_packed = (char *) calloc(cs_pack_size, sizeof(char));
|
||||
compressed_seq_pack(&cs, cs_packed);
|
||||
compressed_seq_destroy(&cs);
|
||||
|
||||
fprintf(stderr, "Querying the packed compressed seq structure\n");
|
||||
for(i = 0; i < n; i++)
|
||||
{
|
||||
print_values_packed(cs_packed, i);
|
||||
}
|
||||
|
||||
free(cs_packed);
|
||||
return 0;
|
||||
}
|
||||
67
deps/cmph/tests/graph_tests.c
vendored
Normal file
67
deps/cmph/tests/graph_tests.c
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "../src/graph.h"
|
||||
|
||||
#define DEBUG
|
||||
#include "../src/debug.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
graph_iterator_t it;
|
||||
cmph_uint32 i, neighbor;
|
||||
graph_t *g = graph_new(5, 10);
|
||||
|
||||
fprintf(stderr, "Building random graph\n");
|
||||
for (i = 0; i < 10; ++i)
|
||||
{
|
||||
cmph_uint32 v1 = i % 5;
|
||||
cmph_uint32 v2 = (i*2) % 5;
|
||||
if (v1 == v2) continue;
|
||||
graph_add_edge(g, v1, v2);
|
||||
DEBUGP("Added edge %u %u\n", v1, v2);
|
||||
}
|
||||
graph_print(g);
|
||||
graph_del_edge(g, 4, 3);
|
||||
graph_print(g);
|
||||
graph_clear_edges(g);
|
||||
graph_print(g);
|
||||
graph_destroy(g);
|
||||
|
||||
fprintf(stderr, "Building cyclic graph\n");
|
||||
g = graph_new(4, 5);
|
||||
graph_add_edge(g, 0, 3);
|
||||
graph_add_edge(g, 0, 1);
|
||||
graph_add_edge(g, 1, 2);
|
||||
graph_add_edge(g, 2, 0);
|
||||
if (!graph_is_cyclic(g))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
graph_destroy(g);
|
||||
|
||||
fprintf(stderr, "Building non-cyclic graph\n");
|
||||
g = graph_new(5, 4);
|
||||
graph_add_edge(g, 0, 1);
|
||||
graph_add_edge(g, 1, 2);
|
||||
graph_add_edge(g, 2, 3);
|
||||
graph_add_edge(g, 3, 4);
|
||||
|
||||
if (graph_is_cyclic(g))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Checking neighbors iterator\n");
|
||||
it = graph_neighbors_it(g, 1);
|
||||
neighbor = graph_next_neighbor(g, &it);
|
||||
DEBUGP("Neighbor is %u\n", neighbor);
|
||||
if (neighbor != 0 && neighbor != 2) return 1;
|
||||
neighbor = graph_next_neighbor(g, &it);
|
||||
DEBUGP("Neighbor is %u\n", neighbor);
|
||||
if (neighbor != 0 && neighbor != 2) return 1;
|
||||
neighbor = graph_next_neighbor(g, &it);
|
||||
DEBUGP("Neighbor is %u\n", neighbor);
|
||||
if (neighbor != GRAPH_NO_NEIGHBOR) return 1;
|
||||
|
||||
|
||||
graph_destroy(g);
|
||||
return 0;
|
||||
}
|
||||
161
deps/cmph/tests/mphf_tests.c
vendored
Normal file
161
deps/cmph/tests/mphf_tests.c
vendored
Normal file
@@ -0,0 +1,161 @@
|
||||
#ifdef WIN32
|
||||
#include "../wingetopt.h"
|
||||
#else
|
||||
#include <getopt.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
#include <assert.h>
|
||||
#include <cmph.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#define VERSION "0.8"
|
||||
#else
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
|
||||
void usage(const char *prg)
|
||||
{
|
||||
fprintf(stderr, "usage: %s [-v] [-h] [-V] [-k nkeys] [-m file.mph] keysfile\n", prg);
|
||||
}
|
||||
void usage_long(const char *prg)
|
||||
{
|
||||
fprintf(stderr, "usage: %s [-v] [-h] [-V] [-k nkeys] [-m file.mph] keysfile\n", prg);
|
||||
fprintf(stderr, "Packed MPHFs testing tool\n\n");
|
||||
fprintf(stderr, " -h\t print this help message\n");
|
||||
fprintf(stderr, " -V\t print version number and exit\n");
|
||||
fprintf(stderr, " -v\t increase verbosity (may be used multiple times)\n");
|
||||
fprintf(stderr, " -k\t number of keys\n");
|
||||
fprintf(stderr, " -m\t minimum perfect hash function file \n");
|
||||
fprintf(stderr, " keysfile\t line separated file with keys\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char verbosity = 0;
|
||||
char *mphf_file = NULL;
|
||||
const char *keys_file = NULL;
|
||||
FILE *mphf_fd = stdout;
|
||||
FILE *keys_fd;
|
||||
cmph_uint32 nkeys = UINT_MAX;
|
||||
cmph_uint32 i = 0;
|
||||
cmph_t *mphf = NULL;
|
||||
cmph_io_adapter_t *source;
|
||||
while (1)
|
||||
{
|
||||
char ch = (char)getopt(argc, argv, "hVvk:m:");
|
||||
if (ch == -1) break;
|
||||
switch (ch)
|
||||
{
|
||||
case 'k':
|
||||
{
|
||||
char *endptr;
|
||||
nkeys = (cmph_uint32) strtoul(optarg, &endptr, 10);
|
||||
if(*endptr != 0) {
|
||||
fprintf(stderr, "Invalid number of keys %s\n", optarg);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'm':
|
||||
mphf_file = strdup(optarg);
|
||||
break;
|
||||
case 'v':
|
||||
++verbosity;
|
||||
break;
|
||||
case 'V':
|
||||
printf("%s\n", VERSION);
|
||||
return 0;
|
||||
case 'h':
|
||||
usage_long(argv[0]);
|
||||
return 0;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (optind != argc - 1)
|
||||
{
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
keys_file = argv[optind];
|
||||
|
||||
int ret = 0;
|
||||
if (mphf_file == NULL)
|
||||
{
|
||||
mphf_file = (char *)malloc(strlen(keys_file) + 5);
|
||||
memcpy(mphf_file, keys_file, strlen(keys_file));
|
||||
memcpy(mphf_file + strlen(keys_file), ".mph\0", (size_t)5);
|
||||
}
|
||||
|
||||
keys_fd = fopen(keys_file, "r");
|
||||
|
||||
if (keys_fd == NULL)
|
||||
{
|
||||
fprintf(stderr, "Unable to open file %s: %s\n", keys_file, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(nkeys == UINT_MAX) source = cmph_io_nlfile_adapter(keys_fd);
|
||||
else source = cmph_io_nlnkfile_adapter(keys_fd, nkeys);
|
||||
|
||||
cmph_uint8 * hashtable = NULL;
|
||||
mphf_fd = fopen(mphf_file, "rb");
|
||||
if (mphf_fd == NULL)
|
||||
{
|
||||
fprintf(stderr, "Unable to open input file %s: %s\n", mphf_file, strerror(errno));
|
||||
free(mphf_file);
|
||||
return -1;
|
||||
}
|
||||
mphf = cmph_load(mphf_fd);
|
||||
fclose(mphf_fd);
|
||||
if (!mphf)
|
||||
{
|
||||
fprintf(stderr, "Unable to parser input file %s\n", mphf_file);
|
||||
free(mphf_file);
|
||||
return -1;
|
||||
}
|
||||
cmph_uint32 siz = cmph_size(mphf);
|
||||
hashtable = (cmph_uint8*)malloc(siz*sizeof(cmph_uint8));
|
||||
memset(hashtable, 0, (size_t)siz);
|
||||
//check all keys
|
||||
for (i = 0; i < source->nkeys; ++i)
|
||||
{
|
||||
cmph_uint32 h;
|
||||
char *buf;
|
||||
cmph_uint32 buflen = 0;
|
||||
source->read(source->data, &buf, &buflen);
|
||||
h = cmph_search(mphf, buf, buflen);
|
||||
if (!(h < siz))
|
||||
{
|
||||
fprintf(stderr, "Unknown key %*s in the input.\n", buflen, buf);
|
||||
ret = 1;
|
||||
} else if(hashtable[h])
|
||||
{
|
||||
fprintf(stderr, "Duplicated or unknown key %*s in the input\n", buflen, buf);
|
||||
ret = 1;
|
||||
} else hashtable[h] = 1;
|
||||
|
||||
if (verbosity)
|
||||
{
|
||||
printf("%s -> %u\n", buf, h);
|
||||
}
|
||||
source->dispose(source->data, buf, buflen);
|
||||
}
|
||||
|
||||
cmph_destroy(mphf);
|
||||
free(hashtable);
|
||||
|
||||
fclose(keys_fd);
|
||||
free(mphf_file);
|
||||
cmph_io_nlfile_adapter_destroy(source);
|
||||
return ret;
|
||||
|
||||
}
|
||||
203
deps/cmph/tests/packed_mphf_tests.c
vendored
Normal file
203
deps/cmph/tests/packed_mphf_tests.c
vendored
Normal file
@@ -0,0 +1,203 @@
|
||||
#ifdef WIN32
|
||||
#include "../wingetopt.h"
|
||||
#else
|
||||
#include <getopt.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
#include <assert.h>
|
||||
#include <cmph.h>
|
||||
//#include "hash.h"
|
||||
|
||||
#ifdef WIN32
|
||||
#define VERSION "0.8"
|
||||
#else
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
|
||||
void usage(const char *prg)
|
||||
{
|
||||
fprintf(stderr, "usage: %s [-v] [-h] [-V] [-t keys_per_bin] [-k nkeys] [-m file.mph] keysfile\n", prg);
|
||||
}
|
||||
void usage_long(const char *prg)
|
||||
{
|
||||
fprintf(stderr, "usage: %s [-v] [-h] [-V] [-t keys_per_bin] [-k nkeys] [-m file.mph] keysfile\n", prg);
|
||||
fprintf(stderr, "Packed MPHFs testing tool\n\n");
|
||||
fprintf(stderr, " -h\t print this help message\n");
|
||||
fprintf(stderr, " -V\t print version number and exit\n");
|
||||
fprintf(stderr, " -v\t increase verbosity (may be used multiple times)\n");
|
||||
fprintf(stderr, " -t\t set the number of keys per bin for a t-perfect hashing function.\n");
|
||||
fprintf(stderr, " \t A t-perfect hashing function allows at most t collisions in a given bin.\n");
|
||||
fprintf(stderr, " -k\t number of keys\n");
|
||||
fprintf(stderr, " -m\t minimum perfect hash function file \n");
|
||||
fprintf(stderr, " keysfile\t line separated file with keys\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char verbosity = 0;
|
||||
char *mphf_file = NULL;
|
||||
const char *keys_file = NULL;
|
||||
FILE *mphf_fd = stdout;
|
||||
FILE *keys_fd;
|
||||
cmph_uint32 nkeys = UINT_MAX;
|
||||
cmph_uint32 i = 0;
|
||||
cmph_t *mphf = NULL;
|
||||
cmph_io_adapter_t *source;
|
||||
cmph_uint32 keys_per_bin = 1;
|
||||
|
||||
while (1)
|
||||
{
|
||||
char ch = (char)getopt(argc, argv, "hVvt:k:m:");
|
||||
if (ch == -1) break;
|
||||
switch (ch)
|
||||
{
|
||||
case 'k':
|
||||
{
|
||||
char *endptr;
|
||||
nkeys = (cmph_uint32)strtoul(optarg, &endptr, 10);
|
||||
if(*endptr != 0) {
|
||||
fprintf(stderr, "Invalid number of keys %s\n", optarg);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'm':
|
||||
mphf_file = strdup(optarg);
|
||||
break;
|
||||
case 'v':
|
||||
++verbosity;
|
||||
break;
|
||||
case 't':
|
||||
{
|
||||
char *cptr;
|
||||
keys_per_bin = (cmph_uint32)strtoul(optarg, &cptr, 10);
|
||||
if(*cptr != 0) {
|
||||
fprintf(stderr, "Parameter t was not found: %s\n", optarg);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'V':
|
||||
printf("%s\n", VERSION);
|
||||
return 0;
|
||||
case 'h':
|
||||
usage_long(argv[0]);
|
||||
return 0;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (optind != argc - 1)
|
||||
{
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
keys_file = argv[optind];
|
||||
|
||||
int ret = 0;
|
||||
if (mphf_file == NULL)
|
||||
{
|
||||
mphf_file = (char *)malloc(strlen(keys_file) + 5);
|
||||
memcpy(mphf_file, keys_file, strlen(keys_file));
|
||||
memcpy(mphf_file + strlen(keys_file), ".mph\0", (size_t)5);
|
||||
}
|
||||
|
||||
keys_fd = fopen(keys_file, "r");
|
||||
|
||||
if (keys_fd == NULL)
|
||||
{
|
||||
fprintf(stderr, "Unable to open file %s: %s\n", keys_file, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(nkeys == UINT_MAX) source = cmph_io_nlfile_adapter(keys_fd);
|
||||
else source = cmph_io_nlnkfile_adapter(keys_fd, nkeys);
|
||||
|
||||
cmph_uint8 * hashtable = NULL;
|
||||
mphf_fd = fopen(mphf_file, "rb");
|
||||
if (mphf_fd == NULL)
|
||||
{
|
||||
fprintf(stderr, "Unable to open input file %s: %s\n", mphf_file, strerror(errno));
|
||||
free(mphf_file);
|
||||
return -1;
|
||||
}
|
||||
mphf = cmph_load(mphf_fd);
|
||||
fclose(mphf_fd);
|
||||
if (!mphf)
|
||||
{
|
||||
fprintf(stderr, "Unable to parser input file %s\n", mphf_file);
|
||||
free(mphf_file);
|
||||
return -1;
|
||||
}
|
||||
cmph_uint32 siz = cmph_size(mphf);
|
||||
hashtable = (cmph_uint8*)calloc(siz, sizeof(cmph_uint8));
|
||||
memset(hashtable, 0, (size_t)siz);
|
||||
|
||||
// packing the function
|
||||
/* Determine how much space is needed to pack the mphf. */
|
||||
cmph_uint32 packed_size = cmph_packed_size(mphf);
|
||||
fprintf(stderr, "packed_size = %u\n", packed_size);
|
||||
|
||||
/* Make sure that we have enough space to pack the mphf. */
|
||||
cmph_uint8 * packed_mphf = (cmph_uint8 *)calloc((size_t)packed_size,(size_t)1);
|
||||
|
||||
/* Pack the mphf. */
|
||||
cmph_pack(mphf, packed_mphf);
|
||||
|
||||
// testing the packed function
|
||||
//check all keys
|
||||
#ifdef CMPH_TIMING
|
||||
double evaluation_time_begin = 0.0;
|
||||
double evaluation_time = 0.0;
|
||||
ELAPSED_TIME_IN_SECONDS(&evaluation_time_begin);
|
||||
#endif
|
||||
|
||||
for (i = 0; i < source->nkeys; ++i)
|
||||
{
|
||||
cmph_uint32 h;
|
||||
char *buf;
|
||||
cmph_uint32 buflen = 0;
|
||||
source->read(source->data, &buf, &buflen);
|
||||
h = cmph_search_packed(packed_mphf, buf, buflen);
|
||||
|
||||
if (!(h < siz))
|
||||
{
|
||||
fprintf(stderr, "Unknown key %*s in the input.\n", buflen, buf);
|
||||
ret = 1;
|
||||
} else if(hashtable[h] >= keys_per_bin)
|
||||
{
|
||||
fprintf(stderr, "More than %u keys were mapped to bin %u\n", keys_per_bin, h);
|
||||
fprintf(stderr, "Duplicated or unknown key %*s in the input\n", buflen, buf);
|
||||
ret = 1;
|
||||
} else hashtable[h]++;
|
||||
|
||||
if (verbosity)
|
||||
{
|
||||
printf("%s -> %u\n", buf, h);
|
||||
}
|
||||
source->dispose(source->data, buf, buflen);
|
||||
}
|
||||
#ifdef CMPH_TIMING
|
||||
ELAPSED_TIME_IN_SECONDS(&evaluation_time);
|
||||
evaluation_time = evaluation_time - evaluation_time_begin;
|
||||
fprintf(stdout, "%u\t%.2f\n", source->nkeys, evaluation_time);
|
||||
#endif
|
||||
|
||||
free(packed_mphf);
|
||||
cmph_destroy(mphf);
|
||||
free(hashtable);
|
||||
|
||||
fclose(keys_fd);
|
||||
free(mphf_file);
|
||||
cmph_io_nlfile_adapter_destroy(source);
|
||||
return ret;
|
||||
|
||||
}
|
||||
97
deps/cmph/tests/select_tests.c
vendored
Normal file
97
deps/cmph/tests/select_tests.c
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "../src/select.h"
|
||||
|
||||
#define DEBUG
|
||||
#include "../src/debug.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void print_values(select_t * sel)
|
||||
{
|
||||
register cmph_uint32 index;
|
||||
|
||||
index = select_query(sel, 0);
|
||||
fprintf(stderr, "Index[0]\t= %u\n", index - 0);
|
||||
|
||||
index = select_next_query(sel, index);
|
||||
fprintf(stderr, "Next Index\t= %u\n", index);
|
||||
|
||||
index = select_query(sel, 1);
|
||||
fprintf(stderr, "Index[1]\t= %u\n", index - 1);
|
||||
|
||||
index = select_next_query(sel, index);
|
||||
fprintf(stderr, "Next Index\t= %u\n", index);
|
||||
|
||||
index = select_query(sel, 2);
|
||||
fprintf(stderr, "Index[2]\t= %u\n", index - 2);
|
||||
|
||||
index = select_next_query(sel, index);
|
||||
fprintf(stderr, "Next Index\t= %u\n", index);
|
||||
|
||||
index = select_query(sel, 3);
|
||||
fprintf(stderr, "Index[3]\t= %u\n", index - 3);
|
||||
}
|
||||
|
||||
|
||||
static inline void print_values_packed(char * sel_packed)
|
||||
{
|
||||
register cmph_uint32 index;
|
||||
|
||||
index = select_query_packed(sel_packed, 0);
|
||||
fprintf(stderr, "Index[0]\t= %u\n", index - 0);
|
||||
|
||||
index = select_next_query_packed(sel_packed, index);
|
||||
fprintf(stderr, "Next Index\t= %u\n", index);
|
||||
|
||||
index = select_query_packed(sel_packed, 1);
|
||||
fprintf(stderr, "Index[1]\t= %u\n", index - 1);
|
||||
|
||||
index = select_next_query_packed(sel_packed, index);
|
||||
fprintf(stderr, "Next Index\t= %u\n", index);
|
||||
|
||||
index = select_query_packed(sel_packed, 2);
|
||||
fprintf(stderr, "Index[2]\t= %u\n", index - 2);
|
||||
|
||||
index = select_next_query_packed(sel_packed, index);
|
||||
fprintf(stderr, "Next Index\t= %u\n", index);
|
||||
|
||||
index = select_query_packed(sel_packed, 3);
|
||||
fprintf(stderr, "Index[3]\t= %u\n", index - 3);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
select_t sel;
|
||||
cmph_uint32 n = 4;
|
||||
cmph_uint32 keys_vec[4] = {0,1,2,3};
|
||||
cmph_uint32 m = keys_vec[3];
|
||||
char *buf = NULL;
|
||||
cmph_uint32 buflen = 0;
|
||||
char * select_packed = NULL;
|
||||
cmph_uint32 select_pack_size = 0;
|
||||
|
||||
select_init(&sel);
|
||||
select_generate(&sel, keys_vec, n, m);
|
||||
fprintf(stderr, "Space usage = %u\n", select_get_space_usage(&sel));
|
||||
print_values(&sel);
|
||||
|
||||
fprintf(stderr, "Dumping select structure\n");
|
||||
select_dump(&sel, &buf, &buflen);
|
||||
|
||||
select_destroy(&sel);
|
||||
fprintf(stderr, "Loading select structure\n");
|
||||
|
||||
select_load(&sel, buf, buflen);
|
||||
print_values(&sel);
|
||||
free(buf);;
|
||||
|
||||
select_pack_size = select_packed_size(&sel);
|
||||
|
||||
select_packed = (char *) calloc(select_pack_size, sizeof(char));
|
||||
select_pack(&sel, select_packed);
|
||||
select_destroy(&sel);
|
||||
|
||||
fprintf(stderr, "Querying the packed select structure\n");
|
||||
print_values_packed(select_packed);
|
||||
|
||||
free(select_packed);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user