|
|
|
@ -119,47 +119,53 @@ static void test_FIFO(void) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Test if queue's behavior of expanding is correct. (Only does expansion when
|
|
|
|
|
// it gets full, and each time expands to doubled size).
|
|
|
|
|
static void test_space_efficiency(void) { |
|
|
|
|
gpr_log(GPR_INFO, "test_space_efficiency"); |
|
|
|
|
grpc_core::InfLenFIFOQueue queue; |
|
|
|
|
for (int i = 0; i < 1024; ++i) { |
|
|
|
|
for (int i = 0; i < queue.init_num_nodes(); ++i) { |
|
|
|
|
queue.Put(static_cast<void*>(grpc_core::New<WorkItem>(i))); |
|
|
|
|
} |
|
|
|
|
GPR_ASSERT(queue.num_node() == 1024); |
|
|
|
|
for (int i = 0; i < 1024; ++i) { |
|
|
|
|
// List should not have been expanded at this time.
|
|
|
|
|
GPR_ASSERT(queue.num_nodes() == queue.init_num_nodes()); |
|
|
|
|
for (int i = 0; i < queue.init_num_nodes(); ++i) { |
|
|
|
|
WorkItem* item = static_cast<WorkItem*>(queue.Get()); |
|
|
|
|
queue.Put(item); |
|
|
|
|
} |
|
|
|
|
GPR_ASSERT(queue.num_node() == 1024); |
|
|
|
|
for (int i = 0; i < 1024; ++i) { |
|
|
|
|
GPR_ASSERT(queue.num_nodes() == queue.init_num_nodes()); |
|
|
|
|
for (int i = 0; i < queue.init_num_nodes(); ++i) { |
|
|
|
|
WorkItem* item = static_cast<WorkItem*>(queue.Get()); |
|
|
|
|
grpc_core::Delete(item); |
|
|
|
|
} |
|
|
|
|
GPR_ASSERT(queue.num_node() == 1024); |
|
|
|
|
GPR_ASSERT(queue.num_nodes() == queue.init_num_nodes()); |
|
|
|
|
GPR_ASSERT(queue.count() == 0); |
|
|
|
|
// queue empty now
|
|
|
|
|
for (int i = 0; i < 4000; ++i) { |
|
|
|
|
for (int i = 0; i < queue.init_num_nodes() * 2; ++i) { |
|
|
|
|
queue.Put(static_cast<void*>(grpc_core::New<WorkItem>(i))); |
|
|
|
|
} |
|
|
|
|
GPR_ASSERT(queue.count() == 4000); |
|
|
|
|
GPR_ASSERT(queue.num_node() == 4096); |
|
|
|
|
for (int i = 0; i < 2000; ++i) { |
|
|
|
|
GPR_ASSERT(queue.count() == queue.init_num_nodes() * 2); |
|
|
|
|
// List should have been expanded once.
|
|
|
|
|
GPR_ASSERT(queue.num_nodes() == queue.init_num_nodes() * 2); |
|
|
|
|
for (int i = 0; i < queue.init_num_nodes(); ++i) { |
|
|
|
|
WorkItem* item = static_cast<WorkItem*>(queue.Get()); |
|
|
|
|
grpc_core::Delete(item); |
|
|
|
|
} |
|
|
|
|
GPR_ASSERT(queue.count() == 2000); |
|
|
|
|
GPR_ASSERT(queue.num_node() == 4096); |
|
|
|
|
for (int i = 0; i < 1000; ++i) { |
|
|
|
|
GPR_ASSERT(queue.count() == queue.init_num_nodes()); |
|
|
|
|
// List will never shrink, should keep same number of node as before.
|
|
|
|
|
GPR_ASSERT(queue.num_nodes() == queue.init_num_nodes() * 2); |
|
|
|
|
for (int i = 0; i < queue.init_num_nodes() + 1; ++i) { |
|
|
|
|
queue.Put(static_cast<void*>(grpc_core::New<WorkItem>(i))); |
|
|
|
|
} |
|
|
|
|
GPR_ASSERT(queue.count() == 3000); |
|
|
|
|
GPR_ASSERT(queue.num_node() == 4096); |
|
|
|
|
for (int i = 0; i < 3000; ++i) { |
|
|
|
|
GPR_ASSERT(queue.count() == queue.init_num_nodes() * 2 + 1); |
|
|
|
|
// List should have been expanded twice.
|
|
|
|
|
GPR_ASSERT(queue.num_nodes() == queue.init_num_nodes() * 4); |
|
|
|
|
for (int i = 0; i < queue.init_num_nodes() * 2 + 1; ++i) { |
|
|
|
|
WorkItem* item = static_cast<WorkItem*>(queue.Get()); |
|
|
|
|
grpc_core::Delete(item); |
|
|
|
|
} |
|
|
|
|
GPR_ASSERT(queue.count() == 0); |
|
|
|
|
GPR_ASSERT(queue.num_node() == 4096); |
|
|
|
|
GPR_ASSERT(queue.num_nodes() == queue.init_num_nodes() * 4); |
|
|
|
|
gpr_log(GPR_DEBUG, "Done."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|