1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "courgette/memory_allocator.h"
11 #include "base/stl_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(MemoryAllocatorTest, NoThrowBuffer) {
15 const size_t size_list[] = {0U, 1U, 2U, 11U, 15U, 16U};
17 // Repeat test for different sizes.
18 for (size_t idx = 0; idx < base::size(size_list); ++idx) {
19 size_t size = size_list[idx];
21 courgette::NoThrowBuffer<size_t> buf1;
22 EXPECT_EQ(0U, buf1.size());
23 EXPECT_TRUE(buf1.empty());
25 // Ensure reserve() should not affect size.
26 EXPECT_TRUE(buf1.reserve(size / 2));
27 EXPECT_EQ(0U, buf1.size());
28 EXPECT_TRUE(buf1.empty());
30 // Populate with integers from |size| - 1 to 0.
31 for (size_t i = 0; i < size; ++i) {
32 size_t new_value = size - 1 - i;
33 EXPECT_TRUE(buf1.push_back(new_value));
34 EXPECT_EQ(new_value, buf1.back());
35 EXPECT_EQ(i + 1, buf1.size());
36 EXPECT_FALSE(buf1.empty());
39 // Sort, and verify that list is indeed sorted.
40 std::sort(buf1.begin(), buf1.end());
41 for (size_t i = 0; i < size; ++i)
42 EXPECT_EQ(i, buf1[i]);
44 // Test operator[] for read and write.
45 for (size_t i = 0; i < size; ++i)
46 buf1[i] = buf1[i] * 2;
49 courgette::NoThrowBuffer<size_t> buf2;
52 EXPECT_TRUE(buf2.append(&buf1[0], size));
53 EXPECT_EQ(size, buf2.size());
54 for (size_t i = 0; i < size; ++i)
55 EXPECT_EQ(buf1[i], buf2[i]);
58 // Test shrinking by resize().
59 const size_t kNewValue = 137;
60 size_t new_size = size / 2;
61 EXPECT_TRUE(buf2.resize(new_size, kNewValue));
62 EXPECT_EQ(new_size, buf2.size());
63 for (size_t i = 0; i < new_size; ++i)
64 EXPECT_EQ(buf1[i], buf2[i]);
66 // Test expanding by resize().
67 EXPECT_TRUE(buf2.resize(size, kNewValue));
68 EXPECT_EQ(size, buf2.size());
69 for (size_t i = 0; i < new_size; ++i)
70 EXPECT_EQ(buf1[i], buf2[i]);
71 for (size_t i = new_size; i < size; ++i)
72 EXPECT_EQ(kNewValue, buf2[i]);
76 EXPECT_EQ(0U, buf2.size());
77 EXPECT_TRUE(buf2.empty());