Functional test for a small free list.
| 92 | |
| 93 | // Functional test for a small free list. |
| 94 | TEST_F(FreeListTest, SmallList) { |
| 95 | const int LIST_SIZE = 2; |
| 96 | FreeList small_list; |
| 97 | |
| 98 | // PopFreeBuffer() on empty list returns false. |
| 99 | BufferHandle buffer; |
| 100 | ASSERT_FALSE(small_list.PopFreeBuffer(&buffer)); |
| 101 | ASSERT_FALSE(small_list.PopFreeBuffer(&buffer)); |
| 102 | |
| 103 | // Add various numbers of buffers to the free list and check that they're |
| 104 | // either freed or returned in the order expected. |
| 105 | for (int num_buffers = 0; num_buffers <= LIST_SIZE + 2; ++num_buffers) { |
| 106 | for (int attempt = 0; attempt < 10; ++attempt) { |
| 107 | LOG(INFO) << "num_buffers " << num_buffers << " attempt " << attempt; |
| 108 | vector<BufferHandle> buffers; |
| 109 | AllocateBuffers(num_buffers, MIN_BUFFER_LEN, &buffers); |
| 110 | |
| 111 | // Keep track of the addresses so we can validate the buffer order. |
| 112 | const vector<const void*> addrs = GetSortedAddrs(buffers); |
| 113 | |
| 114 | // Try shuffling to make sure we don't always just add in ascending order. |
| 115 | std::shuffle(buffers.begin(), buffers.end(), rng_); |
| 116 | AddFreeBuffers(&small_list, &buffers); |
| 117 | // Shrink list down to LIST_SIZE. |
| 118 | FreeBuffers( |
| 119 | small_list.GetBuffersToFree(max<int64_t>(0, small_list.Size() - LIST_SIZE))); |
| 120 | |
| 121 | // The LIST_SIZE buffers with the lowest address should be retained, and the |
| 122 | // remaining buffers should have been freed. |
| 123 | for (int i = 0; i < min(num_buffers, LIST_SIZE); ++i) { |
| 124 | ASSERT_TRUE(small_list.PopFreeBuffer(&buffer)) << i; |
| 125 | ASSERT_EQ(addrs[i], buffer.data()) << i; |
| 126 | buffers.push_back(move(buffer)); |
| 127 | } |
| 128 | ASSERT_FALSE(small_list.PopFreeBuffer(&buffer)); |
| 129 | FreeBuffers(move(buffers)); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Functional test that makes sure the free lists return buffers in ascending order |
| 135 | TEST_F(FreeListTest, ReturnOrder) { |