Avi Drissman | 0ff8a7e | 2022-09-13 18:35:42 | [diff] [blame^] | 1 | // Copyright 2015 The Chromium Authors |
huangs | 57264dc | 2015-07-20 21:55:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "courgette/memory_allocator.h" |
| 6 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
huangs | 57264dc | 2015-07-20 21:55:04 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
huangs | 57264dc | 2015-07-20 21:55:04 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | TEST(MemoryAllocatorTest, NoThrowBuffer) { |
| 14 | const size_t size_list[] = {0U, 1U, 2U, 11U, 15U, 16U}; |
| 15 | |
| 16 | // Repeat test for different sizes. |
Daniel Cheng | 08601f80 | 2022-02-28 04:19:32 | [diff] [blame] | 17 | for (size_t idx = 0; idx < std::size(size_list); ++idx) { |
huangs | 57264dc | 2015-07-20 21:55:04 | [diff] [blame] | 18 | size_t size = size_list[idx]; |
| 19 | |
| 20 | courgette::NoThrowBuffer<size_t> buf1; |
| 21 | EXPECT_EQ(0U, buf1.size()); |
| 22 | EXPECT_TRUE(buf1.empty()); |
| 23 | |
| 24 | // Ensure reserve() should not affect size. |
| 25 | EXPECT_TRUE(buf1.reserve(size / 2)); |
| 26 | EXPECT_EQ(0U, buf1.size()); |
| 27 | EXPECT_TRUE(buf1.empty()); |
| 28 | |
| 29 | // Populate with integers from |size| - 1 to 0. |
| 30 | for (size_t i = 0; i < size; ++i) { |
| 31 | size_t new_value = size - 1 - i; |
| 32 | EXPECT_TRUE(buf1.push_back(new_value)); |
| 33 | EXPECT_EQ(new_value, buf1.back()); |
| 34 | EXPECT_EQ(i + 1, buf1.size()); |
| 35 | EXPECT_FALSE(buf1.empty()); |
| 36 | } |
| 37 | |
| 38 | // Sort, and verify that list is indeed sorted. |
| 39 | std::sort(buf1.begin(), buf1.end()); |
| 40 | for (size_t i = 0; i < size; ++i) |
| 41 | EXPECT_EQ(i, buf1[i]); |
| 42 | |
| 43 | // Test operator[] for read and write. |
| 44 | for (size_t i = 0; i < size; ++i) |
| 45 | buf1[i] = buf1[i] * 2; |
| 46 | |
| 47 | // Test append(). |
| 48 | courgette::NoThrowBuffer<size_t> buf2; |
| 49 | |
| 50 | if (size > 0) { |
| 51 | EXPECT_TRUE(buf2.append(&buf1[0], size)); |
| 52 | EXPECT_EQ(size, buf2.size()); |
| 53 | for (size_t i = 0; i < size; ++i) |
| 54 | EXPECT_EQ(buf1[i], buf2[i]); |
| 55 | } |
| 56 | |
| 57 | // Test shrinking by resize(). |
| 58 | const size_t kNewValue = 137; |
| 59 | size_t new_size = size / 2; |
| 60 | EXPECT_TRUE(buf2.resize(new_size, kNewValue)); |
| 61 | EXPECT_EQ(new_size, buf2.size()); |
| 62 | for (size_t i = 0; i < new_size; ++i) |
| 63 | EXPECT_EQ(buf1[i], buf2[i]); |
| 64 | |
| 65 | // Test expanding by resize(). |
| 66 | EXPECT_TRUE(buf2.resize(size, kNewValue)); |
| 67 | EXPECT_EQ(size, buf2.size()); |
| 68 | for (size_t i = 0; i < new_size; ++i) |
| 69 | EXPECT_EQ(buf1[i], buf2[i]); |
| 70 | for (size_t i = new_size; i < size; ++i) |
| 71 | EXPECT_EQ(kNewValue, buf2[i]); |
| 72 | |
| 73 | // Test clear(). |
| 74 | buf2.clear(); |
| 75 | EXPECT_EQ(0U, buf2.size()); |
| 76 | EXPECT_TRUE(buf2.empty()); |
| 77 | } |
| 78 | } |