int main() { #define ENTRY_COUNT 13 // {seconds,entry} const uint32_t testData[ENTRY_COUNT][3] = { // The entry between the */and/* is the one which was just altered. // second col1 col2 col3 average { 0,/* */0/* */, 0}, { 1,/* 0 */1/* */, 0}, { 2,/* 0 1 */2/* */, 1}, { 3,/* */3/* 1 2 */, 2}, { 4,/* 3 */4/* 2 */, 3}, { 5,/* 3 4 */5/* */, 4}, { 6,/* */6/* 4 5 */, 5}, { 7,/* 6 */7/* 5 */, 6}, { 8,/* 6 7 */8/* */, 7}, { 9,/* */9/* 7 8 */, 8}, // 9 */7/* 8 8 skipped a second {11,/* 9 7 */7/* */, 7}, {12,/* */6/* 7 7 */, 6}, // 6 */7/* 7 6 skipped a second {14,/* 6 7 */4/* */, 5} }; const uint32_t windowSeconds = 3; uint8_t buffer[4096]; struct Allocator* allocator = BufferAllocator_new(buffer, 4096); struct event_base* eventBase = event_base_new(); struct AverageRoller* roller = (struct AverageRoller*) AverageRoller_new(windowSeconds, eventBase, allocator); // To make life easy we will pretend it's january first 1970... roller->lastUpdateTime = 0; uint32_t ret = 0; for (uint32_t i = 0; i < ENTRY_COUNT; i++) { uint32_t average = update(roller, testData[i][0], testData[i][1]); if (average != testData[i][2]) { printf("For average #%d, expected %d, got %d, float: %f, entryCount: %d\n", (int) i, (int) testData[i][2], (int) average, ((float) roller->sum) / roller->entryCount, (int) roller->entryCount); ret = 1; } } return ret; }
struct DHTModule* dm = Allocator_clone(allocator, (&(struct DHTModule) { .name = "RouterModule", .context = out, .handleIncoming = handleIncoming, .handleOutgoing = handleOutgoing })); DHTModuleRegistry_register(dm, registry); Hex_decode(out->gitVersionBytes, 20, Version_gitVersion(), 40); out->gitVersion.len = 20; out->gitVersion.bytes = (char*) out->gitVersionBytes; Address_forKey(&out->address, myAddress); out->gmrtRoller = AverageRoller_new(GMRT_SECONDS, eventBase, allocator); AverageRoller_update(out->gmrtRoller, GMRT_INITAL_MILLISECONDS); out->nodeStore = NodeStore_new(&out->address, NODE_STORE_SIZE, allocator, logger, rand); out->registry = registry; out->eventBase = eventBase; out->logger = logger; out->allocator = allocator; out->admin = admin; out->rand = rand; out->pinger = Pinger_new(eventBase, rand, logger, allocator); out->janitor = Janitor_new(LOCAL_MAINTENANCE_SEARCH_MILLISECONDS, GLOBAL_MAINTENANCE_SEARCH_MILLISECONDS, out, out->nodeStore, logger, allocator,