#include <stdint.h> #include <stdio.h> #include <criterion/criterion.h> #include "util/hash.h" #include "sketch/count_median.h" #include "sketch/sketch.h" Test(count_median_sketch, expected_d, .disabled=0) { uint8_t b = 6; double epsilon = 0.25; double delta = 0.20; sketch_t *s = sketch_create(&countMedian, &carterWegman, b, epsilon, delta); count_median_t *cm = s->sketch; cr_assert_eq(cm->size.d, 15, "Wrong d value, expected %d got %"PRIu32, 16, cm->size.d); sketch_destroy(s); } Test(count_median_sketch, expected_w_16, .disabled=0) { uint8_t b = 6; double epsilon = 0.25; double delta = 0.20; sketch_t *s = sketch_create(&countMedian, &carterWegman, b, epsilon, delta); count_median_t *cm = s->sketch; cr_assert_eq(cm->size.w, 96, "Wrong w value, expected %d got %"PRIu32, 96, cm->size.w);
hh_sketch_t *hh_sketch_create(heavy_hitter_params_t *restrict p) { int8_t i; uint8_t np2_base; uint32_t w, d, top_tree_size; hh_sketch_params_t *restrict params = (hh_sketch_params_t *)p->params; const uint32_t m = params->m; const uint8_t logm = floor(log2((uint64_t)m+1)); const double phi = params->phi; const double epsilon = params->epsilon; const uint32_t twophi = ceil(2./phi); double delta = (double)((params->delta*phi)/(2.*logm)); const uint32_t b = params->b; const uint32_t result_size = sizeof(uint32_t) * twophi; hh_sketch_t *restrict hh = xmalloc( sizeof(hh_sketch_t) ); sketch_t *restrict s = sketch_create(params->f, p->hash, b, epsilon, delta); assert(phi > epsilon); // This only works since the sketch_size_t appears first in the *_sketch_t // structures! w = sketch_width(s->sketch); d = sketch_depth(s->sketch); hh->logm = logm; hh->params = params; hh->norm = 0; hh->result.count = 0; hh->fifo = fifo_create(twophi); hh->result.size = twophi; hh->result.hitters = xmalloc( result_size );
int main() { Sketch *sketch; SketchNode *node1, *node2; sketch = sketch_create(); /* Compute 4 + 3 */ node1 = node_create(sketch); node_set_input_number(node1, 0, 4); node_set_input_number(node1, 1, 3); node_set_transformation(node1, node1_transform); GArray *node1_output = node_get_output(node1); if(node1_output == NULL) { fprintf(stderr, "node_get_output returned NULL\n"); exit(1); } if(node1_output->len != 1) { fprintf(stderr, "node_get_output returned %d elements instead of 1\n", node1_output->len); exit(1); } { SketchValue *node1_output_1 = &g_array_index(node1_output, SketchValue, 0); if(node1_output_1->type != SKETCH_VALUE_NUMBER) { fprintf(stderr, "node_get_output didn't return a number (type code: %d)\n", node1_output_1->type); exit(1); } if(fabs(node1_output_1->value.number - 7) > 1e-7) { fprintf(stderr, "node_get_output didn't returned %f instead of 7\n", node1_output_1->value.number); exit(1); } } g_array_free(node1_output, TRUE); /* Compute (4 + 2) * 5 */ node2 = node_create(sketch); node_set_input_number(node1, 1, 2); node_set_input_node(node2, 0, node1, 0); node_set_input_number(node2, 1, 5); node_set_transformation(node2, node2_transform); GArray *node2_output = node_get_output(node2); if(node2_output == NULL) { fprintf(stderr, "node_get_output returned NULL\n"); exit(1); } if(node2_output->len != 1) { fprintf(stderr, "node_get_output returned %d elements instead of 1\n", node2_output->len); exit(1); } { SketchValue *node2_output_1 = &g_array_index(node2_output, SketchValue, 0); if(node2_output_1->type != SKETCH_VALUE_NUMBER) { fprintf(stderr, "node_get_output didn't return a number (type code: %d)\n", node2_output_1->type); exit(1); } if(fabs(node2_output_1->value.number - 30) > 1e-7) { fprintf(stderr, "node_get_output didn't returned %f instead of 30\n", node2_output_1->value.number); exit(1); } } g_array_free(node2_output, TRUE); node_destroy(sketch, &node2); node_destroy(sketch, &node1); sketch_destroy(&sketch); exit(0); }