| #include <assert.h> // assert |
| #include <stdio.h> // fprintf, stderr |
| #include <stdlib.h> // size_t |
| #include "hex.h" |
| #include "median.h" |
| |
| #define TEST_SIZE 1024 * 1024 * 256 // 256M |
| // A sinple main routine meant as an example, |
| // and also to unit test most of the intersting |
| // functions of the median module. |
| int main(int argc, char* argv[]) { |
| // Call suggest buffer size to determine the size of the buffer |
| // we need to build the data structure. |
| size_t n = 0; |
| |
| // Allocate the buffer of the required size. And then |
| // initialize it (median_init_buffer). |
| assert(median_suggest_buffer_size(.001, TEST_SIZE, N_WINDOWS_DEFAULT, &n) == MEDIAN_OK); |
| void* b = malloc(n); |
| assert(b); |
| median_buffer_t buf; |
| assert(median_init_buffer(b, n, .001, TEST_SIZE, N_WINDOWS_DEFAULT, &buf) == MEDIAN_OK); |
| |
| // Initialize the spare buffer. |
| assert(median_suggest_scratch_buffer_size(buf, &n) == MEDIAN_OK); |
| void* s = malloc(n); |
| assert(s); |
| median_scratch_buffer_t scratch; |
| assert(median_init_scratch_buffer(s, n, buf, &scratch) == MEDIAN_OK); |
| |
| // Insert some data. |
| for (median_data_t i = 1; i < TEST_SIZE; i++) { // 1G |
| assert(median_insert_data(buf, i, scratch) == MEDIAN_OK); |
| } |
| |
| // Dump the buffer. |
| assert(median_dump_stderr(buf) == MEDIAN_OK); |
| |
| median_data_t median; |
| assert(median_output_median(buf, &median, scratch) == MEDIAN_OK); |
| fprintf(stderr, "And the median is %x\n", (unsigned)median); |
| |
| size_t obs = 0; |
| assert(median_suggest_json_string_size(buf, &obs) == MEDIAN_OK); |
| void* ob = malloc(obs); |
| assert(ob); |
| median_json_string_t mob; |
| assert(median_init_json_string(ob, obs, buf, &mob) == MEDIAN_OK); |
| |
| // Free up the resources. |
| assert(median_deinit_buffer(buf) == MEDIAN_OK); |
| assert(median_deinit_scratch_buffer(scratch) == MEDIAN_OK); |
| assert(median_deinit_json_string(mob) == MEDIAN_OK); |
| |
| // Free up the malloc'd space. |
| free(b); |
| free(s); |
| free(ob); |
| |
| // All done, return 0 |
| return 0; |
| } |