void test_nghttp2_pq(void) { int i; nghttp2_pq pq; nghttp2_pq_init(&pq, pq_compar); CU_ASSERT(nghttp2_pq_empty(&pq)); CU_ASSERT(0 == nghttp2_pq_size(&pq)); CU_ASSERT(0 == nghttp2_pq_push(&pq, (void *)"foo")); CU_ASSERT(0 == nghttp2_pq_empty(&pq)); CU_ASSERT(1 == nghttp2_pq_size(&pq)); CU_ASSERT(strcmp("foo", nghttp2_pq_top(&pq)) == 0); CU_ASSERT(0 == nghttp2_pq_push(&pq, (void *)"bar")); CU_ASSERT(strcmp("bar", nghttp2_pq_top(&pq)) == 0); CU_ASSERT(0 == nghttp2_pq_push(&pq, (void *)"baz")); CU_ASSERT(strcmp("bar", nghttp2_pq_top(&pq)) == 0); CU_ASSERT(0 == nghttp2_pq_push(&pq, (void *)"C")); CU_ASSERT(4 == nghttp2_pq_size(&pq)); CU_ASSERT(strcmp("C", nghttp2_pq_top(&pq)) == 0); nghttp2_pq_pop(&pq); CU_ASSERT(3 == nghttp2_pq_size(&pq)); CU_ASSERT(strcmp("bar", nghttp2_pq_top(&pq)) == 0); nghttp2_pq_pop(&pq); CU_ASSERT(strcmp("baz", nghttp2_pq_top(&pq)) == 0); nghttp2_pq_pop(&pq); CU_ASSERT(strcmp("foo", nghttp2_pq_top(&pq)) == 0); nghttp2_pq_pop(&pq); CU_ASSERT(nghttp2_pq_empty(&pq)); CU_ASSERT(0 == nghttp2_pq_size(&pq)); CU_ASSERT(NULL == nghttp2_pq_top(&pq)); /* Add bunch of entry to see realloc works */ for (i = 0; i < 10000; ++i) { CU_ASSERT(0 == nghttp2_pq_push(&pq, (void *)"foo")); CU_ASSERT((size_t)(i + 1) == nghttp2_pq_size(&pq)); } for (i = 10000; i > 0; --i) { CU_ASSERT(NULL != nghttp2_pq_top(&pq)); nghttp2_pq_pop(&pq); CU_ASSERT((size_t)(i - 1) == nghttp2_pq_size(&pq)); } nghttp2_pq_free(&pq); }
nghttp2_outbound_item * nghttp2_stream_next_outbound_item(nghttp2_stream *stream) { nghttp2_pq_entry *ent; for (;;) { if (stream_active(stream)) { return stream->item; } ent = nghttp2_pq_top(&stream->obq); if (!ent) { return NULL; } stream = nghttp2_struct_of(ent, nghttp2_stream, pq_entry); } }
nghttp2_outbound_item * nghttp2_stream_next_outbound_item(nghttp2_stream *stream) { nghttp2_pq_entry *ent; nghttp2_stream *si; for (;;) { if (stream_active(stream)) { /* Update ascendant's descendant_last_cycle here, so that we can assure that new stream is scheduled based on it. */ for (si = stream; si->dep_prev; si = si->dep_prev) { si->dep_prev->descendant_last_cycle = si->cycle; } return stream->item; } ent = nghttp2_pq_top(&stream->obq); if (!ent) { return NULL; } stream = nghttp2_struct_of(ent, nghttp2_stream, pq_entry); } }
void test_nghttp2_pq(void) { int i; nghttp2_pq pq; string_entry *top; nghttp2_pq_init(&pq, pq_less, nghttp2_mem_default()); CU_ASSERT(nghttp2_pq_empty(&pq)); CU_ASSERT(0 == nghttp2_pq_size(&pq)); CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("foo")->ent)); CU_ASSERT(0 == nghttp2_pq_empty(&pq)); CU_ASSERT(1 == nghttp2_pq_size(&pq)); top = (string_entry *)nghttp2_pq_top(&pq); CU_ASSERT(strcmp("foo", top->s) == 0); CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("bar")->ent)); top = (string_entry *)nghttp2_pq_top(&pq); CU_ASSERT(strcmp("bar", top->s) == 0); CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("baz")->ent)); top = (string_entry *)nghttp2_pq_top(&pq); CU_ASSERT(strcmp("bar", top->s) == 0); CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("C")->ent)); CU_ASSERT(4 == nghttp2_pq_size(&pq)); top = (string_entry *)nghttp2_pq_top(&pq); CU_ASSERT(strcmp("C", top->s) == 0); string_entry_del(top); nghttp2_pq_pop(&pq); CU_ASSERT(3 == nghttp2_pq_size(&pq)); top = (string_entry *)nghttp2_pq_top(&pq); CU_ASSERT(strcmp("bar", top->s) == 0); nghttp2_pq_pop(&pq); string_entry_del(top); top = (string_entry *)nghttp2_pq_top(&pq); CU_ASSERT(strcmp("baz", top->s) == 0); nghttp2_pq_pop(&pq); string_entry_del(top); top = (string_entry *)nghttp2_pq_top(&pq); CU_ASSERT(strcmp("foo", top->s) == 0); nghttp2_pq_pop(&pq); string_entry_del(top); CU_ASSERT(nghttp2_pq_empty(&pq)); CU_ASSERT(0 == nghttp2_pq_size(&pq)); CU_ASSERT(NULL == nghttp2_pq_top(&pq)); /* Add bunch of entry to see realloc works */ for (i = 0; i < 10000; ++i) { CU_ASSERT(0 == nghttp2_pq_push(&pq, &string_entry_new("foo")->ent)); CU_ASSERT((size_t)(i + 1) == nghttp2_pq_size(&pq)); } for (i = 10000; i > 0; --i) { top = (string_entry *)nghttp2_pq_top(&pq); CU_ASSERT(NULL != top); nghttp2_pq_pop(&pq); string_entry_del(top); CU_ASSERT((size_t)(i - 1) == nghttp2_pq_size(&pq)); } nghttp2_pq_free(&pq); }